Skip to content

Commit

Permalink
[LMAE-22] - Aspect Ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
progrematic committed Aug 30, 2021
1 parent 1c47fbb commit 4c8a144
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 164 deletions.
19 changes: 19 additions & 0 deletions hippo/include/hippo/core/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using SDL_GLContext = void*;
namespace hippo::graphics
{
class Framebuffer;
class VertexArray;
class Shader;
}

namespace hippo::core
Expand All @@ -23,6 +25,7 @@ namespace hippo::core
int x, y, w, h;
int wMin, hMin;
int flags;
float aspectRatio;
glm::vec3 clearColour;
ImguiWindowProperties imguiProps;

Expand All @@ -35,6 +38,8 @@ namespace hippo::core
Window();
~Window();

inline void SetShouldRenderToScreen(bool render) { mShouldRenderToScreen = render; }

bool Create(const WindowProperties& props);
void Shutdown();

Expand All @@ -49,10 +54,24 @@ namespace hippo::core
void BeginRender();
void EndRender();

glm::ivec2 GetSizeInAspectRatio(int width, int height);

private:
void InitializeScreenRender();
void RenderToScreen();
void HandleResize(int width, int height);

private:
WindowProperties mWindowProperties;
SDL_Window* mWindow;
SDL_GLContext mGLContext;
ImguiWindow mImguiWindow;
std::shared_ptr<graphics::Framebuffer> mFramebuffer;

// Screen Render
bool mShouldRenderToScreen;
glm::vec2 mFramebufferSize;
std::shared_ptr<graphics::VertexArray> mScreenVA;
std::shared_ptr<graphics::Shader> mScreenShader;
};
}
117 changes: 116 additions & 1 deletion hippo/src/core/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "hippo/app.h"

#include "hippo/graphics/framebuffer.h"
#include "hippo/graphics/vertex.h"
#include "hippo/graphics/shader.h"

#include "hippo/input/mouse.h"
#include "hippo/input/keyboard.h"
Expand All @@ -12,6 +14,8 @@
#include "SDL2/SDL.h"
#include "glad/glad.h"

#include "external/glm/gtc/matrix_transform.hpp"

namespace hippo::core
{
WindowProperties::WindowProperties()
Expand All @@ -24,13 +28,17 @@ namespace hippo::core
wMin = 320;
hMin = 180;
flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
aspectRatio = 16.f / 9.f;
clearColour = glm::vec3(
static_cast<float>(0x64) / static_cast<float>(0xFF),
static_cast<float>(0x95) / static_cast<float>(0xFF),
static_cast<float>(0xED) / static_cast<float>(0xFF));
}

Window::Window() : mWindow(nullptr) {}
Window::Window()
: mWindow(nullptr)
, mShouldRenderToScreen(true)
{}
Window::~Window()
{
if (mWindow)
Expand All @@ -41,6 +49,7 @@ namespace hippo::core

bool Window::Create(const WindowProperties& props)
{
mWindowProperties = props;
mWindow = SDL_CreateWindow(props.title.c_str(), props.x, props.y, props.w, props.h, props.flags);
if (!mWindow)
{
Expand Down Expand Up @@ -72,6 +81,9 @@ namespace hippo::core
glm::vec4 cc{ props.clearColour.r, props.clearColour.g, props.clearColour.b, 1.f };
mFramebuffer->SetClearColour(cc);

InitializeScreenRender();
HandleResize(props.w, props.h);

mImguiWindow.Create(props.imguiProps);
return true;
}
Expand Down Expand Up @@ -102,6 +114,13 @@ namespace hippo::core
input::Joystick::OnJoystickDisconnected(e.cdevice);
break;

case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_RESIZED)
{
HandleResize(e.window.data1, e.window.data2);
}
break;

default:
break;
}
Expand Down Expand Up @@ -134,6 +153,11 @@ namespace hippo::core
rm.Submit(HIPPO_SUBMIT_RC(PopFramebuffer));
rm.Flush();

