added GameObject class for future use (replace current renering with this class before continuing with the chapters)

This commit is contained in:
Daniel 2023-08-04 22:33:17 +02:00
parent 60f01db034
commit 5c42364e43
5 changed files with 115 additions and 6 deletions

View File

@ -138,7 +138,6 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\libraries\glad\src\glad.c" />
<ClCompile Include="util\camera\camera.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Shaders\Shader.cpp" />
<ClCompile Include="util\stb_image.cpp" />
@ -152,6 +151,7 @@
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<ClInclude Include="object\game_object.h" />
<ClInclude Include="util\camera\camera.h" />
<ClInclude Include="Exceptions\IOException.h" />
<ClInclude Include="Exceptions\Shaders\CreateProgramException.h" />

View File

@ -27,9 +27,6 @@
<ClCompile Include="util\stb_image.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util\camera\camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Shaders\Shader.h">
@ -62,6 +59,9 @@
<ClInclude Include="util\camera\camera.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="object\game_object.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />

View File

@ -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

View File

@ -0,0 +1,110 @@
#pragma once
#include <vector>
#include <exception>
#include <glm/glm.hpp>
#include "../shaders/Shader.h"
namespace Nebulix
{
struct VertexAttribute
{
GLenum AttributeType = GL_FLOAT;
GLboolean Normalised = GL_FALSE;
/// <summary>
/// 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
/// </summary>
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<float>& vertices, std::vector<VertexAttribute> vertexAttributes, Shader shader)
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, shader{shader}, Position{glm::vec3(0.0f)}
{
Init();
}
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> 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<float> vertices;
Shader shader;
GLuint vertexBuffer, vertexArray;
std::vector<VertexAttribute> 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;
}
}
};
}

View File

@ -1 +0,0 @@
#include "camera.h"