diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 962079f4..b52b4860 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -93,17 +93,20 @@ int main(int argc, char *argv[]) if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( #version 140 - in vec4 position; //This will be the position of the vertex in model-space + in vec4 position; // This will be the position of the vertex in model-space + in vec4 normal; // The normal data may not have been set uniform mat4 cameraToClipMatrix; uniform mat4 worldToCameraMatrix; uniform mat4 modelToWorldMatrix; out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals + out vec4 worldNormal; void main() { worldPosition = modelToWorldMatrix * position; + worldNormal = normal; vec4 cameraPosition = worldToCameraMatrix * worldPosition; gl_Position = cameraToClipMatrix * cameraPosition; } @@ -117,15 +120,20 @@ int main(int argc, char *argv[]) #version 130 in vec4 worldPosition; //Passed in from the vertex shader + in vec4 worldNormal; out vec4 outputColor; void main() { - vec3 normal = normalize(cross(dFdy(worldPosition.xyz), dFdx(worldPosition.xyz))); + //vec3 normal = normalize(cross(dFdy(worldPosition.xyz), dFdx(worldPosition.xyz))); + + vec3 normal = worldNormal.xyz; + + outputColor = vec4(normalize(normal.xyz), 1.0); - float color = clamp(abs(dot(normalize(normal.xyz), vec3(0.9,0.1,0.5))), 0, 1); - outputColor = vec4(1.0, 1.0, 1.0, 1.0); + //float color = clamp(abs(dot(normalize(normal.xyz), vec3(0.9,0.1,0.5))), 0, 1); + //outputColor = vec4(1.0, 0.5, color, 1.0); } )")) { diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.cpp index 1088d9fe..93d1a369 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.cpp @@ -56,7 +56,7 @@ void OpenGLWidget::initializeGL() if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( #version 140 - in vec4 position; //This will be the position of the vertex in model-space + in vec4 position; // This will be the position of the vertex in model-space uniform mat4 cameraToClipMatrix; uniform mat4 worldToCameraMatrix; @@ -80,6 +80,7 @@ void OpenGLWidget::initializeGL() #version 130 in vec4 worldPosition; //Passed in from the vertex shader + in vec4 worldNormal; out vec4 outputColor; @@ -97,6 +98,7 @@ void OpenGLWidget::initializeGL() } shader->bindAttributeLocation("position", 0); + shader->bindAttributeLocation("normal", 1); if (!shader->link()) { diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index 96102a99..eff6366a 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -86,7 +86,10 @@ public: //We need to tell OpenGL how to understand the format of the vertex data glEnableVertexAttribArray(0); //We're talking about shader attribute '0' - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), 0); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type) + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, position))); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type) + + glEnableVertexAttribArray(1); //We're talking about shader attribute '1' + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MeshType::VertexType), (GLvoid*)(offsetof(MeshType::VertexType, normal))); glBindVertexArray(0);