From 5c42364e4353ed5f8cf0cf43b6cfe29af77656a1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 4 Aug 2023 22:33:17 +0200 Subject: [PATCH] added GameObject class for future use (replace current renering with this class before continuing with the chapters) --- src/Engine/Engine.vcxproj | 2 +- src/Engine/Engine.vcxproj.filters | 6 +- src/Engine/main.cpp | 2 +- src/Engine/object/game_object.h | 110 ++++++++++++++++++++++++++++++ src/Engine/util/camera/camera.cpp | 1 - 5 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 src/Engine/object/game_object.h delete mode 100644 src/Engine/util/camera/camera.cpp diff --git a/src/Engine/Engine.vcxproj b/src/Engine/Engine.vcxproj index d6d4c6e..6ef7053 100644 --- a/src/Engine/Engine.vcxproj +++ b/src/Engine/Engine.vcxproj @@ -138,7 +138,6 @@ - @@ -152,6 +151,7 @@ + diff --git a/src/Engine/Engine.vcxproj.filters b/src/Engine/Engine.vcxproj.filters index 4e69393..bb4b653 100644 --- a/src/Engine/Engine.vcxproj.filters +++ b/src/Engine/Engine.vcxproj.filters @@ -27,9 +27,6 @@ Source Files - - Source Files - @@ -62,6 +59,9 @@ Header Files + + Header Files + diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 09ef7c6..e9c3b9a 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -13,7 +13,7 @@ #include "util/camera/camera.h" // Continue: https://learnopengl.com/Lighting/Colors -// Chapter: Not yet started +// Chapter: A lighting scene // // 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 diff --git a/src/Engine/object/game_object.h b/src/Engine/object/game_object.h new file mode 100644 index 0000000..84d62d1 --- /dev/null +++ b/src/Engine/object/game_object.h @@ -0,0 +1,110 @@ +#pragma once +#include +#include +#include + +#include "../shaders/Shader.h" + +namespace Nebulix +{ + struct VertexAttribute + { + GLenum AttributeType = GL_FLOAT; + GLboolean Normalised = GL_FALSE; + /// + /// The number of elements for this attribute. + /// For example: 1 float value for each the x,y,z coordinate of the vertex which means this variable should be set to 3 + /// + int NrOfElements = 3; + int Stride; + + VertexAttribute(GLenum attributeType = GL_FLOAT, GLboolean normalised = GL_FALSE, int nrOfElements = 3) + : AttributeType{ attributeType }, Normalised{ normalised }, NrOfElements{ nrOfElements } + { + if (attributeType == GL_INT) + Stride = nrOfElements * sizeof(int); + else if (attributeType == GL_FLOAT) + Stride = nrOfElements * sizeof(float); + else if (attributeType == GL_DOUBLE) + Stride = nrOfElements * sizeof(double); + else if (attributeType == GL_BOOL) + Stride = nrOfElements * sizeof(bool); + else if (attributeType == GL_BYTE) + Stride = nrOfElements * sizeof(char); + else + throw std::exception("ERROR::CONSTRUCT::VERTEX_ATTRIBUTE::NOT_IMPLEMENTED"); + } + }; + + // TODO: move the triangle stuff to a seperate mesh class and use the mesh class here + // (in the final engine, this mesh class will be a component and a game object will just hold a list of different components) + class GameObject + { + public: + glm::vec3 Position; + + GameObject(std::vector& vertices, std::vector vertexAttributes, Shader shader) + : vertices{ vertices }, vertexAttributes{ vertexAttributes }, shader{shader}, 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} + { + Init(); + } + + // NOTE: This method will only 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() + { + glBindVertexArray(vertexArray); + + // TODO: Set shader matrices. Wait for materials + shader.Use(); + + //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); // for VAOs + + } + + glm::mat4 GetModelMatrix() + { + 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); + } + + private: + std::vector vertices; + Shader shader; + + GLuint vertexBuffer, vertexArray; + std::vector vertexAttributes; + + void Init() + { + glGenVertexArrays(1, &vertexArray); + glGenBuffers(1, &vertexBuffer); + glBindVertexArray(vertexArray); + + glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW); + + int stride = 0; + for (size_t i = 0; i < vertexAttributes.size(); i++) + { + stride += vertexAttributes[i].Stride; + } + + int offset = 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; + } + } + }; +} diff --git a/src/Engine/util/camera/camera.cpp b/src/Engine/util/camera/camera.cpp deleted file mode 100644 index 729cfb7..0000000 --- a/src/Engine/util/camera/camera.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "camera.h"