Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
sprite_2d.cpp
Go to the documentation of this file.
1#include "sprite_2d.hpp"
2#include "camera_2d.hpp"
5#include "texture_cache.hpp"
6#include "image_data_gpu.hpp"
7#include "image_data.hpp"
8#include "material_cache.hpp"
9#include "material.hpp"
10#include "material_types.hpp"
11#include "scene.hpp"
12
13namespace rendering_engine
14{
15Sprite2D::Sprite2D(RenderResourceContext renderContext, Scene& scene, std::string textureName)
16 :
17 Drawable2D(renderContext, scene),
18 mTextureName(textureName)
19{}
20
22{
24 const float width = static_cast<float>(texture->GetCpuImageData().GetWidth());
25 const float height = static_cast<float>(texture->GetCpuImageData().GetHeight());
26
27 const std::string materialName = "Sprite2D_" + mTextureName + "Mat";
28 auto materialCache = mRenderContext.materialCache;
29
30 Material* material = materialCache->GetMaterial(materialName);
31
32 // CREATE MATERIAL ONLY ONCE
33 if (!material)
34 {
35 MaterialSettings materialSettings;
36 materialSettings.parentMaterialName = "Quad2D";
37 materialSettings.materialName = materialName;
39 materialSettings.shadingModel = ShadingModel::Unlit;
40 materialSettings.blendMode = BlendMode::Opaque;
41
42 materialCache->AddMaterial(materialSettings);
43
44 material = materialCache->GetMaterial(materialName);
45 material->AddTexture(mTextureName);
46 material->InitializeRenderResources();
47 }
48
49 AddRenderBatch("Quad2D", materialName);
50
52
53 mTextureRespectiveScale = glm::vec2(width, height);
55}
56
57void Sprite2D::Update(float deltaTime)
58{
59 Drawable2D::Update(deltaTime);
60}
61
62void Sprite2D::Draw(const Camera2D& camera)
63{
64 Transformations2D transformations;
65 transformations.model = GetTransform().GetWorldMatrix();
66 transformations.view = camera.GetWorldView();
67 transformations.proj = camera.GetProjectionMatrix();
68
69 for (auto& renderBatch : mRenderBatches)
70 {
71 renderBatch.renderResources->SubmitResources(transformations, renderBatch.materialParameters);
72 }
73}
74
75void Sprite2D::SetSpriteScale(float scale)
76{
78}
79
80} // namespace rendering_engine
Represents a 2D camera with position, rotation, and zoom control.
Definition: camera_2d.hpp:36
glm::mat4 GetProjectionMatrix() const
Returns the current orthographic projection matrix.
Definition: camera_2d.cpp:30
const glm::mat4 & GetWorldView() const
Gets the world-view (model) matrix for the camera.
Definition: camera_2d.cpp:87
2D drawable component for rendering objects in 2D space.
Definition: drawable_2d.hpp:27
SceneComponent2D & GetTransform()
Access to the underlying SceneComponent2D (transform).
Definition: drawable_2d.cpp:55
void Update(float deltaTime) override
Updates model matrix (and any other logic).
Definition: drawable_2d.cpp:17
void Initialize() override
Initializes render resources.
Definition: drawable_2d.cpp:12
void SetScale(const glm::vec2 &scale)
Sets the quad scale along each axis.
Definition: drawable_2d.cpp:35
std::vector< RenderBatch > mRenderBatches
void AddRenderBatch(std::string meshName, std::string materialName)
Represents a material instance with parameter values, texture bindings, and rendering configuration.
Definition: material.hpp:30
void InitializeRenderResources()
Initializes backend-specific GPU resources associated with this material.
Definition: material.cpp:26
void AddTexture(const std::string &textureName)
Adds a texture name to the material's list of used textures.
Definition: material.cpp:112
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix.
Base class representing a renderable scene.
Definition: scene.hpp:44
void SetSpriteScale(float scale)
Sets a uniform scale relative to the sprite's texture size.
Definition: sprite_2d.cpp:75
glm::vec2 mTextureRespectiveScale
Definition: sprite_2d.hpp:61
void Update(float deltaTime) override
Updates logic (animation, movement, etc.) for this drawable.
Definition: sprite_2d.cpp:57
void Initialize() override
Initializes render resource pointers (material, mesh, etc.). Must be called after setting material an...
Definition: sprite_2d.cpp:21
void Draw(const Camera2D &camera) override
Submits this quad to the renderer for drawing.
Definition: sprite_2d.cpp:62
Sprite2D(RenderResourceContext renderContext, Scene &scene, std::string textureName)
Constructs the Sprite2D with a render context.
Definition: sprite_2d.cpp:15
std::shared_ptr< ImageDataGpu > GetTextureResources(std::string filename)
Retrieves the full texture resource wrapper from cache.
Settings required to define a material instance.
Aggregates pointers to global rendering resource managers.
Contains transformation matrices for 2D rendering.
glm::mat4 view
View (camera) transformation matrix.
glm::mat4 proj
Projection transformation matrix.
glm::mat4 model
Model transformation matrix.