Skip to content

Commit

Permalink
Merge pull request #92 from nightingazer/dev
Browse files Browse the repository at this point in the history
Sync main with dev branch
  • Loading branch information
Alex Grishan authored Feb 2, 2023
2 parents f4466d2 + 8c5e9c1 commit 0fe8cc1
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 74 deletions.
1 change: 1 addition & 0 deletions AGE/include/Age/Age.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Age/Renderer/Buffer.h"
#include "Age/Renderer/Shader.h"
#include "Age/Renderer/Texture.h"
#include "Age/Renderer/SubTexture2D.h"
#include "Age/Renderer/Context.h"
#include "Age/Renderer/OrthographicCamera.h"
#include "Age/Renderer/OrthographicCameraController.h"
Expand Down
5 changes: 5 additions & 0 deletions AGE/src/Age/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ namespace AGE {
template<typename T>
using Ref = std::shared_ptr<T>;

template<typename T, typename ... Args>
constexpr Ref<T> MakeRef(Args&& ... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}

template<typename T>
using Scope = std::unique_ptr<T>;
}
Expand Down
17 changes: 12 additions & 5 deletions AGE/src/Age/Renderer/OrthographicCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ namespace AGE {
AGE_PROFILE_FUNCTION();

m_AspectRatio = width / height;
m_Camera.SetProjection(
-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel
);
CalculateView();
}

bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e) {
AGE_PROFILE_FUNCTION();

m_ZoomLevel -= e.YOffset() * 0.25f;
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
m_Bounds = {-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel};
m_Camera.SetProjection(m_Bounds.Left, m_Bounds.Right, m_Bounds.Bottom, m_Bounds.Top);
CalculateView();
return false;
}

Expand All @@ -93,4 +90,14 @@ namespace AGE {
OnResize((float)e.Width(), (float)e.Height());
return false;
}

void OrthographicCameraController::SetZoomLevel(float level) {
m_ZoomLevel = level;
CalculateView();
}

void OrthographicCameraController::CalculateView() {
m_Bounds = {-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel};
m_Camera.SetProjection(m_Bounds.Left, m_Bounds.Right, m_Bounds.Bottom, m_Bounds.Top);
}
} // AGE
3 changes: 2 additions & 1 deletion AGE/src/Age/Renderer/OrthographicCameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ namespace AGE {
void OnEvent(Event& e);

void OnResize(float width, float height);
void CalculateView();

OrthographicCamera& GetCamera() { return m_Camera; }
const OrthographicCamera& GetCamera() const { return m_Camera; }

float ZoomLevel() const { return m_ZoomLevel; }
void SetZoomLevel(float level) { m_ZoomLevel = level; }
void SetZoomLevel(float level);
[[nodiscard]] inline CameraBounds Bounds() const { return m_Bounds; }

private:
Expand Down
185 changes: 159 additions & 26 deletions AGE/src/Age/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace AGE {

uint32_t QuadIndexCount = 0;

std::array<Ref<Texture2D>, MaxTextureSlots> TextureSlots;
std::array<Ref<Texture2D>, MaxTextureSlots> TextureSlots{UnitTexture};
uint32_t TextureSlotIndex = 1; //0 - Unit Texture;

glm::vec4 QuadVertexPositions[4];
Expand Down Expand Up @@ -161,39 +161,39 @@ namespace AGE {
void Renderer2D::DrawQuad(const glm::vec3& pos, const glm::vec2& size, const glm::vec4& color) {
AGE_PROFILE_FUNCTION();

DrawQuad(pos, size, nullptr, color);
DrawQuad(pos, size, s_Data.UnitTexture, color);
}

void Renderer2D::DrawQuad(
const glm::vec2& pos, const glm::vec2& size, const Ref<Texture2D>& texture,
const float tillingFactor
const float tilingFactor
) {
AGE_PROFILE_FUNCTION();

DrawQuad(glm::vec3(pos, 0.0f), size, texture, tillingFactor);
DrawQuad(glm::vec3(pos, 0.0f), size, texture, tilingFactor);
}

void Renderer2D::DrawQuad(
const glm::vec3& pos, const glm::vec2& size, const Ref<Texture2D>& texture,
const float tillingFactor
const float tilingFactor
) {
AGE_PROFILE_FUNCTION();

DrawQuad(pos, size, texture, glm::vec4(1.0f), tillingFactor);
DrawQuad(pos, size, texture, glm::vec4(1.0f), tilingFactor);
}

void Renderer2D::DrawQuad(
const glm::vec2& pos, const glm::vec2& size, const Ref<Texture2D>& texture,
const glm::vec4& color
const glm::vec4& color, float tilingFactor
) {
AGE_PROFILE_FUNCTION();

DrawQuad(glm::vec3(pos, 0.0f), size, texture, color);
DrawQuad(glm::vec3(pos, 0.0f), size, texture, color, tilingFactor);
}

void Renderer2D::DrawQuad(
const glm::vec3& pos, const glm::vec2& size, const Ref<Texture2D>& texture,
const glm::vec4& color, const float tillingFactor
const glm::vec4& color, const float tilingFactor
) {
AGE_PROFILE_FUNCTION();

Expand All @@ -213,36 +213,105 @@ namespace AGE {
*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x - halfSize.x, pos.y + halfSize.y, pos.z},
vertexColor,
{1.0f, 1.0f},
{0.0f, 1.0f},
(float)textureIndex,
tillingFactor
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x + halfSize.x, pos.y + halfSize.y, pos.z},
vertexColor,
{0.0f, 1.0f},
{1.0f, 1.0f},
(float)textureIndex,
tillingFactor
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x + halfSize.x, pos.y - halfSize.y, pos.z},
vertexColor,
{1.0f, 0.0f},
(float)textureIndex,
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x - halfSize.x, pos.y - halfSize.y, pos.z},
vertexColor,
{0.0f, 0.0f},
(float)textureIndex,
tillingFactor
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

s_Data.QuadIndexCount += 6;
s_Data.Stats.QuadCount++;
}

