Camera2D
This commit is contained in:
@ -3,6 +3,7 @@
|
|||||||
#include <Ground.hpp>
|
#include <Ground.hpp>
|
||||||
|
|
||||||
#include <IAEngine/ResourceManager.hpp>
|
#include <IAEngine/ResourceManager.hpp>
|
||||||
|
#include <IAEngine/Rendering/Camera.hpp>
|
||||||
|
|
||||||
#include <IACore/File.hpp>
|
#include <IACore/File.hpp>
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ namespace ia::iae::game
|
|||||||
|
|
||||||
VOID Game::Update()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -13,3 +13,10 @@
|
|||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IAEngine/Rendering/Camera.hpp>
|
||||||
|
|
||||||
|
namespace ia::iae
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@ -35,7 +35,7 @@ namespace ia::iae
|
|||||||
{
|
{
|
||||||
const auto res = MakeRefPtr<Pipeline_UnlitMesh>();
|
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);
|
const auto pixelShader = LoadShaderFromMemory(ShaderStage::PIXEL, SHADER_SOURCE_UNLITMESH_FRAG, sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 1, 1, 0, 0);
|
||||||
|
|
||||||
SDL_GPUColorTargetDescription colorTargets[] = {
|
SDL_GPUColorTargetDescription colorTargets[] = {
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include <IAEngine/IAEngine.hpp>
|
#include <IAEngine/IAEngine.hpp>
|
||||||
|
#include <IAEngine/Rendering/Camera.hpp>
|
||||||
#include <IAEngine/Rendering/GPUBuffer.hpp>
|
#include <IAEngine/Rendering/GPUBuffer.hpp>
|
||||||
#include <IAEngine/Rendering/GPUTexture.hpp>
|
#include <IAEngine/Rendering/GPUTexture.hpp>
|
||||||
#include <IAEngine/Rendering/Mesh/Quad.hpp>
|
#include <IAEngine/Rendering/Mesh/Quad.hpp>
|
||||||
@ -46,9 +47,16 @@ namespace ia::iae
|
|||||||
|
|
||||||
RefPtr<Pipeline_UnlitMesh> g_pipelineUnlitMesh;
|
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)
|
BOOL Renderer::Initialize(IN Engine *engine)
|
||||||
{
|
{
|
||||||
g_windowHandle = (SDL_Window *) engine->GetWindowHandle();
|
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)))
|
if (!(g_gpuDevice = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, engine->IsDebugMode, nullptr)))
|
||||||
{
|
{
|
||||||
@ -94,6 +102,8 @@ namespace ia::iae
|
|||||||
|
|
||||||
QuadMesh::Initialize();
|
QuadMesh::Initialize();
|
||||||
|
|
||||||
|
matProjection = glm::orthoLH(0.0f, (FLOAT32) s_width, (FLOAT32) s_height, 0.0f, -0.1f, 100.0f);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,6 +177,9 @@ namespace ia::iae
|
|||||||
g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, NULL);
|
g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, NULL);
|
||||||
|
|
||||||
g_pipelineUnlitMesh->Bind((Handle) g_renderPass);
|
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()
|
VOID Renderer::EndFrame()
|
||||||
@ -195,19 +208,36 @@ namespace ia::iae
|
|||||||
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &textureState, sizeof(textureState));
|
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_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}};
|
||||||
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
|
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
|
||||||
SDL_DrawGPUPrimitives(g_renderPass, vertexCount, 1, 0, 0);
|
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},
|
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0},
|
||||||
{.buffer = (SDL_GPUBuffer *) indexBufferHandle, .offset = 0}};
|
{.buffer = (SDL_GPUBuffer *) indexBufferHandle, .offset = 0}};
|
||||||
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
|
SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1);
|
||||||
SDL_BindGPUIndexBuffer(g_renderPass, &bindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
|
SDL_BindGPUIndexBuffer(g_renderPass, &bindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
|
||||||
SDL_DrawGPUIndexedPrimitives(g_renderPass, indexCount, 1, 0, 0, 0);
|
SDL_DrawGPUIndexedPrimitives(g_renderPass, indexCount, 1, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Camera2D *Renderer::GetCamera()
|
||||||
|
{
|
||||||
|
return &g_camera;
|
||||||
|
}
|
||||||
} // namespace ia::iae
|
} // namespace ia::iae
|
||||||
|
|||||||
@ -15,6 +15,7 @@ layout(set = 3, binding = 0) uniform UniformBufferObject {
|
|||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec2 uv = inTexCoord;
|
vec2 uv = inTexCoord;
|
||||||
|
uv.y = 1 - uv.y;
|
||||||
if(ubo.flipH) uv.x = 1 - uv.x;
|
if(ubo.flipH) uv.x = 1 - uv.x;
|
||||||
if(ubo.flipV) uv.y = 1 - uv.y;
|
if(ubo.flipV) uv.y = 1 - uv.y;
|
||||||
outColor = texture(texSampler, uv) * ubo.colorOverlay;
|
outColor = texture(texSampler, uv) * ubo.colorOverlay;
|
||||||
|
|||||||
@ -6,14 +6,18 @@ layout (location = 1) in vec2 inTexCoord;
|
|||||||
|
|
||||||
layout(location = 0) out vec2 outTexCoord;
|
layout(location = 0) out vec2 outTexCoord;
|
||||||
|
|
||||||
layout(set = 1, binding = 0) uniform UniformBufferObject {
|
layout(set = 1, binding = 0) uniform UBO_Vertex_PerScene {
|
||||||
mat4 model;
|
mat4 projection;
|
||||||
|
} uboPerScene;
|
||||||
|
layout(set = 1, binding = 1) uniform UBO_Vertex_PerFrame {
|
||||||
mat4 view;
|
mat4 view;
|
||||||
mat4 proj;
|
} uboPerFrame;
|
||||||
} ubo;
|
layout(set = 1, binding = 2) uniform UBO_Vertex_PerDraw {
|
||||||
|
mat4 model;
|
||||||
|
} uboPerDraw;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(inPosition, 1.0f);
|
gl_Position = uboPerScene.projection * uboPerFrame.view * uboPerDraw.model * vec4(inPosition, 1.0f);
|
||||||
outTexCoord = inTexCoord;
|
outTexCoord = inTexCoord;
|
||||||
}
|
}
|
||||||
File diff suppressed because one or more lines are too long
@ -30,7 +30,7 @@
|
|||||||
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
|
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
|
||||||
#include <glm/ext/scalar_constants.hpp> // glm::pi
|
#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_INFO(...) ia::Logger::Info(IAE_LOG_TAG, __VA_ARGS__)
|
||||||
#define IAE_LOG_WARN(...) ia::Logger::Warn(IAE_LOG_TAG, __VA_ARGS__)
|
#define IAE_LOG_WARN(...) ia::Logger::Warn(IAE_LOG_TAG, __VA_ARGS__)
|
||||||
|
|||||||
@ -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
|
||||||
@ -21,6 +21,7 @@
|
|||||||
namespace ia::iae
|
namespace ia::iae
|
||||||
{
|
{
|
||||||
class Engine;
|
class Engine;
|
||||||
|
class Camera2D;
|
||||||
|
|
||||||
class Renderer
|
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 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 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:
|
public:
|
||||||
STATIC INT32 Width()
|
STATIC INT32 Width()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user