renderer2D DrawQuad can now draw sub-regions of textures, this allows for sprite sheets again

master
Joey Pollack 3 years ago
parent a3a89a291a
commit f88c8a8918

@ -194,7 +194,7 @@ namespace lunarium
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// DRAW METHODS // DRAW METHODS
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle) void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area)
{ {
int texture_slot = -1; int texture_slot = -1;
if (texture) if (texture)
@ -237,12 +237,27 @@ namespace lunarium
} }
float tex_is_grey_scale = (texture && texture->GetFormat() == TextureFormat::RED) ? 1.0f : 0.0f; float tex_is_grey_scale = (texture && texture->GetFormat() == TextureFormat::RED) ? 1.0f : 0.0f;
float left = sub_texture_area.X;
if (left < 0)
{
sub_texture_area = Rectangle::MakeFromTopLeft(0, 0, mLoadedTextures[texture_slot]->GetWidth(), mLoadedTextures[texture_slot]->GetHeight());
}
float xScale = (sub_texture_area.HalfWidth * 2) / mLoadedTextures[texture_slot]->GetWidth();
float xOffset = sub_texture_area.left() / mLoadedTextures[texture_slot]->GetWidth();
float yScale = (sub_texture_area.HalfHeight * 2) / mLoadedTextures[texture_slot]->GetHeight();
float yOffset = sub_texture_area.top() / mLoadedTextures[texture_slot]->GetHeight();
yOffset *= -1.0f;
// mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale * -1.0f, yOffset});
// vec2(vertex.z * uvManip.x + uvManip.y, vertex.w * uvManip.z - uvManip.w);
// FIRST // FIRST
QuadData::Vertex v1; QuadData::Vertex v1;
int vert_size = sizeof(QuadData::Vertex); int vert_size = sizeof(QuadData::Vertex);
v1.pos = mQuadData.vert_pos[0]; v1.pos = mQuadData.vert_pos[0];
v1.tex_coord = mQuadData.vert_tex[0]; v1.tex_coord = glm::vec2(mQuadData.vert_tex[0].x * xScale + xOffset, mQuadData.vert_tex[0].y * yScale - yOffset);
v1.color = color; v1.color = color;
v1.tex_slot = texture_slot; v1.tex_slot = texture_slot;
v1.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v1.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f);
@ -255,7 +270,7 @@ namespace lunarium
// SECOND // SECOND
QuadData::Vertex v2; QuadData::Vertex v2;
v2.pos = mQuadData.vert_pos[1]; v2.pos = mQuadData.vert_pos[1];
v2.tex_coord = mQuadData.vert_tex[1]; v2.tex_coord = glm::vec2(mQuadData.vert_tex[1].x * xScale + xOffset, mQuadData.vert_tex[1].y * yScale - yOffset);
v2.color = color; v2.color = color;
v2.tex_slot = texture_slot; v2.tex_slot = texture_slot;
v2.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v2.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f);
@ -268,7 +283,7 @@ namespace lunarium
// THIRD // THIRD
QuadData::Vertex v3; QuadData::Vertex v3;
v3.pos = mQuadData.vert_pos[2]; v3.pos = mQuadData.vert_pos[2];
v3.tex_coord = mQuadData.vert_tex[2]; v3.tex_coord = glm::vec2(mQuadData.vert_tex[2].x * xScale + xOffset, mQuadData.vert_tex[2].y * yScale - yOffset);
v3.color = color; v3.color = color;
v3.tex_slot = texture_slot; v3.tex_slot = texture_slot;
v3.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v3.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f);
@ -281,7 +296,7 @@ namespace lunarium
// FOURTH // FOURTH
QuadData::Vertex v4; QuadData::Vertex v4;
v4.pos = mQuadData.vert_pos[3]; v4.pos = mQuadData.vert_pos[3];
v4.tex_coord = mQuadData.vert_tex[3]; v4.tex_coord = glm::vec2(mQuadData.vert_tex[3].x * xScale + xOffset, mQuadData.vert_tex[3].y * yScale - yOffset);
v4.color = color; v4.color = color;
v4.tex_slot = texture_slot; v4.tex_slot = texture_slot;
v4.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v4.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f);
@ -358,11 +373,13 @@ namespace lunarium
// glBindVertexArray(0); // glBindVertexArray(0);
} }
void Renderer2D::DrawString(const char* string, Rectangle boundingArea, Color color, float scale, int font) void Renderer2D::DrawString(const char* string, Rectangle bounding_box, Color color, float scale, int font)
{ {
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented"); //Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented");
// mText.DrawString(font, string, glm::vec2(boundingArea.left(), boundingArea.top()), // mText.DrawString(font, string, glm::vec2(boundingArea.left(), boundingArea.top()),
// glm::vec2(boundingArea.right(), boundingArea.bottom()), color, scale, mProjection); // glm::vec2(boundingArea.right(), boundingArea.bottom()), color, scale, mProjection);
u32 f = font < 0 ? mDefaultFont : font;
mTextAPI.DrawString(*this, f, string, bounding_box, color, scale);
} }

