From d5788228f665ec077e6b97bede899b3d7764798b Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 13 Jan 2021 13:17:31 -0600 Subject: [PATCH] Show my square --- core/Ball.cpp | 8 +++++--- core/Shader.cpp | 15 ++++++++++++++- core/Shader.h | 5 +++++ core/couch.cpp | 12 +++++++++++- core/types.h | 1 + shaders/flat.vert | 9 ++++----- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/core/Ball.cpp b/core/Ball.cpp index 7748d65..7ee30f2 100644 --- a/core/Ball.cpp +++ b/core/Ball.cpp @@ -1,10 +1,12 @@ #include "Ball.h" Ball::Ball() { - vertices.push_back(Vertex(0.0f, 0.0f, 0.0f)); - vertices.push_back(Vertex(1.0f, 0.0f, 0.0f)); - vertices.push_back(Vertex(0.5f, 1.0f, 0.0f)); + vertices.push_back(Vertex(1.0f, 1.0f, -1.0f)); + vertices.push_back(Vertex(1.0f, -1.0f, -1.0f)); + vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f)); + vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f)); indices.push_back(Index(0, 1, 2)); + indices.push_back(Index(1, 2, 3)); } diff --git a/core/Shader.cpp b/core/Shader.cpp index 6fb9210..e146806 100644 --- a/core/Shader.cpp +++ b/core/Shader.cpp @@ -65,9 +65,22 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) { glDeleteShader(vertex); glDeleteShader(fragment); - } + + void Shader::Use() { glUseProgram(id); } + +void Shader::UpdateView(Matrix view) { + glUniformMatrix4fv(glGetUniformLocation(id, "VIEW"), 1, GL_FALSE, glm::value_ptr(view)); +} + +void Shader::UpdateModel(Matrix model) { + glUniformMatrix4fv(glGetUniformLocation(id, "MODEL"), 1, GL_FALSE, glm::value_ptr(model)); +} + +void Shader::UpdateProjection(Matrix projection) { + glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection)); +} diff --git a/core/Shader.h b/core/Shader.h index 8bd981d..32e5d93 100644 --- a/core/Shader.h +++ b/core/Shader.h @@ -6,6 +6,8 @@ #include #include +#include + #include "types.h" class Shader { @@ -14,6 +16,9 @@ public: Shader(const char *vertexPath, const char *fragmentPath); void Use(); + void UpdateView(Matrix view); + void UpdateModel(Matrix model); + void UpdateProjection(Matrix projection); }; #endif /* SHADER_H */ diff --git a/core/couch.cpp b/core/couch.cpp index 122b01d..6028a1b 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include "types.h" #include "Shader.h" @@ -42,10 +44,18 @@ int main() { Ball ball; ball.SetupMesh(); + Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f); + shader.UpdateProjection(projection); + Matrix view(1.0f); + shader.UpdateView(view); + while(!glfwWindowShouldClose(window)) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - + + Matrix model(1.0f); + model = glm::translate(model, ball.transform.position); + shader.UpdateModel(model); ball.Draw(); glfwSwapBuffers(window); diff --git a/core/types.h b/core/types.h index e9d6418..7f57050 100644 --- a/core/types.h +++ b/core/types.h @@ -9,6 +9,7 @@ typedef GLFWwindow Window; typedef glm::vec3 Vector3; +typedef glm::mat4 Matrix; typedef GLfloat cfloat; typedef GLuint Id; diff --git a/shaders/flat.vert b/shaders/flat.vert index 803d897..81a7b73 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -2,11 +2,10 @@ layout (location = 0) in vec3 pos; -// uniform mat4 MODEL; -// uniform mat4 VIEW; -// uniform mat4 PROJECTION; +uniform mat4 MODEL; +uniform mat4 VIEW; +uniform mat4 PROJECTION; void main() { - //gl_Position = MODEL * VIEW * PROJECTION * vec4(pos, 1.0); - gl_Position = vec4(pos, 1.0); + gl_Position = PROJECTION * VIEW * MODEL * vec4(pos, 1.0); }