TDME2 1.9.121
EnvironmentMappingRenderer.cpp
Go to the documentation of this file.
2
3#include <vector>
4
5#include <tdme/tdme.h>
10#include <tdme/engine/Engine.h>
11#include <tdme/engine/Entity.h>
15#include <tdme/engine/Light.h>
22#include <tdme/math/Math.h>
23#include <tdme/math/Matrix4x4.h>
24#include <tdme/math/Vector3.h>
25#include <tdme/utilities/Time.h>
26
27using std::vector;
28
30
49
50GeometryBuffer* EnvironmentMappingRenderer::geometryBuffer = nullptr;
51
52EnvironmentMappingRenderer::EnvironmentMappingRenderer(Engine* engine, int32_t width, int32_t height)
53{
54 this->engine = engine;
55 this->width = width;
56 this->height = height;
59 if (engine->renderer->getRendererType() == Renderer::RENDERERTYPE_VULKAN) {
61 {{ 1.0f, 0.0f, 0.0f }}, // left
62 {{ -1.0f, 0.0f, 0.0f }}, // right
63 {{ 0.0f, -1.0f, 0.0f }}, // top
64 {{ 0.0f, 1.0f, 0.0f }}, // bottom
65 {{ 0.0f, 0.0f, 1.0f }}, // front
66 {{ 0.0f, 0.0f, -1.0f }} // back
67 }};
68 sideVectors = {{
69 {{ 0.0f, 0.0f, -1.0f }}, // left
70 {{ 0.0f, 0.0f, 1.0f }}, // right
71 {{ 1.0f, 0.0f, 0.0f }}, // top
72 {{ 1.0f, 0.0f, 0.0f }}, // bottom
73 {{ 1.0f, 0.0f, 0.0f }}, // front
74 {{ -1.0f, 0.0f, 0.0f }} // back
75 }};
76 } else {
78 {{ -1.0f, 0.0f, 0.0f }}, // left
79 {{ 1.0f, 0.0f, 0.0f }}, // right
80 {{ 0.0f, 1.0f, 0.0f }}, // top
81 {{ 0.0f, -1.0f, 0.0f }}, // bottom
82 {{ 0.0f, 0.0f, 1.0f }}, // front
83 {{ 0.0f, 0.0f, -1.0f }} // back
84 }};
85 sideVectors = {{
86 {{ 0.0f, 0.0f, 1.0f }}, // left
87 {{ 0.0f, 0.0f, -1.0f }}, // right
88 {{ 1.0f, 0.0f, 0.0f }}, // top
89 {{ 1.0f, 0.0f, 0.0f }}, // bottom
90 {{ 1.0f, 0.0f, 0.0f }}, // front
91 {{ -1.0f, 0.0f, 0.0f }} // back
92 }};
93 }
94}
95
97 delete camera;
98}
99
101{
102 //
103 for (auto i = 0; i < frameBuffers.size(); i++) {
105 for (auto j = 0; j < frameBuffers[i].size(); j++) {
107 frameBuffers[i][j]->initialize();
108 }
109 }
110 // deferred shading
111 if (engine->renderer->isDeferredShadingAvailable() == true && geometryBuffer == nullptr) {
114 } else
115 if (geometryBuffer != nullptr) {
116 // TODO: fix me by using a hash map or something to match static geometry buffers with env maps of given dimension
118 Console::println("EnvironmentMappingRenderer::initialize(): " + to_string(geometryBuffer->getId()) + ": geometry buffer width != width, height != height!");
119 }
120 }
121}
122
123void EnvironmentMappingRenderer::reshape(int32_t width, int32_t height)
124{
125}
126
128{
129 for (auto i = 0; i < frameBuffers.size(); i++) {
130 for (auto j = 0; j < frameBuffers[i].size(); j++) {
131 frameBuffers[i][j]->dispose();
132 delete frameBuffers[i][j];
133 }
135 }
136}
137
139{
140 //
141 auto now = Time::getCurrentMillis();
142 if (timeRenderLast != -1LL && now - timeRenderLast < timeRenderUpdateFrequency) return;
143
144 //
145 auto engineCamera = engine->getCamera();
146
147 //
148 for (auto runs = 0; runs < (timeRenderLast == -1LL?2:1); runs++) {
149 //
152
153 //
154 for (auto i = 0; i < frameBuffers[renderCubeMapTextureIdx].size(); i++) {
155 // bind frame buffer
156 frameBuffers[renderCubeMapTextureIdx][i]->enableFrameBuffer();
157
158 // set up camera
159 camera->setZNear(engineCamera->getZNear());
160 camera->setZFar(engineCamera->getZFar());
161 camera->setFovX(90.0f);
162 camera->setLookFrom(position);
165 camera->setUpVector(Vector3::computeCrossProduct(sideVectors[i], forwardVectors[i]));
168
169 // set up clear color
175 );
176
177 // clear previous frame values
179
180 //
182
183 // do a render pass
184 engine->render(
190 string(),
191 true,
192 false,
193 false,
194 true,
195 true,
196 EntityRenderer::RENDERTYPE_NORMALS |
197 EntityRenderer::RENDERTYPE_TEXTUREARRAYS |
198 EntityRenderer::RENDERTYPE_TEXTUREARRAYS_DIFFUSEMASKEDTRANSPARENCY |
199 EntityRenderer::RENDERTYPE_EFFECTCOLORS |
200 EntityRenderer::RENDERTYPE_MATERIALS |
201 EntityRenderer::RENDERTYPE_MATERIALS_DIFFUSEMASKEDTRANSPARENCY |
202 EntityRenderer::RENDERTYPE_TEXTURES |
203 EntityRenderer::RENDERTYPE_TEXTURES_DIFFUSEMASKEDTRANSPARENCY |
204 EntityRenderer::RENDERTYPE_LIGHTS
205 );
206
207 //
209 }
210 }
211
212 // unbind frame buffer
214
215 //
216 timeRenderLast = now;
217}
void setSideVector(const Vector3 &sideVector)
Set side vector.
Definition: Camera.h:206
void update(int contextIdx, int32_t width, int32_t height)
Sets up camera while resizing the view port.
Definition: Camera.cpp:154
void setFovX(float fovX)
Set field of view X.
Definition: Camera.h:131
Frustum * getFrustum()
Definition: Camera.h:271
void setUpVector(const Vector3 &upVector)
Set up vector.
Definition: Camera.h:176
void setCameraMode(CameraMode cameraMode)
Set camera mode.
Definition: Camera.h:86
void setLookFrom(const Vector3 &lookFrom)
Set look from.
Definition: Camera.h:221
void setForwardVector(const Vector3 &forwardVector)
Set forward vector.
Definition: Camera.h:191
void setZNear(float zNear)
Set z near.
Definition: Camera.h:146
void setZFar(float zFar)
Set z far.
Definition: Camera.h:161
Engine main class.
Definition: Engine.h:122
static STATIC_DLL_IMPEXT Renderer * renderer
Definition: Engine.h:184
void render(FrameBuffer *renderFrameBuffer, GeometryBuffer *renderGeometryBuffer, DecomposedEntities &visibleDecomposedEntities, int32_t effectPass, int32_t renderPassMask, const string &shaderPrefix, bool useEZR, bool applyShadowMapping, bool applyPostProcessing, bool doRenderLightSource, bool doRenderParticleSystems, int32_t renderTypes)
Do a render/effect pass.
Definition: Engine.cpp:2143
Camera * getCamera()
Definition: Engine.h:907
void computeTransformations(Camera *camera, DecomposedEntities &decomposedEntites, bool autoEmit, bool computeTransformations)
Computes visibility and transformations.
Definition: Engine.cpp:1142
void resetLists(DecomposedEntities &decomposedEntites)
Reset lists.
Definition: Engine.cpp:970
Entity hierarchy to be used with engine class.
TDME engine entity.
Definition: Entity.h:31
Frame buffer class.
Definition: FrameBuffer.h:21
static void disableFrameBuffer()
Switches back to non offscreen main frame buffer to be rendered.
Definition: FrameBuffer.cpp:85
static constexpr int32_t FRAMEBUFFER_COLORBUFFER
Definition: FrameBuffer.h:25
static constexpr int32_t FRAMEBUFFER_DEPTHBUFFER
Definition: FrameBuffer.h:24
void update()
Setups frustum, should be called if frustum did change.
Definition: Frustum.cpp:30
Geometry buffer class.
void initialize()
Initialize the geometry buffer.
LOD object 3D to be used with engine class.
Definition: LODObject3D.h:47
Light representation.
Definition: Light.h:32
Object 3D render group for static objects that might be animated by shaders.
Object 3D to be used with engine class.
Definition: Object3D.h:60
Object particle system entity to be used with engine class.
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:25
void reshape(int32_t width, int32_t height)
Reshape frame buffers.
void render(const Vector3 &position)
Renders given objects in to environment cube map.
virtual void setClearColor(float red, float green, float blue, float alpha)=0
Set up clear color.
virtual void disposeTexture(int32_t textureId)=0
Dispose a texture.
virtual int32_t createCubeMapTexture(int contextIdx, int32_t width, int32_t height)=0
Create cube map texture from frame buffers.
virtual void clear(int32_t mask)=0
Clear render buffer with given mask.
Standard math functions.
Definition: Math.h:21
4x4 3D Matrix class
Definition: Matrix4x4.h:24
3D vector 3 class
Definition: Vector3.h:22
Time utility class.
Definition: Time.h:21
Partition interface.
Definition: Partition.h:19