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
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;
@ -22,8 +25,15 @@ 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 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));
@ -44,8 +54,16 @@ vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) {
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;
@ -74,7 +92,7 @@ main()
fColor = ifColor;
}else{
if(drawTextures) {
texColor = texture(tex, texCoords);
texColor = texture(texCol, texCoords);
} else {
texColor = ifColor;
}

View File

@ -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
@ -195,6 +197,7 @@ void Viewer::drawUi(){
uiViewMatrix.translate(-TEX_LENGTH/2.0, 0, 0);
for(int i = 0; i<TEX_LENGTH; i++)
{
glActiveTexture(GL_TEXTURE0);
TexturePrograms[i]->bind();
m_program->setUniformValue(m_mvMatrixLocation, uiViewMatrix);
uiViewMatrix.translate(1.2, 0, 0);
@ -374,6 +377,15 @@ void Viewer::initShaders()
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"));/*/
@ -447,6 +463,11 @@ void Viewer::initShaders()
TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i]));
TexturePrograms[i]->setMinificationFilter(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);
}//*/
}
@ -703,7 +724,11 @@ void Viewer::visit(Cube &s)
QColor* faceColor = new QColor;
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);

View File

@ -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;
@ -182,6 +185,14 @@ private:
"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;