Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
render_stage.hpp
Go to the documentation of this file.
1
10
11#ifndef FOOLSENGINE_RENDERSTAGE_H
12#define FOOLSENGINE_RENDERSTAGE_H
13
14#include <string>
15#include <map>
16#include <queue>
17
18#include <SDL2/SDL.h>
19
20#include "texture.hpp"
21#include "shader_program.hpp"
22#include "framebuffer.hpp"
23#include "mesh.hpp"
24#include "material.hpp"
25#include "instance.hpp"
26#include "model.hpp"
27#include "light.hpp"
28#include "util.hpp"
29
30namespace ToyMaker {
43
51 OpaqueRenderUnit(std::shared_ptr<StaticMesh> meshHandle,std::shared_ptr<Material> materialHandle, glm::mat4 modelMatrix):
52 mMeshHandle{meshHandle}, mMaterialHandle{materialHandle}, mModelMatrix{modelMatrix}
53 {
54 setSortKey();
55 }
56
64 bool operator<(const OpaqueRenderUnit& other) const {
65 return mSortKey < other.mSortKey;
66 }
67
72 std::uint32_t mSortKey {};
73
78 std::shared_ptr<StaticMesh> mMeshHandle;
79
84 std::shared_ptr<Material> mMaterialHandle;
85
90 glm::mat4 mModelMatrix;
91
96 void setSortKey() {
97 std::uint32_t meshHash { static_cast<uint32_t>(std::hash<StaticMesh*>{}(mMeshHandle.get())) };
98 std::uint32_t materialHash { static_cast<uint32_t>(std::hash<Material*>{}(mMaterialHandle.get()))};
99 mSortKey |= (meshHash << ((sizeof(uint32_t)/2)*8)) & 0xFFFF0000;
100 mSortKey |= (materialHash << 0) & 0x0000FFFF;
101 }
102 };
103
117 LightRenderUnit(std::shared_ptr<StaticMesh> meshHandle, const LightEmissionData& lightEmissionData, const glm::mat4& modelMatrix):
118 mMeshHandle{ meshHandle },
119 mModelMatrix{ modelMatrix },
120 mLightAttributes { lightEmissionData }
121 { setSortKey(); }
122
130 bool operator<(const LightRenderUnit& other) const {
131 return mSortKey < other.mSortKey;
132 }
133
138 std::uint32_t mSortKey {};
139
144 std::shared_ptr<StaticMesh> mMeshHandle;
145
150 glm::mat4 mModelMatrix;
151
157
162 void setSortKey() {
163 std::uint32_t meshHash { static_cast<uint32_t>(std::hash<StaticMesh*>{}(mMeshHandle.get())) };
164 mSortKey = meshHash;
165 }
166 };
167
176 public:
182 BaseRenderStage(const std::string& shaderFilepath);
183
184 BaseRenderStage(const BaseRenderStage& other) = delete;
185 BaseRenderStage(BaseRenderStage&& other) = delete;
186 BaseRenderStage& operator=(const BaseRenderStage& other) = delete;
187 BaseRenderStage& operator=(BaseRenderStage&& other) = delete;
188
193 virtual ~BaseRenderStage();
194
205 virtual void setup(const glm::u16vec2& targetDimensions) = 0;
206
213 virtual void validate() = 0;
214
221 virtual void execute() = 0;
222
231 void attachTexture(const std::string& name, std::shared_ptr<Texture> textureHandle);
232
241 void attachMesh(const std::string& name, std::shared_ptr<StaticMesh> meshHandle);
242
251 void attachMaterial(const std::string& name, std::shared_ptr<Material> materialHandle);
252
259 std::shared_ptr<Texture> getTexture(const std::string& name);
260
267 std::shared_ptr<StaticMesh> getMesh(const std::string& name);
268
275 std::shared_ptr<Material> getMaterial(const std::string& name);
276
282 void useViewport();
283
289 void setTargetViewport(const SDL_Rect& targetViewport);
290
296 void submitToRenderQueue(OpaqueRenderUnit renderUnit);
297
303 void submitToRenderQueue(LightRenderUnit lightRenderUnit);
304
305 protected:
306
316
321 std::shared_ptr<ShaderProgram> mShaderHandle;
322
327 std::map<std::string, std::shared_ptr<Texture>> mTextureAttachments {};
328
333 std::map<std::string, std::shared_ptr<StaticMesh>> mMeshAttachments {};
334
339 std::map<std::string, std::shared_ptr<Material>> mMaterialAttachments {};
340
345 std::priority_queue<OpaqueRenderUnit> mOpaqueMeshQueue {};
346
351 std::priority_queue<LightRenderUnit> mLightQueue {};
352
357 SDL_Rect mTargetViewport {0, 0, 800, 600};
358 };
359
366 public:
373 BaseOffscreenRenderStage(const std::string& shaderFilepath, const nlohmann::json& templateFramebufferDescription);
374
383 void setTargetTexture(std::shared_ptr<Texture> texture, std::size_t targetID);
384
391 std::size_t attachTextureAsTarget(std::shared_ptr<Texture> textureHandle);
401 std::size_t attachTextureAsTarget(const std::string& targetName, std::shared_ptr<Texture> textureHandle);
402
409 void declareRenderTarget(const std::string& name, unsigned int index);
410
420 std::shared_ptr<Texture> getRenderTarget(const std::string& name);
421
429 void detachTargetTexture(std::size_t targetTextureID);
430
431
439 void removeRenderTarget(const std::string& name);
440
447 bool hasAttachedRBO() const;
448
455 bool hasOwnRBO() const;
456
462 RBO& getOwnRBO();
463
469 void attachRBO(RBO& rbo);
470
475 void detachRBO();
476
477 protected:
482 std::shared_ptr<Framebuffer> mFramebufferHandle;
483
489
494 std::map<std::string, unsigned int> mRenderTargets {};
495 };
496
509 public:
515 GeometryRenderStage(const std::string& shaderFilepath)
516 : BaseOffscreenRenderStage{shaderFilepath, nlohmann::json::object({
519 {"parameters", {
520 {"nColorAttachments", 3},
521 {"dimensions", nlohmann::json::array({
522 800, 600
523 })},
524 {"ownsRBO", true },
525 {"colorBufferDefinitions",{
527 .mDataType=GL_FLOAT,
528 .mComponentCount=4
529 },
531 .mDataType=GL_FLOAT,
532 .mComponentCount=4
533 },
535 .mDataType=GL_UNSIGNED_BYTE,
536 .mComponentCount=4
537 }
538 }},
539 }}
540 })}
541 {}
542
543 virtual void setup(const glm::u16vec2& textureDimensions) override;
544 virtual void validate() override;
545 virtual void execute() override;
546 };
547
561 class LightingRenderStage : public BaseOffscreenRenderStage {
562 public:
563 LightingRenderStage(const std::string& shaderFilepath)
564 : BaseOffscreenRenderStage{shaderFilepath, nlohmann::json::object({
567 {"parameters", {
568 {"nColorAttachments", 2},
569 {"dimensions", nlohmann::json::array({
570 800, 600
571 })},
572 {"ownsRBO", true },
573 {"colorBufferDefinitions",{
575 .mDataType=GL_FLOAT,
576 .mComponentCount=4
577 },
579 .mDataType=GL_FLOAT,
580 .mComponentCount=4
581 },
582 }},
583 }}
584 })}
585 {}
586 virtual void setup(const glm::u16vec2& textureDimensions) override;
587 virtual void validate() override;
588 virtual void execute() override;
589 };
590
600 class BlurRenderStage : public BaseOffscreenRenderStage {
601 public:
602 BlurRenderStage(const std::string& shaderFilepath)
603 : BaseOffscreenRenderStage{shaderFilepath, nlohmann::json::object({
606 {"parameters", {
607 {"nColorAttachments", 2},
608 {"dimensions", nlohmann::json::array({
609 800, 600
610 })},
611 {"ownsRBO", false},
612 {"colorBufferDefinitions",{
614 .mDataType=GL_FLOAT,
615 .mComponentCount=4
616 },
618 .mDataType=GL_FLOAT,
619 .mComponentCount=4
620 },
621 }},
622 }}
623 })}
624 {}
625 virtual void setup(const glm::u16vec2& textureDimensions) override;
626 virtual void validate() override;
627 virtual void execute() override;
628 };
629
639 class SkyboxRenderStage : public BaseOffscreenRenderStage {
640 public:
641 SkyboxRenderStage(const std::string& filepath)
642 : BaseOffscreenRenderStage(filepath, nlohmann::json::object({
645 {"parameters", {
646 {"nColorAttachments", 1},
647 {"dimensions", nlohmann::json::array({
648 800, 600
649 })},
650 {"ownsRBO", false},
651 {"colorBufferDefinitions", nlohmann::json::array()}
652 }}
653 }))
654 {}
655
656 virtual void setup(const glm::u16vec2& textureDimensions) override;
657 virtual void validate() override;
658 virtual void execute() override;
659 };
660
672 class TonemappingRenderStage : public BaseOffscreenRenderStage {
673 public:
674 TonemappingRenderStage(const std::string& shaderFilepath)
675 : BaseOffscreenRenderStage{shaderFilepath, nlohmann::json::object({
678 {"parameters", {
679 {"nColorAttachments", 1},
680 {"dimensions", nlohmann::json::array({
681 800, 600
682 })},
683 {"ownsRBO", false},
684 {"colorBufferDefinitions",{
686 .mDataType=GL_UNSIGNED_BYTE,
687 .mComponentCount=4
688 },
689 }},
690 }}
691 })}
692 {}
693
694 virtual void setup(const glm::u16vec2& textureDimensions) override;
695 virtual void validate() override;
696 virtual void execute() override;
697 };
698
708 class AdditionRenderStage: public BaseOffscreenRenderStage {
709 public:
710 AdditionRenderStage(const std::string& shaderFilepath):
711 BaseOffscreenRenderStage(shaderFilepath, nlohmann::json::object({
714 {"parameters", {
715 {"nColorAttachments", 1},
716 {"dimensions", nlohmann::json::array({
717 800, 600
718 })},
719 {"ownsRBO", false},
720 {"colorBufferDefinitions", {
722 .mDataType=GL_UNSIGNED_BYTE,
723 .mComponentCount=4,
724 },
725 }},
726 }},
727 }))
728 {}
729
730 virtual void setup(const glm::u16vec2& textureDimensions) override;
731 virtual void validate() override;
732 virtual void execute() override;
733 private:
734 };
735
745 class ScreenRenderStage: public BaseRenderStage {
746 public:
747 ScreenRenderStage(const std::string& shaderFilepath):
748 BaseRenderStage(shaderFilepath)
749 {}
750
751 virtual void setup(const glm::u16vec2& targetDimensions) override;
752 virtual void validate() override;
753 virtual void execute() override;
754 };
755
767 class ResizeRenderStage : public BaseOffscreenRenderStage {
768 public:
769 ResizeRenderStage(const std::string& shaderFilepath)
770 : BaseOffscreenRenderStage{shaderFilepath, nlohmann::json::object({
773 {"parameters", {
774 {"nColorAttachments", 1},
775 {"dimensions", nlohmann::json::array({
776 800, 600
777 })},
778 {"ownsRBO", false},
779 {"colorBufferDefinitions",{
781 .mDataType=GL_UNSIGNED_BYTE,
782 .mComponentCount=4,
783 },
784 }},
785 }}
786 })}
787 {}
788
789 virtual void setup(const glm::u16vec2& textureDimensions) override;
790 virtual void validate() override;
791 virtual void execute() override;
792
793 private:
794 };
795
796}
797
798#endif
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:602
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:627
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:619
void attachRBO(RBO &rbo)
Attaches an RBO to this stage's Framebuffer which may or may not be owned by it.
Definition render_stage.cpp:110
RBO & getOwnRBO()
Returns the RBO owned by this stage's Framebuffer, if it has one.
Definition render_stage.cpp:106
bool hasOwnRBO() const
Tests whether this stage has created and owns an RBO.
Definition render_stage.cpp:102
BaseOffscreenRenderStage(const std::string &shaderFilepath, const nlohmann::json &templateFramebufferDescription)
Constructs a new Base Offscreen Render Stage object.
Definition render_stage.cpp:90
void removeRenderTarget(const std::string &name)
(in theory) Removes a target texture that was given a name from a list of named target textures.
std::shared_ptr< Texture > getRenderTarget(const std::string &name)
Gets a named render target texture from this stage.
Definition render_stage.cpp:132
void detachRBO()
Detaches the RBO currently attached to this stage's Framebuffer.
Definition render_stage.cpp:114
bool hasAttachedRBO() const
Tests whether an RBO was attached to this render stage.
Definition render_stage.cpp:98
std::shared_ptr< Framebuffer > mFramebufferHandle
The framebuffer owned by this stage, to which this stage renders its results.
Definition render_stage.hpp:482
std::map< std::string, unsigned int > mRenderTargets
A list of named render target textures along with their indices in this stage's framebuffer's target ...
Definition render_stage.hpp:494
nlohmann::json mTemplateFramebufferDescription
A description of this stage's framebuffer, used as a template to create a framebuffer matching reques...
Definition render_stage.hpp:488
void declareRenderTarget(const std::string &name, unsigned int index)
Assigns a name to an attached target texture, intended to be used by the system connecting render sta...
Definition render_stage.cpp:128
void detachTargetTexture(std::size_t targetTextureID)
(in theory) Removes a target texture from this stage's list of target textures.
void setTargetTexture(std::shared_ptr< Texture > texture, std::size_t targetID)
I don't really know what my intention for this was.
std::size_t attachTextureAsTarget(std::shared_ptr< Texture > textureHandle)
Adds a texture to this stage's list of target textures.
Definition render_stage.cpp:118
void setTargetViewport(const SDL_Rect &targetViewport)
Sets the rendering area for this stage, a rectangular sub-region of the target texture.
Definition render_stage.cpp:41
void attachMesh(const std::string &name, std::shared_ptr< StaticMesh > meshHandle)
Attaches a named texture to this rendering stage.
Definition render_stage.cpp:49
void attachMaterial(const std::string &name, std::shared_ptr< Material > materialHandle)
Attaches a named material to this rendering stage.
Definition render_stage.cpp:71
std::map< std::string, std::shared_ptr< Texture > > mTextureAttachments
This stage's named texture attachments.
Definition render_stage.hpp:327
void useViewport()
Should be called once this stage's shader is made active; applies the viewport config associated with...
Definition render_stage.cpp:45
std::shared_ptr< StaticMesh > getMesh(const std::string &name)
Gets a reference to an attached mesh by its name.
Definition render_stage.cpp:56
std::shared_ptr< Material > getMaterial(const std::string &name)
Gets a reference to an attached material by its name.
Definition render_stage.cpp:78
virtual void setup(const glm::u16vec2 &targetDimensions)=0
Sets up the program per its configuration.
void attachTexture(const std::string &name, std::shared_ptr< Texture > textureHandle)
Attaches a named texture to this rendering stage.
Definition render_stage.cpp:60
std::priority_queue< OpaqueRenderUnit > mOpaqueMeshQueue
A queue containing all the opaque meshes to be rendered this frame by this stage.
Definition render_stage.hpp:345
std::shared_ptr< Texture > getTexture(const std::string &name)
Gets a reference to an attached texture by its name.
Definition render_stage.cpp:67
std::map< std::string, std::shared_ptr< Material > > mMaterialAttachments
This stage's named material attachments.
Definition render_stage.hpp:339
virtual ~BaseRenderStage()
Destroys the Base Render Stage object.
Definition render_stage.cpp:35
std::map< std::string, std::shared_ptr< StaticMesh > > mMeshAttachments
This stage's named mesh attachments.
Definition render_stage.hpp:333
virtual void execute()=0
Executes this render stage, presumably after preceding render stages have been executed.
void submitToRenderQueue(OpaqueRenderUnit renderUnit)
Adds an opaque render unit expected by this stage to its associated render queue.
Definition render_stage.cpp:82
GLuint mVertexArrayObject
The OpenGL vertex array object associated with this object.
Definition render_stage.hpp:315
SDL_Rect mTargetViewport
The rectangle defining the sub-region of its target texture to which this render stage renders.
Definition render_stage.hpp:357
std::shared_ptr< ShaderProgram > mShaderHandle
A handle to the compiled and uploaded shader program associated with this render stage.
Definition render_stage.hpp:321
BaseRenderStage(const std::string &shaderFilepath)
Constructs a new BaseRenderStage object.
Definition render_stage.cpp:15
std::priority_queue< LightRenderUnit > mLightQueue
A queue containing all the light units to be rendered this frame by this stage.
Definition render_stage.hpp:351
virtual void validate()=0
Validates this stage by checking for availability of required resources, connections with adjacent re...
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:448
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:467
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:480
static std::string getResourceConstructorName()
Get the resource constructor type string associated with this constructor.
Definition framebuffer.hpp:309
static std::string getResourceTypeName()
Gets the resource type string associated with the Framebuffer resource.
Definition framebuffer.hpp:207
GeometryRenderStage(const std::string &shaderFilepath)
Constructs a new geometry render stage object with templates for its position, normal,...
Definition render_stage.hpp:515
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:273
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:295
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:310
Wrapper class over OpenGL RBOs.
Definition framebuffer.hpp:34
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:735
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:727
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:712
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:682
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:687
virtual void setup(const glm::u16vec2 &targetDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:677
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:417
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:406
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:392
virtual void execute() override
Executes this render stage, presumably after preceding render stages have been executed.
Definition render_stage.cpp:562
virtual void setup(const glm::u16vec2 &textureDimensions) override
Sets up the program per its configuration.
Definition render_stage.cpp:532
virtual void validate() override
Validates this stage by checking for availability of required resources, connections with adjacent re...
Definition render_stage.cpp:552
Classes, constructors for this engine's representation of 3D models.
File containing wrapper over OpenGL Framebuffers and related objects.
A wrapper over regular shader attributes intended to be used as "instance" attributes,...
A file that contains definitions for different types of lights, as components to entities,...
Functions related to rendering materials.
A file containing the ToyMaker::StaticMesh class and related structures.
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
A file containing class definitions for wrappers over OpenGL shader programs.
A struct containing the definition of a color buffer, using which similar color buffers can be create...
Definition texture.hpp:30
A struct, used as a component, describing the emissive properties of the light it represents per the ...
Definition light.hpp:65
Contains the model matrix, mesh, and light emission for a single light object being rendered this fra...
Definition render_stage.hpp:109
bool operator<(const LightRenderUnit &other) const
Compares one light unit with another based on its sort key.
Definition render_stage.hpp:130
void setSortKey()
Sets the sort key for this object based on its mesh.
Definition render_stage.hpp:162
glm::mat4 mModelMatrix
The matrix which places this renderable light into the scene.
Definition render_stage.hpp:150
std::shared_ptr< StaticMesh > mMeshHandle
The mesh representing the lighting volume for this light.
Definition render_stage.hpp:144
LightRenderUnit(std::shared_ptr< StaticMesh > meshHandle, const LightEmissionData &lightEmissionData, const glm::mat4 &modelMatrix)
Constructs a new Light Render Unit object.
Definition render_stage.hpp:117
std::uint32_t mSortKey
The sort key for this light unit, based on its mesh.
Definition render_stage.hpp:138
LightEmissionData mLightAttributes
The emissive properties for this light.
Definition render_stage.hpp:156
An object representing a single opaque mesh-material pair, to be rendered this frame.
Definition render_stage.hpp:42
std::uint32_t mSortKey
The computed sort key for this object.
Definition render_stage.hpp:72
std::shared_ptr< Material > mMaterialHandle
The material handle for this render unit.
Definition render_stage.hpp:84
bool operator<(const OpaqueRenderUnit &other) const
Compares this unit to another based on priority.
Definition render_stage.hpp:64
glm::mat4 mModelMatrix
The model matrix to apply to this unit.
Definition render_stage.hpp:90
void setSortKey()
Method responsible for actually computing this unit's sort key.
Definition render_stage.hpp:96
OpaqueRenderUnit(std::shared_ptr< StaticMesh > meshHandle, std::shared_ptr< Material > materialHandle, glm::mat4 modelMatrix)
Constructs a new Opaque Render Unit object.
Definition render_stage.hpp:51
std::shared_ptr< StaticMesh > mMeshHandle
The mesh handle for this render unit.
Definition render_stage.hpp:78
Header containing definitions of classes and functions related to loading and using Texture resources...
Contains a couple of classes not tied to any part of the engine in particular, but useful to those pa...