Ejemplo n.º 1
0
// MODIFY THIS FUNCTION
void drawMesh(Context &ctx, GLuint program, const MeshVAO &meshVAO)
{
    glUseProgram(program);

    glm::mat4 model      = trackballGetRotationMatrix(ctx.trackball);
    model                = glm::scale(model, glm::vec3(0.5f));
    //model              = glm::translate(model, glm::vec3(0.0f, 0.5f, 0.5f));
    //model              = glm::rotate(model, 3.14159f, glm::vec3(0.0f, 1.0f, 0.0f));

    glm::mat4 view       = glm::lookAt(glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

    glm::mat4 projection = glm::perspective(3.14159f/2, 1.0f, 0.1f, 100.0f);

    glm::mat4 mv  = view * model;
    glm::mat4 mvp = projection * mv;

    glm::vec3 light_pos = glm::vec3(1.0f, 5.0f, 0.0f);

    glUniformMatrix4fv(glGetUniformLocation(ctx.program, "u_mvp"), 1, GL_FALSE, &mvp[0][0]);
    glUniformMatrix4fv(glGetUniformLocation(ctx.program, "u_mv"),  1, GL_FALSE, &mv[0][0]);
    glUniform3fv(glGetUniformLocation(ctx.program, "u_light_position"),  1, &light_pos[0]);

    glBindVertexArray(meshVAO.vao);
    glDrawElements(GL_TRIANGLES, meshVAO.numIndices, GL_UNSIGNED_INT, 0);
    glBindVertexArray(ctx.defaultVAO);

    glUseProgram(0);
}
Ejemplo n.º 2
0
// MODIFY THIS FUNCTION
void drawMesh(Context &ctx, GLuint program, const MeshVAO &meshVAO)
{
    // Define uniforms
    glm::mat4 model = trackballGetRotationMatrix(ctx.trackball);

	glm::mat4 view;
	getViewMatrix(&view);
	
	float zNear = 0.1f;
	float zFar = 100.f;
	glm::mat4 projection;
	if (ctx.lensType == LensType::PERSPECTIVE) {
		float fovy = getFovy(ctx);
		projection = glm::perspective(fovy, ctx.aspect, zNear, zFar);
	}
	else {
		float hh = 2.0f / pow(2.0f, ctx.zoom);
		projection = glm::ortho(-hh * ctx.aspect, hh * ctx.aspect, -hh, hh, zNear, zFar);
	}

    glm::mat4 mv = view * model;
    glm::mat4 mvp = projection * mv;

    // Activate program
    glUseProgram(program);

    // Bind textures
    // ...
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, ctx.cubemap_prefiltered_levels[ctx.cubemap_index]);
	//glBindTexture(GL_TEXTURE_CUBE_MAP, ctx.cubemap_prefiltered_mipmap);
	glUniform1i(glGetUniformLocation(program, "u_cubemap"), /*GL_TEXTURE0*/ 0);
	//glUniform1f(glGetUniformLocation(program, "u_cubemap_lod"), (float)ctx.cubemap_index);
	//glBindTexture(GL_TEXTURE_CUBE_MAP, 0);


    // Pass uniforms
	glUniformMatrix4fv(glGetUniformLocation(program, "u_v"), 1, GL_FALSE, &view[0][0]);
    glUniformMatrix4fv(glGetUniformLocation(program, "u_mv"), 1, GL_FALSE, &mv[0][0]);
    glUniformMatrix4fv(glGetUniformLocation(program, "u_mvp"), 1, GL_FALSE, &mvp[0][0]);
    glUniform1f(glGetUniformLocation(program, "u_time"), ctx.elapsed_time);
    // ...
	glUniform3fv(glGetUniformLocation(program, "u_ambient_light"), 1, &ctx.ambient_light[0]);

	glUniform3fv(glGetUniformLocation(program, "u_light_position"), 1, &ctx.light_position[0]);
	glUniform3fv(glGetUniformLocation(program, "u_light_color"), 1, &ctx.light_color[0]);
	
	glUniform3fv(glGetUniformLocation(program, "u_diffuse_color"), 1, &ctx.diffuse_color[0]);
	glUniform3fv(glGetUniformLocation(program, "u_specular_color"), 1, &ctx.specular_color[0]);
	glUniform1f(glGetUniformLocation(program, "u_specular_power"), ctx.specular_power);

	glUniform1f(glGetUniformLocation(program, "u_ambient_weight"), ctx.ambient_weight);
	glUniform1f(glGetUniformLocation(program, "u_diffuse_weight"), ctx.diffuse_weight);
	glUniform1f(glGetUniformLocation(program, "u_specular_weight"), ctx.specular_weight);

	glUniform1i(glGetUniformLocation(program, "u_color_mode"), ctx.color_mode);
	glUniform1i(glGetUniformLocation(program, "u_use_gamma_correction"), ctx.use_gamma_correction);
	glUniform1i(glGetUniformLocation(program, "u_use_color_inversion"), ctx.use_color_inversion);

    // Draw!
    glBindVertexArray(meshVAO.vao);
    glDrawElements(GL_TRIANGLES, meshVAO.numIndices, GL_UNSIGNED_INT, 0);
    glBindVertexArray(ctx.defaultVAO);
}