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 #version 400 core
uniform vec3 lDirection;
in vec3 fNormal;
in vec3 fPosition;
in vec4 ifColor; in vec4 ifColor;
out vec4 fColor; out vec4 fColor;
void void
main() 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 #version 400 core
uniform mat4 mvMatrix; uniform mat4 mvMatrix;
uniform mat4 projMatrix; uniform mat4 projMatrix;
uniform mat3 normalMatrix;
uniform vec4 color; uniform vec4 color;
in vec4 vPosition; in vec4 vPosition;
in vec3 vNormal;
out vec4 ifColor; out vec4 ifColor;
out vec3 fPosition;
out vec3 fNormal;
void void
main() main()
{ {
gl_Position = projMatrix * mvMatrix * vPosition; vec4 vEyeCoord = mvMatrix * vPosition;
ifColor = color; gl_Position = projMatrix * vEyeCoord;
fPosition = vEyeCoord.xyz;
fNormal = normalMatrix * vNormal;
ifColor = color;
} }

View File

@ -7,5 +7,5 @@ uniform sampler2D skybox;
void main() void main()
{ {
//fColor = texture(skybox, texCoords); //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; int vUVLocation;
GLuint Buffers[NumBuffers]; GLuint Buffers[NumBuffers];
int m_lDirectionLocation;
int m_vNormalLocation;
int m_normalMatrixLoc;
} }
class SkyboxCamera : public qglviewer::Camera class SkyboxCamera : public qglviewer::Camera
@ -144,7 +148,7 @@ void Viewer::draw()
// Traverse the Scene in order to draw its components // Traverse the Scene in order to draw its components
modelStack.push(modelViewMatrix); modelStack.push(modelViewMatrix);
//root.accept(*this); root.accept(*this);
frame++; frame++;
update(); update();
} }
@ -258,7 +262,16 @@ void Viewer::initShaders()
qDebug() << "Unable to find shader location for " << "mvMatrix"; qDebug() << "Unable to find shader location for " << "mvMatrix";
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix"; 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";
/* /*
@ -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]); glBindVertexArray(m_VAOs[VAO_Cube]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
glGenBuffers(NumBuffers, Buffers); glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); 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); NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verticesCube), verticesCube); 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), 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]); // glBindVertexArray(m_VAOs[VAO_Cube]);
// glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); // glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
@ -403,16 +436,24 @@ void Viewer::initGeometries()
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube))); GL_FALSE, 0, BUFFER_OFFSET(sizeof(verticesCube)));
glEnableVertexAttribArray(s_vUvLocation); 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) void Viewer::visit(Cube &s)
{ {
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QMatrix4x4 lookAt;
camera()->getProjectionMatrix(lookAt);
int faces = floor(numVerticesCube/6); int faces = floor(numVerticesCube/6);
for(int i = 0; i < faces; i++){ // 6 vertexes par face for(int i = 0; i < faces; i++){ // 6 vertexes par face
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
m_program->setUniformValue(m_normalMatrixLoc, lookAt.normalMatrix());
m_program->setUniformValue(m_colorLocation, s.getColor()); m_program->setUniformValue(m_colorLocation, s.getColor());
glDrawArrays(GL_TRIANGLES, i*6, 6); glDrawArrays(GL_TRIANGLES, i*6, 6);