Normal Maps

This commit is contained in:
Riku Avelar 2016-11-30 15:20:47 -05:00
parent 3c7ad6d8da
commit f9f86d3700
15 changed files with 84 additions and 30 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 472 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 KiB

BIN
src/data/grass_normals.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 472 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 KiB

View File

@ -1,8 +1,10 @@
#version 400 core #version 400 core
uniform sampler2D texCol;
uniform sampler2D texNormal;
uniform vec3 lDirection; uniform vec3 lDirection;
uniform bool isSky; uniform bool isSky;
uniform bool isPhong; uniform bool isPhong;
uniform sampler2D tex;
uniform float skyMult; uniform float skyMult;
uniform bool drawTextures; uniform bool drawTextures;
uniform bool isLightSource; uniform bool isLightSource;
@ -10,6 +12,7 @@ uniform bool isPickingMode;
uniform vec3 pointLight[3]; uniform vec3 pointLight[3];
uniform vec4 pointLightCol[3]; uniform vec4 pointLightCol[3];
uniform mat3 normalMatrix; uniform mat3 normalMatrix;
uniform bool useNormalMap;
in vec3 fNormal; in vec3 fNormal;
in vec3 fPosition; in vec3 fPosition;
@ -21,9 +24,16 @@ out vec4 fColor;
vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(lDirection); vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNorm); vec3 nviewDirection = normalize(fPos);
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 // Compute diffuse component
float diff = 0.2*max(0.0, dot(nfNormal, -LightDirection)); 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); float mult = 1;//max(0.0, -LightDirection.y+1.5);
//return vec4(0); //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) { vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(pointLight[i] + fPos); vec3 LightDirection = normalize(pointLight[i] + fPos);
vec3 nfNormal = normalize(fNorm); // vec3 nfNormal = normalize(fNorm);
vec3 nviewDirection = normalize(fPos); vec3 nviewDirection = normalize(fPos);
vec3 nfNormal;
if(useNormalMap) {
vec3 vNorm = texture(texNormal, texCoords).rgb;
nfNormal = normalize(normalMatrix * vNorm);
} else {
nfNormal = normalize(fNorm);
}
// Attenuation // Attenuation
float distance = length(nviewDirection - pointLight[i] - fPos) / 3; 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 // Compute ambient component
float amb = 0.2; 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 void
@ -74,12 +92,12 @@ main()
fColor = ifColor; fColor = ifColor;
}else{ }else{
if(drawTextures) { if(drawTextures) {
texColor = texture(tex, texCoords); texColor = texture(texCol, texCoords);
} else { } else {
texColor = ifColor; texColor = ifColor;
} }
if(isLightSource) { if(isLightSource) {
fColor = texColor; fColor = texColor;
} else if(isSky) { } else if(isSky) {
fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2; fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2;
@ -89,7 +107,7 @@ main()
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition); vec3 nviewDirection = normalize(fPosition);
fColor = calcDirLight(texColor, fPosition, fNormal) fColor = calcDirLight(texColor, fPosition, fNormal)
+ calcPointLight(texColor, fPosition, fNormal, 0)/4 + calcPointLight(texColor, fPosition, fNormal, 0)/4
+ calcPointLight(texColor, fPosition, fNormal, 1)/4 + calcPointLight(texColor, fPosition, fNormal, 1)/4
+ calcPointLight(texColor, fPosition, fNormal, 2)/4; + calcPointLight(texColor, fPosition, fNormal, 2)/4;

View File

@ -103,6 +103,8 @@ void Viewer::cleanup()
void Viewer::drawSkybox() void Viewer::drawSkybox()
{ {
// Use the Skybox Shaders // Use the Skybox Shaders
glActiveTexture(GL_TEXTURE0);
m_program->bind(); m_program->bind();
s_texture->bind(); s_texture->bind();
// Get projection and camera transformations // Get projection and camera transformations
@ -194,7 +196,8 @@ void Viewer::drawUi(){
uiViewMatrix.scale(.25, .25, .01); uiViewMatrix.scale(.25, .25, .01);
uiViewMatrix.translate(-TEX_LENGTH/2.0, 0, 0); uiViewMatrix.translate(-TEX_LENGTH/2.0, 0, 0);
for(int i = 0; i<TEX_LENGTH; i++) for(int i = 0; i<TEX_LENGTH; i++)
{ {
glActiveTexture(GL_TEXTURE0);
TexturePrograms[i]->bind(); TexturePrograms[i]->bind();
m_program->setUniformValue(m_mvMatrixLocation, uiViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, uiViewMatrix);
uiViewMatrix.translate(1.2, 0, 0); uiViewMatrix.translate(1.2, 0, 0);
@ -371,8 +374,17 @@ void Viewer::initShaders()
if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0)
qDebug() << "Unable to find shader location for " << "vPosition"; qDebug() << "Unable to find shader location for " << "vPosition";
if ((m_colorLocation = m_program->uniformLocation("color")) < 0) if ((m_colorLocation = m_program->uniformLocation("color")) < 0)
qDebug() << "Unable to find shader location for " << "color"; 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) if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "mvMatrix"; 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_isPhongLoc, true);
m_program->setUniformValue(m_drawTextLoc, false); m_program->setUniformValue(m_drawTextLoc, false);
m_program->setUniformValue(m_isPickingModeLoc, 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"));/*/ s_texture = new QOpenGLTexture(QImage("src/data/skybox.jpg"));/*/
@ -442,11 +458,16 @@ void Viewer::initShaders()
s_texture->setMagnificationFilter(QOpenGLTexture::Linear); s_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// load remaining textures // load remaining textures
for(int i = 0; i<TEX_LENGTH; i++){ for(int i = 0; i<TEX_LENGTH; i++){
std::cout << "Binding " << TexturePaths[i].toStdString() << endl; std::cout << "Binding " << TexturePaths[i].toStdString() << endl;
TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i])); TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i]));
TexturePrograms[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); TexturePrograms[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear); 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); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QColor* faceColor = new QColor; QColor* faceColor = new QColor;
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
TexturePrograms[s.getType()]->bind();
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_isSkyLoc, false);
m_program->setUniformValue(m_drawTextLoc, true); m_program->setUniformValue(m_drawTextLoc, true);

