Updates the Simple Render Scene with better gui features

Adds flip vertical method to the Texture class
master
Joey Pollack 3 years ago
parent f0fb3fdec1
commit 1353fba393

@ -262,6 +262,21 @@ namespace lunarium
glDrawElements(GL_TRIANGLES, mQuadData.NumQuads * 6, GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, mQuadData.NumQuads * 6, GL_UNSIGNED_INT, nullptr);
mQuadData.WireFrameShader->Use();
uprojview.Location = -1;
mQuadData.WireFrameShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection())).LogIfFailed(LogCategory::GRAPHICS);
// Uniform ucolor;
// ucolor.Type = UniformType::F4;
// ucolor.Location = -1;
// ucolor.Name = "in_color";
// mQuadData.WireFrameShader->SetUniform(ucolor, (void*)glm::value_ptr(glm::vec4(0.2f, 0.1f, 1.0f, 1.0f))).LogIfFailed(LogCategory::GRAPHICS);
// glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
// glLineWidth(5.0f);
// glDrawElements(GL_TRIANGLES, mQuadData.NumQuads * 6, GL_UNSIGNED_INT, nullptr);
// glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
mFrameStats.DrawCalls++; mFrameStats.DrawCalls++;
} }
@ -333,6 +348,15 @@ namespace lunarium
return OpRes::Fail("Failed to create the quad shader"); return OpRes::Fail("Failed to create the quad shader");
} }
// Wireframe shader
std::string wire_vert_source = File::ReadTextFile("quad_wireframe.vert");
std::string wire_frag_source = File::ReadTextFile("quad_wireframe.frag");
mQuadData.WireFrameShader = new Shader(wire_vert_source.c_str(), nullptr, wire_frag_source.c_str());
if (!mQuadData.WireFrameShader->IsValid())
{
return OpRes::Fail("Failed to create the quad wireframe shader");
}
// INIT WHITE TEXTURE // INIT WHITE TEXTURE
u8 data[4] = {255, 255, 255, 255}; u8 data[4] = {255, 255, 255, 255};
mQuadData.WhiteTexture = Texture::Create(data, 1, 1); mQuadData.WhiteTexture = Texture::Create(data, 1, 1);

@ -144,6 +144,7 @@ namespace lunarium
BufferLayout BufferLayout; BufferLayout BufferLayout;
VertexBuffer* VertexBuffer; VertexBuffer* VertexBuffer;
Shader* QuadShader; Shader* QuadShader;
Shader* WireFrameShader;
} mQuadData; } mQuadData;
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////

@ -159,6 +159,12 @@ namespace lunarium
default: return OpRes::Fail("Can not set uniform value - Unknown type"); default: return OpRes::Fail("Can not set uniform value - Unknown type");
} }
int error = glGetError();
if (error > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Error setting uniform - Name: %s error code: %d", uniform.Name.c_str(), error);
}
return OpRes::OK(); return OpRes::OK();
} }

@ -0,0 +1,15 @@
#version 450 core
layout (location = 0) in vec2 f_tex_coords;
layout (location = 1) in vec4 f_vert_color;
layout (location = 2) flat in int f_tex_index;
layout (location = 3) flat in int f_tex_is_grey;
uniform vec4 in_color;
out vec4 color;
layout (binding = 0) uniform sampler2D textures[32];
void main()
{
color = in_color;
}

