PoC working for parent/child in quad rendering

master
Joey Pollack 3 years ago
parent c44b4917aa
commit 9686b05a67

@ -38,6 +38,7 @@ Editor:
✔ Open existing project @done (2/8/2022, 4:05:42 PM) ✔ Open existing project @done (2/8/2022, 4:05:42 PM)
Content Manager: Content Manager:
☐ Switch to using LUUIDs for asset ids @critical
✔ Design interface @done (2/24/2022, 3:15:39 PM) ✔ Design interface @done (2/24/2022, 3:15:39 PM)
✔ Generate new content file @done (2/24/2022, 3:16:00 PM) ✔ Generate new content file @done (2/24/2022, 3:16:00 PM)
✔ Load existing contents @done (3/3/2022, 3:16:21 PM) ✔ Load existing contents @done (3/3/2022, 3:16:21 PM)
@ -47,7 +48,8 @@ Editor:
World View: World View:
✔ Middle Mouse view dragging @done(22-09-08 15:45) ✔ Middle Mouse view dragging @done(22-09-08 15:45)
✔ Render the current world and display on view panel @done(22-09-08 15:45) ✔ Render the current world and display on view panel @done(22-09-08 15:45)
☐ Optional and adjustable grid display
☐ Toolbar with play/pause/stop buttons
World Hierarchy (Tree View): World Hierarchy (Tree View):
☐ Handle showing Enities with children @high ☐ Handle showing Enities with children @high

@ -110,10 +110,10 @@ namespace lunarium
// DRAW METHODS // DRAW METHODS
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
void Renderer2D::DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color, float angle) void Renderer2D::DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color, float angle, glm::mat4 parent_transform)
{ {
Rectangle quad = Rectangle::MakeFromTopLeft(position.x, position.y, sub_texture_area.width(), sub_texture_area.height()); Rectangle quad = Rectangle::MakeFromTopLeft(position.x, position.y, sub_texture_area.width(), sub_texture_area.height());
DrawQuad(quad, color, texture, angle, sub_texture_area); DrawQuad(quad, color, texture, angle, sub_texture_area, parent_transform);
} }
// void Renderer2D::DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f) // void Renderer2D::DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f)
@ -121,7 +121,7 @@ namespace lunarium
// DrawQuad(destination, color, texture, angle, sub_texture_area); // DrawQuad(destination, color, texture, angle, sub_texture_area);
// } // }
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area) void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area, glm::mat4 parent_transform)
{ {
if (!mQuadData.VertexBuffer->WillFit(4)) if (!mQuadData.VertexBuffer->WillFit(4))
{ {
@ -168,6 +168,8 @@ namespace lunarium
// Need to invert the offset_y because OpenGL likes being weird // Need to invert the offset_y because OpenGL likes being weird
offset_y *= -1.0f; offset_y *= -1.0f;
glm::vec3 parent_pos = parent_transform[3];
// FIRST // FIRST
QuadData::Vertex verts[4]; QuadData::Vertex verts[4];
int vert_size = sizeof(QuadData::Vertex); int vert_size = sizeof(QuadData::Vertex);
@ -179,6 +181,7 @@ namespace lunarium
verts[0].angle = angle; verts[0].angle = angle;
verts[0].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[0].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f);
verts[0].tex_is_grey_scale = tex_is_grey_scale; verts[0].tex_is_grey_scale = tex_is_grey_scale;
verts[0].parent_transform = parent_pos;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v1, vert_size); //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v1, vert_size);
//mQuadData.RawBufferIndex += 1; //mQuadData.RawBufferIndex += 1;
//mQuadData.VertexBuffer->PushVertices((void*)&v1, 1); //mQuadData.VertexBuffer->PushVertices((void*)&v1, 1);
@ -192,6 +195,7 @@ namespace lunarium
verts[1].angle = angle; verts[1].angle = angle;
verts[1].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[1].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f);
verts[1].tex_is_grey_scale = tex_is_grey_scale; verts[1].tex_is_grey_scale = tex_is_grey_scale;
verts[1].parent_transform = parent_pos;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v2, vert_size); //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v2, vert_size);
//mQuadData.RawBufferIndex += 1; //mQuadData.RawBufferIndex += 1;
// mQuadData.VertexBuffer->PushVertices((void*)&v2, 1); // mQuadData.VertexBuffer->PushVertices((void*)&v2, 1);
@ -205,6 +209,7 @@ namespace lunarium
verts[2].angle = angle; verts[2].angle = angle;
verts[2].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[2].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f);
verts[2].tex_is_grey_scale = tex_is_grey_scale; verts[2].tex_is_grey_scale = tex_is_grey_scale;
verts[2].parent_transform = parent_pos;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v3, vert_size); //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v3, vert_size);
//mQuadData.RawBufferIndex += 1; //mQuadData.RawBufferIndex += 1;
//mQuadData.VertexBuffer->PushVertices((void*)&v3, 1); //mQuadData.VertexBuffer->PushVertices((void*)&v3, 1);
@ -218,6 +223,7 @@ namespace lunarium
verts[3].angle = angle; verts[3].angle = angle;
verts[3].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[3].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f);
verts[3].tex_is_grey_scale = tex_is_grey_scale; verts[3].tex_is_grey_scale = tex_is_grey_scale;
verts[3].parent_transform = parent_pos;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v4, vert_size); //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v4, vert_size);
//mQuadData.RawBufferIndex += 1; //mQuadData.RawBufferIndex += 1;
@ -498,6 +504,7 @@ namespace lunarium
mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle
mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Scale mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Scale
mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // tex_is_grey_scale mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // tex_is_grey_scale
mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // parent_transform
mQuadData.VertexBuffer = VertexBuffer::Create(mQuadData.BufferLayout, mQuadData.MaxVertices, nullptr, mQuadData.MaxIndices, indices); mQuadData.VertexBuffer = VertexBuffer::Create(mQuadData.BufferLayout, mQuadData.MaxVertices, nullptr, mQuadData.MaxIndices, indices);
// RAW VERTEX BUFFER // RAW VERTEX BUFFER

