TDME2  1.9.200
FrameBuffer.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
9 #include <tdme/engine/Engine.h>
10 #include <tdme/math/Math.h>
11 #include <tdme/utilities/Float.h>
12 
18 using tdme::math::Math;
20 
21 FrameBuffer::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;
31  this->cubeMapTextureId = cubeMapTextureId;
32  this->cubeMapTextureIndex = cubeMapTextureIndex;
33 }
34 
36 }
37 
39 {
41  if (depthBufferTextureId == Engine::getRenderer()->ID_NONE) {
43  } else {
44  ownsDepthBufferTexture = false;
45  }
46  }
47 
48  if ((buffers & FRAMEBUFFER_COLORBUFFER) == FRAMEBUFFER_COLORBUFFER && (Engine::getRenderer()->getRendererType() == Renderer::RENDERERTYPE_VULKAN || cubeMapTextureId == TEXTUREID_NONE)) {
49  if (colorBufferTextureId == Engine::getRenderer()->ID_NONE) {
51  } else {
52  ownsColorBufferTexture = false;
53  }
54  }
55 
56  auto rendererCubeMapTextureIndex = -1;
57  switch(cubeMapTextureIndex) {
58  case CUBEMAPTEXTUREINDEX_NONE: rendererCubeMapTextureIndex = -1; break;
59  case CUBEMAPTEXTUREINDEX_NEGATIVE_X: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_NEGATIVE_X; break;
60  case CUBEMAPTEXTUREINDEX_POSITIVE_X: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_POSITIVE_X; break;
61  case CUBEMAPTEXTUREINDEX_POSITIVE_Y: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_POSITIVE_Y; break;
62  case CUBEMAPTEXTUREINDEX_NEGATIVE_Y: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_NEGATIVE_Y; break;
63  case CUBEMAPTEXTUREINDEX_POSITIVE_Z: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_POSITIVE_Z; break;
64  case CUBEMAPTEXTUREINDEX_NEGATIVE_Z: rendererCubeMapTextureIndex = Engine::getRenderer()->CUBEMAPTEXTUREINDEX_NEGATIVE_Z; break;
65  }
66 
68 }
69 
70 void FrameBuffer::reshape(int32_t width, int32_t height)
71 {
74 
77 
78  this->width = width;
79  this->height = height;
80 }
81 
83 {
86 
89 
91 }
92 
94 {
98 }
99 
101 {
102  Engine::getRenderer()->bindFrameBuffer(Engine::getRenderer()->FRAMEBUFFER_DEFAULT);
105 }
106 
108 {
110 }
111 
113 {
115 }
116 
117 void FrameBuffer::renderToScreen(Engine* engine, int32_t depthBufferTextureId, int32_t colorBufferTextureId)
118 {
119  auto renderer = Engine::getRenderer();
120 
121  // use default context
122  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
123 
124  //
125  renderer->disableCulling(contextIdx);
126 
127  // clear
128  renderer->setClearColor(engine->sceneColor.getRed(), engine->sceneColor.getGreen(), engine->sceneColor.getBlue(), engine->sceneColor.getAlpha());
129  renderer->clear(renderer->CLEAR_COLOR_BUFFER_BIT | renderer->CLEAR_DEPTH_BUFFER_BIT);
130 
131  // use frame buffer render shader
132  auto frameBufferRenderShader = Engine::getFrameBufferRenderShader();
133  frameBufferRenderShader->useProgram();
134 
135  // bind color buffer texture
136  renderer->setTextureUnit(contextIdx, 0);
137  renderer->bindTexture(contextIdx, colorBufferTextureId);
138 
139  // bind depth buffer texture
140  renderer->setTextureUnit(contextIdx, 1);
141  renderer->bindTexture(contextIdx, depthBufferTextureId);
142 
143  //
144  renderer->bindVerticesBufferObject(contextIdx, frameBufferRenderShader->getVBOVertices());
145  renderer->bindTextureCoordinatesBufferObject(contextIdx, frameBufferRenderShader->getVBOTextureCoordinates());
146 
147  // draw
148  renderer->drawTrianglesFromBufferObjects(contextIdx, 2, 0);
149 
150  // unbind buffers
151  renderer->unbindBufferObjects(contextIdx);
152 
153  // unbind color buffer texture
154  renderer->setTextureUnit(contextIdx, 0);
155  renderer->bindTexture(contextIdx, renderer->ID_NONE);
156 
157  // unbind depth buffer texture
158  renderer->setTextureUnit(contextIdx, 1);
159  renderer->bindTexture(contextIdx, renderer->ID_NONE);
160 
161  // switch back to diffuse texture unit
162  renderer->setTextureUnit(contextIdx, 0);
163 
164  //
165  frameBufferRenderShader->unUseProgram();
166 
167  // unset
168  renderer->enableCulling(contextIdx);
169 }
170 
171 void FrameBuffer::doPostProcessing(Engine* engine, FrameBuffer* target, FrameBuffer* source, const string& programId, const string& shaderId, FrameBuffer* temporary, FrameBuffer* blendToSource)
172 {
173  if (target != nullptr) {
174  target->enableFrameBuffer();
175  } else {
177  }
178 
179  //
180  auto renderer = Engine::getRenderer();
181 
182  // if we blend source over blend to source?
183  if (blendToSource != nullptr) {
184  // yup
185  blendToSource->renderToScreen(engine);
186  renderer->enableAdditionBlending();
187  renderer->disableDepthBufferTest();
188  } else {
189  // otherwise just clear target
190  renderer->setClearColor(engine->sceneColor.getRed(), engine->sceneColor.getGreen(), engine->sceneColor.getBlue(), engine->sceneColor.getAlpha());
191  renderer->clear(renderer->CLEAR_COLOR_BUFFER_BIT | renderer->CLEAR_DEPTH_BUFFER_BIT);
192  }
193 
194  // use default context
195  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
196 
197  //
198  renderer->disableCulling(contextIdx);
199 
200  // use frame buffer render shader
201  auto frameBufferRenderShader = Engine::getFrameBufferRenderShader();
202 
203  //
204  int _width = engine->getScaledWidth() != -1?engine->getScaledWidth():engine->getWidth();
205  int _height = engine->getScaledHeight() != -1?engine->getScaledHeight():engine->getHeight();
206 
207  // use post processing shader
208  auto postProcessingShader = Engine::getInstance()->getPostProcessingShader();
209  postProcessingShader->useProgram();
210  postProcessingShader->setShader(contextIdx, shaderId);
211  postProcessingShader->setBufferPixelWidth(contextIdx, 1.0f / static_cast<float>(source->getWidth()));
212  postProcessingShader->setBufferPixelHeight(contextIdx, 1.0f / static_cast<float>(source->getHeight()));
213  postProcessingShader->setShaderParameters(contextIdx, engine);
214 
215  // bind color buffer texture
216  renderer->setTextureUnit(contextIdx, 0);
217  renderer->bindTexture(contextIdx, source->colorBufferTextureId);
218 
219  // bind depth buffer texture
220  renderer->setTextureUnit(contextIdx, 1);
221  renderer->bindTexture(contextIdx, source->depthBufferTextureId);
222 
223  // bind temporary if any given
224  if (temporary != nullptr) {
225  renderer->setTextureUnit(contextIdx, 2);
226  renderer->bindTexture(contextIdx, temporary->colorBufferTextureId);
227 
228  renderer->setTextureUnit(contextIdx, 3);
229  renderer->bindTexture(contextIdx, temporary->depthBufferTextureId);
230  }
231 
232  //
233  renderer->bindVerticesBufferObject(contextIdx, frameBufferRenderShader->getVBOVertices());
234  renderer->bindTextureCoordinatesBufferObject(contextIdx, frameBufferRenderShader->getVBOTextureCoordinates());
235 
236  // unuse frame buffer render shader
237  renderer->drawTrianglesFromBufferObjects(contextIdx, 2, 0);
238 
239  // unbind buffers
240  renderer->unbindBufferObjects(contextIdx);
241 
242  // unbind color buffer texture
243  renderer->setTextureUnit(contextIdx, 0);
244  renderer->bindTexture(contextIdx, renderer->ID_NONE);
245 
246  // unbind depth buffer texture
247  renderer->setTextureUnit(contextIdx, 1);
248  renderer->bindTexture(contextIdx, renderer->ID_NONE);
249 
250  // unbind temporary if any given
251  if (temporary != nullptr) {
252  renderer->setTextureUnit(contextIdx, 2);
253  renderer->bindTexture(contextIdx, renderer->ID_NONE);
254 
255  renderer->setTextureUnit(contextIdx, 3);
256  renderer->bindTexture(contextIdx, renderer->ID_NONE);
257  }
258 
259  // switch back to diffuse texture unit
260  renderer->setTextureUnit(contextIdx, 0);
261 
262  //
263  postProcessingShader->unUseProgram();
264 
265  // unset
266  renderer->enableCulling(contextIdx);
267 
268  // did we blend?
269  if (blendToSource != nullptr) {
270  renderer->disableBlending();
271  renderer->enableDepthBufferTest();
272  }
273 
274  //
275  if (target != nullptr) {
277  }
278 }
279 
float getRed() const
Definition: Color4.h:92
float getGreen() const
Definition: Color4.h:107
float getAlpha() const
Definition: Color4.h:137
float getBlue() const
Definition: Color4.h:122
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
int32_t getWidth()
Definition: Engine.h:1029
static FrameBufferRenderShader * getFrameBufferRenderShader()
Definition: Engine.h:490
static Renderer * getRenderer()
Definition: Engine.h:434
int32_t getHeight()
Definition: Engine.h:1036
static PostProcessingShader * getPostProcessingShader()
Definition: Engine.h:649
static STATIC_DLL_IMPEXT Engine * instance
Definition: Engine.h:204
int32_t getScaledWidth()
Definition: Engine.h:1043
int32_t getScaledHeight()
Definition: Engine.h:1050
Frame buffer class.
Definition: FrameBuffer.h:22
void bindColorBufferTexture(int contextIdx)
Bind color texture.
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_Z
Definition: FrameBuffer.h:36
void reshape(int32_t width, int32_t height)
Resize the frame buffer.
Definition: FrameBuffer.cpp:70
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:38
virtual ~FrameBuffer()
Destructor.
Definition: FrameBuffer.cpp:35
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_X
Definition: FrameBuffer.h:31
static void disableFrameBuffer()
Switches back to non offscreen main frame buffer to be rendered.
void enableFrameBuffer()
Enables this frame buffer to be rendered.
Definition: FrameBuffer.cpp:93
static constexpr int32_t FRAMEBUFFER_COLORBUFFER
Definition: FrameBuffer.h:26
static constexpr int32_t CUBEMAPTEXTUREINDEX_NONE
Definition: FrameBuffer.h:30
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_X
Definition: FrameBuffer.h:32
static constexpr int32_t TEXTUREID_NONE
Definition: FrameBuffer.h:28
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:25
void dispose()
Disposes this frame buffer.
Definition: FrameBuffer.cpp:82
static constexpr int32_t CUBEMAPTEXTUREINDEX_NEGATIVE_Y
Definition: FrameBuffer.h:34
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_Y
Definition: FrameBuffer.h:33
static constexpr int32_t CUBEMAPTEXTUREINDEX_POSITIVE_Z
Definition: FrameBuffer.h:35
void bindDepthBufferTexture(int contextIdx)
Bind depth texture.
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:19
Float class.
Definition: Float.h:27