if (mShouldRenderToScreen)
{
RenderToScreen();
}

mImguiWindow.BeginRender();
Engine::Instance().GetApp().ImguiRender();
mImguiWindow.EndRender();
Expand All @@ -148,4 +172,95 @@ namespace hippo::core
return glm::ivec2(w, h);
}

void Window::InitializeScreenRender()
{
mScreenVA = std::make_shared<graphics::VertexArray>();
{
HIPPO_CREATE_VERTEX_BUFFER(vb, short);
vb->PushVertex({ 1, 1, 1, 1 });
vb->PushVertex({ 1, -1, 1, 0 });
vb->PushVertex({ -1, -1, 0, 0 });
vb->PushVertex({ -1, 1, 0, 1 });
vb->SetLayout({ 2, 2 });
mScreenVA->PushBuffer(std::move(vb));
}

mScreenVA->SetElements({ 0, 3, 1, 1, 3, 2 });
mScreenVA->Upload();

const char* vertexShader = R"(
#version 410 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texcoords;
out vec2 uvs;
uniform mat4 model = mat4(1.0);
void main()
{
uvs = texcoords;
gl_Position = model * vec4(position, 0.0, 1.0);
}
)";

const char* fragmentShader = R"(
#version 410 core
out vec4 outColor;
in vec2 uvs;
uniform sampler2D tex;
void main()
{
outColor = texture(tex, uvs);
}
)";
mScreenShader = std::make_shared<graphics::Shader>(vertexShader, fragmentShader);
}

void Window::RenderToScreen()
{
HIPPO_ASSERT(mScreenVA->IsValid(), "Attempting to render with invalid VertexArray - did you forget to call VertexArray::Upload?");
if (mScreenVA->IsValid())
{
// Black Bars
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

mScreenVA->Bind();
glBindTexture(GL_TEXTURE_2D, mFramebuffer->GetTextureId());
mScreenShader->Bind();

glm::vec2 scale = mFramebufferSize / (glm::vec2)GetSize();
glm::mat4 model(1.0);
model = glm::scale(model, { scale.x, scale.y, 1.f });
mScreenShader->SetUniformMat4("model", model);
glDrawElements(GL_TRIANGLES, mScreenVA->GetElementCount(), GL_UNSIGNED_INT, 0);

mScreenShader->Unbind();
glBindTexture(GL_TEXTURE_2D, 0);
mScreenVA->Unbind();
}
}

void Window::HandleResize(int width, int height)
{
mFramebufferSize = GetSizeInAspectRatio(width, height);
}

glm::ivec2 Window::GetSizeInAspectRatio(int width, int height)
{
int framebufferWidth = (int)(height * mWindowProperties.aspectRatio);
int framebufferHeight = (int)(width * (1.f / mWindowProperties.aspectRatio));

if (height >= framebufferHeight)
{
framebufferWidth = width;
}
else
{
framebufferHeight = height;
}

return { framebufferWidth, framebufferHeight };
}

}
60 changes: 30 additions & 30 deletions hippoeditor/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ DockId=0x00000001,0

[Window][DockSpaceViewport_11111111]
Pos=0,0
Size=800,600
Size=1280,720
Collapsed=0

[Window][GameView]
Pos=186,0
Size=503,415
Pos=169,0
Size=920,557
Collapsed=0
DockId=0x0000000E,0
DockId=0x00000003,0

[Window][RectSize]
Pos=605,272
Expand All @@ -62,42 +62,42 @@ DockId=0x00000007,0
Pos=691,0
Size=109,448
Collapsed=0
DockId=0x0000000B,0
DockId=0x0000000A,0

[Window][Options]
Pos=691,0
Size=109,415
Pos=1091,0
Size=189,720
Collapsed=0
DockId=0x0000000C,0
DockId=0x00000010,0

[Window][Asset Library Viewer]
Pos=0,0
Size=184,415
Size=167,557
Collapsed=0
DockId=0x0000000D,0
DockId=0x0000000B,0