@ -47,11 +47,11 @@ namespace lunarium
/// sub_texture_area is the area of the texture to draw /// sub_texture_area is the area of the texture to draw
/// position is the top-left corner of the quad /// position is the top-left corner of the quad
void DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f); void DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f, glm::mat4 parent_transform = glm::mat4(1.0f));
//void DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f); //void DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f);
/// sub_texture_area is the area of the texture to draw /// sub_texture_area is the area of the texture to draw
void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1)); void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1), glm::mat4 parent_transform = glm::mat4(1.0f));
void DrawLine(glm::vec2 point_a, glm::vec2 point_b, Color color, float thickness = 1.0f, float angle = 0.0f); void DrawLine(glm::vec2 point_a, glm::vec2 point_b, Color color, float thickness = 1.0f, float angle = 0.0f);
void DrawBox(Rectangle box, Color color, float thickness = 1.5f, float angle = 0.0f); void DrawBox(Rectangle box, Color color, float thickness = 1.5f, float angle = 0.0f);
@ -142,6 +142,7 @@ namespace lunarium
float angle; float angle;
glm::vec3 scale; glm::vec3 scale;
float tex_is_grey_scale; // 0 or 1, this is mostly used for text rendering float tex_is_grey_scale; // 0 or 1, this is mostly used for text rendering
glm::vec3 parent_transform;
}; };
const u32 indices[6] = { 0, 1, 2, 2, 3, 0 }; const u32 indices[6] = { 0, 1, 2, 2, 3, 0 };

