Phong, but direction is fucked

This commit is contained in:
Riku Avelar 2016-11-09 20:15:54 -05:00
parent a883722d25
commit 9b8c991307
4 changed files with 82 additions and 10 deletions

View File

@ -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);
}

View File

@ -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;
vec4 vEyeCoord = mvMatrix * vPosition;
gl_Position = projMatrix * vEyeCoord;
fPosition = vEyeCoord.xyz;
fNormal = normalMatrix * vNormal;
ifColor = color;
}

View File

@ -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);
}

View File

@ -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();
}
@ -260,6 +264,15 @@ void Viewer::initShaders()
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
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";
/*
* Adding Texture Shader
@ -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);
m_program->setUniformValue(m_normalMatrixLoc, lookAt.normalMatrix());
m_program->setUniformValue(m_colorLocation, s.getColor());
glDrawArrays(GL_TRIANGLES, i*6, 6);