This commit is contained in:
Isuru Samarathunga
2025-09-13 17:46:11 +05:30
parent 3687a70aba
commit 594180c5d3
10 changed files with 100 additions and 14 deletions

View File

@ -3,6 +3,7 @@
#include <Ground.hpp>
#include <IAEngine/ResourceManager.hpp>
#include <IAEngine/Rendering/Camera.hpp>
#include <IACore/File.hpp>
@ -36,6 +37,7 @@ namespace ia::iae::game
VOID Game::Update()
{
g_tex->Draw({}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f});
g_tex->Draw({200.0f, 150.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f});
iae::Renderer::GetCamera()->Position().x += 0.1f;
}
}

View File

@ -13,3 +13,10 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Rendering/Camera.hpp>
namespace ia::iae
{
}

View File

@ -35,7 +35,7 @@ namespace ia::iae
{
const auto res = MakeRefPtr<Pipeline_UnlitMesh>();
const auto vertexShader = LoadShaderFromMemory(ShaderStage::VERTEX, SHADER_SOURCE_UNLITMESH_VERT, sizeof(SHADER_SOURCE_UNLITMESH_VERT), 0, 1, 0, 0);
const auto vertexShader = LoadShaderFromMemory(ShaderStage::VERTEX, SHADER_SOURCE_UNLITMESH_VERT, sizeof(SHADER_SOURCE_UNLITMESH_VERT), 0, 3, 0, 0);
const auto pixelShader = LoadShaderFromMemory(ShaderStage::PIXEL, SHADER_SOURCE_UNLITMESH_FRAG, sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 1, 1, 0, 0);
SDL_GPUColorTargetDescription colorTargets[] = {

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/IAEngine.hpp>
#include <IAEngine/Rendering/Camera.hpp>
#include <IAEngine/Rendering/GPUBuffer.hpp>
#include <IAEngine/Rendering/GPUTexture.hpp>
#include <IAEngine/Rendering/Mesh/Quad.hpp>
@ -46,9 +47,16 @@ namespace ia::iae
RefPtr<Pipeline_UnlitMesh> g_pipelineUnlitMesh;
Camera2D g_camera{};
glm::mat4 matProjection{1.0f};
glm::mat4 matView{1.0f};
glm::mat4 matModel{1.0f};
BOOL Renderer::Initialize(IN Engine *engine)
{
g_windowHandle = (SDL_Window *) engine->GetWindowHandle();
SDL_GetWindowSizeInPixels(g_windowHandle, &s_width, &s_height);
if (!(g_gpuDevice = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, engine->IsDebugMode, nullptr)))
{
@ -94,6 +102,8 @@ namespace ia::iae
QuadMesh::Initialize();
matProjection = glm::orthoLH(0.0f, (FLOAT32) s_width, (FLOAT32) s_height, 0.0f, -0.1f, 100.0f);
return true;
}
@ -167,6 +177,9 @@ namespace ia::iae
g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, NULL);
g_pipelineUnlitMesh->Bind((Handle) g_renderPass);
SDL_PushGPUVertexUniformData(g_cmdBuffer, 0, &matProjection, sizeof(matProjection));
matView = g_camera.GetViewMatrix();
SDL_PushGPUVertexUniformData(g_cmdBuffer, 1, &matView, sizeof(matView));
}
VOID Renderer::EndFrame()
@ -195,19 +208,36 @@ namespace ia::iae
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &textureState, sizeof(textureState));
}
VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount)
VOID SetModelTransformMatrix(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation)
{
matModel = glm::translate(glm::mat4(1.0f), position);
matModel = glm::rotate(matModel, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
matModel = glm::scale(matModel, scale);
SDL_PushGPUVertexUniformData(g_cmdBuffer, 2, &matModel, sizeof(matModel));
}
VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation,
IN Handle vertexBufferHandle, IN INT32 vertexCount)
{
SetModelTransformMatrix(position, scale, rotation);
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}};
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
SDL_DrawGPUPrimitives(g_renderPass, vertexCount, 1, 0, 0);
}
VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount)
VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation,
IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount)
{
SetModelTransformMatrix(position, scale, rotation);
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0},
{.buffer = (SDL_GPUBuffer *) indexBufferHandle, .offset = 0}};
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
SDL_BindGPUIndexBuffer(g_renderPass, &bindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
SDL_DrawGPUIndexedPrimitives(g_renderPass, indexCount, 1, 0, 0, 0);
}
Camera2D *Renderer::GetCamera()
{
return &g_camera;
}
} // namespace ia::iae

View File

@ -15,6 +15,7 @@ layout(set = 3, binding = 0) uniform UniformBufferObject {
void main()
{
vec2 uv = inTexCoord;
uv.y = 1 - uv.y;
if(ubo.flipH) uv.x = 1 - uv.x;
if(ubo.flipV) uv.y = 1 - uv.y;
outColor = texture(texSampler, uv) * ubo.colorOverlay;

View File

@ -6,14 +6,18 @@ layout (location = 1) in vec2 inTexCoord;
layout(location = 0) out vec2 outTexCoord;
layout(set = 1, binding = 0) uniform UniformBufferObject {
mat4 model;
layout(set = 1, binding = 0) uniform UBO_Vertex_PerScene {
mat4 projection;
} uboPerScene;
layout(set = 1, binding = 1) uniform UBO_Vertex_PerFrame {
mat4 view;
mat4 proj;
} ubo;
} uboPerFrame;
layout(set = 1, binding = 2) uniform UBO_Vertex_PerDraw {
mat4 model;
} uboPerDraw;
void main()
{
gl_Position = vec4(inPosition, 1.0f);
gl_Position = uboPerScene.projection * uboPerFrame.view * uboPerDraw.model * vec4(inPosition, 1.0f);
outTexCoord = inTexCoord;
}

File diff suppressed because one or more lines are too long

View File

@ -30,7 +30,7 @@
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
#include <glm/ext/scalar_constants.hpp> // glm::pi
#define IAE_LOG_TAG "IAE"
#define IAE_LOG_TAG "[IAE]: "
#define IAE_LOG_INFO(...) ia::Logger::Info(IAE_LOG_TAG, __VA_ARGS__)
#define IAE_LOG_WARN(...) ia::Logger::Warn(IAE_LOG_TAG, __VA_ARGS__)

View File

@ -0,0 +1,39 @@
// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Base.hpp>
namespace ia::iae
{
class Camera2D
{
public:
glm::vec2 &Position()
{
return m_position;
}
glm::mat4 GetViewMatrix()
{
return glm::lookAtLH(glm::vec3{m_position, -2.0f}, {m_position, 0.0f}, {0.0f, 1.0f, 0.0f});
}
private:
glm::vec2 m_position{};
};
} // namespace ia::iae

View File

@ -21,6 +21,7 @@
namespace ia::iae
{
class Engine;
class Camera2D;
class Renderer
{
@ -49,6 +50,8 @@ namespace ia::iae
STATIC VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount);
STATIC VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount);
STATIC Camera2D* GetCamera();
public:
STATIC INT32 Width()
{