diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/exercise1/glsl/shader.vert b/exercise1/glsl/shader.vert index d49c70c..9cad82e 100644 --- a/exercise1/glsl/shader.vert +++ b/exercise1/glsl/shader.vert @@ -6,11 +6,14 @@ in vec4 in_position; in vec4 in_color; +uniform mat4 view; +uniform mat4 proj; + out vec4 fragment_color; void main() { - gl_Position = in_position; + gl_Position = proj * view * in_position; fragment_color = in_color; diff --git a/exercise1/include/Viewer.h b/exercise1/include/Viewer.h index 99810f3..261284c 100644 --- a/exercise1/include/Viewer.h +++ b/exercise1/include/Viewer.h @@ -35,6 +35,9 @@ private: color_buffer_id, // ID of the color buffer uv_map_buffer_id; // ID of the uv_map + GLint view_uniform_id; + GLint proj_uniform_id; + // Read, Compile and link the shader codes to a shader program void CreateShaders(); // Create and define the vertex array and add a number of vertex buffers diff --git a/exercise1/src/Viewer.cpp b/exercise1/src/Viewer.cpp index 06e42b6..f1ed00b 100644 --- a/exercise1/src/Viewer.cpp +++ b/exercise1/src/Viewer.cpp @@ -23,6 +23,9 @@ Viewer::Viewer() CreateShaders(); CreateVertexBuffers(); + view_uniform_id = glGetUniformLocation(program_id, "view"); + proj_uniform_id = glGetUniformLocation(program_id, "proj"); + modelViewMatrix.setIdentity(); projectionMatrix.setIdentity(); @@ -56,25 +59,40 @@ void Viewer::CreateVertexBuffers() // Define 3 vertices for one face GLfloat positions[] = { - 0, 1, 0, 1, - -1, -1, 0, 1, - 1, -1, 0, 1}; + -1, 1, -1, 1, + 1, -1, -1, 1, + 1, 1, 1, 1, + + -1, 1, -1, 1, + -1, -1, 1, 1, + 1, -1, -1, 1, + + -1, 1, -1, 1, + -1, -1, 1, 1, + 1, 1, 1, 1, + + 1, -1, -1, 1, + 1, 1, 1, 1, + -1, -1, 1, 1, + + }; GLfloat colors[] = { - 1, - 0, - 0, - 1, + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1, - 0, - 1, - 0, - 1, + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1, - 0, - 0, - 1, - 1, + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1, + + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1, }; // Generate the vertex array @@ -234,10 +252,13 @@ void Viewer::drawContents() then set them with the command glUniformMatrix4fv. */ + glUniformMatrix4fv(view_uniform_id, 1, false, modelViewMatrix.data()); + glUniformMatrix4fv(proj_uniform_id, 1, false, projectionMatrix.data()); + // Bind the vertex array glBindVertexArray(vertex_array_id); // Draw the bound vertex array. Start at element 0 and draw 3 vertices - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawArrays(GL_TRIANGLES, 0, 12); /*** End of task 1.2.4 (b) ***/