View File

@ -100,7 +100,10 @@ private:
QOpenGLShaderProgram *skyboxRenderShaderProgram; QOpenGLShaderProgram *skyboxRenderShaderProgram;
QOpenGLShaderProgram *m_program; QOpenGLShaderProgram *m_program;
int m_vPositionLocation; int m_vPositionLocation;
int m_texColor;
int m_texNormal;
int m_colorLocation; int m_colorLocation;
int m_useNormalMap;
int m_projMatrixLocation; int m_projMatrixLocation;
int m_mvMatrixLocation; int m_mvMatrixLocation;
@ -174,14 +177,22 @@ private:
unsigned int selectedTexture = TEX_WOODFLOOR; unsigned int selectedTexture = TEX_WOODFLOOR;
QString TexturePaths[TEX_LENGTH] = { QString TexturePaths[TEX_LENGTH] = {
"src/data/dry_ground.jpg", "src/data/dry_ground.jpg",
"src/data/granite_floor.jpg", "src/data/granite_floor.jpg",
"src/data/grass.jpg", "src/data/grass.jpg",
"src/data/limestone_wall.jpg", "src/data/limestone_wall.jpg",
"src/data/pierre_bouchardee.jpg", "src/data/pierre_bouchardee.jpg",
"src/data/wood_floor.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] = { int sideColors[6] = {
0xFF0000, 0xFF0000,
@ -192,7 +203,7 @@ private:
0xFFFF00, 0xFFFF00,
}; };
QOpenGLTexture *TexturePrograms[TEX_LENGTH]; QOpenGLTexture *TexturePrograms[TEX_LENGTH * 2];
QMatrix4x4 identityMatrix; QMatrix4x4 identityMatrix;
QQuaternion rot; QQuaternion rot;