Camera2D
This commit is contained in:
@ -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
|
||||
{
|
||||
|
||||
}
|
||||
@ -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[] = {
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user