@ -43,11 +43,13 @@ namespace lunarium
void EndDraw(); void EndDraw();
// Draw methods // Draw methods
void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f);
/// 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 DrawQuads(Rectangle* quads, u32 num_quads, Color* pColors); void DrawQuads(Rectangle* quads, u32 num_quads, Color* pColors);
void DrawSprite(Texture& image, glm::vec2 position, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); void DrawSprite(Texture& image, glm::vec2 position, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0);
void DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); void DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0);
void DrawString(const char* string, Rectangle boundingArea, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float scale = 1.0f, int font = 0); void DrawString(const char* string, Rectangle bounding_box, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float scale = 1.0f, int font = -1);
// DEBUG: // DEBUG:
Texture* GetTextDebugTexture(); Texture* GetTextDebugTexture();

@ -157,25 +157,6 @@ namespace lunarium
return -1; return -1;
} }
// Create buffer of the correct size (single row texture for now - this may be improved later)
// u32 structured_width = max_width * f.CharSet.size();
// u32 structured_height = max_height;
// u8** structured_buffer = new u8*[structured_height];
// for (int i = 0; i < structured_height; i++)
// {
// structured_buffer[i] = new u8[structured_width];
// memset(structured_buffer[i], 0, structured_width);
// // Copy the current row for each char into the buffer
// u8* index = structured_buffer[i];
// for (auto iter = f.CharSet.begin(); iter != f.CharSet.end(); iter++)
// {
// memcpy(index, &iter->second.PixelDataBuffer[iter->second.Size.Width * i], iter->second.Size.Width);
// index += max_width;
// }
// }
u8* texture_buffer = new u8[single_row_width * max_height]; u8* texture_buffer = new u8[single_row_width * max_height];
memset(texture_buffer, 0, single_row_width * max_height); memset(texture_buffer, 0, single_row_width * max_height);
@ -209,6 +190,11 @@ namespace lunarium
return fontIdx; return fontIdx;
} }
void TextRenderer::DrawString(Renderer2D& r, u32 font_id, const char* str, Rectangle bounding_box, Color c, float scale)
{
}
void TextRenderer::FreeTempPixelData(std::map<u8, Character>& char_set) void TextRenderer::FreeTempPixelData(std::map<u8, Character>& char_set)
{ {

@ -24,12 +24,14 @@ typedef struct FT_FaceRec_* FT_Face;
namespace lunarium namespace lunarium
{ {
class Renderer2D;
class Texture; class Texture;
class TextRenderer class TextRenderer
{ {
private: private:
friend class Renderer2D; friend class Renderer2D;
OpRes Initialize(); OpRes Initialize();
void DrawString(Renderer2D& r, u32 font_id, const char* str, Rectangle bounding_box, Color c, float scale);
// DEBUG // DEBUG
Texture* GetDebugTexture(); Texture* GetDebugTexture();

@ -67,13 +67,18 @@ namespace lunarium
//delete[] buffer; //delete[] buffer;
stbi_set_flip_vertically_on_load(0); stbi_set_flip_vertically_on_load(0);
buffer = stbi_load("lunarium_text_test.png", &w, &h, &n, 0); buffer = stbi_load("debug_texture.jpeg", &w, &h, &n, 0);
format = TextureFormat::RGBA; format = TextureFormat::RGBA;
if (n == 1) if (n == 3)
{
format = TextureFormat::RGB;
}
else if (n == 1)
{ {
format = TextureFormat::RED; format = TextureFormat::RED;
} }
mpTestImageLoad2 = Texture::Create(buffer, w, h, format); mpTestImageLoad2 = Texture::Create(buffer, w, h, format);
//delete[] buffer; //delete[] buffer;
@ -103,6 +108,7 @@ namespace lunarium
angle = 0.0f; angle = 0.0f;
box_angle = 0.0f; box_angle = 0.0f;
mTextDebugPosX = 200; mTextDebugPosX = 200;
mSubTex = Rectangle::MakeFromTopLeft(0, 0, 256, 256);
} }
void SimpleRenderScene::OnTick(double delta) void SimpleRenderScene::OnTick(double delta)
@ -174,10 +180,10 @@ namespace lunarium
if (mTestMode == TestMode::Basic) if (mTestMode == TestMode::Basic)
mTestMode = TestMode::Stress; mTestMode = TestMode::Stress;
if (mTestMode == TestMode::Stress) else if (mTestMode == TestMode::Stress)
mTestMode = TestMode::String; mTestMode = TestMode::String;
if (mTestMode == TestMode::String) else if (mTestMode == TestMode::String)
mTestMode = TestMode::Basic; mTestMode = TestMode::Basic;
} }
@ -288,6 +294,8 @@ namespace lunarium
//g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 0.0f, 1.0f), nullptr, angle); //g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 0.0f, 1.0f), nullptr, angle);
g.DrawQuad(Rectangle(100, 100, 16, 16), Color::White(), mpTestImageLoad2, 0.0f, mSubTex);
// g.DrawQuad(Rectangle(200, 200, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f));//, nullptr, box_angle); // g.DrawQuad(Rectangle(200, 200, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f));//, nullptr, box_angle);
// g.DrawQuad(Rectangle(400, 300, 64.0f, 64.0f), Color(0.0f, 1.0f, 1.0f, 1.0f), nullptr, box_angle); // g.DrawQuad(Rectangle(400, 300, 64.0f, 64.0f), Color(0.0f, 1.0f, 1.0f, 1.0f), nullptr, box_angle);
// g.DrawQuad(Rectangle(600, 300, 100.0f, 100.0f), Color::Blue(), nullptr, 45.0f); // g.DrawQuad(Rectangle(600, 300, 100.0f, 100.0f), Color::Blue(), nullptr, 45.0f);
@ -324,14 +332,22 @@ namespace lunarium
{ {
Texture* dt = g.GetTextDebugTexture(); Texture* dt = g.GetTextDebugTexture();
g.DrawQuad(Rectangle(mTextDebugPosX, 400, dt->GetWidth() , dt->GetHeight() ), Color::Blue(), dt); g.DrawQuad(Rectangle(mTextDebugPosX, 400, dt->GetWidth() , dt->GetHeight() ), Color::Blue(), dt);
//g.DrawString("This is a test string!", Rectangle(100, 400, 200, 50), Color::Green());
} }
void SimpleRenderScene::DrawStatsGUI() void SimpleRenderScene::DrawStatsGUI()
{ {
std::string mode_names[3] = { "Basic", "Stress", "String" };
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(400, 600)); 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::Text("Scene Info");
ImGui::Separator();
ImGui::Text("Scene Mode: %s", mode_names[mTestMode].c_str());
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");
ImGui::Separator(); ImGui::Separator();
@ -345,11 +361,16 @@ namespace lunarium
ImGui::Text("FPS: %d", Core::GetInstance().GetFrameData().CurrentFPS); ImGui::Text("FPS: %d", Core::GetInstance().GetFrameData().CurrentFPS);
ImGui::Text("Frame Number: %d", mNumFrames); ImGui::Text("Frame Number: %d", mNumFrames);
ImGui::EndChild(); ImGui::EndChild();
ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 4.5f), true);
ImGui::Text("Settings"); ImGui::Text("Settings");
ImGui::Separator(); ImGui::Separator();
ImGui::InputInt("Number of Quads to draw", &mNumQuadsToRender); ImGui::InputInt("Number of Quads to draw", &mNumQuadsToRender);
ImGui::Checkbox("Draw Textures", &mUseTextures); ImGui::Checkbox("Draw Textures", &mUseTextures);
float vals[4] = {mSubTex.left(), mSubTex.top(), mSubTex.HalfWidth * 2, mSubTex.HalfHeight * 2 };
if (ImGui::DragFloat4("Sub Texture Rect", vals))
{
mSubTex = Rectangle::MakeFromTopLeft(vals[0], vals[1], vals[2], vals[3]);
}
ImGui::EndChild(); ImGui::EndChild();
ImGui::End(); ImGui::End();

@ -51,6 +51,7 @@ namespace lunarium
float mSrcWidth; float mSrcWidth;
float mSrcHeight; float mSrcHeight;
i32 mTextDebugPosX; i32 mTextDebugPosX;
Rectangle mSubTex;
HighResTimer mTimer; HighResTimer mTimer;
double mFrameTime; double mFrameTime;

Loading…
Cancel
Save