This commit is contained in:
Isuru Samarathunga
2025-09-18 19:45:49 +05:30
parent 8809375282
commit 05aa5b08f8
9 changed files with 46 additions and 16 deletions

View File

@ -17,6 +17,7 @@
#include <IAEngine/Components/SpriteRenderer.hpp>
#include <IAEngine/Nodes/Node.hpp>
#include <IAEngine/Rendering/Renderer.hpp>
#include <IAEngine/Time.hpp>
@ -60,6 +61,7 @@ namespace ia::iae
VOID SpriteRendererComponent::Draw()
{
Renderer::SetParallaxFactor(m_parallaxFactor);
const auto &animFrame = m_currentAnimationState;
animFrame.Texture.Draw(m_node->SortOffset() + m_sortOffset, m_node->GetPosition() + animFrame.Position,
m_node->GetScale() * animFrame.Scale, m_node->GetRotation().z + animFrame.Rotation.z,

View File

@ -41,7 +41,7 @@ namespace ia::iae
SDL_Window *g_windowHandle{};
Vector<RefPtr<GPUTexture>> g_gpuTextureRefs;
Scene* g_activeScene;
Scene *g_activeScene;
BOOL Engine::Initialize(IN CONST InitConfig &config)
{
@ -139,7 +139,7 @@ namespace ia::iae
g_activeScene->Draw();
}
VOID Engine::ChangeScene(IN Scene* scene)
VOID Engine::ChangeScene(IN Scene *scene)
{
g_activeScene = scene;
}

View File

@ -31,9 +31,9 @@ namespace ia::iae
SDL_GPUSamplerCreateInfo createInfo{.min_filter = SDL_GPU_FILTER_LINEAR,
.mag_filter = SDL_GPU_FILTER_LINEAR,
.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR,
.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
.max_anisotropy = 1.0f,
.enable_anisotropy = true};
g_defaultSampler = SDL_CreateGPUSampler(g_gpuDevice, &createInfo);

View File

@ -38,7 +38,7 @@ namespace ia::iae
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);
sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 1, 2, 0, 0);
SDL_GPUColorTargetDescription colorTargets[] = {{
.format = SDL_GetGPUSwapchainTextureFormat(g_gpuDevice, g_windowHandle),

View File

@ -55,6 +55,8 @@ namespace ia::iae
glm::mat4 matView{1.0f};
glm::mat4 matModel{1.0f};
FLOAT32 g_parallaxFactor{0.0f};
BOOL Renderer::Initialize()
{
SDL_GetWindowSizeInPixels(g_windowHandle, &s_width, &s_height);
@ -100,8 +102,7 @@ namespace ia::iae
{
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
.format = SDL_GPU_TEXTUREFORMAT_D16_UNORM,
.usage =
SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
.width = (UINT32) s_width,
.height = (UINT32) s_height,
.layer_count_or_depth = 1,
@ -234,17 +235,26 @@ namespace ia::iae
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &textureState, sizeof(textureState));
}
VOID SetModelTransformMatrix(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation)
VOID SetModelTransformMatrix(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale,
IN FLOAT32 rotation)
{
if (g_parallaxFactor <= 0.0f)
matView = g_camera.GetViewMatrix();
else
matView = glm::mat4(1.0f);
SDL_PushGPUVertexUniformData(g_cmdBuffer, 1, &matView, sizeof(matView));
g_parallaxFactor *= g_camera.Position().x/640.0f;
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 1, &g_parallaxFactor, sizeof(g_parallaxFactor));
const auto depthTestOffset = sortOffset + (Engine::GetActiveScene()->YSortingEnabled() ? position.y : 0);
matModel = glm::translate(glm::mat4(1.0f), {position.x, position.y, position.z + depthTestOffset});
matModel = glm::translate(glm::mat4(1.0f), {position.x, position.y, position.z + depthTestOffset + (g_parallaxFactor < 0.0f ? 0.0f : -100.0f)});
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 FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation,
IN Handle vertexBufferHandle, IN INT32 vertexCount)
VOID Renderer::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale,
IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount)
{
SetModelTransformMatrix(sortOffset, position, scale, rotation);
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}};
@ -252,8 +262,9 @@ namespace ia::iae
SDL_DrawGPUPrimitives(g_renderPass, vertexCount, 1, 0, 0);
}
VOID Renderer::Draw(IN FLOAT32 sortOffset, 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 FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale,
IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle,
IN INT32 indexCount)
{
SetModelTransformMatrix(sortOffset, position, scale, rotation);
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0},
@ -267,4 +278,9 @@ namespace ia::iae
{
return &g_camera;
}
VOID Renderer::SetParallaxFactor(IN FLOAT32 value)
{
g_parallaxFactor = value;
}
} // namespace ia::iae

View File

@ -11,10 +11,14 @@ layout(set = 3, binding = 0) uniform UniformBufferObject {
bool flipV;
bool flipH;
} ubo;
layout(set = 3, binding = 1) uniform UniformBufferObject2 {
float parallaxFactor;
} ubo2;
void main()
{
vec2 uv = inTexCoord;
uv.x += ubo2.parallaxFactor;
if(ubo.flipH) uv.x = 1 - uv.x;
if(ubo.flipV) uv.y = 1 - uv.y;
outColor = texture(texSampler, uv) * ubo.colorOverlay;

File diff suppressed because one or more lines are too long

View File

@ -76,6 +76,11 @@ namespace ia::iae
return m_sortOffset;
}
FLOAT32& ParallaxFactor()
{
return m_parallaxFactor;
}
public:
VOID Draw();
VOID Update();
@ -84,6 +89,7 @@ namespace ia::iae
VOID UpdateAnimation();
private:
FLOAT32 m_parallaxFactor{0.0f};
FLOAT32 m_sortOffset{};
BOOL m_isFlippedV{false};
BOOL m_isFlippedH{false};

View File

@ -51,6 +51,8 @@ namespace ia::iae
STATIC Camera2D* GetCamera();
STATIC VOID SetParallaxFactor(IN FLOAT32 value);
public:
STATIC INT32 Width()
{