2
2
* ***** BEGIN GPL LICENSE BLOCK *****
4
4
* This program is free software; you can redistribute it and/or
5
5
* modify it under the terms of the GNU General Public License
6
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version. The Blender
8
* Foundation also sells licenses for use in proprietary software under
9
* the Blender License. See http://www.blender.org/BL/ for information
7
* of the License, or (at your option) any later version.
12
9
* This program is distributed in the hope that it will be useful,
13
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
15
* along with this program; if not, write to the Free Software Foundation,
19
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22
* All rights reserved.
24
* The Original Code is: all of this file.
26
18
* Contributor(s): none yet.
28
20
* ***** END GPL LICENSE BLOCK *****
23
/** \file gameengine/Rasterizer/RAS_2DFilterManager.cpp
31
28
#define STRINGIFY(A) #A
61
58
isshadersupported = GLEW_ARB_shader_objects &&
62
59
GLEW_ARB_fragment_shader && GLEW_ARB_multitexture;
64
/* used to return before 2.49 but need to initialize values so dont */
65
if(!isshadersupported)
61
/* used to return before 2.49 but need to initialize values so don't */
62
if (!isshadersupported)
66
63
std::cout<<"shaders not supported!" << std::endl;
69
for(passindex =0; passindex<MAX_RENDER_PASS; passindex++)
66
for (passindex =0; passindex<MAX_RENDER_PASS; passindex++)
71
68
m_filters[passindex] = 0;
72
69
m_enabled[passindex] = 0;
185
183
void RAS_2DFilterManager::AnalyseShader(int passindex, vector<STR_String>& propNames)
187
185
texflag[passindex] = 0;
188
if(glGetUniformLocationARB(m_filters[passindex], "bgl_DepthTexture") != -1)
186
if (glGetUniformLocationARB(m_filters[passindex], "bgl_DepthTexture") != -1)
190
if(GLEW_ARB_depth_texture)
188
if (GLEW_ARB_depth_texture)
191
189
texflag[passindex] |= 0x1;
193
if(glGetUniformLocationARB(m_filters[passindex], "bgl_LuminanceTexture") != -1)
191
if (glGetUniformLocationARB(m_filters[passindex], "bgl_LuminanceTexture") != -1)
195
193
texflag[passindex] |= 0x2;
198
if(m_gameObjects[passindex])
196
if (m_gameObjects[passindex])
200
198
int objProperties = propNames.size();
202
for(i=0; i<objProperties; i++)
203
if(glGetUniformLocationARB(m_filters[passindex], propNames[i]) != -1)
200
for (i=0; i<objProperties; i++)
201
if (glGetUniformLocationARB(m_filters[passindex], propNames[i]) != -1)
204
202
m_properties[passindex].push_back(propNames[i]);
221
219
/* send depth texture to glsl program if it needs */
222
if(texflag[passindex] & 0x1){
220
if (texflag[passindex] & 0x1) {
223
221
uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_DepthTexture");
224
222
glActiveTextureARB(GL_TEXTURE1);
225
223
glBindTexture(GL_TEXTURE_2D, texname[1]);
233
231
/* send luminance texture to glsl program if it needs */
234
if(texflag[passindex] & 0x2){
232
if (texflag[passindex] & 0x2) {
235
233
uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_LuminanceTexture");
236
234
glActiveTextureARB(GL_TEXTURE2);
237
235
glBindTexture(GL_TEXTURE_2D, texname[2]);
261
259
int i, objProperties = m_properties[passindex].size();
262
for(i=0; i<objProperties; i++)
260
for (i=0; i<objProperties; i++)
264
262
uniformLoc = glGetUniformLocationARB(m_filters[passindex], m_properties[passindex][i]);
263
if (uniformLoc != -1)
267
265
float value = ((CValue*)m_gameObjects[passindex])->GetPropertyNumber(m_properties[passindex][i], 0.0);
268
266
glUniform1fARB(uniformLoc,value);
278
276
void RAS_2DFilterManager::FreeTextures()
280
if(texname[0]!=(unsigned int)-1)
278
if (texname[0]!=(unsigned int)-1)
281
279
glDeleteTextures(1, (GLuint*)&texname[0]);
282
if(texname[1]!=(unsigned int)-1)
280
if (texname[1]!=(unsigned int)-1)
283
281
glDeleteTextures(1, (GLuint*)&texname[1]);
284
if(texname[2]!=(unsigned int)-1)
282
if (texname[2]!=(unsigned int)-1)
285
283
glDeleteTextures(1, (GLuint*)&texname[2]);
298
296
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
299
297
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
302
300
glGenTextures(1, (GLuint*)&texname[1]);
303
301
glBindTexture(GL_TEXTURE_2D, texname[1]);
304
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, texturewidth,textureheight,
305
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE,NULL);
302
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, texturewidth,textureheight,
303
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE,NULL);
306
304
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
308
306
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
311
309
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
315
313
glGenTextures(1, (GLuint*)&texname[2]);
316
314
glBindTexture(GL_TEXTURE_2D, texname[2]);
317
315
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, texturewidth, textureheight,
359
357
void RAS_2DFilterManager::UpdateCanvasTextureCoord(unsigned int * viewport)
362
This function update canvascoord[].
363
These parameters are used to create texcoord[1]
364
That way we can access the texcoord relative to the canvas:
365
(0.0,0.0) bottom left, (1.0,1.0) top right, (0.5,0.5) center
360
* This function update canvascoord[].
361
* These parameters are used to create texcoord[1]
362
* That way we can access the texcoord relative to the canvas:
363
* (0.0,0.0) bottom left, (1.0,1.0) top right, (0.5,0.5) center
367
365
canvascoord[0] = (GLfloat) viewport[0] / viewport[2];
368
366
canvascoord[0] *= -1;
369
367
canvascoord[1] = (GLfloat) (texturewidth - viewport[0]) / viewport[2];
384
if(!isshadersupported)
382
if (!isshadersupported)
387
for(passindex =0; passindex<MAX_RENDER_PASS; passindex++)
385
for (passindex =0; passindex<MAX_RENDER_PASS; passindex++)
389
if(m_filters[passindex] && m_enabled[passindex]){
387
if (m_filters[passindex] && m_enabled[passindex]) {
391
if(texflag[passindex] & 0x1)
389
if (texflag[passindex] & 0x1)
392
390
need_depth = true;
393
if(texflag[passindex] & 0x2)
391
if (texflag[passindex] & 0x2)
394
392
need_luminance = true;
395
if(need_depth && need_luminance)
393
if (need_depth && need_luminance)
398
if (num_filters <= 0)
403
401
GLuint viewport[4]={0};
404
402
glGetIntegerv(GL_VIEWPORT,(GLint *)viewport);
406
if(canvaswidth != canvas->GetWidth() || canvasheight != canvas->GetHeight())
404
if (canvaswidth != canvas->GetWidth() || canvasheight != canvas->GetHeight())
408
406
UpdateOffsetMatrix(canvas);
409
407
UpdateCanvasTextureCoord((unsigned int*)viewport);
410
408
need_tex_update = true;
415
413
SetupTextures(need_depth, need_luminance);
416
414
need_tex_update = false;
420
418
glActiveTextureARB(GL_TEXTURE1);
421
419
glBindTexture(GL_TEXTURE_2D, texname[1]);
422
420
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT, 0, 0, texturewidth,textureheight, 0);
423
if (need_luminance) {
426
424
glActiveTextureARB(GL_TEXTURE2);
427
425
glBindTexture(GL_TEXTURE_2D, texname[2]);
428
426
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE16, 0, 0, texturewidth,textureheight, 0);
429
// reverting to texunit 0, without this we get bug [#28462]
430
glActiveTextureARB(GL_TEXTURE0);
431
432
glViewport(0,0, texturewidth, textureheight);
433
434
glDisable(GL_DEPTH_TEST);
435
// in case the previous material was wire
436
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
437
// if the last rendered face had alpha add it would messes with the color of the plane we apply 2DFilter to
434
439
glPushMatrix(); //GL_MODELVIEW
435
440
glLoadIdentity(); // GL_MODELVIEW
436
441
glMatrixMode(GL_TEXTURE);
440
445
glLoadIdentity();
442
for(passindex =0; passindex<MAX_RENDER_PASS; passindex++)
447
for (passindex =0; passindex<MAX_RENDER_PASS; passindex++)
444
if(m_filters[passindex] && m_enabled[passindex])
449
if (m_filters[passindex] && m_enabled[passindex])
446
451
StartShaderProgram(passindex);
471
476
void RAS_2DFilterManager::EnableFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFILTER_MODE mode, int pass, STR_String& text)
473
if(!isshadersupported)
478
if (!isshadersupported)
475
if(pass<0 || pass>=MAX_RENDER_PASS)
480
if (pass<0 || pass>=MAX_RENDER_PASS)
477
482
need_tex_update = true;
478
if(mode == RAS_2DFILTER_DISABLED)
483
if (mode == RAS_2DFILTER_DISABLED)
480
485
m_enabled[pass] = 0;
484
if(mode == RAS_2DFILTER_ENABLED)
489
if (mode == RAS_2DFILTER_ENABLED)
486
491
m_enabled[pass] = 1;
490
if(mode == RAS_2DFILTER_NOFILTER)
495
if (mode == RAS_2DFILTER_NOFILTER)
493
498
glDeleteObjectARB(m_filters[pass]);
494
499
m_enabled[pass] = 0;
495
500
m_filters[pass] = 0;
513
if(mode>=RAS_2DFILTER_MOTIONBLUR && mode<=RAS_2DFILTER_INVERT)
516
glDeleteObjectARB(m_filters[pass]);
517
m_filters[pass] = CreateShaderProgram(mode);
518
// We've checked all other cases, which means we must be dealing with a builtin filter
520
glDeleteObjectARB(m_filters[pass]);
521
m_filters[pass] = CreateShaderProgram(mode);
522
m_gameObjects[pass] = NULL;
523
AnalyseShader(pass, propNames);