From d47424658682fe9a6dca6ecb340141b9874de76c Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 7 Aug 2023 16:41:17 +0200 Subject: [PATCH] finished chapter "Colors" --- src/Engine/Engine.vcxproj | 4 ++ src/Engine/Engine.vcxproj.filters | 4 ++ src/Engine/main.cpp | 57 ++++++++++++++++++------- src/Engine/object/game_object.h | 17 ++++---- src/Engine/shaders/default/default.frag | 4 +- src/Engine/shaders/default/simple.frag | 7 +++ src/Engine/shaders/default/simple.vert | 12 ++++++ src/Engine/vertices.h | 2 +- 8 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 src/Engine/shaders/default/simple.frag create mode 100644 src/Engine/shaders/default/simple.vert diff --git a/src/Engine/Engine.vcxproj b/src/Engine/Engine.vcxproj index 1163116..12ba5e3 100644 --- a/src/Engine/Engine.vcxproj +++ b/src/Engine/Engine.vcxproj @@ -169,6 +169,10 @@ + + + + diff --git a/src/Engine/Engine.vcxproj.filters b/src/Engine/Engine.vcxproj.filters index e908a9c..624be92 100644 --- a/src/Engine/Engine.vcxproj.filters +++ b/src/Engine/Engine.vcxproj.filters @@ -83,4 +83,8 @@ Resource Files + + + + \ No newline at end of file diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index b3ea354..ef5fe2f 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -15,8 +15,8 @@ #include "vertices.h" -// Continue: https://learnopengl.com/Lighting/Colors -// Chapter: A lighting scene +// Continue: https://learnopengl.com/Lighting/Basic-Lighting +// Chapter: Not yet started // // 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 @@ -63,7 +63,8 @@ void ProcessInput(GLFWwindow* window) cam.ProcessKeyboard(MovementDirection::RIGHT, deltaTime); if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) cam.ProcessKeyboard(MovementDirection::UP, deltaTime); - if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) + if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS + || glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) cam.ProcessKeyboard(MovementDirection::DOWN, deltaTime); } @@ -147,13 +148,17 @@ int main() }; - std::string vertexPath = "shaders/default/default.vert"; - std::string fragmentPath = "shaders/default/default.frag"; + std::string lightingVertexPath = "shaders/default/default.vert", lampVertexPath = "shaders/default/simple.vert"; + std::string lightingFragmentPath = "shaders/default/default.frag", lampFragmentPath = "shaders/default/simple.frag"; - std::unique_ptr shader; + std::shared_ptr lighting, lamp; + std::vector> allShaders = std::vector>(); try { - shader = std::make_unique(vertexPath, fragmentPath); + lighting = std::make_shared(lightingVertexPath, lightingFragmentPath); + lamp = std::make_shared(lampVertexPath, lampFragmentPath); + allShaders.push_back(lighting); + allShaders.push_back(lamp); } catch (const std::exception &e) { @@ -166,9 +171,16 @@ int main() //Texture2D containerImage("images/container.jpg"); //Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA); - std::vector verts(std::begin(vertices_container), std::end(vertices_container)); - std::vector vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) }; - Nebulix::GameObject cube = Nebulix::GameObject(verts, vertexAttribs); + std::vector verts(std::begin(vertices_cube), std::end(vertices_cube)); + std::vector vertexAttribs = { Nebulix::VertexAttribute() }; + Nebulix::GameObject cube(verts, vertexAttribs); + vec3 lightPosition = glm::vec3(1.2f, 1.0f, 2.0f); + Nebulix::GameObject lightBulb(verts, vertexAttribs, lightPosition, glm::vec3(0.2f)); + + std::vector allObjects = std::vector(); + allObjects.push_back(cube); + allObjects.push_back(lightBulb); + // main loop while(!glfwWindowShouldClose(window)) { @@ -176,18 +188,31 @@ int main() deltaTime = currentFrameTime - lastFrameTime; lastFrameTime = currentFrameTime; - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ProcessInput(window); mat4 projectionMatrix = glm::perspective(glm::radians(cam.Fov), 800.0f / 600.0f, 0.1f, 100.0f); - shader->Use(); - shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera - shader->SetMatrix("projectionMatrix", projectionMatrix); - shader->SetMatrix("modelMatrix", cube.GetModelMatrix()); - cube.Draw(); + for (size_t i = 0; i < allShaders.size(); i++) + { + auto shader = allShaders[i]; + shader->Use(); + shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera + shader->SetMatrix("projectionMatrix", projectionMatrix); + shader->SetMatrix("modelMatrix", allObjects[i].GetModelMatrix()); + } + + lighting->Use(); + lighting->SetFloat("lightColour", 1.0f, 1.0f, 1.0f); + lighting->SetFloat("objectColour", 1.0f, 0.5f, 0.31f); + + for (size_t i = 0; i < allObjects.size(); i++) + { + allShaders[i]->Use(); + allObjects[i].Draw(); + } glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/Engine/object/game_object.h b/src/Engine/object/game_object.h index d26f5a0..025d962 100644 --- a/src/Engine/object/game_object.h +++ b/src/Engine/object/game_object.h @@ -40,14 +40,11 @@ namespace Nebulix { public: glm::vec3 Position; + glm::vec3 Scale; - GameObject(std::vector& vertices, std::vector vertexAttributes) - : vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{glm::vec3(0.0f)} - { - Init(); - } - GameObject(std::vector &vertices, std::vector vertexAttributes, glm::vec3& position) - : vertices{vertices}, vertexAttributes{ vertexAttributes }, Position{position} + GameObject(std::vector& vertices, std::vector vertexAttributes, + glm::vec3 position = glm::vec3(0.0f), glm::vec3 scale = glm::vec3(1.0f)) + : vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{position}, Scale{scale} { Init(); } @@ -57,7 +54,7 @@ namespace Nebulix glDeleteBuffers(1, &vertexBuffer); } - // NOTE: This method will only call "Shader.Use()" but will not set any properties. + // NOTE: This method will not call "Shader.Use()" but will not set any properties. // This is because I do not know the names and values of the properties. Most likely this will be possible after learning about materials void Draw() { @@ -74,7 +71,9 @@ namespace Nebulix { glm::mat4 model = glm::mat4(1.0f); // TODO: add properties for rotation/scale and add these to the model matrix - return glm::translate(model, Position); + model = glm::translate(model, Position); + model = glm::scale(model, Scale); + return model; } private: diff --git a/src/Engine/shaders/default/default.frag b/src/Engine/shaders/default/default.frag index 3214fb3..1df68ff 100644 --- a/src/Engine/shaders/default/default.frag +++ b/src/Engine/shaders/default/default.frag @@ -1,7 +1,9 @@ #version 330 core out vec4 FragColor; +uniform vec3 lightColour, objectColour; + void main() { - FragColor = vec4(1.0); + FragColor = vec4(lightColour * objectColour, 1.0); } \ No newline at end of file diff --git a/src/Engine/shaders/default/simple.frag b/src/Engine/shaders/default/simple.frag new file mode 100644 index 0000000..ab6371f --- /dev/null +++ b/src/Engine/shaders/default/simple.frag @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); +} \ No newline at end of file diff --git a/src/Engine/shaders/default/simple.vert b/src/Engine/shaders/default/simple.vert new file mode 100644 index 0000000..c9f0c55 --- /dev/null +++ b/src/Engine/shaders/default/simple.vert @@ -0,0 +1,12 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 modelMatrix; +uniform mat4 viewMatrix; +uniform mat4 projectionMatrix; + +void main() +{ + // note that we read the multiplication from right to left + gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0); +} diff --git a/src/Engine/vertices.h b/src/Engine/vertices.h index fc1a867..a551f56 100644 --- a/src/Engine/vertices.h +++ b/src/Engine/vertices.h @@ -1,7 +1,7 @@ #pragma once -float vertices_light[] = { +float vertices_cube[] = { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f,