Exemplo n.º 1
0
// Initializes the rendering.
void init(void)
{
    globals.period = 10.0f;
    globals.pointSize = 2.0f;
    createTriangleShader();
    gen_particles();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    creatTweakBar();
}
Exemplo n.º 2
0
int main () {
	restart_gl_log ();
	// use GLFW and GLEW to start GL context. see gl_utils.cpp for details
	start_gl ();

	/* create buffer of particle initial attributes and a VAO */
	GLuint vao = gen_particles ();
	
	GLuint shader_programme = create_programme_from_files (
		"test_vs.glsl", "test_fs.glsl");
	
	#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
	// input variables
	float near = 0.1f; // clipping plane
	float far = 100.0f; // clipping plane
	float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians
	float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio
	// matrix components
	float range = tan (fov * 0.5f) * near;
	float Sx = (2.0f * near) / (range * aspect + range * aspect);
	float Sy = near / range;
	float Sz = -(far + near) / (far - near);
	float Pz = -(2.0f * far * near) / (far - near);
	GLfloat proj_mat[] = {
		Sx, 0.0f, 0.0f, 0.0f,
		0.0f, Sy, 0.0f, 0.0f,
		0.0f, 0.0f, Sz, -1.0f,
		0.0f, 0.0f, Pz, 0.0f
	};
	
	float cam_speed = 1.0f; // 1 unit per second
	float cam_yaw_speed = 10.0f; // 10 degrees per second
	float cam_pos[] = {0.0f, 0.0f, 2.0f}; // don't start at zero, or we will be too close
	float cam_yaw = 0.0f; // y-rotation in degrees
	mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2]));
	mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw);
	mat4 view_mat = R * T;
	
	/* make up a world position for the emitter */
	vec3 emitter_world_pos (0.0f, 0.0f, 0.0f);
	
	// locations of view and projection matrices
	int V_loc = glGetUniformLocation (shader_programme, "V");
	assert (V_loc > -1);
	int P_loc = glGetUniformLocation (shader_programme, "P");
	assert (P_loc > -1);
	int emitter_pos_wor_loc = glGetUniformLocation (shader_programme,
		"emitter_pos_wor");
	assert (emitter_pos_wor_loc > -1);
	int elapsed_system_time_loc = glGetUniformLocation (shader_programme,
		"elapsed_system_time");
	assert (elapsed_system_time_loc > -1);
	glUseProgram (shader_programme);
	glUniformMatrix4fv (V_loc, 1, GL_FALSE, view_mat.m);
	glUniformMatrix4fv (P_loc, 1, GL_FALSE, proj_mat);
	glUniform3f (emitter_pos_wor_loc,
		emitter_world_pos.v[0],
		emitter_world_pos.v[1],
		emitter_world_pos.v[2]);
	
	// load texture
	GLuint tex;
	if (!load_texture ("Droplet.png", &tex)) {
		gl_log_err ("ERROR: loading Droplet.png texture\n");
		return 1;
	}
	
	glEnable (GL_CULL_FACE); // cull face
	glCullFace (GL_BACK); // cull back face
	glFrontFace (GL_CCW); // GL_CCW for counter clock-wise
	glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glClearColor (0.2, 0.2, 0.2, 1.0);
	/* MUST use this is in compatibility profile. doesn't exist in core
	glEnable(GL_POINT_SPRITE);
	*/
	
	while (!glfwWindowShouldClose (g_window)) {
		static double previous_seconds = glfwGetTime ();
		double current_seconds = glfwGetTime ();
		double elapsed_seconds = current_seconds - previous_seconds;
		previous_seconds = current_seconds;
	
		_update_fps_counter (g_window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0, 0, g_gl_width, g_gl_height);
		
		/* Render Particles. Enabling point re-sizing in vertex shader */
		glEnable (GL_PROGRAM_POINT_SIZE);
		glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
		
		glEnable (GL_BLEND);
		glDepthMask (GL_FALSE);
		glActiveTexture (GL_TEXTURE0);
		glBindTexture (GL_TEXTURE_2D, tex);
		glUseProgram (shader_programme);
		
		/* update time in shaders */
		glUniform1f (elapsed_system_time_loc, (GLfloat)current_seconds);
		
		glBindVertexArray (vao);
		// draw points 0-3 from the currently bound VAO with current in-use shader
		glDrawArrays (GL_POINTS, 0, PARTICLE_COUNT);
		glDisable (GL_BLEND);
		glDepthMask (GL_TRUE);
		glDisable (GL_PROGRAM_POINT_SIZE);
		
		// update other events like input handling 
		glfwPollEvents ();
		
		// control keys
		bool cam_moved = false;
		if (glfwGetKey (g_window, GLFW_KEY_A)) {
			cam_pos[0] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_D)) {
			cam_pos[0] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_UP)) {
			cam_pos[1] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_DOWN)) {
			cam_pos[1] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_W)) {
			cam_pos[2] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_S)) {
			cam_pos[2] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_LEFT)) {
			cam_yaw += cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_RIGHT)) {
			cam_yaw -= cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		// update view matrix
		if (cam_moved) {
			mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2])); // cam translation
			mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); // 
			mat4 view_mat = R * T;
			glUniformMatrix4fv (V_loc, 1, GL_FALSE, view_mat.m);
		}
		
		
		if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (g_window, 1);
		}
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (g_window);
	}
	
	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;
}
Exemplo n.º 3
0
int main (int argc, char** argv) {
	// Standard stuff...
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Index Buffers");
	glutReshapeFunc(changeViewport);
	
	glewInit();

	initMatrices(); // New <========================================
	vao=gen_particles();

/*#ifdef USING_INDEX_BUFFER


	
#endif*/

	// Make a shader
	char* vertexShaderSourceCode = readFile("vertexShader1.vsh");
	char* fragmentShaderSourceCode = readFile("fragmentShader1.fsh");
	GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
	GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
	shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);

	/*/ / Create the "remember all"
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	// Create the buffer, but don't load anything yet
	//glBufferData(GL_ARRAY_BUFFER, 7*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
	glBufferData(GL_ARRAY_BUFFER, 6*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
	
	// Load the vertex points
	glBufferSubData(GL_ARRAY_BUFFER, 0, 3*NUM_VERTICES*sizeof(GLfloat), vertices);
	// Load the colors right after that
	//glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),4*NUM_VERTICES*sizeof(GLfloat), colors);
	glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),3*NUM_VERTICES*sizeof(GLfloat), normals);*/
	
	