@ -130,7 +130,7 @@ namespace lunarium
} }
OpRes Shader::SetUniform(Uniform& uniform, void* values) OpRes Shader::SetUniform(Uniform& uniform, void* values, int num_uniforms)
{ {
if (uniform.Location == -1) if (uniform.Location == -1)
{ {
@ -143,18 +143,18 @@ namespace lunarium
switch (uniform.Type) switch (uniform.Type)
{ {
case UniformType::F1: glUniform1fv(uniform.Location, 1, (GLfloat*)values); break; case UniformType::F1: glUniform1fv(uniform.Location, num_uniforms, (GLfloat*)values); break;
case UniformType::F2: glUniform2fv(uniform.Location, 1, (GLfloat*)values); break; case UniformType::F2: glUniform2fv(uniform.Location, num_uniforms, (GLfloat*)values); break;
case UniformType::F3: glUniform3fv(uniform.Location, 1, (GLfloat*)values); break; case UniformType::F3: glUniform3fv(uniform.Location, num_uniforms, (GLfloat*)values); break;
case UniformType::F4: glUniform4fv(uniform.Location, 1, (GLfloat*)values); break; case UniformType::F4: glUniform4fv(uniform.Location, num_uniforms, (GLfloat*)values); break;
case UniformType::I1: glUniform1iv(uniform.Location, 1, (GLint*)values); break; case UniformType::I1: glUniform1iv(uniform.Location, num_uniforms, (GLint*)values); break;
case UniformType::I2: glUniform2iv(uniform.Location, 1, (GLint*)values); break; case UniformType::I2: glUniform2iv(uniform.Location, num_uniforms, (GLint*)values); break;
case UniformType::I3: glUniform3iv(uniform.Location, 1, (GLint*)values); break; case UniformType::I3: glUniform3iv(uniform.Location, num_uniforms, (GLint*)values); break;
case UniformType::I4: glUniform4iv(uniform.Location, 1, (GLint*)values); break; case UniformType::I4: glUniform4iv(uniform.Location, num_uniforms, (GLint*)values); break;
case UniformType::FMAT3: glUniformMatrix3fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break; case UniformType::FMAT3: glUniformMatrix3fv(uniform.Location, num_uniforms, GL_FALSE, (GLfloat*)values); break;
case UniformType::FMAT4: glUniformMatrix4fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break; case UniformType::FMAT4: glUniformMatrix4fv(uniform.Location, num_uniforms, GL_FALSE, (GLfloat*)values); break;
default: return OpRes::Fail("Can not set uniform value - Unknown type"); default: return OpRes::Fail("Can not set uniform value - Unknown type");
} }

@ -50,7 +50,7 @@ namespace lunarium
OpRes GetAllUniforms(std::vector<Uniform>& uniforms); OpRes GetAllUniforms(std::vector<Uniform>& uniforms);
void GetUniformLocation(Uniform& uniform); void GetUniformLocation(Uniform& uniform);
OpRes SetUniform(Uniform& uniform, void* values); OpRes SetUniform(Uniform& uniform, void* values, int num_uniforms = 1);
private: private:
u32 mGLID; u32 mGLID;

@ -8,6 +8,7 @@ layout(location = 4) in vec3 translation;
layout(location = 5) in float angle; layout(location = 5) in float angle;
layout(location = 6) in vec3 scale; layout(location = 6) in vec3 scale;
layout(location = 7) in vec3 tex_is_grey_scale; layout(location = 7) in vec3 tex_is_grey_scale;
layout(location = 8) in vec3 parent_pos;
layout (location = 0) out vec2 f_tex_coords; layout (location = 0) out vec2 f_tex_coords;
layout (location = 1) out vec4 f_vert_color; layout (location = 1) out vec4 f_vert_color;
@ -15,6 +16,7 @@ layout (location = 2) flat out int f_tex_index;
layout (location = 3) flat out int f_tex_is_grey; layout (location = 3) flat out int f_tex_is_grey;
uniform mat4 projview; uniform mat4 projview;
uniform mat4 parents[16];
void main() void main()
{ {
@ -55,5 +57,11 @@ void main()
model = model * Rotation; model = model * Rotation;
model = model * Scale; model = model * Scale;
gl_Position = projview * model * vec4(pos, 1.0); mat4 Parent_Translation = mat4(
vec4( 1.0, 0.0, 0.0, 0.0),
vec4( 0.0, 1.0, 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( parent_pos.xyz, 1.0));
gl_Position = projview * Parent_Translation * model * vec4(pos, 1.0);
} }

@ -122,6 +122,12 @@ namespace lunarium
glBufferData(GL_ARRAY_BUFFER, mSize, nullptr, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mSize, nullptr, GL_DYNAMIC_DRAW);
} }
int err = glGetError();
if (err > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Failed to set vertex buffer data, error code: %d", err);
}
if (mEBOSize > 0) if (mEBOSize > 0)
{ {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
@ -131,7 +137,19 @@ namespace lunarium
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mEBOSize, indices, mode); glBufferData(GL_ELEMENT_ARRAY_BUFFER, mEBOSize, indices, mode);
} }
err = glGetError();
if (err > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Failed to create EBO, error code: %d", err);
}
mLayout.ImplementLayout(); mLayout.ImplementLayout();
err = glGetError();
if (err > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Failed to implement vertex layout: error code: %d", err);
}
Unbind(); Unbind();
UnbindVBO(); UnbindVBO();
} }

