From 5bd42f50bbdd3da44437aa8df91a4c2f3ba6d1b8 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Fri, 3 Jan 2025 20:33:17 -0700 Subject: [PATCH] projection and model matrices --- 2dGameProject/main.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/2dGameProject/main.cpp b/2dGameProject/main.cpp index a8f22fb..5712632 100644 --- a/2dGameProject/main.cpp +++ b/2dGameProject/main.cpp @@ -4,7 +4,9 @@ #include #include "Window.h" #include -#include +#include +#include +#include #include #include #include @@ -15,10 +17,10 @@ int OpenGLVersion; spdlog::logger logger("none"); float square[] = { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left + 1.0f, 1.0f, 0.0f, // top right + 1.0f, -1.0f, 0.0f, // bottom right + -1.0f, -1.0f, 0.0f, // bottom left + -1.0f, 1.0f, 0.0f // top left }; unsigned int squareIndices[]{ @@ -36,10 +38,12 @@ float squareColors[] = { const char* vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "layout (location = 1) in vec4 color;\n" +"uniform mat4 projection;\n" +"uniform mat4 model;\n" "out vec4 vertexColor;\n" "void main()\n" "{\n" -" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" +" gl_Position = projection * model * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" " vertexColor = color;\n" "}\0"; @@ -166,6 +170,11 @@ int main() { float interval = 0.001; glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + unsigned int projMatLocation = glGetUniformLocation(shaderProgram, "projection"); + glUniformMatrix4fv(projMatLocation, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 0.0f, 600.0f))); + + unsigned int modelMatLocation = glGetUniformLocation(shaderProgram, "model"); + glUniformMatrix4fv(modelMatLocation, 1, GL_FALSE, glm::value_ptr(glm::scale(glm::translate(glm::mat4(1.0), glm::vec3(400, 300, 0)), glm::vec3(100, 100, 1)))); while (!window->isClosing()) { if (up) { squareColors[0] += interval;