diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index 7252e9d..8777e0c 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -1,9 +1,30 @@ #version 400 core +uniform vec3 lDirection; + +in vec3 fNormal; +in vec3 fPosition; in vec4 ifColor; + out vec4 fColor; void main() { - fColor = ifColor; + // Get lighting vectors + vec3 LightDirection = normalize(vec3(0.0)-fPosition); // We assume light is at camera position + vec3 nfNormal = normalize(fNormal); + vec3 nviewDirection = normalize(vec3(0.0)-fPosition); + + // Compute diffuse component + float diffuse = max(0.0, dot(nfNormal, LightDirection)); + + // Compute specular component + vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection)); + float specular = 0.0;//0.1*pow(max(0.0, dot(Rl, nviewDirection)), 16); + + // Fragment color (currently static, to be replaced with texture) + vec3 color = ifColor.xyz; + + // Compute final color + fColor = vec4(color * (diffuse + specular), 1); } diff --git a/src/shaders/basicShader.vert b/src/shaders/basicShader.vert index 8edff8e..a0f2a96 100644 --- a/src/shaders/basicShader.vert +++ b/src/shaders/basicShader.vert @@ -1,14 +1,24 @@ #version 400 core uniform mat4 mvMatrix; uniform mat4 projMatrix; +uniform mat3 normalMatrix; + uniform vec4 color; + in vec4 vPosition; +in vec3 vNormal; + out vec4 ifColor; +out vec3 fPosition; +out vec3 fNormal; void main() { - gl_Position = projMatrix * mvMatrix * vPosition; - ifColor = color; + vec4 vEyeCoord = mvMatrix * vPosition; + gl_Position = projMatrix * vEyeCoord; + fPosition = vEyeCoord.xyz; + fNormal = normalMatrix * vNormal; + ifColor = color; } diff --git a/src/shaders/skyboxShader.frag b/src/shaders/skyboxShader.frag index f1b865c..6d1e725 100644 --- a/src/shaders/skyboxShader.frag +++ b/src/shaders/skyboxShader.frag @@ -7,5 +7,5 @@ uniform sampler2D skybox; void main() { //fColor = texture(skybox, texCoords); - fColor = vec4(1.0, 0, 1.0, 1.0);// * texture(skybox, texCoords); + fColor = texture(skybox, texCoords); } diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index f5a1949..7353d97 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -50,6 +50,10 @@ namespace int vUVLocation; GLuint Buffers[NumBuffers]; + + int m_lDirectionLocation; + int m_vNormalLocation; + int m_normalMatrixLoc; } class SkyboxCamera : public qglviewer::Camera @@ -144,7 +148,7 @@ void Viewer::draw() // Traverse the Scene in order to draw its components modelStack.push(modelViewMatrix); - //root.accept(*this); + root.accept(*this); frame++; update(); } @@ -258,7 +262,16 @@ void Viewer::initShaders() qDebug() << "Unable to find shader location for " << "mvMatrix"; if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) - qDebug() << "Unable to find shader location for " << "projMatrix"; + qDebug() << "Unable to find shader location for " << "projMatrix"; + + if ((m_lDirectionLocation = m_program->uniformLocation("lDirection")) < 0) + qDebug() << "Unable to find m_shader location for" << "lDirection"; + + if ((m_normalMatrixLoc = m_program->uniformLocation("normalMatrix")) < 0) + qDebug() << "Unable to find m_shader location for" << "normalMatrix"; + + if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0) + qDebug() << "Unable to find m_shader location for" << "vNormal"; /* @@ -382,16 +395,36 @@ void Viewer::initGeometries() }; + GLfloat normals[numVerticesCube][3] = { + { 0, 0, 1 }, {0, 0, 1}, {0, 0, 1}, + { 0, 0, 1 }, {0, 0, 1}, {0, 0, 1}, + + { 0, 0, -1 }, {0, 0, -1}, {0, 0, -1}, + { 0, 0, -1 }, {0, 0, -1}, {0, 0, -1}, + + { 1, 0, 0 }, {1, 0, 0}, {1, 0, 0}, + { 1, 0, 0 }, {1, 0, 0}, {1, 0, 0}, + + { -1, 0, 0 }, {-1, 0, 0}, {-1, 0, 0}, + { -1, 0, 0 }, {-1, 0, 0}, {-1, 0, 0}, + + { 0, 1, 0 }, {0, 1, 0}, {0, 1, 0}, + { 0, 1, 0 }, {0, 1, 0}, {0, 1, 0}, + + { 0, -1, 0 }, {0, -1, 0}, {0, -1, 0}, + { 0, -1, 0 }, {0, -1, 0}, {0, -1, 0}, + }; + glBindVertexArray(m_VAOs[VAO_Cube]); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); glGenBuffers(NumBuffers, Buffers); glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube) + sizeof(uvs)/* + sizeof(normals)*/, + glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube) + sizeof(uvs) + sizeof(normals), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verticesCube), verticesCube); glBufferSubData(GL_ARRAY_BUFFER, sizeof(verticesCube), sizeof(uvs), uvs); - //glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices)+sizeof(uvs), sizeof(normals), normals); + glBufferSubData(GL_ARRAY_BUFFER, sizeof(verticesCube)+sizeof(uvs), sizeof(normals), normals); // glBindVertexArray(m_VAOs[VAO_Cube]); // glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); @@ -403,16 +436,24 @@ void Viewer::initGeometries() glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube))); glEnableVertexAttribArray(s_vUvLocation); + + glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, + GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube) + sizeof(uvs))); + glEnableVertexAttribArray(m_vNormalLocation); } void Viewer::visit(Cube &s) { QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); + QMatrix4x4 lookAt; + + camera()->getProjectionMatrix(lookAt); int faces = floor(numVerticesCube/6); for(int i = 0; i < faces; i++){ // 6 vertexes par face - glBindVertexArray(m_VAOs[VAO_Cube]); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + glBindVertexArray(m_VAOs[VAO_Cube]); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_normalMatrixLoc, lookAt.normalMatrix()); m_program->setUniformValue(m_colorLocation, s.getColor()); glDrawArrays(GL_TRIANGLES, i*6, 6);