@ -0,0 +1,59 @@
#version 450 core
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 tex_coords;
layout(location = 2) in vec4 color;
layout(location = 3) in float tex_slot;
layout(location = 4) in vec3 translation;
layout(location = 5) in float angle;
layout(location = 6) in vec3 scale;
layout(location = 7) in vec3 tex_is_grey_scale;
layout (location = 0) out vec2 f_tex_coords;
layout (location = 1) out vec4 f_vert_color;
layout (location = 2) flat out int f_tex_index;
layout (location = 3) flat out int f_tex_is_grey;
uniform mat4 projview;
void main()
{
f_tex_index = int(tex_slot);
f_tex_coords = tex_coords;
f_vert_color = color;
f_tex_is_grey = int(tex_is_grey_scale);
// mat4 ModelTrans = mat4(
// vec4( scale.x * cos(angle), scale.x * -sin(angle), 0.0, 0.0),
// vec4( scale.y * sin(angle), scale.y * cos(angle), 0.0, 0.0),
// vec4( 0.0, 0.0, scale.z, 0.0),
// vec4( translation.xyz, 1.0)
// );
mat4 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( translation.xyz, 1.0));
mat4 Rotation = mat4(
vec4( cos(angle), -sin(angle), 0.0, 0.0),
vec4( sin(angle), cos(angle), 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( 0.0, 0.0, 0.0, 1.0));
mat4 Scale = mat4(
vec4(scale.x, 0.0, 0.0, 0.0),
vec4(0.0, scale.y, 0.0, 0.0),
vec4(0.0, 0.0,scale.z, 0.0),
vec4(0.0, 0.0, 0.0, 1.0));
// mat4 model = Translation * RotationScale;
//gl_Position = Translation * vec4(pos, 1.0);
mat4 model = 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(0.0, 0.0, 0.0, 1.0));
model = model * Translation;
model = model * Rotation;
model = model * Scale;
gl_Position = projview * model * vec4(pos, 1.0);
}

@ -48,7 +48,7 @@ namespace lunarium
int error = glGetError(); int error = glGetError();
if (error > 0) if (error > 0)
{ {
Logger::Error(LogCategory::GRAPHICS, "Error setting texture data"); Logger::Error(LogCategory::GRAPHICS, "Error setting texture data: %d", error);
} }
GLint param = clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT; GLint param = clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT;
@ -82,6 +82,36 @@ namespace lunarium
{ {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void Texture::FlipVertically()
{
i32 components = NumComponents();
u8* pixel_buffer = GetPixels();
u8* flipped_buffer = new u8[mWidth * mHeight * components];
for (int i = 0; i < mWidth; ++i)
{
for (int j = 0; j < mHeight; ++j)
{
for (int k = 0; k < components; ++k)
{
flipped_buffer[(i + j * mWidth) * components + k] = pixel_buffer[(i + (mHeight - 1 - j) * mWidth) * components + k];
}
}
}
u32 formats[3] = { GL_RED, GL_RGB, GL_RGBA };
Bind();
glTexImage2D(GL_TEXTURE_2D, 0, formats[(int)mFormat], mWidth, mHeight, 0, formats[(int)mFormat], GL_UNSIGNED_BYTE, flipped_buffer);
Unbind();
int error = glGetError();
if (error > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Error setting texture data: %d", error);
}
}
u32 Texture::GetGLID() const u32 Texture::GetGLID() const
@ -123,5 +153,11 @@ namespace lunarium
return buffer; return buffer;
} }
i32 Texture::NumComponents() const
{
u32 components_size[3] = { 1, 3, 4 };
return components_size[(int)mFormat];
}
} }

@ -30,6 +30,8 @@ namespace lunarium
void Bind(u32 slot = 0) const; void Bind(u32 slot = 0) const;
void Unbind() const; void Unbind() const;
void FlipVertically();
u32 GetGLID() const; u32 GetGLID() const;
u64 GetGLID64() const; u64 GetGLID64() const;
TextureFormat GetFormat() const; TextureFormat GetFormat() const;
@ -37,6 +39,7 @@ namespace lunarium
u32 GetHeight() const; u32 GetHeight() const;
u8* GetPixels() const; u8* GetPixels() const;
i32 NumComponents() const;
private: private:
TextureFormat mFormat; TextureFormat mFormat;

