Exemplo n.º 1
0
void TestGame::engine_render() {
    buffer_clear();
    
    static float rot = 0;
    rot += PI / 120;
    if (rot > 4 * PI)
        rot -= 4 * PI;

    camera_.x = (float)sin(rot) * 2.5f;
    camera_.y = -0.3f;
    camera_.z = (float)cos(rot) * 2.5f;
    
    Matrix4 viewMatrix = matrix_look_at(camera_, Vector3(0, 0, 0), vector_up());
    Matrix4 projectionMatrix = matrix_perspective(0.1f, 
        ((float)ENGINE_WIDTH) / ENGINE_HEIGHT, 0.01f, 1.0f);
    
    for (int i = 0; i < meshes_count_; i++) {
        Matrix4 transform = meshes_[i].world * viewMatrix * projectionMatrix;

        for (int j = 0; j < meshes_[i].face_count; j++)
            draw_triangle(
                project(meshes_[i].vertices[meshes_[i].faces[j].a], transform),
                project(meshes_[i].vertices[meshes_[i].faces[j].b], transform),
                project(meshes_[i].vertices[meshes_[i].faces[j].c], transform),
                meshes_[i].texture);
    }
    
}
Exemplo n.º 2
0
//3D viewing pipeline. VTM is complete view matrix. none of the values of the View structure should be edited.
void matrix_setView3D(Matrix *vtm, View3D *view){
	Vector u, vup, vpn;
	double b, d;

	matrix_identity(vtm);
	matrix_translate(vtm, -view->vrp.val[0], -view->vrp.val[1], -view->vrp.val[2]);

	vpn = view->vpn;
	vector_cross(&view->vup, &vpn, &u);
	vector_cross(&vpn, &u, &vup);
	vector_normalize(&u);
	vector_normalize(&vup);
	vector_normalize(&vpn);

	matrix_rotateXYZ(vtm, &u, &vup, &vpn);
	matrix_translate(vtm, 0.0, 0.0, view->d);
	
	// in lecture notes here (6 and 7) it says to shear but as we only have d to define the COP I don't think we have to

	b = view->d + view->b;
	
	matrix_scale(vtm, ((2.0*view->d) / (b*view->du)), ((2.0*view->d) / (b*view->dv)), (1/b));
	
	d = view->d / b;
	matrix_perspective(vtm, d);
	matrix_scale2D(vtm, (-view->screenx / (2.0*d)), (-view->screeny / (2.0*d)));
	matrix_translate2D(vtm, (view->screenx / 2.0), (view->screeny / 2.0));
}
Exemplo n.º 3
0
int main(int argc, char *argv[]){
	//glfwSetKeyCallback(window, &keypress);
	GLFWwindow *window;
	GLuint VAO, VBO;
	GLuint program;
	GLuint crate;

	matrix camera, perspective, rotate, scale, translate;
	GLuint l_camera, l_rotate, l_scale, l_translate, l_time, l_perspective;
	float time;
	
	// init window
	window = dg_window(1920-192, 1080-108, "OpenGL Experiment");

	// init shaders
	dg_program(&program);
	  dg_attach("vertex.glsl", GL_VERTEX_SHADER);
	  dg_attach("fragment.glsl", GL_FRAGMENT_SHADER);
	dg_compile();
	
	// init texture
	crate = dg_texture("crate.jpg");

	// init model
	glGenBuffers(1, &VBO);
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
	glEnable(GL_DEPTH_TEST);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MODEL_CUBE), MODEL_CUBE, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(0));
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glBindVertexArray(0);

	// locate shader variables
	l_scale = glGetUniformLocation(program, "scale");
	l_camera = glGetUniformLocation(program, "camera");
	l_rotate = glGetUniformLocation(program, "rotate");
	l_translate = glGetUniformLocation(program, "translate");
	l_perspective = glGetUniformLocation(program, "perspective");
	
	// init matrices
	matrix_perspective(&perspective, 90.0f, 90.0f, 0.1f, 100.0f);
	matrix_identity(&rotate);
	matrix_identity(&scale);
	matrix_identity(&translate);
	
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // uncomment for wireframe

	while(!glfwWindowShouldClose(window)){
		glfwPollEvents();
		time = (float)(glfwGetTime() * 100);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		//eye target up
		matrix_camera(&camera,
			// position 
			0.0f, 0.0f, 0.0f,
			// angle
			0.0f, 0.0f, 0.0f
		);
		
		glUseProgram(program);
		
		// send to gpu
		glUniform1f(l_time, time);
		glUniformMatrix4fv(l_camera, 1, GL_TRUE, camera.data);
		glUniformMatrix4fv(l_perspective, 1, GL_TRUE, perspective.data);
		
		// bind model
		glBindVertexArray(VAO);

			// calculate transformations
			matrix_scale(&scale, 1.0f, 1.0f, 1.0f);
			matrix_rotate_y(&rotate, time);
			matrix_translate(&translate, 0.0f, 0.0f, -1.0f);

			// send to gpu
			glUniformMatrix4fv(l_scale, 1, GL_TRUE, scale.data);
			glUniformMatrix4fv(l_rotate, 1, GL_TRUE, rotate.data);
			glUniformMatrix4fv(l_translate, 1, GL_TRUE, translate.data);
				
			// texture
			glBindTexture(GL_TEXTURE_2D, crate);

			// draw
			glDrawArrays(GL_TRIANGLES, 0, 36);

		// unbind model VAO
		glBindVertexArray(0);
		
		glfwSwapBuffers(window);
	}
	
	glfwTerminate();

	return EXIT_SUCCESS;
}