/*#ifdef USING_INDEX_BUFFER
	glGenBuffers(1, &indexBufferID);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES*sizeof(GLuint), indices, GL_STATIC_DRAW);
#endif*/
	

	// Find the position of the variables in the shader
	//ositionID = glGetAttribLocation(shaderProgramID, "v_i");
	//colorID = glGetAttribLocation(shaderProgramID, "elapsed_system_time");
	
	// ============ New! glUniformLocation is how you pull IDs for uniform variables===============
	perspectiveMatrixID = glGetUniformLocation(shaderProgramID, "P");
	viewMatrixID = glGetUniformLocation(shaderProgramID, "V");
	emitterID=glGetUniformLocation(shaderProgramID, "emitter_pos_wor");
	timeID=glGetUniformLocation(shaderProgramID, "elapsed_system_time");
	
	//=============================================================================================

	/*glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
	//glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
	glVertexAttribPointer(colorID, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
	printf ("BUFFER_OFFSET is: %d\n", sizeof(vertices));*/

	glUseProgram(shaderProgramID);
	//glEnableVertexAttribArray(positionID);
	//glEnableVertexAttribArray(colorID);
	//glEnable(GL_DEPTH_TEST);
    //glClearColor( 0.0, 0.0, 0.0, 0.2 );

	//current_seconds=Timer();
	glutDisplayFunc(render);

	glutKeyboardFunc( keyboard );
	glutMainLoop();
	
	return 0;
}