TDME2 1.9.121
FrameBuffer.cpp
Go to the documentation of this file.
2
3#include <string>
4
5#include <tdme/tdme.h>
10#include <tdme/math/Math.h>
12
20
21FrameBuffer::FrameBuffer(int32_t width, int32_t height, int32_t buffers, int32_t cubeMapTextureId, int32_t cubeMapTextureIndex)
22{
23 this->width = width;
24 this->height = height;
25 this->buffers = buffers;
26 frameBufferId = -1;
29 this->cubeMapTextureId = cubeMapTextureId;
30 this->cubeMapTextureIndex = cubeMapTextureIndex;
31}
32
34{
37
38 if ((buffers & FRAMEBUFFER_COLORBUFFER) == FRAMEBUFFER_COLORBUFFER && (Engine::renderer->getRendererType() == Renderer::RENDERERTYPE_VULKAN || cubeMapTextureId == CUBEMAPTEXTUREID_NONE))
40
41 auto rendererCubeMapTextureIndex = -1;
42 switch(cubeMapTextureIndex) {
43 case CUBEMAPTEXTUREINDEX_NONE: rendererCubeMapTextureIndex = -1; break;
44 case CUBEMAPTEXTUREINDEX_NEGATIVE_X: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_NEGATIVE_X; break;
45 case CUBEMAPTEXTUREINDEX_POSITIVE_X: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_POSITIVE_X; break;
46 case CUBEMAPTEXTUREINDEX_POSITIVE_Y: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_POSITIVE_Y; break;
47 case CUBEMAPTEXTUREINDEX_NEGATIVE_Y: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_NEGATIVE_Y; break;
48 case CUBEMAPTEXTUREINDEX_POSITIVE_Z: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_POSITIVE_Z; break;
49 case CUBEMAPTEXTUREINDEX_NEGATIVE_Z: rendererCubeMapTextureIndex = Engine::renderer->CUBEMAPTEXTUREINDEX_NEGATIVE_Z; break;
50 }
51
53}
54
55void FrameBuffer::reshape(int32_t width, int32_t height)
56{
59
62
63 this->width = width;
64 this->height = height;
65}
66
68{
71
74
76}
77
79{
83}
84
86{
90}
91
93{
95}
96
98{
100}
101
102void FrameBuffer::renderToScreen(Engine* engine, int32_t depthBufferTextureId, int32_t colorBufferTextureId)
103{
104 auto renderer = Engine::renderer;
105
106 // use default context
107 auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
108
109 //
110 renderer->disableCulling(contextIdx);
111
112 // clear
113 renderer->setClearColor(engine->sceneColor.getRed(), engine->sceneColor.getGreen(), engine->sceneColor.getBlue(), engine->sceneColor.getAlpha());
114 renderer->clear(renderer->CLEAR_COLOR_BUFFER_BIT | renderer->CLEAR_DEPTH_BUFFER_BIT);
115
116 // use frame buffer render shader
117 auto frameBufferRenderShader = Engine::getFrameBufferRenderShader();
118 frameBufferRenderShader->useProgram();
119
120 // bind color buffer texture
121 renderer->setTextureUnit(contextIdx, 0);
122 renderer->bindTexture(contextIdx, colorBufferTextureId);
123
124 // bind depth buffer texture
125 renderer->setTextureUnit(contextIdx, 1);
126 renderer->bindTexture(contextIdx, depthBufferTextureId);
127
128 //
129 renderer->bindVerticesBufferObject(contextIdx, frameBufferRenderShader->getVBOVertices());
130 renderer->bindTextureCoordinatesBufferObject(contextIdx, frameBufferRenderShader->getVBOTextureCoordinates());
131
132 // draw
133 renderer->drawTrianglesFromBufferObjects(contextIdx, 2, 0);
134
135 // unbind buffers
136 renderer->unbindBufferObjects(contextIdx);
137
138 // unbind color buffer texture
139 renderer->setTextureUnit(contextIdx, 0);
140 renderer->bindTexture(contextIdx, renderer->ID_NONE);
141
142 // unbind depth buffer texture
143 renderer->setTextureUnit(contextIdx, 1);
144 renderer->bindTexture(contextIdx, renderer->ID_NONE);
145
146 // switch back to diffuse texture unit
147 renderer->setTextureUnit(contextIdx, 0);
148
149 //
150 frameBufferRenderShader->unUseProgram();
151
152 // unset
153 renderer->enableCulling(contextIdx);
154}
155
156void FrameBuffer::doPostProcessing(Engine* engine, FrameBuffer* target, FrameBuffer* source, const string& programId, const string& shaderId, FrameBuffer* temporary, FrameBuffer* blendToSource)
157{
158 if (target != nullptr) {
159 target->enableFrameBuffer();
160 } else {
162 }
163
164 //
165 auto renderer = Engine::renderer;
166
167 // if we blend source over blend to source?
168 if (blendToSource != nullptr) {
169 // yup
170 blendToSource->renderToScreen(engine);
171 renderer->enableAdditionBlending();
172 renderer->disableDepthBufferTest();
173 } else {
174 // otherwise just clear target
175 renderer->setClearColor(engine->sceneColor.getRed(), engine->sceneColor.getGreen(), engine->sceneColor.getBlue(), engine->sceneColor.getAlpha());
176 renderer->clear(renderer->CLEAR_COLOR_BUFFER_BIT | renderer->CLEAR_DEPTH_BUFFER_BIT);
177 }
178
179 // use default context
180 auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
181
182 //
183 renderer->disableCulling(contextIdx);
184
185 // use frame buffer render shader
186 auto frameBufferRenderShader = Engine::getFrameBufferRenderShader();
187
188 //
189 int _width = engine->getScaledWidth() != -1?engine->getScaledWidth():engine->getWidth();
190 int _height = engine->getScaledHeight() != -1?engine->getScaledHeight():engine->getHeight();
191
192 // use post processing shader
193 auto postProcessingShader = Engine::getInstance()->getPostProcessingShader();
194 postProcessingShader->useProgram();
195 postProcessingShader->setShader(contextIdx, shaderId);
196 postProcessingShader->setBufferPixelWidth(contextIdx, 1.0f / static_cast<float>(source->getWidth()));
197 postProcessingShader->setBufferPixelHeight(contextIdx, 1.0f / static_cast<float>(source->getHeight()));
198 postProcessingShader->setShaderParameters(contextIdx, engine);
199
200 // bind color buffer texture
201 renderer->setTextureUnit(contextIdx, 0);
202 renderer->bindTexture(contextIdx, source->colorBufferTextureId);
203
204 // bind depth buffer texture
205 renderer->setTextureUnit(contextIdx, 1);
206 renderer->bindTexture(contextIdx, source->depthBufferTextureId);
207
208 // bind temporary if any given
209 if (temporary != nullptr) {
210 renderer->setTextureUnit(contextIdx, 2);
211 renderer->bindTexture(contextIdx, temporary->colorBufferTextureId);
212
213 renderer->setTextureUnit(contextIdx, 3);
214 renderer->bindTexture(contextIdx, temporary->depthBufferTextureId);
215 }
216
217 //
218 renderer->bindVerticesBufferObject(contextIdx, frameBufferRenderShader->getVBOVertices());
219 renderer->bindTextureCoordinatesBufferObject(contextIdx, frameBufferRenderShader->getVBOTextureCoordinates());
220
221 // unuse frame buffer render shader
222 renderer->drawTrianglesFromBufferObjects(contextIdx, 2, 0);
223
224 // unbind buffers
225 renderer->unbindBufferObjects(contextIdx);
226
227 // unbind color buffer texture
228 renderer->setTextureUnit(contextIdx, 0);
229 renderer->bindTexture(contextIdx, renderer->ID_NONE);
230
231 // unbind depth buffer texture
232 renderer->setTextureUnit(contextIdx, 1);
233 renderer->bindTexture(contextIdx, renderer->ID_NONE);
234
235 // unbind temporary if any given
236 if (temporary != nullptr) {
237 renderer->setTextureUnit(contextIdx, 2);
238 renderer->bindTexture(contextIdx, renderer->ID_NONE);
239
240 renderer->setTextureUnit(contextIdx, 3);
241 renderer->bindTexture(contextIdx, renderer->ID_NONE);
242 }
243
244 // switch back to diffuse texture unit
245 renderer->setTextureUnit(contextIdx, 0);
246
247 //
248 postProcessingShader->unUseProgram();
249
250 // unset
251 renderer->enableCulling(contextIdx);
252
253 // did we blend?
254 if (blendToSource != nullptr) {
255 renderer->disableBlending();
256 renderer->enableDepthBufferTest();
257 }
258
259 //
260 if (target != nullptr) {
262 }
263}
264
Engine main class.
Definition: Engine.h:122
static FrameBufferRenderShader * getFrameBufferRenderShader()
Definition: Engine.h:449
static STATIC_DLL_IMPEXT Renderer * renderer
Definition: Engine.h:184
int32_t getWidth()
Definition: Engine.h:865
int32_t getHeight()
Definition: Engine.h:872
static STATIC_DLL_IMPEXT Engine * instance
Definition: Engine.h:183
static PostProcessingShader * getPostProcessingShader()
Definition: Engine.h:463
int32_t getScaledWidth()
Definition: Engine.h:879
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:554
int32_t getScaledHeight()
Definition: Engine.h:886
Frame buffer class.
Definition: FrameBuffer.h:21
void bindColorBufferTexture(int contextIdx)
Bind color texture.
Definition: FrameBuffer.cpp:97
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_Z
Definition: FrameBuffer.h:35
void reshape(int32_t width, int32_t height)
Resize the frame buffer.
Definition: FrameBuffer.cpp:55
void renderToScreen(Engine *engine, int32_t depthBufferTextureId, int32_t colorBufferTextureId)
Render given depth texture and color buffer texture to screen @parma engine engine.
void initialize()
Initialize the frame buffer.
Definition: FrameBuffer.cpp:33
static constexpr int32_t CUBEMAPTEXTUREID_NONE
Definition: FrameBuffer.h:27
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_X
Definition: FrameBuffer.h:30
static void disableFrameBuffer()
Switches back to non offscreen main frame buffer to be rendered.
Definition: FrameBuffer.cpp:85
void enableFrameBuffer()
Enables this frame buffer to be rendered.
Definition: FrameBuffer.cpp:78
static constexpr int32_t FRAMEBUFFER_COLORBUFFER
Definition: FrameBuffer.h:25
static constexpr int32_t CUBEMAPTEXTUREINDEX_NONE
Definition: FrameBuffer.h:29
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_X
Definition: FrameBuffer.h:31
static void doPostProcessing(Engine *engine, FrameBuffer *target, FrameBuffer *source, const string &programId, const string &shaderId, FrameBuffer *temporary=nullptr, FrameBuffer *blendToSource=nullptr)
Do post processing into target frame buffer (which can be screen as well when passing nullptr)
static constexpr int32_t FRAMEBUFFER_DEPTHBUFFER
Definition: FrameBuffer.h:24
void dispose()
Disposes this frame buffer.
Definition: FrameBuffer.cpp:67
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_Y
Definition: FrameBuffer.h:33
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_Y
Definition: FrameBuffer.h:32
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_Z
Definition: FrameBuffer.h:34
void bindDepthBufferTexture(int contextIdx)
Bind depth texture.
Definition: FrameBuffer.cpp:92
virtual void resizeDepthBufferTexture(int32_t textureId, int32_t width, int32_t height)=0
Resizes a depth texture.
virtual void setViewPort(int32_t width, int32_t height)=0
Set up viewport parameter.
virtual void resizeColorBufferTexture(int32_t textureId, int32_t width, int32_t height)=0
Resize color buffer texture.
virtual int32_t createColorBufferTexture(int32_t width, int32_t height, int32_t cubeMapTextureId, int32_t cubeMapTextureIndex)=0
Creates a color buffer texture.
virtual void updateViewPort()=0
Update viewport.
virtual int32_t createFramebufferObject(int32_t depthBufferTextureId, int32_t colorBufferTextureId, int32_t cubeMapTextureId=0, int32_t cubeMapTextureIndex=0)=0
Creates a frame buffer object with depth texture attached.
virtual void bindTexture(int contextIdx, int32_t textureId)=0
Binds a texture with given id or unbinds when using ID_NONE.
virtual int32_t createDepthBufferTexture(int32_t width, int32_t height, int32_t cubeMapTextureId, int32_t cubeMapTextureIndex)=0
Creates a depth buffer texture.
virtual void disposeTexture(int32_t textureId)=0
Dispose a texture.
virtual void bindFrameBuffer(int32_t frameBufferId)=0
Enables a framebuffer to be rendered.
virtual void disposeFrameBufferObject(int32_t frameBufferId)=0
Disposes a frame buffer object.
Standard math functions.
Definition: Math.h:21
Float class.
Definition: Float.h:23