[Window][Controls]
Pos=0,417
Size=800,183
Pos=0,559
Size=1089,161
Collapsed=0
DockId=0x00000010,0
DockId=0x0000000F,0

[Docking][Data]
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,0 Size=800,600 Split=Y Selected=0xF1886CD9
DockNode ID=0x0000000F Parent=0x8B93E3BD SizeRef=800,415 Split=X
DockNode ID=0x00000009 Parent=0x0000000F SizeRef=689,600 Split=X
DockNode ID=0x00000006 Parent=0x00000009 SizeRef=603,600 Split=X
DockNode ID=0x00000003 Parent=0x00000006 SizeRef=585,600 Split=X Selected=0xF1886CD9
DockNode ID=0x0000000D Parent=0x00000003 SizeRef=184,600 Selected=0x0A0884DF
DockNode ID=0x0000000E Parent=0x00000003 SizeRef=503,600 CentralNode=1 Selected=0xF1886CD9
DockNode ID=0x00000004 Parent=0x00000006 SizeRef=213,600 Split=Y Selected=0xC0B90327
DockNode ID=0x00000001 Parent=0x00000004 SizeRef=315,69 Selected=0xC0B90327
DockNode ID=0x00000002 Parent=0x00000004 SizeRef=315,529 Selected=0xB7BE33B1
DockNode ID=0x00000005 Parent=0x00000009 SizeRef=195,600 Split=Y Selected=0xCE7AA50B
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=224,270 Selected=0xCE7AA50B
DockNode ID=0x00000008 Parent=0x00000005 SizeRef=224,328 Selected=0x5F2B2723
DockNode ID=0x0000000A Parent=0x0000000F SizeRef=109,600 Split=Y Selected=0x8F9AAF83
DockNode ID=0x0000000B Parent=0x0000000A SizeRef=232,448 Selected=0x8F9AAF83
DockNode ID=0x0000000C Parent=0x0000000A SizeRef=232,150 Selected=0x1F88C31B
DockNode ID=0x00000010 Parent=0x8B93E3BD SizeRef=800,183 Selected=0x039BEE69
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,0 Size=1280,720 Split=X Selected=0xF1886CD9
DockNode ID=0x0000000E Parent=0x8B93E3BD SizeRef=1089,720 Split=Y
DockNode ID=0x0000000D Parent=0x0000000E SizeRef=1280,557 Split=X
DockNode ID=0x0000000B Parent=0x0000000D SizeRef=167,720 Selected=0x0A0884DF
DockNode ID=0x0000000C Parent=0x0000000D SizeRef=920,720 Split=X
DockNode ID=0x00000009 Parent=0x0000000C SizeRef=690,600 Split=X
DockNode ID=0x00000006 Parent=0x00000009 SizeRef=603,600 Split=X
DockNode ID=0x00000003 Parent=0x00000006 SizeRef=585,600 CentralNode=1 Selected=0xF1886CD9
DockNode ID=0x00000004 Parent=0x00000006 SizeRef=213,600 Split=Y Selected=0xC0B90327
DockNode ID=0x00000001 Parent=0x00000004 SizeRef=315,69 Selected=0xC0B90327
DockNode ID=0x00000002 Parent=0x00000004 SizeRef=315,529 Selected=0xB7BE33B1
DockNode ID=0x00000005 Parent=0x00000009 SizeRef=195,600 Split=Y Selected=0xCE7AA50B
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=224,270 Selected=0xCE7AA50B
DockNode ID=0x00000008 Parent=0x00000005 SizeRef=224,328 Selected=0x5F2B2723
DockNode ID=0x0000000A Parent=0x0000000C SizeRef=108,600 Selected=0x8F9AAF83
DockNode ID=0x0000000F Parent=0x0000000E SizeRef=1280,161 Selected=0x039BEE69
DockNode ID=0x00000010 Parent=0x8B93E3BD SizeRef=189,720 Selected=0x1F88C31B

Loading

0 comments on commit 4c8a144

Please sign in to comment.