diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index e9c3b9a..0fba4d9 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -11,7 +11,9 @@ #include "textures/Texture2D.h" #include "util/stb_image.h" #include "util/camera/camera.h" +#include "object/game_object.h" +// TODO: Replace current rendering with he GameObject // Continue: https://learnopengl.com/Lighting/Colors // Chapter: A lighting scene // @@ -167,6 +169,7 @@ int main() -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; + std::vector vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) }; GLuint faces[]{ 0, 1, 3, @@ -203,9 +206,9 @@ int main() // position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - // uv-coordinates (or more general the third vertex attribute which can be anything not just uv-coordinates) - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(2); + // uv-coordinates (or more general the second vertex attribute which can be anything not just uv-coordinates) + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); // 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 std::string vertexPath = "shaders/default/default.vert"; @@ -227,7 +230,8 @@ int main() Texture2D containerImage("images/container.jpg"); Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA); - + std::vector verts(std::begin(vertices), std::end(vertices)); + Nebulix::GameObject cube = Nebulix::GameObject(verts, vertexAttribs); // main loop while(!glfwWindowShouldClose(window)) { @@ -254,7 +258,7 @@ int main() glBindVertexArray(vertexArrayObject); - for (int i = 0; i < 10; i++) // somehow cubePositions->length() does not return the correct number? + for (int i = 1; i < 10; i++) { mat4 model = mat4(1.0f); model = glm::translate(model, cubePositions[i]); @@ -267,6 +271,8 @@ int main() //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs glDrawArrays(GL_TRIANGLES, 0, 36); // for VAOs } + shader->SetMatrix("modelMatrix", cube.GetModelMatrix()); + cube.Draw(); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/Engine/object/game_object.h b/src/Engine/object/game_object.h index 84d62d1..8e16ab4 100644 --- a/src/Engine/object/game_object.h +++ b/src/Engine/object/game_object.h @@ -3,8 +3,6 @@ #include #include -#include "../shaders/Shader.h" - namespace Nebulix { struct VertexAttribute @@ -43,13 +41,13 @@ namespace Nebulix public: glm::vec3 Position; - GameObject(std::vector& vertices, std::vector vertexAttributes, Shader shader) - : vertices{ vertices }, vertexAttributes{ vertexAttributes }, shader{shader}, Position{glm::vec3(0.0f)} + GameObject(std::vector& vertices, std::vector vertexAttributes) + : vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{glm::vec3(0.0f)} { Init(); } - GameObject(std::vector &vertices, std::vector vertexAttributes, Shader shader, glm::vec3& position) - : vertices{vertices}, vertexAttributes{ vertexAttributes }, shader{ shader }, Position{position} + GameObject(std::vector &vertices, std::vector vertexAttributes, glm::vec3& position) + : vertices{vertices}, vertexAttributes{ vertexAttributes }, Position{position} { Init(); } @@ -60,11 +58,10 @@ namespace Nebulix { glBindVertexArray(vertexArray); - // TODO: Set shader matrices. Wait for materials - shader.Use(); + // TODO: Use shader; Set shader matrices. Wait for materials //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); // for VAOs + glDrawArrays(GL_TRIANGLES, 0, nrOfVertices); // for VAOs } @@ -77,11 +74,12 @@ namespace Nebulix private: std::vector vertices; - Shader shader; GLuint vertexBuffer, vertexArray; std::vector vertexAttributes; + int nrOfVertices; + void Init() { glGenVertexArrays(1, &vertexArray); @@ -89,7 +87,7 @@ namespace Nebulix glBindVertexArray(vertexArray); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW); int stride = 0; for (size_t i = 0; i < vertexAttributes.size(); i++) @@ -98,12 +96,22 @@ namespace Nebulix } int offset = 0; + int nrNonVertices = 0; for (size_t i = 0; i < vertexAttributes.size(); i++) { VertexAttribute attr = vertexAttributes[i]; glVertexAttribPointer(i, attr.NrOfElements, attr.AttributeType, attr.Normalised, stride, (void*)offset); glEnableVertexAttribArray(i); offset += attr.Stride; + + if (i != 0) + nrNonVertices += attr.NrOfElements; + } + + for (size_t i = 0; i < vertices.size(); i += 3) + { + nrOfVertices++; + i += nrNonVertices; } } }; diff --git a/src/Engine/shaders/default/default.vert b/src/Engine/shaders/default/default.vert index 182d35d..afa2115 100644 --- a/src/Engine/shaders/default/default.vert +++ b/src/Engine/shaders/default/default.vert @@ -1,7 +1,6 @@ #version 330 core layout (location = 0) in vec3 aPos; -//layout (location = 1) in vec3 aColour; -layout (location = 2) in vec2 aTexCoord; +layout (location = 1) in vec2 aTexCoord; out vec3 ourColour; out vec2 texCoord;