From f7c3663e18762aef66fcdf2e548e00e5a7c49ff8 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Fri, 29 Oct 2021 19:35:06 -0400 Subject: [PATCH] Box rotation works correctly now. Code still pretty messy. --- docs/core.todo | 2 + src/graphics/igraphics.h | 9 +-- src/graphics/opengl/glGraphics.cpp | 63 ++++++++++++++++---- src/graphics/opengl/glGraphics.h | 9 +-- src/run_modes/tester/scenes/physicsScene.cpp | 45 +++++++++----- src/run_modes/tester/scenes/physicsScene.h | 4 ++ 6 files changed, 97 insertions(+), 35 deletions(-) diff --git a/docs/core.todo b/docs/core.todo index d2efa25..94ce7ab 100644 --- a/docs/core.todo +++ b/docs/core.todo @@ -18,6 +18,8 @@ Core: ✔ Implement Render to Texture @done (9/15/2021, 7:00:33 PM) ✔ Adjust the font loading code to use the binary file buffer instead of ifstream @done (9/17/2021, 6:11:06 PM) ☐ Find a way to add rotation to shapes and images + ✔ Add a DrawPolygon method that takes vertices and draws arbirary shapes @done (10/29/2021, 6:24:14 PM) + ☐ Allow DrawPolygon to add a texture to the polygon GUI: diff --git a/src/graphics/igraphics.h b/src/graphics/igraphics.h index 4ba61e5..1a8ba79 100644 --- a/src/graphics/igraphics.h +++ b/src/graphics/igraphics.h @@ -48,13 +48,14 @@ namespace lunarium virtual Color GetClearColor() const = 0; // Draw Methods + virtual void DrawFilledPolygon(float* pVerts, int numVerts, Color color) = 0; virtual void DrawLine(glm::vec2 point1, glm::vec2 point2, Color color, float lineWidth) = 0; virtual void DrawEllipse(glm::vec2 center, glm::vec2 radii, Color color, float thickness, int resolution) = 0; virtual void DrawFilledEllipse(glm::vec2 center, glm::vec2 radii, Color color, int resolution) = 0; - virtual void DrawBox(Rectangle dimensions, Color color, float thickness) = 0; - virtual void DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color) = 0; - virtual void DrawImage(Image& image, glm::vec2 topLeft, Color color) = 0; - virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color) = 0; + virtual void DrawBox(Rectangle dimensions, Color color, float thickness, float angle = 0.0f) = 0; + virtual void DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color, float angle = 0.0f) = 0; + virtual void DrawImage(Image& image, glm::vec2 topLeft, Color color, float angle = 0.0f) = 0; + virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color, float angle = 0.0f) = 0; virtual void DrawString(const char* string, Rectangle boundingArea, Color color, float scale = 1.0f, int font = 0) = 0; virtual void RegisterImage(Image& image) = 0; diff --git a/src/graphics/opengl/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp index 04dae44..9cb998b 100644 --- a/src/graphics/opengl/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -273,23 +273,64 @@ namespace lunarium glBindVertexArray(0); } - void OglGraphics::DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color) + void OglGraphics::DrawFilledPolygon(float* pVerts, int numVerts, Color color) + { + mShapeShader.MakeActive(); + mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); + glm::mat4 model = glm::mat4(1.0f); + mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); + mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + glBindVertexArray(mRectVAO); + + glBindBuffer(GL_ARRAY_BUFFER, mRectVBO); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(pVerts), pVerts); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glDrawArrays(GL_TRIANGLES, 0, numVerts); + glBindVertexArray(0); + } + + void OglGraphics::DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color, float angle) { mShapeShader.MakeActive(); mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); glm::mat4 model = glm::mat4(1.0f); + float width = botRight.x - topLeft.x; + float height = botRight.y - topLeft.y; + model = glm::translate(model, glm::vec3(topLeft.x + width / 2, topLeft.y + height / 2 , 0.0f)); + model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); + model = glm::scale(model, glm::vec3(width, height, 1.0f)); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); glBindVertexArray(mRectVAO); + // GLfloat vertices[6][4] = { + // { topLeft.x, botRight.y, }, + // { botRight.x, topLeft.y, }, + // { topLeft.x, topLeft.y, }, + + // { topLeft.x, botRight.y, }, + // { botRight.x, botRight.y, }, + // { botRight.x, topLeft.y, } + // }; + + // Pos + // 0.0f, 1.0f, + // 1.0f, 0.0f, + // 0.0f, 0.0f, + + // 0.0f, 1.0f, + // 1.0f, 1.0f, + // 1.0f, 0.0f, + GLfloat vertices[6][4] = { - { topLeft.x, botRight.y, }, - { botRight.x, topLeft.y, }, - { topLeft.x, topLeft.y, }, + { -0.5f, 0.5f, }, + { 0.5f, -0.5f, }, + { -0.5f, -0.5f, }, - { topLeft.x, botRight.y, }, - { botRight.x, botRight.y, }, - { botRight.x, topLeft.y, } + { -0.5f, 0.5f, }, + { 0.5f, 0.5f, }, + { 0.5f, -0.5f, } }; glBindBuffer(GL_ARRAY_BUFFER, mRectVBO); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); @@ -299,7 +340,7 @@ namespace lunarium glBindVertexArray(0); } - void OglGraphics::DrawBox(Rectangle dimensions, Color color, float thickness) + void OglGraphics::DrawBox(Rectangle dimensions, Color color, float thickness, float angle) { glm::vec2 topLeft(dimensions.Left, dimensions.Top); glm::vec2 botRight(dimensions.right(), dimensions.bottom()); @@ -317,13 +358,13 @@ namespace lunarium DrawLine(glm::vec2(botRight.x, topLeft.y), glm::vec2(topLeft.x, topLeft.y), color, thickness); } - void OglGraphics::DrawImage(Image& image, glm::vec2 topLeft, Color color) + void OglGraphics::DrawImage(Image& image, glm::vec2 topLeft, Color color, float angle) { DrawImage(image, Rectangle(0.0f, 0.0f, (float)image.GetWidth(), (float)image.GetHeight()), - Rectangle(topLeft.x, topLeft.y, (float)image.GetWidth(), (float)image.GetHeight()), color); + Rectangle(topLeft.x, topLeft.y, (float)image.GetWidth(), (float)image.GetHeight()), color, angle); } - void OglGraphics::DrawImage(Image& image, Rectangle source, Rectangle destination, Color color) + void OglGraphics::DrawImage(Image& image, Rectangle source, Rectangle destination, Color color, float angle) { glm::mat4 id = glm::mat4(1.0f); glm::vec3 pos = glm::vec3(destination.Left, destination.Top, 0.0f); diff --git a/src/graphics/opengl/glGraphics.h b/src/graphics/opengl/glGraphics.h index b237e6c..b824a98 100644 --- a/src/graphics/opengl/glGraphics.h +++ b/src/graphics/opengl/glGraphics.h @@ -43,13 +43,14 @@ namespace lunarium virtual Color GetClearColor() const; // Draw Methods + virtual void DrawFilledPolygon(float* pVerts, int numVerts, Color color); virtual void DrawLine(glm::vec2 point1, glm::vec2 point2, Color color, float lineWidth); virtual void DrawEllipse(glm::vec2 center, glm::vec2 radii, Color color, float thickness, int resolution); virtual void DrawFilledEllipse(glm::vec2 center, glm::vec2 radii, Color color, int resolution); - virtual void DrawBox(Rectangle dimensions, Color color, float thickness); - virtual void DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color); - virtual void DrawImage(Image& image, glm::vec2 topLeft, Color color); - virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color); + virtual void DrawBox(Rectangle dimensions, Color color, float thickness, float angle = 0.0f); + virtual void DrawFilledBox(glm::vec2 topLeft, glm::vec2 botRight, Color color, float angle = 0.0f); + virtual void DrawImage(Image& image, glm::vec2 topLeft, Color color, float angle = 0.0f); + virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color, float angle = 0.0f); virtual void DrawString(const char* string, Rectangle boundingArea, Color color, float scale = 1.0f, int font = 0); virtual void RegisterImage(Image& image); diff --git a/src/run_modes/tester/scenes/physicsScene.cpp b/src/run_modes/tester/scenes/physicsScene.cpp index 4c6bb8c..8b13727 100644 --- a/src/run_modes/tester/scenes/physicsScene.cpp +++ b/src/run_modes/tester/scenes/physicsScene.cpp @@ -51,7 +51,7 @@ namespace lunarium // For step 1 we create the ground body. For this we need a body definition. // With the body definition we specify the initial position of the ground body. b2BodyDef groundBodyDef; - groundBodyDef.position.Set(30.0f, 50.0f); + groundBodyDef.position.Set(60.0f, 50.0f); // For step 2 the body definition is passed to the world object to create the ground body. // The world object does not keep a reference to the body definition. Bodies are static by default. @@ -69,6 +69,13 @@ namespace lunarium // to the body without creating a fixture definition. mpGroundBody->CreateFixture(mpGroundBox, 0.0f); + // Create a second ground body + groundBodyDef.position.Set(20.0f, 70.0f); + mpGroundBody2 = mb2World->CreateBody(&groundBodyDef); + mpGroundBox2 = new b2PolygonShape; + mpGroundBox2->SetAsBox(30.0f, 5.0f); + mpGroundBody2->CreateFixture(mpGroundBox2, 0.0f); + ///////////////////////////////////////////////////////////////////////////// // So now we have a ground body. We can use the same technique to create a dynamic body. // The main difference, besides dimensions, is that we must establish the dynamic body's mass properties. @@ -77,7 +84,7 @@ namespace lunarium // at construction time to make the body dynamic. b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; // Caution: You must set the body type to b2_dynamicBody if you want the body to move in response to forces. - bodyDef.position.Set(9.0f, 4.0f); + bodyDef.position.Set(39.0f, 4.0f); mpDynamicBody = mb2World->CreateBody(&bodyDef); @@ -118,39 +125,45 @@ namespace lunarium b2AABB groundbox; mpGroundBox->ComputeAABB(&groundbox, mpGroundBody->GetTransform(), 0); - // float hwidth = groundbox.GetExtents().x; - // float hheight = groundbox.GetExtents().y; - // float left = (groundbox.GetCenter().x - hwidth); - // float top = (groundbox.GetCenter().y - hheight); - // float right = left + hwidth * 2; - // float bot = top + hheight * 2; - + Rectangle groundRect(groundbox); g->DrawFilledBox(groundRect.LeftTop() * scaleFactor, groundRect.RightBottom() * scaleFactor, Color(0.0f, 1.0f, 0.0f, 1.0f)); + b2AABB groundbox2; + mpGroundBox2->ComputeAABB(&groundbox2, mpGroundBody2->GetTransform(), 0); + + Rectangle groundRect2(groundbox2); + g->DrawFilledBox(groundRect2.LeftTop() * scaleFactor, + groundRect2.RightBottom() * scaleFactor, + Color(0.0f, 1.0f, 0.0f, 1.0f)); + b2AABB dynbox; - mpDynamicBox->ComputeAABB(&dynbox, mpDynamicBody->GetTransform(), 0); + b2Transform transform; + transform.p = mpDynamicBody->GetTransform().p; + transform.q = b2Rot(0.0f); + mpDynamicBox->ComputeAABB(&dynbox, transform, 0); Rectangle dynRect(dynbox); + float angle = mpDynamicBody->GetAngle(); g->DrawFilledBox(dynRect.LeftTop() * scaleFactor, dynRect.RightBottom() * scaleFactor, - Color(0.0f, 0.0f, 1.0f, 1.0f)); + Color(0.0f, 0.0f, 1.0f, 1.0f), angle); // Debug info char str[256] = { 0 }; sprintf(str, "GroundBox: (%f, %f) (%f, %f)", groundRect.LeftTop().x * scaleFactor, groundRect.LeftTop().y * scaleFactor, groundRect.RightBottom().x * scaleFactor, groundRect.RightBottom().y * scaleFactor); - g->DrawString(str, Rectangle(10.0f, 10.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f); + g->DrawString(str, Rectangle(10.0f, 10.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4f); sprintf(str, "GroundBox Half Size: (%f, %f)", groundRect.Width, groundRect.Height); - g->DrawString(str, Rectangle(10.0f, 35.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f); + g->DrawString(str, Rectangle(10.0f, 35.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4f); - sprintf(str, "DynBox: (%f, %f) (%f, %f)", + sprintf(str, "DynBox: (%f, %f) (%f, %f), Angle: %f", dynRect.LeftTop().x * scaleFactor, dynRect.LeftTop().y * scaleFactor, - dynRect.RightBottom().x * scaleFactor, dynRect.RightBottom().y * scaleFactor); - g->DrawString(str, Rectangle(10.0f, 60.0f, 800.0f, 100.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f); + dynRect.RightBottom().x * scaleFactor, dynRect.RightBottom().y * scaleFactor, angle); + g->DrawString(str, Rectangle(10.0f, 60.0f, 1000.0f, 100.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4); int ww, wh; Core::MainWindow().GetFramebufferSize(&ww, &wh); diff --git a/src/run_modes/tester/scenes/physicsScene.h b/src/run_modes/tester/scenes/physicsScene.h index 0309ec8..84d11cd 100644 --- a/src/run_modes/tester/scenes/physicsScene.h +++ b/src/run_modes/tester/scenes/physicsScene.h @@ -14,6 +14,7 @@ class b2World; class b2Body; class b2PolygonShape; +class b2AABB; namespace lunarium { @@ -29,9 +30,12 @@ namespace lunarium private: b2World* mb2World; b2Body* mpGroundBody; + b2Body* mpGroundBody2; b2PolygonShape* mpGroundBox; + b2PolygonShape* mpGroundBox2; b2Body* mpDynamicBody; b2PolygonShape* mpDynamicBox; + b2AABB* mpDynAABB; }; }