void Renderer2D::DrawQuad(const glm::vec2& pos, const glm::vec2& size, const Ref <SubTexture2D>& subTexture, float tilingFactor) {
DrawQuad(glm::vec3{pos, 0.0f}, size, subTexture, glm::vec4{1.0f}, tilingFactor);
}

void Renderer2D::DrawQuad(const glm::vec3& pos, const glm::vec2& size, const Ref <SubTexture2D>& subTexture, float tilingFactor) {
DrawQuad(pos, size, subTexture, glm::vec4{1.0f}, tilingFactor);
}

void Renderer2D::DrawQuad(const glm::vec2& pos, const glm::vec2& size, const Ref <SubTexture2D>& subTexture, const glm::vec4& color, float tilingFactor) {
DrawQuad(glm::vec3{pos, 0.0f}, size, subTexture, color, tilingFactor);
}

void Renderer2D::DrawQuad(const glm::vec3& pos, const glm::vec2& size, const Ref <SubTexture2D>& subTexture, const glm::vec4& color, float tilingFactor) {
auto texture = subTexture->GetTexture();

int textureIndex = FindTextureIndex(texture);

// Reset color to white if texture is corrupted to properly display error texture
glm::vec4 vertexColor{color};
if (texture && !texture->IsCorrect()) {
vertexColor = {1.0f, 1.0f, 1.0f, 1.0f};
}

if (s_Data.QuadIndexCount >= s_Data.MaxIndices)
NextBatch();

glm::vec2 halfSize{size * 0.5f};
const glm::vec2* texCoords = subTexture->TexCoords();

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x - halfSize.x, pos.y + halfSize.y, pos.z},
vertexColor,
texCoords[0],
(float)textureIndex,
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x + halfSize.x, pos.y + halfSize.y, pos.z},
vertexColor,
texCoords[1],
(float)textureIndex,
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x + halfSize.x, pos.y - halfSize.y, pos.z},
vertexColor,
texCoords[2],
(float)textureIndex,
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
{pos.x - halfSize.x, pos.y - halfSize.y, pos.z},
vertexColor,
{1.0f, 0.0f},
texCoords[3],
(float)textureIndex,
tillingFactor
tilingFactor
};
s_Data.QuadVertexBufferPtr++;

Expand All @@ -251,7 +320,7 @@ namespace AGE {
}

