Box rotation works correctly now. Code still pretty messy.

Gui_Panel_Refactor
Joeyrp 4 years ago
parent 00cd8a2b62
commit f7c3663e18

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

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

@ -273,7 +273,7 @@ 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));
@ -282,14 +282,55 @@ namespace lunarium
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);

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

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

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

Loading…
Cancel
Save