From 350aef487193b524746a706bbc12a48e106f104f Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 31 Jul 2023 11:51:09 +0200 Subject: [PATCH] started with coordinate system section --- src/Engine/main.cpp | 105 ++++++++++++++++++------ src/Engine/shaders/default/default.vert | 14 +++- 2 files changed, 92 insertions(+), 27 deletions(-) diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index e7e35f8..0380b20 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -11,7 +11,7 @@ #include "util/stb_image.h" // Continue: https://learnopengl.com/Getting-started/Coordinate-Systems -// Chapter: not started +// Chapter: More Cubes! // // TODO: look at project->properties->VC++ Directories-> change everything to the Environment var // For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS (so basically having an "external" folder which has open gl, GLAD, etc. in it or something like this) @@ -27,6 +27,8 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 +typedef glm::mat4 mat4; +typedef glm::vec3 vec3; void OnWindowResize(GLFWwindow* window, int width, int height) @@ -68,16 +70,69 @@ int main() OnWindowResize(window, WINDOW_WIDTH, WINDOW_HEIGHT); glfwSetFramebufferSizeCallback(window, OnWindowResize); - GLfloat vertices[] = { // world coordinate, colour, uv coordinates - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f // top left + float vertices[] = { // object coordinates, uv-coordinates + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.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 5, 6, + 5, 6, 7, + + 8, 9, 10, + 9, 10, 11, + + 12, 13, 14, + 13, 14, 15, + + 16, 17, 18, + 17, 18, 19, + + 20, 21, 22, + 21, 22, 23 + }; // 23 // VBO == all verticies; EBO == connection of vertices (faces) and is optional although it is a bit more efficient than using VBOs; // VAO == collection of VBOs/EBOs used for rendering @@ -95,13 +150,10 @@ int main() glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW); // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - // colour (or more general, the second vertex Attribute) - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(1); // uv-coordinates (or more general the third vertex attribute) - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(2); // I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem @@ -156,27 +208,32 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // use nearest neighbour when zooming out glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // use bilinear when zooming in + mat4 modelMatrix = mat4(1.0f); + modelMatrix = glm::rotate(modelMatrix, glm::radians(-55.0f), vec3(1.0f, 0, 0)); + mat4 viewMatrix = mat4(1.0f); + viewMatrix = glm::translate(viewMatrix, vec3(0, 0, -3.0f)); + mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + glEnable(GL_DEPTH_TEST); + float timeLastFrame = glfwGetTime(); // main loop while(!glfwWindowShouldClose(window)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ProcessInput(window); + + float time = glfwGetTime(); - glm::mat4 transform = glm::mat4(1.0f); - transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0, 0, 1.0f)); - transform = glm::translate(transform, glm::vec3(glm::sin((float)glfwGetTime()), 0, 0)); - //transform = glm::scale(transform, glm::vec3(glm::sin((float)glfwGetTime()))); + modelMatrix = glm::rotate(modelMatrix, time * (time - timeLastFrame) + 0.001f * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f)); shader->Use(); shader->SetInt("ourTexture1", 0); shader->SetInt("ourTexture2", 1); - shader->SetMatrix("transform", transform); - //float time = glfwGetTime(); - //float greenValue = (sin(time) / 2.0f) + 0.5f; - //shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f); + shader->SetMatrix("modelMatrix", modelMatrix); + shader->SetMatrix("viewMatrix", viewMatrix); + shader->SetMatrix("projectionMatrix", projectionMatrix); glActiveTexture(GL_TEXTURE0); // before binding texture, activate correct textre Unit (some drivers might show nothing if this is omitted) glBindTexture(GL_TEXTURE_2D, textureID_container); @@ -184,10 +241,12 @@ int main() glBindTexture(GL_TEXTURE_2D, textureID_face); glBindVertexArray(vertexArrayObject); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs + glDrawArrays(GL_TRIANGLES, 0, 36); // for VAOs glfwSwapBuffers(window); glfwPollEvents(); + timeLastFrame = glfwGetTime(); } // optional, as we are at the end of the program. diff --git a/src/Engine/shaders/default/default.vert b/src/Engine/shaders/default/default.vert index 56b8b19..182d35d 100644 --- a/src/Engine/shaders/default/default.vert +++ b/src/Engine/shaders/default/default.vert @@ -1,16 +1,22 @@ #version 330 core layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColour; +//layout (location = 1) in vec3 aColour; layout (location = 2) in vec2 aTexCoord; out vec3 ourColour; out vec2 texCoord; -uniform mat4 transform; +uniform mat4 modelMatrix; +uniform mat4 viewMatrix; +uniform mat4 projectionMatrix; + +//uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); - ourColour = aColour; + // note that we read the multiplication from right to left + gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0); + //gl_Position = transform * vec4(aPos, 1.0); + ourColour = vec3(1.0); texCoord = aTexCoord; }