int Renderer2D::FindTextureIndex(const Ref<Texture2D>& texture) {
int textureIndex = 0;
int textureIndex = -1;

if (texture != nullptr) {
for (int i{0}; i < s_Data.TextureSlotIndex; i++) {
Expand All @@ -261,7 +330,7 @@ namespace AGE {
}
}

if (textureIndex == 0) {
if (textureIndex == -1) {
textureIndex = (int)s_Data.TextureSlotIndex;
s_Data.TextureSlots[textureIndex] = texture;
s_Data.TextureSlotIndex++;
Expand All @@ -279,23 +348,23 @@ namespace AGE {
// /////////////////////////////////////////////////////////////////////////////////////////////////

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, color, 1.0f);
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, s_Data.UnitTexture, color, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotationDeg, const glm::vec4& color) {
DrawRotatedQuad(pos, size, rotationDeg, nullptr, color, 1.0f);
DrawRotatedQuad(pos, size, rotationDeg, s_Data.UnitTexture, color, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tilingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, s_Data.UnitTexture, glm::vec4{1.0f}, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tillingFactor) {
DrawRotatedQuad(pos, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, float tilingFactor) {
DrawRotatedQuad(pos, size, rotationDeg, s_Data.UnitTexture, glm::vec4{1.0f}, 1.0f);
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, const glm::vec4& color) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, nullptr, glm::vec4{1.0f}, 1.0f);
void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, const float rotationDeg, const Ref<Texture2D>& texture, const glm::vec4& color, float tilingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, s_Data.UnitTexture, glm::vec4{1.0f}, tilingFactor);
}

void Renderer2D::DrawRotatedQuad(
Expand Down Expand Up @@ -349,6 +418,70 @@ namespace AGE {
s_Data.Stats.QuadCount++;
}

void Renderer2D::DrawRotatedQuad(const glm::vec2& pos, const glm::vec2& size, float rotationDeg, const Ref <SubTexture2D>& subTexture, float tilingFactor) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, subTexture, glm::vec4{1.0f}, tilingFactor);
}

void Renderer2D::DrawRotatedQuad(const glm::vec3& pos, const glm::vec2& size, float rotationDeg, const Ref <SubTexture2D>& subTexture, float tilingFactor) {
DrawRotatedQuad(pos, size, rotationDeg, subTexture, glm::vec4{1.0f}, tilingFactor);
}

void Renderer2D::DrawRotatedQuad(
const glm::vec2& pos, const glm::vec2& size, float rotationDeg, const Ref <SubTexture2D>& subTexture, const glm::vec4& color, float tilingFactor
) {
DrawRotatedQuad(glm::vec3{pos, 0.0f}, size, rotationDeg, subTexture, color, tilingFactor);
}

void Renderer2D::DrawRotatedQuad(
const glm::vec3& pos, const glm::vec2& size, float rotationDeg, const Ref <SubTexture2D>& subTexture, const glm::vec4& color, float tillingFactor
) {
glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotationDeg), {0.0f, 0.0f, 1.0f})
* glm::scale(glm::mat4(1.0f), {size.x, size.y, 0.0f});

int textureIndex = FindTextureIndex(subTexture->GetTexture());
const glm::vec2* texCoord = subTexture->TexCoords();

*s_Data.QuadVertexBufferPtr = QuadVertex{
transform * s_Data.QuadVertexPositions[0],
color,
texCoord[0],
(float)textureIndex,
tillingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
transform * s_Data.QuadVertexPositions[1],
color,
texCoord[1],
(float)textureIndex,
tillingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
transform * s_Data.QuadVertexPositions[2],
color,
texCoord[2],
(float)textureIndex,
tillingFactor
};
s_Data.QuadVertexBufferPtr++;

*s_Data.QuadVertexBufferPtr = QuadVertex{
transform * s_Data.QuadVertexPositions[3],
color,
texCoord[3],
(float)textureIndex,
tillingFactor
};
s_Data.QuadVertexBufferPtr++;

s_Data.QuadIndexCount += 6;
s_Data.Stats.QuadCount++;
}

#pragma endregion

#pragma region Statistics
Expand Down
Loading

0 comments on commit 0fe8cc1

Please sign in to comment.