diff --git a/src/data/assets/dry_ground_normals.jpg b/src/data/assets/dry_ground_normals.jpg new file mode 100644 index 0000000..d483fdf Binary files /dev/null and b/src/data/assets/dry_ground_normals.jpg differ diff --git a/src/data/assets/granite_floor_normals.jpg b/src/data/assets/granite_floor_normals.jpg new file mode 100644 index 0000000..566ddd6 Binary files /dev/null and b/src/data/assets/granite_floor_normals.jpg differ diff --git a/src/data/assets/grass_normals.jpg b/src/data/assets/grass_normals.jpg new file mode 100644 index 0000000..6e11b1b Binary files /dev/null and b/src/data/assets/grass_normals.jpg differ diff --git a/src/data/assets/limestone_wall_normals.jpg b/src/data/assets/limestone_wall_normals.jpg new file mode 100644 index 0000000..48c46ea Binary files /dev/null and b/src/data/assets/limestone_wall_normals.jpg differ diff --git a/src/data/assets/pierre_bouchardee_normals.jpg b/src/data/assets/pierre_bouchardee_normals.jpg new file mode 100644 index 0000000..99088c8 Binary files /dev/null and b/src/data/assets/pierre_bouchardee_normals.jpg differ diff --git a/src/data/assets/wood_floor_normals.jpg b/src/data/assets/wood_floor_normals.jpg new file mode 100644 index 0000000..53d8b63 Binary files /dev/null and b/src/data/assets/wood_floor_normals.jpg differ diff --git a/src/data/dry_ground_normals.jpg b/src/data/dry_ground_normals.jpg new file mode 100644 index 0000000..d483fdf Binary files /dev/null and b/src/data/dry_ground_normals.jpg differ diff --git a/src/data/granite_floor_normals.jpg b/src/data/granite_floor_normals.jpg new file mode 100644 index 0000000..566ddd6 Binary files /dev/null and b/src/data/granite_floor_normals.jpg differ diff --git a/src/data/grass_normals.jpg b/src/data/grass_normals.jpg new file mode 100644 index 0000000..6e11b1b Binary files /dev/null and b/src/data/grass_normals.jpg differ diff --git a/src/data/limestone_wall_normals.jpg b/src/data/limestone_wall_normals.jpg new file mode 100644 index 0000000..48c46ea Binary files /dev/null and b/src/data/limestone_wall_normals.jpg differ diff --git a/src/data/pierre_bouchardee_normals.jpg b/src/data/pierre_bouchardee_normals.jpg new file mode 100644 index 0000000..99088c8 Binary files /dev/null and b/src/data/pierre_bouchardee_normals.jpg differ diff --git a/src/data/wood_floor_normals.jpg b/src/data/wood_floor_normals.jpg new file mode 100644 index 0000000..53d8b63 Binary files /dev/null and b/src/data/wood_floor_normals.jpg differ diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index ee6f271..d1eec2b 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -1,8 +1,10 @@ #version 400 core +uniform sampler2D texCol; +uniform sampler2D texNormal; + uniform vec3 lDirection; uniform bool isSky; uniform bool isPhong; -uniform sampler2D tex; uniform float skyMult; uniform bool drawTextures; uniform bool isLightSource; @@ -10,6 +12,7 @@ uniform bool isPickingMode; uniform vec3 pointLight[3]; uniform vec4 pointLightCol[3]; uniform mat3 normalMatrix; +uniform bool useNormalMap; in vec3 fNormal; in vec3 fPosition; @@ -21,9 +24,16 @@ out vec4 fColor; vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { // Get lighting vectors - vec3 LightDirection = normalize(lDirection); - vec3 nfNormal = normalize(fNorm); - vec3 nviewDirection = normalize(fPos); + vec3 LightDirection = normalize(lDirection); + vec3 nviewDirection = normalize(fPos); + vec3 nfNormal; + + if(useNormalMap) { + vec3 vNorm = texture(texNormal, texCoords).rgb; + nfNormal = normalize(normalMatrix * vNorm); + } else { + nfNormal = normalize(fNorm); + } // Compute diffuse component float diff = 0.2*max(0.0, dot(nfNormal, -LightDirection)); @@ -38,14 +48,22 @@ vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { float mult = 1;//max(0.0, -LightDirection.y+1.5); //return vec4(0); - return vec4(tex.xyz * (diff + amb + spec) * mult, tex.w); + return vec4(tex.xyz * (diff + amb + spec) * mult, tex.w); } vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) { // Get lighting vectors vec3 LightDirection = normalize(pointLight[i] + fPos); - vec3 nfNormal = normalize(fNorm); + // vec3 nfNormal = normalize(fNorm); vec3 nviewDirection = normalize(fPos); + vec3 nfNormal; + + if(useNormalMap) { + vec3 vNorm = texture(texNormal, texCoords).rgb; + nfNormal = normalize(normalMatrix * vNorm); + } else { + nfNormal = normalize(fNorm); + } // Attenuation float distance = length(nviewDirection - pointLight[i] - fPos) / 3; @@ -61,7 +79,7 @@ vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) { // Compute ambient component float amb = 0.2; - return vec4(pointLightCol[i].xyz * attenuation * (amb + diff + spec) * tex.xyz, pointLightCol[i].w); + return vec4(pointLightCol[i].xyz * attenuation * (amb + diff + spec) * tex.xyz, pointLightCol[i].w); } void @@ -74,12 +92,12 @@ main() fColor = ifColor; }else{ if(drawTextures) { - texColor = texture(tex, texCoords); + texColor = texture(texCol, texCoords); } else { - texColor = ifColor; + texColor = ifColor; } - if(isLightSource) { + if(isLightSource) { fColor = texColor; } else if(isSky) { fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2; @@ -89,7 +107,7 @@ main() vec3 nfNormal = normalize(fNormal); vec3 nviewDirection = normalize(fPosition); - fColor = calcDirLight(texColor, fPosition, fNormal) + fColor = calcDirLight(texColor, fPosition, fNormal) + calcPointLight(texColor, fPosition, fNormal, 0)/4 + calcPointLight(texColor, fPosition, fNormal, 1)/4 + calcPointLight(texColor, fPosition, fNormal, 2)/4; diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 11cca62..e85ec64 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -103,6 +103,8 @@ void Viewer::cleanup() void Viewer::drawSkybox() { // Use the Skybox Shaders + + glActiveTexture(GL_TEXTURE0); m_program->bind(); s_texture->bind(); // Get projection and camera transformations @@ -194,7 +196,8 @@ void Viewer::drawUi(){ uiViewMatrix.scale(.25, .25, .01); uiViewMatrix.translate(-TEX_LENGTH/2.0, 0, 0); for(int i = 0; ibind(); m_program->setUniformValue(m_mvMatrixLocation, uiViewMatrix); uiViewMatrix.translate(1.2, 0, 0); @@ -371,8 +374,17 @@ void Viewer::initShaders() if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) qDebug() << "Unable to find shader location for " << "vPosition"; - if ((m_colorLocation = m_program->uniformLocation("color")) < 0) - qDebug() << "Unable to find shader location for " << "color"; + if ((m_colorLocation = m_program->uniformLocation("color")) < 0) + qDebug() << "Unable to find shader location for " << "color"; + + if ((m_useNormalMap = m_program->uniformLocation("useNormalMap")) < 0) + qDebug() << "Unable to find shader location for " << "useNormalMap"; + + if ((m_texNormal = m_program->uniformLocation("texNormal")) < 0) + qDebug() << "Unable to find shader location for " << "texNormal"; + + if ((m_texColor = m_program->uniformLocation("texCol")) < 0) + qDebug() << "Unable to find shader location for " << "texCol"; if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) qDebug() << "Unable to find shader location for " << "mvMatrix"; @@ -434,6 +446,10 @@ void Viewer::initShaders() m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_drawTextLoc, false); m_program->setUniformValue(m_isPickingModeLoc, false); + m_program->setUniformValue(m_useNormalMap, true); + + m_program->setUniformValue(m_texColor, 0); + m_program->setUniformValue(m_texNormal, 1); s_texture = new QOpenGLTexture(QImage("src/data/skybox.jpg"));/*/ @@ -442,11 +458,16 @@ void Viewer::initShaders() s_texture->setMagnificationFilter(QOpenGLTexture::Linear); // load remaining textures - for(int i = 0; isetMinificationFilter(QOpenGLTexture::LinearMipMapLinear); - TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear); + for(int i = 0; isetMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear); + + std::cout << "Binding " << NormalPaths[i].toStdString() << endl; + TexturePrograms[TEX_LENGTH + i] = new QOpenGLTexture(QImage(NormalPaths[i])); + TexturePrograms[TEX_LENGTH + i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + TexturePrograms[TEX_LENGTH + i]->setMagnificationFilter(QOpenGLTexture::Linear); }//*/ } @@ -702,8 +723,12 @@ void Viewer::visit(Cube &s) QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QColor* faceColor = new QColor; - glBindVertexArray(m_VAOs[VAO_Cube]); - TexturePrograms[s.getType()]->bind(); + glBindVertexArray(m_VAOs[VAO_Cube]); + + glActiveTexture(GL_TEXTURE0); + TexturePrograms[s.getType()]->bind(); + glActiveTexture(GL_TEXTURE1); + TexturePrograms[TEX_LENGTH + s.getType()]->bind(); m_program->setUniformValue(m_isSkyLoc, false); m_program->setUniformValue(m_drawTextLoc, true); diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index c4ba9f5..635d328 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -100,7 +100,10 @@ private: QOpenGLShaderProgram *skyboxRenderShaderProgram; QOpenGLShaderProgram *m_program; int m_vPositionLocation; + int m_texColor; + int m_texNormal; int m_colorLocation; + int m_useNormalMap; int m_projMatrixLocation; int m_mvMatrixLocation; @@ -174,14 +177,22 @@ private: unsigned int selectedTexture = TEX_WOODFLOOR; - QString TexturePaths[TEX_LENGTH] = { - "src/data/dry_ground.jpg", - "src/data/granite_floor.jpg", - "src/data/grass.jpg", - "src/data/limestone_wall.jpg", - "src/data/pierre_bouchardee.jpg", - "src/data/wood_floor.jpg" - }; + QString TexturePaths[TEX_LENGTH] = { + "src/data/dry_ground.jpg", + "src/data/granite_floor.jpg", + "src/data/grass.jpg", + "src/data/limestone_wall.jpg", + "src/data/pierre_bouchardee.jpg", + "src/data/wood_floor.jpg" + }; + QString NormalPaths[TEX_LENGTH] = { + "src/data/dry_ground_normals.jpg", + "src/data/granite_floor_normals.jpg", + "src/data/grass_normals.jpg", + "src/data/limestone_wall_normals.jpg", + "src/data/pierre_bouchardee_normals.jpg", + "src/data/wood_floor_normals.jpg" + }; int sideColors[6] = { 0xFF0000, @@ -192,7 +203,7 @@ private: 0xFFFF00, }; - QOpenGLTexture *TexturePrograms[TEX_LENGTH]; + QOpenGLTexture *TexturePrograms[TEX_LENGTH * 2]; QMatrix4x4 identityMatrix; QQuaternion rot;