The rumour come out, does phong lighting is work?

This commit is contained in:
Riku Avelar 2016-11-09 21:10:25 -05:00
parent fff57b7285
commit eb4386ef23
4 changed files with 21 additions and 22 deletions

View File

@ -11,9 +11,9 @@ void
main() main()
{ {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(vec3(0.0)-fPosition); // We assume light is at camera position vec3 LightDirection = normalize(vec3(-1,-3,0)); // We assume light is at camera position
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(vec3(0.0)-fPosition); vec3 nviewDirection = normalize(fPosition);
// Compute diffuse component // Compute diffuse component
float diffuse = max(0.0, dot(nfNormal, LightDirection)); float diffuse = max(0.0, dot(nfNormal, LightDirection));
@ -26,5 +26,5 @@ main()
vec3 color = ifColor.xyz; vec3 color = ifColor.xyz;
// Compute final color // Compute final color
fColor = vec4(color * (diffuse + specular), 1); fColor = vec4(color * (0.1 + diffuse + specular), 1);
} }

View File

@ -17,7 +17,7 @@ main()
{ {
vec4 vEyeCoord = mvMatrix * vPosition; vec4 vEyeCoord = mvMatrix * vPosition;
gl_Position = projMatrix * vEyeCoord; gl_Position = projMatrix * vEyeCoord;
fPosition = vEyeCoord.xyz; fPosition = -vEyeCoord.xyz;
fNormal = normalMatrix * vNormal; fNormal = normalMatrix * vNormal;
ifColor = color; ifColor = color;
} }

View File

@ -6,6 +6,6 @@ uniform sampler2D skybox;
void main() void main()
{ {
//fColor = texture(skybox, texCoords); //fColor = vec4(1);
fColor = texture(skybox, texCoords); fColor = texture(skybox, texCoords);
} }

View File

@ -142,6 +142,8 @@ void Viewer::draw()
projectionMatrix.translate(0, -5, 0); projectionMatrix.translate(0, -5, 0);
projectionMatrix.rotate(15, 1, 0, 0);//*/ projectionMatrix.rotate(15, 1, 0, 0);//*/
modelViewMatrix.rotate(30,0,1,0);
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
@ -394,23 +396,23 @@ void Viewer::initGeometries()
}; };
GLfloat normals[numVerticesCube][3] = { GLfloat normals[numVerticesCube][3] = {
{ 0, 0, 1 }, {0, 0, 1}, {0, 0, 1}, { 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0},
{ 0, 0, 1 }, {0, 0, 1}, {0, 0, 1}, { 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0},
{ 0, 0, -1 }, {0, 0, -1}, {0, 0, -1}, { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0},
{ 0, 0, -1 }, {0, 0, -1}, {0, 0, -1}, { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0},
{ 1, 0, 0 }, {1, 0, 0}, {1, 0, 0}, { 1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
{ 1, 0, 0 }, {1, 0, 0}, {1, 0, 0}, { 1.0, 0.0, 0.0 }, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
{ -1, 0, 0 }, {-1, 0, 0}, {-1, 0, 0}, { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0},
{ -1, 0, 0 }, {-1, 0, 0}, {-1, 0, 0}, { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0},
{ 0, 1, 0 }, {0, 1, 0}, {0, 1, 0}, { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{ 0, 1, 0 }, {0, 1, 0}, {0, 1, 0}, { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{ 0, -1, 0 }, {0, -1, 0}, {0, -1, 0}, { 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0},
{ 0, -1, 0 }, {0, -1, 0}, {0, -1, 0}, { 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0}
}; };
@ -436,22 +438,19 @@ void Viewer::initGeometries()
glEnableVertexAttribArray(s_vUvLocation); glEnableVertexAttribArray(s_vUvLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube) + sizeof(uvs))); GL_TRUE, 0, BUFFER_OFFSET(sizeof(verticesCube) + sizeof(uvs)));
glEnableVertexAttribArray(m_vNormalLocation); glEnableVertexAttribArray(m_vNormalLocation);
} }
void Viewer::visit(Cube &s) void Viewer::visit(Cube &s)
{ {
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QMatrix4x4 lookAt;
camera()->getProjectionMatrix(lookAt);
int faces = floor(numVerticesCube/6); int faces = floor(numVerticesCube/6);
for(int i = 0; i < faces; i++){ // 6 vertexes par face for(int i = 0; i < faces; i++){ // 6 vertexes par face
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
m_program->setUniformValue(m_normalMatrixLoc, lookAt.normalMatrix()); m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix());
m_program->setUniformValue(m_colorLocation, s.getColor()); m_program->setUniformValue(m_colorLocation, s.getColor());
glDrawArrays(GL_TRIANGLES, i*6, 6); glDrawArrays(GL_TRIANGLES, i*6, 6);