@ -53,7 +53,7 @@ namespace lunarium
// Load test image // Load test image
int w, h, n; int w, h, n;
stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(0);
unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0); unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
TextureFormat format = TextureFormat::RGBA; TextureFormat format = TextureFormat::RGBA;
@ -61,7 +61,7 @@ namespace lunarium
{ {
format = TextureFormat::RGB; format = TextureFormat::RGB;
} }
mpTestImageLoad = Texture::Create(buffer, w, h, format); mpTestImageLoad = Texture::Create(buffer, w, h, format, true);
mSrcWidth = w; mSrcWidth = w;
mSrcHeight = h; mSrcHeight = h;
@ -254,6 +254,11 @@ namespace lunarium
} }
if (Core::Input().IsKeyPressed(KeyCode::F))
{
flip_image = !flip_image;
}
mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280); mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280);
mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720); mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720);
@ -270,6 +275,7 @@ namespace lunarium
Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f); Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f);
g.EndDraw(); g.EndDraw();
mpRenderedImage = mFrameBufferOne->GetTexture(); mpRenderedImage = mFrameBufferOne->GetTexture();
if (flip_image) mpRenderedImage->FlipVertically();
mFrameBufferOne->Unbind(); mFrameBufferOne->Unbind();
box_angle += 0.01f; box_angle += 0.01f;
@ -314,9 +320,9 @@ namespace lunarium
//g->DrawImage(*mpTestImageLoad, glm::vec2(0.0f, 0.0f), Color::White()); //g->DrawImage(*mpTestImageLoad, glm::vec2(0.0f, 0.0f), Color::White());
//Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpTestImageLoad->GetWidth(), (float)mpTestImageLoad->GetHeight()); //Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpTestImageLoad->GetWidth(), (float)mpTestImageLoad->GetHeight());
//Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, mSrcWidth, mSrcHeight); //Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, mSrcWidth, mSrcHeight);
// Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, 16, 16); Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, 16, 16);
// Rectangle dest = Rectangle::MakeFromTopLeft(100.0f, 100.0f, 512.0f, 512.0f); Rectangle dest = Rectangle::MakeFromTopLeft(900.0f, 350.0f, 256.0f, 256.0f);
//g.DrawImage(*mpTestImageLoad, src, dest, Color(1.0f, 1.0f, 1.0f, 0.9f)); g.DrawQuad(dest, Color::White(), mpTestImageLoad, 0.0f, src);
// g->DrawImage(*mpTestImageLoad, src, // g->DrawImage(*mpTestImageLoad, src,
// dest, Color(1.0f, 1.0f, 1.0f, 0.8f)); // dest, Color(1.0f, 1.0f, 1.0f, 0.8f));
@ -348,14 +354,14 @@ namespace lunarium
void SimpleRenderScene::DrawStatsGUI() void SimpleRenderScene::DrawStatsGUI()
{ {
std::string mode_names[3] = { "Basic", "Stress", "String" }; const char* mode_names[3] = { "Basic", "Stress", "String" };
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::Text("Scene Mode: %s", mode_names[mTestMode].c_str()); ImGui::Combo("Scene Mode", (int*)&mTestMode, mode_names, 3);
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");
@ -373,13 +379,27 @@ namespace lunarium
ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 4.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); if (mTestMode == TestMode::Stress)
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::InputInt("Number of Quads to draw", &mNumQuadsToRender);
ImGui::Checkbox("Draw Textures", &mUseTextures);
} }
else if (mTestMode == TestMode::Basic)
{
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]);
}
if (ImGui::Button("Reset"))
{
mSubTex = Rectangle::MakeFromTopLeft(0.0f, 0.0f, 256.0f, 256.0f);
}
}
ImGui::EndChild(); ImGui::EndChild();
ImGui::End(); ImGui::End();

@ -40,6 +40,7 @@ namespace lunarium
int mTextBoxWidth; int mTextBoxWidth;
Sizei mImageSize; Sizei mImageSize;
bool flip_image = true;
Texture* mpRenderedImage; Texture* mpRenderedImage;
Texture* mpTestImageLoad; Texture* mpTestImageLoad;
Texture* mpTestImageLoad2; Texture* mpTestImageLoad2;

Loading…
Cancel
Save