changed to use Vector of entitys instead of a test object
This commit is contained in:
parent
0c7fd361f0
commit
0260fa7db4
120
src/Engine.cpp
120
src/Engine.cpp
@ -31,6 +31,9 @@ AssetManager g_AssetManager;
|
||||
|
||||
LoggerWindow *g_LoggerWindow;
|
||||
|
||||
std::vector<GameObject> m_GameObjects;
|
||||
|
||||
|
||||
bool MyEngine::Init(int width, int height, const std::string& title)
|
||||
{
|
||||
DEBUG_PRINT("[START] Engine Init");
|
||||
@ -108,9 +111,126 @@ bool MyEngine::Init(int width, int height, const std::string& title)
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint CreateCubeVAO()
|
||||
{
|
||||
// Define cube vertices (Position + UVs)
|
||||
static float g_CubeVertices[] =
|
||||
{
|
||||
// Front face
|
||||
-1.f, -1.f, 1.f, 0.f, 0.f,
|
||||
1.f, -1.f, 1.f, 1.f, 0.f,
|
||||
1.f, 1.f, 1.f, 1.f, 1.f,
|
||||
-1.f, 1.f, 1.f, 0.f, 1.f,
|
||||
|
||||
// Back face
|
||||
-1.f, -1.f, -1.f, 1.f, 0.f,
|
||||
1.f, -1.f, -1.f, 0.f, 0.f,
|
||||
1.f, 1.f, -1.f, 0.f, 1.f,
|
||||
-1.f, 1.f, -1.f, 1.f, 1.f,
|
||||
|
||||
// Left face
|
||||
-1.f, -1.f, -1.f, 0.f, 0.f,
|
||||
-1.f, -1.f, 1.f, 1.f, 0.f,
|
||||
-1.f, 1.f, 1.f, 1.f, 1.f,
|
||||
-1.f, 1.f, -1.f, 0.f, 1.f,
|
||||
|
||||
// Right face
|
||||
1.f, -1.f, -1.f, 1.f, 0.f,
|
||||
1.f, -1.f, 1.f, 0.f, 0.f,
|
||||
1.f, 1.f, 1.f, 0.f, 1.f,
|
||||
1.f, 1.f, -1.f, 1.f, 1.f,
|
||||
|
||||
// Top face
|
||||
-1.f, 1.f, -1.f, 0.f, 0.f,
|
||||
1.f, 1.f, -1.f, 1.f, 0.f,
|
||||
1.f, 1.f, 1.f, 1.f, 1.f,
|
||||
-1.f, 1.f, 1.f, 0.f, 1.f,
|
||||
|
||||
// Bottom face
|
||||
-1.f, -1.f, -1.f, 1.f, 0.f,
|
||||
1.f, -1.f, -1.f, 0.f, 0.f,
|
||||
1.f, -1.f, 1.f, 0.f, 1.f,
|
||||
-1.f, -1.f, 1.f, 1.f, 1.f,
|
||||
};
|
||||
|
||||
// Define cube indices
|
||||
static unsigned int g_CubeIndices[] =
|
||||
{
|
||||
// Front face
|
||||
0, 1, 2, 2, 3, 0,
|
||||
// Back face
|
||||
4, 5, 6, 6, 7, 4,
|
||||
// Left face
|
||||
8, 9, 10, 10, 11, 8,
|
||||
// Right face
|
||||
12, 13, 14, 14, 15, 12,
|
||||
// Top face
|
||||
16, 17, 18, 18, 19, 16,
|
||||
// Bottom face
|
||||
20, 21, 22, 22, 23, 20
|
||||
};
|
||||
|
||||
GLuint VAO, VBO, EBO;
|
||||
|
||||
// Generate and bind VAO
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
// Generate and bind VBO
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_CubeVertices), g_CubeVertices, GL_STATIC_DRAW);
|
||||
|
||||
// Generate and bind EBO
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(g_CubeIndices), g_CubeIndices, GL_STATIC_DRAW);
|
||||
|
||||
// Define vertex attributes
|
||||
// Position attribute (location = 0)
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
|
||||
5 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// UV attribute (location = 1)
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
|
||||
5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Unbind VAO (not EBO!)
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Optionally, unbind VBO and EBO for cleanliness
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
// Debug: Print VAO ID
|
||||
printf("[MeshUtils] Initialized CubeVAO with ID: %u\n", VAO);
|
||||
|
||||
return VAO;
|
||||
}
|
||||
|
||||
void MyEngine::Run()
|
||||
{
|
||||
DEBUG_PRINT("[START] Engine Run ");
|
||||
|
||||
|
||||
// Pseudocode:
|
||||
GameObject cube;
|
||||
cube.transform.position = glm::vec3(0.f, 0.f, 0.f);
|
||||
cube.transform.rotation = glm::vec3(0.f, 0.f, 0.f);
|
||||
cube.transform.scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
|
||||
// Suppose we loaded a VAO, an EBO with 36 indices for the cube,
|
||||
// and a texture ID from the asset manager
|
||||
cube.mesh.vao = CreateCubeVAO();
|
||||
cube.mesh.indexCount = 36;
|
||||
cube.mesh.textureID = static_cast<GLuint>(reinterpret_cast<uintptr_t>(g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/wood.png")));
|
||||
|
||||
m_GameObjects.push_back(cube);
|
||||
|
||||
// Possibly create more GameObjects with different positions or textures
|
||||
|
||||
|
||||
while (!glfwWindowShouldClose(m_Window) && m_Running)
|
||||
{
|
||||
|
@ -9,6 +9,12 @@
|
||||
#include "Engine/AssetManager.h"
|
||||
#include "Windows/InspectorWindow.h"
|
||||
|
||||
#include "Componenets/GameObject.h"
|
||||
#include "Componenets/Mesh.h"
|
||||
#include "Componenets/Transform.h"
|
||||
|
||||
#include "TestModel.h"
|
||||
|
||||
|
||||
//#define DEBUG
|
||||
#include "gcml.h"
|
||||
@ -35,6 +41,7 @@ private:
|
||||
private:
|
||||
|
||||
|
||||
|
||||
GLFWwindow* m_Window = nullptr;
|
||||
bool m_Running = false;
|
||||
|
||||
|
0
src/TestModel.h
Normal file
0
src/TestModel.h
Normal file
@ -4,13 +4,10 @@
|
||||
#include <glm/glm.hpp> // or <glm/vec3.hpp> if you prefer
|
||||
#include "imgui.h"
|
||||
|
||||
// Example struct for a Transform component
|
||||
struct Transform
|
||||
{
|
||||
float position[3] = {0.0f, 0.0f, 0.0f};
|
||||
float rotation[3] = {0.0f, 0.0f, 0.0f};
|
||||
float scale[3] = {1.0f, 1.0f, 1.0f};
|
||||
};
|
||||
#include "Componenets/GameObject.h"
|
||||
#include "Componenets/Mesh.h"
|
||||
#include "Componenets/Transform.h"
|
||||
|
||||
|
||||
// Example struct for a Script component
|
||||
struct Script
|
||||
|
@ -1,11 +1,22 @@
|
||||
// RenderWindow.cpp
|
||||
|
||||
#include "RenderWindow.h"
|
||||
#include <vector> // Add this line
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "imgui.h"
|
||||
|
||||
|
||||
#include "Componenets/GameObject.h"
|
||||
#include "Componenets/Mesh.h"
|
||||
#include "Componenets/Transform.h"
|
||||
|
||||
extern std::vector<GameObject> m_GameObjects;
|
||||
|
||||
|
||||
|
||||
// Include your AssetManager & Shader headers
|
||||
#include "Engine/AssetManager.h"
|
||||
#include "Rendering/Shader.h"
|
||||
@ -160,12 +171,19 @@ void RenderWindow::InitGLResources()
|
||||
m_TextureID = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texAsset));
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// 4) Initialize GameObjects
|
||||
// ----------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
|
||||
void RenderWindow::RenderSceneToFBO()
|
||||
{
|
||||
m_RotationAngle += 0.5f; // spin per frame
|
||||
m_RotationAngle += 0.1f; // spin per frame
|
||||
|
||||
// Bind the FBO
|
||||
m_FBO.Bind();
|
||||
glViewport(0, 0, m_LastWidth, m_LastHeight);
|
||||
|
||||
@ -175,40 +193,65 @@ void RenderWindow::RenderSceneToFBO()
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Use our loaded shader
|
||||
if (m_ShaderPtr)
|
||||
m_ShaderPtr->Use();
|
||||
else
|
||||
return; // No shader? Can't render
|
||||
if (!m_ShaderPtr)
|
||||
return; // Can't render without a shader
|
||||
|
||||
// Build MVP
|
||||
glm::mat4 model = glm::rotate(glm::mat4(1.0f),
|
||||
glm::radians(m_RotationAngle),
|
||||
glm::vec3(1.f, 1.f, 0.f));
|
||||
|
||||
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.f, 0.f, -5.f));
|
||||
float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f;
|
||||
glm::mat4 proj = glm::perspective(glm::radians(45.f), aspect, 0.1f, 100.f);
|
||||
glm::mat4 mvp = proj * view * model;
|
||||
|
||||
// Pass MVP to the shader
|
||||
m_ShaderPtr->Use();
|
||||
GLuint programID = m_ShaderPtr->GetProgramID();
|
||||
GLint mvpLoc = glGetUniformLocation(programID, "uMVP");
|
||||
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(mvp));
|
||||
|
||||
// Bind the texture to unit 0
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, m_TextureID);
|
||||
// Define view and projection matrices once
|
||||
glm::mat4 view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -5.f));
|
||||
float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f;
|
||||
glm::mat4 proj = glm::perspective(glm::radians(45.f), aspect, 0.1f, 100.f);
|
||||
|
||||
// If your shader has a uniform sampler2D named "uTexture"
|
||||
GLint texLoc = glGetUniformLocation(programID, "uTexture");
|
||||
glUniform1i(texLoc, 0);
|
||||
// Iterate over each GameObject and render it
|
||||
for (auto& obj : m_GameObjects)
|
||||
{
|
||||
// -----------------------------------
|
||||
// 1) Build MVP from obj.transform
|
||||
// -----------------------------------
|
||||
glm::mat4 model = glm::mat4(1.f);
|
||||
|
||||
// Draw the cube
|
||||
glBindVertexArray(m_VAO);
|
||||
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
||||
// Translate
|
||||
model = glm::translate(model, obj.transform.position);
|
||||
|
||||
// Rotate around X, Y, Z
|
||||
model = glm::rotate(model, glm::radians(obj.transform.rotation.x), glm::vec3(1.f, 0.f, 0.f));
|
||||
model = glm::rotate(model, glm::radians(obj.transform.rotation.y), glm::vec3(0.f, 1.f, 0.f));
|
||||
model = glm::rotate(model, glm::radians(obj.transform.rotation.z), glm::vec3(0.f, 0.f, 1.f));
|
||||
|
||||
// Scale
|
||||
model = glm::scale(model, obj.transform.scale);
|
||||
|
||||
// Compute MVP
|
||||
glm::mat4 mvp = proj * view * model;
|
||||
|
||||
// Pass MVP to the shader
|
||||
GLint mvpLoc = glGetUniformLocation(programID, "uMVP");
|
||||
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(mvp));
|
||||
|
||||
// -----------------------------------
|
||||
// 2) Bind the object's texture
|
||||
// -----------------------------------
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, obj.mesh.textureID);
|
||||
|
||||
// Set the sampler uniform to texture unit 0
|
||||
GLint texLoc = glGetUniformLocation(programID, "uTexture");
|
||||
glUniform1i(texLoc, 0);
|
||||
|
||||
// -----------------------------------
|
||||
// 3) Draw the object's mesh
|
||||
// -----------------------------------
|
||||
glBindVertexArray(obj.mesh.vao);
|
||||
glDrawElements(GL_TRIANGLES, obj.mesh.indexCount, GL_UNSIGNED_INT, nullptr);
|
||||
|
||||
// Unbind for cleanliness
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glUseProgram(0);
|
||||
m_FBO.Unbind();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user