diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index 3b7068d..92fbf99 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -1,30 +1,41 @@ #version 400 core uniform vec3 lDirection; +uniform bool isSky; +uniform bool isPhong; +uniform sampler2D skybox; in vec3 fNormal; in vec3 fPosition; in vec4 ifColor; +in vec2 texCoords; out vec4 fColor; void main() { - // Get lighting vectors - vec3 LightDirection = normalize(vec3(-1,-3,0)); // We assume light is at camera position - vec3 nfNormal = normalize(fNormal); - vec3 nviewDirection = normalize(fPosition); + if(isSky) { + fColor = texture(skybox, texCoords); + } else if(isPhong) { + // Get lighting vectors + vec3 LightDirection = normalize(vec3(1,3,1)); + vec3 nfNormal = normalize(fNormal); + vec3 nviewDirection = normalize(fPosition); - // Compute diffuse component - float diffuse = max(0.0, dot(nfNormal, LightDirection)); + // 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); + // 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; + // Fragment color (currently static, to be replaced with texture) + vec3 color = ifColor.xyz; - // Compute final color - fColor = vec4(color * (0.1 + diffuse + specular), 1); + // Compute final color + //fColor = vec4(color * (0.1 + diffuse + specular), 1); + fColor = vec4(fNormal, 1); + } else { + fColor = ifColor; + } } diff --git a/src/shaders/basicShader.vert b/src/shaders/basicShader.vert index 9a9d533..da24b1d 100644 --- a/src/shaders/basicShader.vert +++ b/src/shaders/basicShader.vert @@ -2,12 +2,18 @@ uniform mat4 mvMatrix; uniform mat4 projMatrix; uniform mat3 normalMatrix; +uniform vec3 lDirection; uniform vec4 color; +uniform bool isSky; +uniform bool isPhong; + in vec4 vPosition; in vec3 vNormal; +in vec2 vUv; +out vec2 texCoords; out vec4 ifColor; out vec3 fPosition; out vec3 fNormal; @@ -19,6 +25,30 @@ main() gl_Position = projMatrix * vEyeCoord; fPosition = -vEyeCoord.xyz; fNormal = normalMatrix * vNormal; + + texCoords = vUv; + ifColor = color; + + if(!isPhong && !isSky) { + // Get lighting vectors + vec3 LightDirection = normalize(vec3(1,3,1)); + vec3 nfNormal = normalize(fNormal); + vec3 nviewDirection = normalize(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); + + // Vertex color (currently static, to be replaced with texture) + vec3 color = ifColor.xyz; + + // Compute final color + //fColor = vec4(color * (0.1 + diffuse + specular), 1); + ifColor = vec4(fNormal, 1); + } } diff --git a/src/shaders/skyboxShader.frag b/src/shaders/skyboxShader.frag index 9d56e35..429b5e6 100644 --- a/src/shaders/skyboxShader.frag +++ b/src/shaders/skyboxShader.frag @@ -7,5 +7,5 @@ uniform sampler2D skybox; void main() { //fColor = vec4(1); - fColor = texture(skybox, texCoords); + //fColor = texture(skybox, texCoords); } diff --git a/src/shaders/skyboxShader.vert b/src/shaders/skyboxShader.vert index 2c2603a..8d621b1 100644 --- a/src/shaders/skyboxShader.vert +++ b/src/shaders/skyboxShader.vert @@ -3,8 +3,8 @@ uniform mat4 mvMatrix; uniform mat4 projMatrix; out vec2 texCoords; -in vec4 vPosition; in vec2 vUv; +in vec4 vPosition; void main() diff --git a/src/shaders/skyboxshader.vert b/src/shaders/skyboxshader.vert index 2c2603a..8d621b1 100644 --- a/src/shaders/skyboxshader.vert +++ b/src/shaders/skyboxshader.vert @@ -3,8 +3,8 @@ uniform mat4 mvMatrix; uniform mat4 projMatrix; out vec2 texCoords; -in vec4 vPosition; in vec2 vUv; +in vec4 vPosition; void main() diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 3cdaf56..3f50239 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -46,10 +46,6 @@ namespace const double inc_mult = 5; const double inc_offset = 1.05; - enum Buffer_IDs { ArrayBuffer, NumBuffers }; - - GLuint Buffers[NumBuffers]; - int m_lDirectionLocation; int m_normalMatrixLoc; } @@ -90,7 +86,7 @@ void Viewer::cleanup() void Viewer::drawSkybox() { // Use the Skybox Shaders - skyboxRenderShaderProgram->bind(); + //skyboxRenderShaderProgram->bind(); s_texture->bind(); // Get projection and camera transformations @@ -103,9 +99,9 @@ void Viewer::drawSkybox() modelViewMatrix.scale(100); - skyboxRenderShaderProgram->setUniformValue(s_projMatrixLocation, projectionMatrix); - skyboxRenderShaderProgram->setUniformValue(s_mvMatrixLocation, modelViewMatrix); -// skyboxRenderShaderProgram->setAttributeValue(s_colorLocation, ); + + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_isSkyLoc, true); int faces = floor(numVerticesCube/6); for(int i = 0; i < faces; i++){ // 6 vertexes par face @@ -273,6 +269,18 @@ void Viewer::initShaders() if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0) qDebug() << "Unable to find m_shader location for" << "vNormal"; + if ((s_vUvLocation = m_program->attributeLocation("vUv")) < 0) + qDebug() << "Unable to find shader location for " << "vUv"; + + if ((m_isSkyLoc = m_program->uniformLocation("isSky")) < 0) + qDebug() << "Unable to find m_shader location for" << "isSky"; + + if ((m_isPhongLoc = m_program->uniformLocation("isPhong")) < 0) + qDebug() << "Unable to find m_shader location for" << "isPhong"; + + if ((m_lDirLoc = m_program->uniformLocation("lDirection")) < 0) + qDebug() << "Unable to find m_shader location for" << "lDirection"; + /* * Adding Texture Shader @@ -324,9 +332,6 @@ void Viewer::initShaders() if ((s_mvMatrixLocation = skyboxRenderShaderProgram->uniformLocation("mvMatrix")) < 0) qDebug() << "Unable to find shader location for " << "mvMatrix"; - if ((s_vUvLocation = skyboxRenderShaderProgram->attributeLocation("vUv")) < 0) - qDebug() << "Unable to find shader location for " << "vUv"; - s_texture = new QOpenGLTexture(QImage("src/data/skybox.jpg"));/*/ s_texture = new QOpenGLTexture(QImage("src/data/uvLayoutGrid.png"));//*/ s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); @@ -399,12 +404,12 @@ void Viewer::initGeometries() { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, + { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, + { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, + { 1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, { 1.0, 0.0, 0.0 }, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, - { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, - { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, - { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, @@ -413,30 +418,33 @@ void Viewer::initGeometries() }; + // Fill vertex VBO + GLsizeiptr offsetVertices = 0; + GLsizeiptr offsetNormals = sizeof(verticesCube); + GLsizeiptr offsetUV = offsetNormals + sizeof(normals); + GLsizeiptr dataSize = offsetUV + sizeof(uvs); + + glBindVertexArray(m_VAOs[VAO_Cube]); // glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); - glGenBuffers(NumBuffers, Buffers); + glGenBuffers(NumBuffers, m_Buffers); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube) + sizeof(uvs) + sizeof(normals), + glBufferData(GL_ARRAY_BUFFER, dataSize, 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(verticesCube)+sizeof(uvs), sizeof(normals), normals); -// glBindVertexArray(m_VAOs[VAO_Cube]); -// glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); -// glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, offsetVertices, sizeof(verticesCube), verticesCube); + glBufferSubData(GL_ARRAY_BUFFER, offsetNormals, sizeof(normals), normals); + glBufferSubData(GL_ARRAY_BUFFER, offsetUV, sizeof(uvs), uvs); - glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); - glEnableVertexAttribArray(m_vPositionLocation); + glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); + glEnableVertexAttribArray(m_vPositionLocation); - glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, - GL_TRUE, 0, BUFFER_OFFSET(sizeof(verticesCube) + sizeof(uvs))); + glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals)); glEnableVertexAttribArray(m_vNormalLocation); - glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, - GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube))); + glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV)); glEnableVertexAttribArray(s_vUvLocation); + } void Viewer::visit(Cube &s) @@ -446,6 +454,8 @@ void Viewer::visit(Cube &s) 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_isSkyLoc, false); + m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); m_program->setUniformValue(m_colorLocation, s.getColor()); diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index 80ab402..497a9d5 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -93,6 +93,10 @@ private: int s_vUvLocation; int m_vNormalLocation; + int m_isPhongLoc; + int m_isSkyLoc; + int m_lDirLoc; + QOpenGLTexture *s_texture; SceneGroup* activeCell;