@ -18,6 +18,8 @@
#include <utils/stb/std_image_write.h> #include <utils/stb/std_image_write.h>
#include <utils/stb/stb_image.h> #include <utils/stb/stb_image.h>
#include <glm/gtc/matrix_transform.hpp>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
@ -58,6 +60,9 @@ namespace lunarium
GenerateQuads(); GenerateQuads();
GenerateLines(); GenerateLines();
mParentPos = glm::vec3(300.0f, 300.0f, 0.0f);
mChildPos = glm::vec3(250.0f, 0.0f, 0.0f);
angle = 0.0f; angle = 0.0f;
box_angle = 0.0f; box_angle = 0.0f;
mTextDebugPosX = 200; mTextDebugPosX = 200;
@ -183,6 +188,7 @@ namespace lunarium
case TestMode::String: RenderStringTest(Core::Graphics()); break; case TestMode::String: RenderStringTest(Core::Graphics()); break;
case TestMode::Shapes: RenderShapesTest(Core::Graphics()); break; case TestMode::Shapes: RenderShapesTest(Core::Graphics()); break;
case TestMode::TileMap: RenderTileMapTest(Core::Graphics()); break; case TestMode::TileMap: RenderTileMapTest(Core::Graphics()); break;
case TestMode::Parent: RenderParentTest(Core::Graphics()); break;
} }
@ -284,19 +290,27 @@ namespace lunarium
mTestMap.Render(&g); mTestMap.Render(&g);
} }
void SimpleRenderScene::RenderParentTest(Renderer2D& g)
{
g.DrawQuad(Rectangle(mParentPos.x, mParentPos.y, 50.0f, 50.0f), Color::Blue());
glm::mat4 parent = glm::mat4(1.0f);
parent = glm::translate(parent, mParentPos);
g.DrawQuad(Rectangle(mChildPos.x, mChildPos.y, 50.0f, 50.0f), Color::Green(), nullptr, 0.0f, Rectangle(-1, -1, -1, -1), parent);
}
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// GUI DRAWING GUI DRAWING // GUI DRAWING GUI DRAWING
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
void SimpleRenderScene::DrawStatsGUI() void SimpleRenderScene::DrawStatsGUI()
{ {
const char* mode_names[5] = { "Basic", "Stress", "String", "Shapes", "TileMap"}; const char* mode_names[6] = { "Basic", "Stress", "String", "Shapes", "TileMap", "Parent/Child"};
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(400, 800)); ImGui::SetNextWindowSize(ImVec2(400, 800));
ImGui::Begin("RENDER INFO"); ImGui::Begin("RENDER INFO");
ImGui::BeginChild("Scene Info", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::BeginChild("Scene Info", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::Text("Scene Info"); ImGui::Text("Scene Info");
ImGui::Separator(); ImGui::Separator();
ImGui::Combo("Scene Mode", (int*)&mTestMode, mode_names, 5); ImGui::Combo("Scene Mode", (int*)&mTestMode, mode_names, 6);
ImGui::EndChild(); ImGui::EndChild();
ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::Text("Per Frame"); ImGui::Text("Per Frame");
@ -383,8 +397,25 @@ namespace lunarium
} }
ImGui::EndChild(); ImGui::EndChild();
} }
else if (mTestMode == TestMode::Parent)
{
float parent_pos[3] = { mParentPos.x, mParentPos.y, mParentPos.z };
float child_pos[3] = { mChildPos.x, mChildPos.y, mChildPos.z };
ImGui::Text("Parent Position:");
ImGui::SameLine();
if (ImGui::DragFloat3("##ParentPos", parent_pos))
{
mParentPos = { parent_pos[0], parent_pos[1], parent_pos[2] };
}
ImGui::Text("Child Position:");
ImGui::SameLine();
if (ImGui::DragFloat3("##ChildPos", child_pos))
{
mChildPos = { child_pos[0], child_pos[1], child_pos[2] };
}
}
ImGui::EndChild(); ImGui::EndChild();
ImGui::End(); ImGui::End();

@ -31,6 +31,7 @@ namespace lunarium
String, String,
Shapes, Shapes,
TileMap, TileMap,
Parent,
}; };
enum ShapeMode enum ShapeMode
@ -50,6 +51,7 @@ namespace lunarium
void RenderStringTest(Renderer2D& g); void RenderStringTest(Renderer2D& g);
void RenderShapesTest(Renderer2D& g); void RenderShapesTest(Renderer2D& g);
void RenderTileMapTest(Renderer2D& g); void RenderTileMapTest(Renderer2D& g);
void RenderParentTest(Renderer2D& g);
private: private:
@ -117,6 +119,10 @@ namespace lunarium
editor::TileMap mTestMap; editor::TileMap mTestMap;
editor::TileSet mTestSet; editor::TileSet mTestSet;
// PARENT CHILD TEST
glm::vec3 mParentPos;
glm::vec3 mChildPos;
struct GridTestObj struct GridTestObj
{ {
int X; int X;

@ -28,7 +28,13 @@ namespace lunarium
void Entity::Init() void Entity::Init()
{ {
mHandle = mWorld.GetEntityRegistry()->create(); auto prev = mHandle;
mHandle = mWorld.GetEntityRegistry()->create(mHandle);
if (prev != mHandle && prev != entt::null)
{
Logger::Warn(LogCategory::GAME_SYSTEM, "Requested entity handle was not used");
}
} }
LUUID Entity::GetUUID() const LUUID Entity::GetUUID() const

@ -2,7 +2,7 @@
"State": "State":
{ {
"DataDirectory": "data/", "DataDirectory": "data/",
"Mode": "editor", "Mode": "test",
"Display": "Display":
{ {

Loading…
Cancel
Save