Skip to content

Commit

Permalink
fix OrthoCamera and prepared initial scene
Browse files Browse the repository at this point in the history
  • Loading branch information
fdefelici committed Jun 14, 2024
1 parent 22a9831 commit 6e4847d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
3 changes: 2 additions & 1 deletion include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ struct Color {
float A;
};

const Color Color_White = Color{1.f, 1.f, 1.f, 1.f};
const Color Color_White = Color{1.f, 1.f, 1.f, 1.f};
const Color Color_Red = Color{1.f, 0.f, 0.f, 1.f};
11 changes: 8 additions & 3 deletions include/OrthoCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
class OrthoCamera
{
public:
OrthoCamera(int w, int h) ;
OrthoCamera(int w, int h, float InOrthoSize) ;
glm::mat4 GetViewProjMat() const;
int GetWidth() const;
int GetHeight() const;
int GetResoWidth() const;
int GetResoHeight() const;
float GetOrthoWidth() const;
float GetOrthoHeight() const;
private:
glm::mat4 View;
glm::mat4 Proj;
glm::mat4 ViewProj;
int Width;
int Height;

float OrthoWidth;
float OrthoHeight;
};
2 changes: 1 addition & 1 deletion include/Quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Quad
{
public:
Quad(int w, int h)
Quad(float w, float h)
: Position(glm::vec3(0)),
Rotation(glm::vec3(0)),
Scale(glm::vec3(w, h, 0)),
Expand Down
8 changes: 4 additions & 4 deletions src/OGLQuadRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ OGLQuadRender::OGLQuadRender()

std::vector<float> Vertices = {
0.5f, -0.5f, 0.0f, //bottom right
-0.5f, -0.5f, 0.0f, //bottom left
-0.5f, 0.5f, 0.0f, //top left
-0.5f, -0.5f, 0.0f, //bottom left
-0.5f, 0.5f, 0.0f, //top left

-0.5f, 0.5f, 0.0f, //top left
-0.5f, 0.5f, 0.0f, //top left
0.5f, 0.5f, 0.0f, //top right
0.5f, -0.5f, 0.0f //bottom right
};
Expand All @@ -41,7 +41,7 @@ OGLQuadRender::OGLQuadRender()

void OGLQuadRender::Draw(const std::vector<Quad*>& Quads, const OrthoCamera* Cam)
{
glViewport(0, 0, Cam->GetWidth(), Cam->GetHeight());
glViewport(0, 0, Cam->GetResoWidth(), Cam->GetResoHeight());
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(Vao);
Expand Down
30 changes: 23 additions & 7 deletions src/OrthoCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <glm/glm.hpp>
#include <glm/ext.hpp>

OrthoCamera::OrthoCamera(int w, int h)
OrthoCamera::OrthoCamera(int w, int h, float InOrthoSize)
{
Width = w;
Height = h;
Expand All @@ -12,10 +12,17 @@ OrthoCamera::OrthoCamera(int w, int h)
auto Up = glm::vec3(0.f, 1.f, 0.f);
View = glm::lookAt(Pos, Pos + Dir, Up);

float left = 0;
float right = w;
float bottom = 0;
float top = h;
float Aspect = (float)Width / (float)Height;
OrthoWidth = InOrthoSize * Aspect;
OrthoHeight = InOrthoSize;

float HalfW = OrthoWidth * 0.5f;
float HalfH = OrthoHeight * 0.5f;

float left = -HalfW;
float right = HalfW;
float bottom = -HalfH;
float top = HalfH;
Proj = glm::ortho(left, right, bottom, top, 0.f, 100.f);

ViewProj = Proj * View;
Expand All @@ -26,13 +33,22 @@ glm::mat4 OrthoCamera::GetViewProjMat() const
return ViewProj;
}

int OrthoCamera::GetWidth() const
int OrthoCamera::GetResoWidth() const
{
return Width;
}

int OrthoCamera::GetHeight() const
int OrthoCamera::GetResoHeight() const
{
return Height;
}

float OrthoCamera::GetOrthoWidth() const
{
return OrthoWidth;
}
float OrthoCamera::GetOrthoHeight() const
{
return OrthoHeight;
}

15 changes: 8 additions & 7 deletions src/PlatformScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

void PlatformScene::Start()
{
Camera = new OrthoCamera(Win.GetWidth(), Win.GetHeight());
Camera = new OrthoCamera(Win.GetWidth(), Win.GetHeight(), 10);

Quad* Q1 = new Quad(100, 10);
Q1->Position = glm::vec3{300, 300, 0};
Quad* Floor = new Quad(Camera->GetOrthoWidth(), 1);
Floor->Position = glm::vec3{0, -Camera->GetOrthoHeight() * 0.5f + Floor->Scale.y * 0.5f, 0};

Quad* Q2 = new Quad(100, 10);
Q2->Position = glm::vec3{500, 300, 0};
Quad* Player = new Quad(1, 1);
Player->Position = glm::vec3{0, 0, 0};
Player->Color = Color_Red;

Quads.push_back(Q1);
Quads.push_back(Q2);
Quads.push_back(Floor);
Quads.push_back(Player);
}

void PlatformScene::Update()
Expand Down

0 comments on commit 6e4847d

Please sign in to comment.