1 | //========================================
|
---|
2 | // Class to interface the VisualCAD specifications.
|
---|
3 | // Nicolau Sunyer & Sergi Funtané (10/13/2005).
|
---|
4 | // Universitat de Girona.
|
---|
5 | //========================================
|
---|
6 |
|
---|
7 | #pragma warning(disable:4244) |
---|
8 | // Includes
|
---|
9 | #define GLEW_STATIC 1
|
---|
10 | #include "glew.h"
|
---|
11 | #include <glut.h>
|
---|
12 | //#include <iostream>
|
---|
13 | #include "stdlib.h"
|
---|
14 | #include "time.h"
|
---|
15 |
|
---|
16 | // Includes
|
---|
17 | #include "CMesh.h"
|
---|
18 | #include "vcObscuranceMap.h"
|
---|
19 |
|
---|
20 | #define ROUND(x) (floor(x+0.5))
|
---|
21 |
|
---|
22 | #ifndef M_PI
|
---|
23 | #define M_PI 3.14159265358979323846
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | // DepthPeeling Layers.
|
---|
27 | #define MAX_LAYERS 12
|
---|
28 |
|
---|
29 | // To use Offsets in the PBOs.
|
---|
30 | #define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
---|
31 |
|
---|
32 | //GLSL
|
---|
33 | // Handlers
|
---|
34 | GLuint vProjection,fProjection,pProjection;
|
---|
35 | GLuint vReflect, fReflect, pReflect;
|
---|
36 | GLuint vCopy, fCopy, pCopy;
|
---|
37 | GLuint vTransfer, fTransfer, pTransfer;
|
---|
38 | GLuint vNormalize, fNormalize, pNormalize;
|
---|
39 |
|
---|
40 | //Uniform Parameters
|
---|
41 | //Projection Step
|
---|
42 | GLint TexturaDepthProjection;
|
---|
43 | GLint SizeProjection;
|
---|
44 | GLint FirstProjection;
|
---|
45 |
|
---|
46 | //Reflectivity texture generation Step.
|
---|
47 |
|
---|
48 | //Texture Copy Step.
|
---|
49 | GLint TextureCopy;
|
---|
50 | GLint Texture2Copy;
|
---|
51 |
|
---|
52 | //Energy Transfer Step.
|
---|
53 | GLint TexturaReflectTransfer;
|
---|
54 | GLint DirectionTransfer;
|
---|
55 | GLint DmaxTransfer;
|
---|
56 |
|
---|
57 | //Lightmap Normalization Step.
|
---|
58 | GLint TexturaNormalize;
|
---|
59 | GLint ResolutionNormalize;
|
---|
60 |
|
---|
61 | // Global variables
|
---|
62 | CVert Vertex, Direccio, Increment, Primer, Ultim, Origin;
|
---|
63 |
|
---|
64 | static GLuint texName[3];
|
---|
65 |
|
---|
66 | static int ResX; //Resolution
|
---|
67 | static int ResY;
|
---|
68 | static int Res2X; //Resolution
|
---|
69 | static int Res2Y; //Resolution
|
---|
70 |
|
---|
71 | //RenderTexture *ob=NULL; // RenderTexture for the Obscurances.
|
---|
72 |
|
---|
73 | static GLuint texProjeccions[2]; //Texture name
|
---|
74 | static GLuint texTransfer[1];
|
---|
75 | static GLuint texReflect[1];
|
---|
76 | static GLuint texCopy[1];
|
---|
77 | static GLuint texNormalize[1];
|
---|
78 | static GLuint fb1; // Framebuffer Reflectivity
|
---|
79 | static GLuint fb2; // Framebuffer Projeccions
|
---|
80 | static GLuint fb3; // Framebuffer Transfer
|
---|
81 | static GLuint fb4; // Framebuffer Copy
|
---|
82 | static GLuint fb5; // Lightmap Normalization
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 | GLenum glerror;
|
---|
87 |
|
---|
88 | GLfloat *m_pTextureImage;// Texture of diferent colors.
|
---|
89 | GLfloat *m_pTextureReflect; //Texture of reflectivitat.
|
---|
90 | GLfloat *m_pBlau;
|
---|
91 | GLfloat *m_pNegre;
|
---|
92 | int obs_global_cancelar;
|
---|
93 |
|
---|
94 | CMesh g_pMesh; // Mesh Data
|
---|
95 | static CSphere Esfera;
|
---|
96 |
|
---|
97 | static GLuint ProjectionsPBO[MAX_LAYERS+2]; // PBOs to store the projections.
|
---|
98 | static GLuint ObscurancesPBO; // PBOs to store the projections.
|
---|
99 | static GLuint ztex; //Texture with the depth.
|
---|
100 |
|
---|
101 |
|
---|
102 | // Function to use when the window is resized
|
---|
103 | void changeSize(int w, int h)
|
---|
104 | {
|
---|
105 |
|
---|
106 | if(h == 0)
|
---|
107 | h = 1;
|
---|
108 |
|
---|
109 | float ratio = 1.0 * (float)w / (float)h;
|
---|
110 |
|
---|
111 | //Camera Set up
|
---|
112 | glMatrixMode(GL_PROJECTION);
|
---|
113 | glLoadIdentity();
|
---|
114 | gluOrtho2D(-w,w,-h,h);
|
---|
115 |
|
---|
116 | glMatrixMode(GL_MODELVIEW);
|
---|
117 | glLoadIdentity();
|
---|
118 |
|
---|
119 | glViewport(0, 0, w, h);
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | float vdc(int i, int base)
|
---|
124 | {
|
---|
125 | float prec = 1.0 / (float)base;
|
---|
126 | float f = 0.0;
|
---|
127 | while(i!=0)
|
---|
128 | {
|
---|
129 | f += (float)(prec * (i % base));
|
---|
130 | i /= base;
|
---|
131 | prec /= base;
|
---|
132 | }
|
---|
133 | return f;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Returns a random point in the surface of the sphere
|
---|
137 | CVert ranpoint(float r, CVert c, int &count)
|
---|
138 | {
|
---|
139 | float ale1, ale2;
|
---|
140 | CVert p;
|
---|
141 | float alfa,beta;
|
---|
142 |
|
---|
143 | count++;
|
---|
144 | ale1 = vdc(count, 2);
|
---|
145 | ale2 = vdc(count, 3);
|
---|
146 | // ale1= (float)rand()/(float)RAND_MAX;
|
---|
147 | // ale2= (float)rand()/(float)RAND_MAX;
|
---|
148 |
|
---|
149 | alfa=1.0-2.0*ale1; /* real between -1 and 1 */
|
---|
150 | alfa=acos(alfa); /* angle between 0 and PI */
|
---|
151 |
|
---|
152 | beta=2*M_PI*ale2; /* angle between 0 and 2*PI */
|
---|
153 |
|
---|
154 | p.x=c.x+r*sin(alfa)*cos(beta);
|
---|
155 | p.y=c.y+r*sin(alfa)*sin(beta);
|
---|
156 | p.z=c.z+r*cos(alfa);
|
---|
157 |
|
---|
158 | return p;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Function to find a point in the sphere in the direction that goes from the point in the surface of the sphere
|
---|
162 | // to the center of the sphere. First it gets the random point in the surface.
|
---|
163 | void BuscarDireccio(int &count)
|
---|
164 | {
|
---|
165 | Esfera.center.x = 0.0;
|
---|
166 | Esfera.center.y = 0.0;
|
---|
167 | Esfera.center.z = 0.0;
|
---|
168 | Esfera.radius = sqrt(12.0)/2.0;
|
---|
169 |
|
---|
170 | Primer = ranpoint(Esfera.radius, Esfera.center, count);
|
---|
171 | Vertex.x = Primer.x + (Esfera.center.x-Primer.x) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
172 | Vertex.y = Primer.y + (Esfera.center.y-Primer.y) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
173 | Vertex.z = Primer.z + (Esfera.center.z-Primer.z) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Function that renders the depth peeling geometry planes and stores them in PBOs.
|
---|
177 | void RenderProjeccions(int buffer, int first)
|
---|
178 | {
|
---|
179 | int i, j, e;
|
---|
180 |
|
---|
181 | glUseProgram(pProjection);
|
---|
182 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
183 |
|
---|
184 | changeSize(ResX, ResY);
|
---|
185 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
186 | glDepthFunc(GL_LESS);
|
---|
187 |
|
---|
188 | glDisable(GL_CULL_FACE);
|
---|
189 |
|
---|
190 | glMatrixMode(GL_PROJECTION);
|
---|
191 | glLoadIdentity();
|
---|
192 | glOrtho(-Esfera.radius,Esfera.radius,-Esfera.radius,Esfera.radius,0,Esfera.radius * 2.0);
|
---|
193 |
|
---|
194 | glMatrixMode(GL_MODELVIEW);
|
---|
195 | glLoadIdentity();
|
---|
196 | gluLookAt(Primer.x,Primer.y,Primer.z,
|
---|
197 | Esfera.center.x,Esfera.center.y,Esfera.center.z,
|
---|
198 | 0.0f,1.0f,0.0f);
|
---|
199 |
|
---|
200 | glViewport(0,0,ResX,ResY);
|
---|
201 |
|
---|
202 | glUniform1f(SizeProjection, (float)ResX);
|
---|
203 | glUniform1f(FirstProjection, (float)first);
|
---|
204 |
|
---|
205 | int other = ((buffer == 1)? 0:1);
|
---|
206 | if(first == 0)
|
---|
207 | {
|
---|
208 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[other] ); // Bind The Buffer
|
---|
209 | glBindTexture(GL_TEXTURE_2D,texName[0]);
|
---|
210 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ResX, ResY);
|
---|
211 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
212 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
213 |
|
---|
214 | glActiveTexture(GL_TEXTURE0);
|
---|
215 | glBindTexture(GL_TEXTURE_2D, texName[0]);
|
---|
216 | glUniform1i(TexturaDepthProjection, 0);
|
---|
217 | }
|
---|
218 |
|
---|
219 | // Enable Pointers
|
---|
220 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
221 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
222 | glEnableClientState( GL_NORMAL_ARRAY ); // Enable Texture Coord Arrays
|
---|
223 |
|
---|
224 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
225 | {
|
---|
226 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
227 | {
|
---|
228 | // Set Pointers To Our Data
|
---|
229 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
230 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
231 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
232 | glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
233 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals );
|
---|
234 | glNormalPointer( GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
235 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
236 |
|
---|
237 | // Render
|
---|
238 | glDrawElements( GL_TRIANGLES, g_pMesh.m_pObject._geo[i]._triSet[j]._numTris*3,GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once
|
---|
239 |
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
244 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
245 | glDisableClientState( GL_NORMAL_ARRAY ); // Disable Texture Coord Arrays
|
---|
246 |
|
---|
247 | //Copy the color buffer to PBO.
|
---|
248 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[buffer] ); // Bind The Buffer
|
---|
249 | glReadPixels(0, 0, ResX, ResY, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
250 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
251 |
|
---|
252 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
253 | glUseProgram(0);
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Function that calculates the lightmap using the neighbouring projections.
|
---|
257 | void RenderTransfer(int buffer, int first)
|
---|
258 | {
|
---|
259 | glUseProgram(pTransfer);
|
---|
260 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
261 |
|
---|
262 | int other = ((buffer == 1)? 0:1);
|
---|
263 |
|
---|
264 | glBlendFunc(GL_ONE, GL_ONE);
|
---|
265 |
|
---|
266 | glEnable(GL_BLEND);
|
---|
267 |
|
---|
268 | glMatrixMode(GL_PROJECTION);
|
---|
269 | glLoadIdentity();
|
---|
270 | gluOrtho2D(-1,1,-1,1);
|
---|
271 |
|
---|
272 | glMatrixMode(GL_MODELVIEW);
|
---|
273 | glLoadIdentity();
|
---|
274 | glPointSize(1.0);
|
---|
275 |
|
---|
276 | glViewport(0, 0, Res2X, Res2Y);
|
---|
277 |
|
---|
278 | glUniform1f(DmaxTransfer, (GLfloat)0.3);
|
---|
279 | glActiveTexture(GL_TEXTURE0);
|
---|
280 | glBindTexture(GL_TEXTURE_2D, texReflect[0]);
|
---|
281 | glUniform1i(TexturaReflectTransfer, 0);
|
---|
282 |
|
---|
283 | if(first == 1)
|
---|
284 | {
|
---|
285 | //First Render
|
---|
286 | glUniform1f(DirectionTransfer, (float)buffer);
|
---|
287 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
288 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
289 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
290 |
|
---|
291 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
292 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
293 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
294 |
|
---|
295 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | glUniform1f(DirectionTransfer, 0.0);
|
---|
300 |
|
---|
301 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
302 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
303 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
304 |
|
---|
305 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
306 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
307 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
308 |
|
---|
309 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
310 |
|
---|
311 | glUniform1f(DirectionTransfer, 1.0);
|
---|
312 |
|
---|
313 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
314 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
315 |
|
---|
316 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
317 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
318 |
|
---|
319 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
320 | }
|
---|
321 |
|
---|
322 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
323 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
324 |
|
---|
325 | glDisable(GL_BLEND);
|
---|
326 | glUseProgram(0);
|
---|
327 |
|
---|
328 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
329 |
|
---|
330 | }
|
---|
331 |
|
---|
332 | // Function that copies the 16-bit fp RGBA buffer of the lightmap to a 32-bit RGBA fb buffer.
|
---|
333 | void CopyTexture(void)
|
---|
334 | {
|
---|
335 | glUseProgram(pCopy);
|
---|
336 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
337 | void *texturePBO;
|
---|
338 |
|
---|
339 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
340 | texturePBO = glMapBuffer(GL_PIXEL_PACK_BUFFER_EXT, GL_READ_ONLY);
|
---|
341 | glBindTexture(GL_TEXTURE_2D,texName[1]);
|
---|
342 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, texturePBO);
|
---|
343 | glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
|
---|
344 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
345 |
|
---|
346 | glEnable(GL_CULL_FACE);
|
---|
347 |
|
---|
348 | glMatrixMode(GL_PROJECTION);
|
---|
349 | glLoadIdentity();
|
---|
350 | gluOrtho2D(-Res2X,Res2Y,-Res2X,Res2Y);
|
---|
351 |
|
---|
352 | glMatrixMode(GL_MODELVIEW);
|
---|
353 | glLoadIdentity();
|
---|
354 |
|
---|
355 | glViewport(0, 0, Res2X, Res2Y);
|
---|
356 |
|
---|
357 | glActiveTexture(GL_TEXTURE0);
|
---|
358 | glBindTexture(GL_TEXTURE_2D, texTransfer[0]);
|
---|
359 | glUniform1i(TextureCopy, 0);
|
---|
360 | glActiveTexture(GL_TEXTURE1);
|
---|
361 | glBindTexture(GL_TEXTURE_2D, texName[1]);
|
---|
362 | glUniform1i(Texture2Copy, 1);
|
---|
363 | glActiveTexture(GL_TEXTURE0);
|
---|
364 |
|
---|
365 |
|
---|
366 | //Draw the geometry.
|
---|
367 | glBegin(GL_QUADS);
|
---|
368 | {
|
---|
369 | glTexCoord2f(0, 0); glVertex3f(-Res2X, -Res2Y, -0.5f);
|
---|
370 | glTexCoord2f(1,0); glVertex3f(Res2X, -Res2Y, -0.5f);
|
---|
371 | glTexCoord2f(1,1); glVertex3f(Res2X, Res2Y, -0.5f);
|
---|
372 | glTexCoord2f(0, 1); glVertex3f(-Res2X, Res2Y, -0.5f);
|
---|
373 | }
|
---|
374 | glEnd();
|
---|
375 |
|
---|
376 | //Copy Buffer to PBO.
|
---|
377 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT,ObscurancesPBO ); // Bind The Buffer
|
---|
378 | glReadPixels(0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
379 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
380 |
|
---|
381 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
382 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
383 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
384 |
|
---|
385 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
386 | glUseProgram(0);
|
---|
387 | }
|
---|
388 |
|
---|
389 | // Function that calculates the reflectivity map from the scene data.
|
---|
390 | void RenderReflect(void)
|
---|
391 | {
|
---|
392 | glUseProgram(pReflect);
|
---|
393 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
394 |
|
---|
395 | int i, j;
|
---|
396 |
|
---|
397 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
398 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
399 |
|
---|
400 | glMatrixMode(GL_PROJECTION);
|
---|
401 | glLoadIdentity();
|
---|
402 | gluOrtho2D(0,1,0,1);
|
---|
403 |
|
---|
404 | glMatrixMode(GL_MODELVIEW);
|
---|
405 | glLoadIdentity();
|
---|
406 |
|
---|
407 | glViewport(0, 0, Res2X, Res2Y);
|
---|
408 |
|
---|
409 | // Enable Pointers
|
---|
410 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
411 | glEnableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
412 |
|
---|
413 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
414 | {
|
---|
415 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
416 | {
|
---|
417 | // Set Pointers To Our Data
|
---|
418 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
419 | glVertexPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
420 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors );
|
---|
421 | glColorPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
422 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
423 |
|
---|
424 | // Render
|
---|
425 | glDrawElements( GL_TRIANGLES, g_pMesh.m_pObject._geo[i]._triSet[j]._numTris*3,GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once
|
---|
426 |
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | glDisableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
431 | glDisableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
432 |
|
---|
433 | glUseProgram(0);
|
---|
434 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
435 | }
|
---|
436 |
|
---|
437 | // Function that copies the 16-bit fp RGBA buffer of the lightmap to a 32-bit RGBA fb buffer.
|
---|
438 | void NormalizeLightmap(void)
|
---|
439 | {
|
---|
440 | glUseProgram(pNormalize);
|
---|
441 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
442 |
|
---|
443 | glEnable(GL_CULL_FACE);
|
---|
444 |
|
---|
445 | glMatrixMode(GL_PROJECTION);
|
---|
446 | glLoadIdentity();
|
---|
447 | gluOrtho2D(-Res2X,Res2Y,-Res2X,Res2Y);
|
---|
448 |
|
---|
449 | glMatrixMode(GL_MODELVIEW);
|
---|
450 | glLoadIdentity();
|
---|
451 |
|
---|
452 | glViewport(0, 0, Res2X, Res2Y);
|
---|
453 |
|
---|
454 | glActiveTexture(GL_TEXTURE0);
|
---|
455 | glBindTexture(GL_TEXTURE_2D, texCopy[0]);
|
---|
456 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
457 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
458 | glUniform1i(TexturaNormalize, 0);
|
---|
459 | glUniform1f(ResolutionNormalize, Res2X);
|
---|
460 |
|
---|
461 | //Draw the geometry.
|
---|
462 | glBegin(GL_QUADS);
|
---|
463 | {
|
---|
464 | glTexCoord2f(0, 0); glVertex3f(-Res2X, -Res2Y, -0.5f);
|
---|
465 | glTexCoord2f(1,0); glVertex3f(Res2X, -Res2Y, -0.5f);
|
---|
466 | glTexCoord2f(1,1); glVertex3f(Res2X, Res2Y, -0.5f);
|
---|
467 | glTexCoord2f(0, 1); glVertex3f(-Res2X, Res2Y, -0.5f);
|
---|
468 | }
|
---|
469 | glEnd();
|
---|
470 |
|
---|
471 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
472 | glUseProgram(0);
|
---|
473 |
|
---|
474 | }
|
---|
475 |
|
---|
476 | // Function that updates the lightmap.
|
---|
477 | // The number of directions used to calculate the lightmap is iterations * steps
|
---|
478 | void UpdateTexture(int iterations, int steps, int &count)
|
---|
479 | {
|
---|
480 | const int range = ((steps * 100) / steps) + ((iterations) * 100)/(steps * iterations);
|
---|
481 |
|
---|
482 | //progress bar
|
---|
483 | wxProgressDialog dialog(wxT("Calculating GPUObscurances"),
|
---|
484 | wxT("Progress..."),
|
---|
485 | range, //range
|
---|
486 | 0, //parent
|
---|
487 | wxPD_APP_MODAL|wxPD_AUTO_HIDE|
|
---|
488 | wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME|wxPD_CAN_ABORT);
|
---|
489 |
|
---|
490 | bool cont = true;
|
---|
491 |
|
---|
492 | count = 0;
|
---|
493 | printf("\nIteracions: %d => Steps: %d\n", iterations, steps);
|
---|
494 | for(int i = 0; (i < steps) && cont; i++)
|
---|
495 | {
|
---|
496 | for(int j = 0; (j < iterations) && cont; j++)
|
---|
497 | {
|
---|
498 | cont = dialog.Update(i*100/steps + j*100/(steps*iterations));
|
---|
499 | BuscarDireccio(count); // Find a random direction
|
---|
500 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
501 | glClearColor(0.0,0.0,1.0,1.0);
|
---|
502 | glClearDepth(1.0);
|
---|
503 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
504 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
505 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[0] ); // Bind The Buffer
|
---|
506 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
507 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
|
---|
508 | RenderProjeccions(1,1);
|
---|
509 | RenderTransfer(1,1);
|
---|
510 | int l = 0;
|
---|
511 | for(int k=1; k < MAX_LAYERS; k++)
|
---|
512 | {
|
---|
513 | RenderProjeccions(l,0);
|
---|
514 | l = (l == 0)? 1:0;
|
---|
515 | RenderTransfer(l,0);
|
---|
516 | }
|
---|
517 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[l] ); // Bind The Buffer
|
---|
518 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
519 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
|
---|
520 | l = (l = 0)? 1:0;
|
---|
521 | RenderTransfer(l,1);
|
---|
522 | }
|
---|
523 | CopyTexture();
|
---|
524 | }
|
---|
525 | printf("\r100%% Done!!!!");
|
---|
526 |
|
---|
527 | NormalizeLightmap();
|
---|
528 | }
|
---|
529 |
|
---|
530 | // Function that generates the VBOs with the geometric data of the scene.
|
---|
531 | // It also initializes the constant textures used.
|
---|
532 | void InitGeometry(void)
|
---|
533 | {
|
---|
534 | int i, j;
|
---|
535 | m_pBlau = new float[ResX * ResY * 4];
|
---|
536 | //Initialize a blue buffer.
|
---|
537 | for(i = 0; i<ResX; i++)
|
---|
538 | {
|
---|
539 | for(j = 0; j<ResY; j++)
|
---|
540 | {
|
---|
541 | m_pBlau[4 * (i * ResY + j)] = 0.0;
|
---|
542 | m_pBlau[4 * (i * ResY + j) + 1] = 0.0;
|
---|
543 | m_pBlau[4 * (i * ResY + j) + 2] = 1.0;
|
---|
544 | m_pBlau[4 * (i * ResY + j) + 3] = 1.0;
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | //Initialize a black buffer
|
---|
549 | for(i = 0; i<Res2X; i++)
|
---|
550 | {
|
---|
551 | for(j = 0; j<Res2Y; j++)
|
---|
552 | {
|
---|
553 | m_pNegre[4*(i*Res2Y+j)] = 0.0;
|
---|
554 | m_pNegre[4*(i*Res2Y+j)+1] = 0.0;
|
---|
555 | m_pNegre[4*(i*Res2Y+j)+2] = 0.0;
|
---|
556 | m_pNegre[4*(i*Res2Y+j)+3] = 0.0;
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | for(i = 0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
561 | {
|
---|
562 | for(j = 0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
563 | {
|
---|
564 | //Aqui creeem els VBOs.
|
---|
565 |
|
---|
566 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs = 0;
|
---|
567 | // Generate And Bind The Index Buffer
|
---|
568 | glGenBuffersARB( 1, (unsigned int *)&g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Get A Valid Name
|
---|
569 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Bind The Buffer
|
---|
570 | // Load The Data
|
---|
571 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j]._numTris * 3 * sizeof(int), g_pMesh.m_pObject._geo[i]._triSet[j]._index, GL_STATIC_DRAW_ARB );
|
---|
572 |
|
---|
573 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices = 0;
|
---|
574 | // Generate And Bind The Vertex Buffer
|
---|
575 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Get A Valid Name
|
---|
576 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Bind The Buffer
|
---|
577 | // Load The Data
|
---|
578 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), g_pMesh.m_pObject._geo[i]._triSet[j]._ver, GL_STATIC_DRAW_ARB );
|
---|
579 |
|
---|
580 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals = 0;
|
---|
581 | // Generate And Bind The Normal Buffer
|
---|
582 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Get A Valid Name
|
---|
583 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Bind The Buffer
|
---|
584 | // Load The Data
|
---|
585 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), g_pMesh.m_pObject._geo[i]._triSet[j]._nor, GL_STATIC_DRAW_ARB );
|
---|
586 |
|
---|
587 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords = 0;
|
---|
588 | // Generate And Bind The TexCoord Buffer
|
---|
589 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Get A Valid Name
|
---|
590 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Bind The Buffer
|
---|
591 | // Load The Data
|
---|
592 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j]._numVer * 2 * sizeof(float), g_pMesh.m_pObject._geo[i]._triSet[j]._tex, GL_STATIC_DRAW_ARB );
|
---|
593 |
|
---|
594 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors = 0;
|
---|
595 | // Generate And Bind The Color Buffer
|
---|
596 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Get A Valid Name
|
---|
597 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Bind The Buffer
|
---|
598 | // Load The Data
|
---|
599 | //*12
|
---|
600 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), g_pMesh.m_pObject._geo[i]._triSet[j]._refl, GL_STATIC_DRAW_ARB );
|
---|
601 | }
|
---|
602 | }
|
---|
603 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); // Bind The Buffer
|
---|
604 | }
|
---|
605 |
|
---|
606 | // Functions that initializes the RenderTextures, the shaders and so on...
|
---|
607 | void init2(char *argv)
|
---|
608 | {
|
---|
609 | int count;
|
---|
610 |
|
---|
611 | srand ( time(NULL) );
|
---|
612 |
|
---|
613 | g_pMesh.LoadMesh(argv);
|
---|
614 |
|
---|
615 | m_pTextureImage = new float[Res2X*Res2Y*4];// Texture of diferent colors.
|
---|
616 | m_pTextureReflect = new float[Res2X*Res2Y*4]; //Texture of reflectivitat.
|
---|
617 | m_pNegre = new float[Res2X*Res2Y*4];
|
---|
618 |
|
---|
619 | //Create Textures.
|
---|
620 | glGenTextures(3, &texName[0]); // Create The Textures
|
---|
621 |
|
---|
622 | InitGeometry();
|
---|
623 |
|
---|
624 | //Texture
|
---|
625 | // Binding
|
---|
626 | glBindTexture( GL_TEXTURE_2D, texName[0] );
|
---|
627 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
628 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
629 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
630 |
|
---|
631 | //Texture
|
---|
632 | // Binding
|
---|
633 | glBindTexture( GL_TEXTURE_2D, texName[1] );
|
---|
634 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
635 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
636 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
637 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
638 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
639 |
|
---|
640 |
|
---|
641 | //Texture
|
---|
642 | // Binding
|
---|
643 | glBindTexture( GL_TEXTURE_2D, texName[2] );
|
---|
644 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, m_pTextureReflect);
|
---|
645 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
646 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
647 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
648 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
649 |
|
---|
650 | //FBOs
|
---|
651 | glGenFramebuffersEXT(1, &fb1);
|
---|
652 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
653 |
|
---|
654 | glGenTextures(1,&texReflect[0]);
|
---|
655 |
|
---|
656 | glBindTexture( GL_TEXTURE_2D, texReflect[0] );
|
---|
657 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
658 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
659 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
660 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
661 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
662 |
|
---|
663 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texReflect[0],0);
|
---|
664 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
665 |
|
---|
666 | glGenFramebuffersEXT(1, &fb2);
|
---|
667 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
668 |
|
---|
669 | glGenTextures(1,&texProjeccions[0]);
|
---|
670 |
|
---|
671 | glBindTexture( GL_TEXTURE_2D, texProjeccions[0] );
|
---|
672 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
673 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
674 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
675 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
676 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
677 |
|
---|
678 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texProjeccions[0],0);
|
---|
679 |
|
---|
680 | glGenRenderbuffersEXT(1, &texProjeccions[1]); |
---|
681 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, texProjeccions[1]); |
---|
682 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, ResX, ResY ); |
---|
683 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, texProjeccions[1]);
|
---|
684 |
|
---|
685 | glGenFramebuffersEXT(1, &fb3);
|
---|
686 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
687 |
|
---|
688 | glGenTextures(1,&texTransfer[0]);
|
---|
689 |
|
---|
690 | glBindTexture( GL_TEXTURE_2D, texTransfer[0] );
|
---|
691 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT16_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
692 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
693 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
694 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
695 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
696 |
|
---|
697 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texTransfer[0],0);
|
---|
698 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
699 |
|
---|
700 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
701 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
702 |
|
---|
703 | glGenFramebuffersEXT(1, &fb4);
|
---|
704 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
705 |
|
---|
706 | glGenTextures(1,&texCopy[0]);
|
---|
707 |
|
---|
708 | glBindTexture( GL_TEXTURE_2D, texCopy[0] );
|
---|
709 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
710 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
711 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
712 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
713 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
714 |
|
---|
715 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texCopy[0],0);
|
---|
716 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
717 |
|
---|
718 | glGenFramebuffersEXT(1, &fb5);
|
---|
719 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
720 |
|
---|
721 | glGenTextures(1,&texNormalize[0]);
|
---|
722 |
|
---|
723 | glBindTexture( GL_TEXTURE_2D, texNormalize[0] );
|
---|
724 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
725 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
726 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
727 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
728 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
729 |
|
---|
730 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texNormalize[0],0);
|
---|
731 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
732 |
|
---|
733 | //Create PBO
|
---|
734 | glGenBuffersARB( MAX_LAYERS+2, &ProjectionsPBO[0] );
|
---|
735 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[0] ); // Bind The Buffer
|
---|
736 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
737 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[MAX_LAYERS+1] ); // Bind The Buffer
|
---|
738 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
739 | for (int e = 1; e < MAX_LAYERS+1; e++ )
|
---|
740 | {
|
---|
741 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[e] ); // Bind The Buffer
|
---|
742 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), NULL, GL_STREAM_COPY );
|
---|
743 | }
|
---|
744 |
|
---|
745 | glGenBuffersARB( 1, &ObscurancesPBO );
|
---|
746 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
747 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, Res2X*Res2Y*4*sizeof(float), m_pNegre, GL_STREAM_COPY );
|
---|
748 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
749 |
|
---|
750 | //GLSL
|
---|
751 | //Projection Shaders
|
---|
752 | // char *vsProjection = NULL, *fsProjection = NULL;
|
---|
753 |
|
---|
754 | vProjection = glCreateShader(GL_VERTEX_SHADER);
|
---|
755 | fProjection = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
756 |
|
---|
757 | // vsProjection = textFileRead("projection.vert");
|
---|
758 | // fsProjection = textFileRead("projection.frag");
|
---|
759 |
|
---|
760 | // const char *vvProjection = vsProjection;
|
---|
761 | static const char *vvProjection =
|
---|
762 | " \n"
|
---|
763 | " varying float cosine; \n"
|
---|
764 | " varying vec2 texCoord; \n"
|
---|
765 | " \n"
|
---|
766 | " void main() \n"
|
---|
767 | " { \n"
|
---|
768 | " gl_Position = ftransform(); \n"
|
---|
769 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
770 | " \n"
|
---|
771 | " cosine = (gl_ModelViewMatrixInverseTranspose * vec4(gl_Normal,1.0)).z; \n"
|
---|
772 | " } \n"
|
---|
773 | " \n";
|
---|
774 |
|
---|
775 | // const char *ffProjection = fsProjection;
|
---|
776 | static const char *ffProjection =
|
---|
777 | " \n"
|
---|
778 | " uniform float res; \n"
|
---|
779 | " uniform float first; \n"
|
---|
780 | " uniform sampler2D ztex; \n"
|
---|
781 | " \n"
|
---|
782 | " varying float cosine; \n"
|
---|
783 | " varying vec2 texCoord; \n"
|
---|
784 | " \n"
|
---|
785 | " void main() \n"
|
---|
786 | " { \n"
|
---|
787 | " if( first == 0.0) \n"
|
---|
788 | " { \n"
|
---|
789 | " float depth = tex2D(ztex,gl_FragCoord.xy/res).a; \n"
|
---|
790 | " if (gl_FragCoord.z < (depth + 0.000001)) discard; \n"
|
---|
791 | " } \n"
|
---|
792 | " gl_FragColor.rg = texCoord+0.5/512.0; \n"
|
---|
793 | " gl_FragColor.b = cosine; \n"
|
---|
794 | " gl_FragColor.a = gl_FragCoord.z; \n"
|
---|
795 | " } \n";
|
---|
796 |
|
---|
797 |
|
---|
798 | glShaderSource(vProjection, 1, &vvProjection, NULL);
|
---|
799 | glShaderSource(fProjection, 1, &ffProjection, NULL);
|
---|
800 |
|
---|
801 | // free(vsProjection); free(fsProjection);
|
---|
802 |
|
---|
803 | glCompileShader(vProjection);
|
---|
804 | glCompileShader(fProjection);
|
---|
805 |
|
---|
806 | pProjection = glCreateProgram();
|
---|
807 | glAttachShader(pProjection, vProjection);
|
---|
808 | glAttachShader(pProjection, fProjection);
|
---|
809 |
|
---|
810 | glLinkProgram(pProjection);
|
---|
811 |
|
---|
812 | glUseProgram(pProjection);
|
---|
813 | TexturaDepthProjection = glGetUniformLocation(pProjection, "ztex");
|
---|
814 | SizeProjection = glGetUniformLocation(pProjection, "res");
|
---|
815 | FirstProjection = glGetUniformLocation(pProjection, "first");
|
---|
816 |
|
---|
817 | glUseProgram(0);
|
---|
818 |
|
---|
819 |
|
---|
820 | //Reflect Shaders
|
---|
821 | // char *vsReflect = NULL, *fsReflect = NULL;
|
---|
822 |
|
---|
823 | vReflect = glCreateShader(GL_VERTEX_SHADER);
|
---|
824 | fReflect = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
825 |
|
---|
826 | // vsReflect = textFileRead("reflect.vert");
|
---|
827 | // fsReflect = textFileRead("reflect.frag");
|
---|
828 |
|
---|
829 | // const char *vvReflect = vsReflect;
|
---|
830 | static const char *vvReflect =
|
---|
831 | " \n"
|
---|
832 | " varying vec4 color; \n"
|
---|
833 | " \n"
|
---|
834 | " void main() \n"
|
---|
835 | " { \n"
|
---|
836 | " vec4 posicio = vec4(gl_Vertex.xy, -0.5, 1.0); \n"
|
---|
837 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
838 | " \n"
|
---|
839 | " color = vec4(gl_Color.xyz,1.0); \n"
|
---|
840 | " } \n";
|
---|
841 |
|
---|
842 | // const char *ffReflect = fsReflect;
|
---|
843 | static const char *ffReflect =
|
---|
844 | " \n"
|
---|
845 | " varying vec4 color; \n"
|
---|
846 | " \n"
|
---|
847 | " void main() \n"
|
---|
848 | " { \n"
|
---|
849 | " gl_FragColor = color; \n"
|
---|
850 | " } \n"
|
---|
851 | " \n";
|
---|
852 |
|
---|
853 | glShaderSource(vReflect, 1, &vvReflect, NULL);
|
---|
854 | glShaderSource(fReflect, 1, &ffReflect, NULL);
|
---|
855 |
|
---|
856 | // free(vsReflect); free(fsReflect);
|
---|
857 |
|
---|
858 | glCompileShader(vReflect);
|
---|
859 | glCompileShader(fReflect);
|
---|
860 |
|
---|
861 | pReflect = glCreateProgram();
|
---|
862 | glAttachShader(pReflect, vReflect);
|
---|
863 | glAttachShader(pReflect, fReflect);
|
---|
864 |
|
---|
865 | glLinkProgram(pReflect);
|
---|
866 |
|
---|
867 | // glUseProgram(pReflect);
|
---|
868 | glUseProgram(0);
|
---|
869 |
|
---|
870 | //Texture Copy Shaders
|
---|
871 | // char *vsCopy = NULL, *fsCopy = NULL;
|
---|
872 |
|
---|
873 | vCopy = glCreateShader(GL_VERTEX_SHADER);
|
---|
874 | fCopy = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
875 |
|
---|
876 | // vsCopy = textFileRead("copy.vert");
|
---|
877 | // fsCopy = textFileRead("copy.frag");
|
---|
878 |
|
---|
879 | // const char *vvCopy = vsCopy;
|
---|
880 | static const char *vvCopy =
|
---|
881 | " \n"
|
---|
882 | " varying vec2 texCoord; \n"
|
---|
883 | " \n"
|
---|
884 | " void main() \n"
|
---|
885 | " { \n"
|
---|
886 | " vec4 posicio = vec4(gl_Vertex.xyz, 1.0); \n"
|
---|
887 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
888 | " \n"
|
---|
889 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
890 | " } \n";
|
---|
891 |
|
---|
892 | // const char *ffCopy = fsCopy;
|
---|
893 | static const char *ffCopy =
|
---|
894 | " \n"
|
---|
895 | " uniform sampler2D texture; \n"
|
---|
896 | " uniform sampler2D texture2; \n"
|
---|
897 | " \n"
|
---|
898 | " varying vec2 texCoord; \n"
|
---|
899 | " \n"
|
---|
900 | " void main() \n"
|
---|
901 | " { \n"
|
---|
902 | " gl_FragColor = tex2D(texture,texCoord) + tex2D(texture2,texCoord); \n"
|
---|
903 | " } \n";
|
---|
904 |
|
---|
905 | glShaderSource(vCopy, 1, &vvCopy, NULL);
|
---|
906 | glShaderSource(fCopy, 1, &ffCopy, NULL);
|
---|
907 |
|
---|
908 | // free(vsCopy); free(fsCopy);
|
---|
909 |
|
---|
910 | glCompileShader(vCopy);
|
---|
911 | glCompileShader(fCopy);
|
---|
912 |
|
---|
913 | pCopy = glCreateProgram();
|
---|
914 | glAttachShader(pCopy, vCopy);
|
---|
915 | glAttachShader(pCopy, fCopy);
|
---|
916 |
|
---|
917 | glLinkProgram(pCopy);
|
---|
918 |
|
---|
919 | glUseProgram(pCopy);
|
---|
920 | TextureCopy = glGetUniformLocation(pCopy, "texture");
|
---|
921 | Texture2Copy = glGetUniformLocation(pCopy, "texture2");
|
---|
922 | glUseProgram(0);
|
---|
923 |
|
---|
924 | //Lightmap Normalization Shaders
|
---|
925 | // char *vsNormalize = NULL, *fsNormalize = NULL;
|
---|
926 |
|
---|
927 | vNormalize = glCreateShader(GL_VERTEX_SHADER);
|
---|
928 | fNormalize = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
929 |
|
---|
930 | // vsNormalize = textFileRead("normalize.vert");
|
---|
931 | // fsNormalize = textFileRead("normalize.frag");
|
---|
932 |
|
---|
933 | // const char *vvNormalize = vsNormalize;
|
---|
934 | static const char *vvNormalize =
|
---|
935 | " \n"
|
---|
936 | " varying vec2 texCoord; \n"
|
---|
937 | " \n"
|
---|
938 | " void main() \n"
|
---|
939 | " { \n"
|
---|
940 | " vec4 posicio = vec4(gl_Vertex.xyz, 1.0); \n"
|
---|
941 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
942 | " \n"
|
---|
943 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
944 | " } \n";
|
---|
945 |
|
---|
946 | // const char *ffNormalize = fsNormalize;
|
---|
947 | static const char *ffNormalize =
|
---|
948 | " \n"
|
---|
949 | " uniform sampler2D texture; \n"
|
---|
950 | " uniform float res; \n"
|
---|
951 | " \n"
|
---|
952 | " varying vec2 texCoord; \n"
|
---|
953 | " \n"
|
---|
954 | " void main() \n"
|
---|
955 | " { \n"
|
---|
956 | " vec4 aux1 = tex2D(texture,texCoord); \n"
|
---|
957 | " if(aux1.a > 10.0) \n"
|
---|
958 | " { \n"
|
---|
959 | " gl_FragColor = vec4(aux1.rgb/aux1.a,1.0); \n"
|
---|
960 | " } \n"
|
---|
961 | " else \n"
|
---|
962 | " { \n"
|
---|
963 | // " float res = 256.0; \n"
|
---|
964 | " float dif = 1.0/res; \n"
|
---|
965 | " \n"
|
---|
966 | " float counter = 0.0; \n"
|
---|
967 | " vec4 aux = vec4(0.0,0.0,0.0,0.0); \n"
|
---|
968 | " \n"
|
---|
969 | " vec4 aux2 = tex2D(texture,vec2(texCoord.r+dif,texCoord.g)); \n"
|
---|
970 | " vec4 aux3 = tex2D(texture,vec2(texCoord.r+dif,texCoord.g+dif)); \n"
|
---|
971 | " vec4 aux4 = tex2D(texture,vec2(texCoord.r,texCoord.g+dif)); \n"
|
---|
972 | " vec4 aux5 = tex2D(texture,vec2(texCoord.r-dif,texCoord.g+dif)); \n"
|
---|
973 | " vec4 aux6 = tex2D(texture,vec2(texCoord.r-dif,texCoord.g)); \n"
|
---|
974 | " vec4 aux7 = tex2D(texture,vec2(texCoord.r-dif,texCoord.g-dif)); \n"
|
---|
975 | " vec4 aux8 = tex2D(texture,vec2(texCoord.r,texCoord.g-dif)); \n"
|
---|
976 | " vec4 aux9 = tex2D(texture,vec2(texCoord.r+dif,texCoord.g-dif)); \n"
|
---|
977 | // " if(aux1.a>0.0) \n"
|
---|
978 | // " { \n"
|
---|
979 | // " aux += aux1; \n"
|
---|
980 | // " counter += 1.0; \n"
|
---|
981 | // " } \n"
|
---|
982 | " if(aux2.a>10.0) \n"
|
---|
983 | " { \n"
|
---|
984 | " aux += aux2; \n"
|
---|
985 | " counter += 1.0; \n"
|
---|
986 | " } \n"
|
---|
987 | " if(aux3.a>10.0) \n"
|
---|
988 | " { \n"
|
---|
989 | " aux += aux3; \n"
|
---|
990 | " counter += 1.0; \n"
|
---|
991 | " } \n"
|
---|
992 | " if(aux4.a>10.0) \n"
|
---|
993 | " { \n"
|
---|
994 | " aux += aux4; \n"
|
---|
995 | " counter += 1.0; \n"
|
---|
996 | " } \n"
|
---|
997 | " if(aux5.a>10.0) \n"
|
---|
998 | " { \n"
|
---|
999 | " aux += aux5; \n"
|
---|
1000 | " counter += 1.0; \n"
|
---|
1001 | " } \n"
|
---|
1002 | " if(aux6.a>10.0) \n"
|
---|
1003 | " { \n"
|
---|
1004 | " aux += aux6; \n"
|
---|
1005 | " counter += 1.0; \n"
|
---|
1006 | " } \n"
|
---|
1007 | " if(aux7.a>10.0) \n"
|
---|
1008 | " { \n"
|
---|
1009 | " aux += aux7; \n"
|
---|
1010 | " counter += 1.0; \n"
|
---|
1011 | " } \n"
|
---|
1012 | " if(aux8.a>10.0) \n"
|
---|
1013 | " { \n"
|
---|
1014 | " aux += aux8; \n"
|
---|
1015 | " counter += 1.0; \n"
|
---|
1016 | " } \n"
|
---|
1017 | " if(aux9.a>10.0) \n"
|
---|
1018 | " { \n"
|
---|
1019 | " aux += aux9; \n"
|
---|
1020 | " counter += 1.0; \n"
|
---|
1021 | " } \n"
|
---|
1022 | " if(counter < 1.0) gl_FragColor = vec4(0.0,0.0,0.0,0.0); \n"
|
---|
1023 | " else \n"
|
---|
1024 | " { \n"
|
---|
1025 | " aux /= counter; \n"
|
---|
1026 | " gl_FragColor = vec4(aux.rgb/aux.a,1.0); \n"
|
---|
1027 | " }; \n"
|
---|
1028 | " } \n"
|
---|
1029 | " } \n";
|
---|
1030 |
|
---|
1031 | glShaderSource(vNormalize, 1, &vvNormalize, NULL);
|
---|
1032 | glShaderSource(fNormalize, 1, &ffNormalize, NULL);
|
---|
1033 |
|
---|
1034 | // free(vsNormalize); free(fsNormalize);
|
---|
1035 |
|
---|
1036 | glCompileShader(vNormalize);
|
---|
1037 | glCompileShader(fNormalize);
|
---|
1038 |
|
---|
1039 | pNormalize = glCreateProgram();
|
---|
1040 | glAttachShader(pNormalize, vNormalize);
|
---|
1041 | glAttachShader(pNormalize, fNormalize);
|
---|
1042 |
|
---|
1043 | glLinkProgram(pNormalize);
|
---|
1044 |
|
---|
1045 | glUseProgram(pNormalize);
|
---|
1046 | TexturaNormalize = glGetUniformLocation(pNormalize, "texture");
|
---|
1047 | ResolutionNormalize = glGetUniformLocation(pNormalize, "res");
|
---|
1048 | glUseProgram(0);
|
---|
1049 |
|
---|
1050 | //Energy Transfer Shaders
|
---|
1051 | // char *vsTransfer = NULL, *fsTransfer = NULL;
|
---|
1052 |
|
---|
1053 | vTransfer = glCreateShader(GL_VERTEX_SHADER);
|
---|
1054 | fTransfer = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
1055 |
|
---|
1056 | // vsTransfer = textFileRead("transfer.vert");
|
---|
1057 | // fsTransfer = textFileRead("transfer.frag");
|
---|
1058 |
|
---|
1059 | // const char *vvTransfer = vsTransfer;
|
---|
1060 | static const char *vvTransfer =
|
---|
1061 | " \n"
|
---|
1062 | " uniform float direction; \n"
|
---|
1063 | " \n"
|
---|
1064 | " varying vec2 p1; \n"
|
---|
1065 | " varying vec2 p2; \n"
|
---|
1066 | " varying float factor; \n"
|
---|
1067 | " \n"
|
---|
1068 | " void main() \n"
|
---|
1069 | " { \n"
|
---|
1070 | " vec4 position = gl_Vertex; \n"
|
---|
1071 | " vec4 texCoord = gl_MultiTexCoord0; \n"
|
---|
1072 | " if(((direction == 0) && (position.b != 1.0) && (position.b < 0.0) && ((texCoord.b > 0.0) || (texCoord.b == 1))) \n"
|
---|
1073 | " || ((direction == 1) && (position.b != 1.0) && (position.b > 0.0) && ((texCoord.b < 0.0) || (texCoord.b == 1)))) \n"
|
---|
1074 | " { \n"
|
---|
1075 | " \n"
|
---|
1076 | " p1 = vec2(position.r, position.g); \n"
|
---|
1077 | " \n"
|
---|
1078 | " p2 = vec2(texCoord.r, texCoord.g); \n"
|
---|
1079 | " \n"
|
---|
1080 | " gl_Position = vec4((p1 * 2.0) - vec2(1.0,1.0), 0.0,1.0); \n"
|
---|
1081 | " \n"
|
---|
1082 | " if (texCoord.b == 1) factor = 1; \n"
|
---|
1083 | " else factor = abs(texCoord.a - position.a); \n"
|
---|
1084 | " } \n"
|
---|
1085 | " else \n"
|
---|
1086 | " { \n"
|
---|
1087 | " gl_Position = vec4( 0.0,0.0, 2.0, 1.0 ); \n"
|
---|
1088 | " p1 = p2 = vec2(1.0,1.0); \n"
|
---|
1089 | " factor = 0.5; \n"
|
---|
1090 | " } \n"
|
---|
1091 | " } \n";
|
---|
1092 |
|
---|
1093 | // const char *ffTransfer = fsTransfer;
|
---|
1094 | static const char *ffTransfer =
|
---|
1095 | " \n"
|
---|
1096 | " uniform sampler2D reflectivity; \n"
|
---|
1097 | " uniform float dmax; \n"
|
---|
1098 | " \n"
|
---|
1099 | " varying vec2 p1; \n"
|
---|
1100 | " varying vec2 p2; \n"
|
---|
1101 | " varying float factor; \n"
|
---|
1102 | " \n"
|
---|
1103 | " void main() \n"
|
---|
1104 | " { \n"
|
---|
1105 | " if(factor>=dmax) gl_FragColor.rgb = vec3(0.5,0.5,0.5); \n"
|
---|
1106 | " else gl_FragColor.rgb = tex2D(reflectivity,p2).rgb * sqrt(factor/dmax); \n"
|
---|
1107 | " gl_FragColor.a = 1.0; \n"
|
---|
1108 | " } \n";
|
---|
1109 |
|
---|
1110 | glShaderSource(vTransfer, 1, &vvTransfer, NULL);
|
---|
1111 | glShaderSource(fTransfer, 1, &ffTransfer, NULL);
|
---|
1112 |
|
---|
1113 | // free(vsTransfer); free(fsTransfer);
|
---|
1114 |
|
---|
1115 | glCompileShader(vTransfer);
|
---|
1116 | glCompileShader(fTransfer);
|
---|
1117 |
|
---|
1118 | pTransfer = glCreateProgram();
|
---|
1119 | glAttachShader(pTransfer, vTransfer);
|
---|
1120 | glAttachShader(pTransfer, fTransfer);
|
---|
1121 |
|
---|
1122 | glLinkProgram(pTransfer);
|
---|
1123 |
|
---|
1124 | glUseProgram(pTransfer);
|
---|
1125 | TexturaReflectTransfer = glGetUniformLocation(pTransfer, "reflectivity");
|
---|
1126 | DirectionTransfer = glGetUniformLocation(pTransfer, "direction");
|
---|
1127 | DmaxTransfer = glGetUniformLocation(pTransfer, "dmax");
|
---|
1128 |
|
---|
1129 | glUseProgram(0);
|
---|
1130 |
|
---|
1131 | // Setup GL States
|
---|
1132 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
1133 |
|
---|
1134 | //Generate the reflectivity texture.
|
---|
1135 | RenderReflect();
|
---|
1136 |
|
---|
1137 | UpdateTexture(60, 3, count);
|
---|
1138 |
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | //Interface of the Obscurance Module
|
---|
1142 | int vcObscurerGenerateImage(CMesh &geom, char *argv, int quality, int *imgUsize, int *imgVsize, float **imgRgbBuffer)
|
---|
1143 | {
|
---|
1144 | // float divisor;
|
---|
1145 | int *DadesTemp = new int[4];
|
---|
1146 | float *dadesf=NULL;
|
---|
1147 | int *dadesi=NULL;
|
---|
1148 | int qual;
|
---|
1149 | float aux[4];
|
---|
1150 | float a;
|
---|
1151 |
|
---|
1152 | glutCreateWindow("T");
|
---|
1153 |
|
---|
1154 | glerror = glewInit();
|
---|
1155 |
|
---|
1156 | FILE *file = NULL;
|
---|
1157 |
|
---|
1158 | file = fopen("GPUObscurancesLog.txt", "w");
|
---|
1159 |
|
---|
1160 | if ((!file) || (glerror != GLEW_OK))
|
---|
1161 | {
|
---|
1162 | fprintf(file, "GPUObscurancesLog: Error initializing glew library");
|
---|
1163 | exit(-1);
|
---|
1164 | }
|
---|
1165 | else
|
---|
1166 | fprintf(file, "GPUObscurancesLog: glew library correctly initialized");
|
---|
1167 |
|
---|
1168 | fclose(file);
|
---|
1169 |
|
---|
1170 | memset(&aux, 0.0, 4);
|
---|
1171 |
|
---|
1172 | qual = quality;
|
---|
1173 |
|
---|
1174 | if (qual < 1) qual = 1;
|
---|
1175 |
|
---|
1176 | switch (qual)
|
---|
1177 | {
|
---|
1178 | case 1: *imgVsize = *imgUsize = 256;
|
---|
1179 | break;
|
---|
1180 | case 2: *imgVsize = *imgUsize = 512;
|
---|
1181 | break;
|
---|
1182 | case 3: *imgVsize = *imgUsize = 1024;
|
---|
1183 | break;
|
---|
1184 | case 4: *imgVsize = *imgUsize = 2048;
|
---|
1185 | break;
|
---|
1186 | default: printf("\nResolution not supported");
|
---|
1187 | };
|
---|
1188 |
|
---|
1189 | Res2X = *imgUsize;
|
---|
1190 | Res2Y = *imgVsize;
|
---|
1191 |
|
---|
1192 | ResX = Res2X * 2.0;
|
---|
1193 | ResY = Res2Y * 2.0;
|
---|
1194 |
|
---|
1195 | if(ResX == 4096) ResX = 2048;
|
---|
1196 | if(ResY == 4096) ResY = 2048;
|
---|
1197 |
|
---|
1198 | printf("\nRes(Plans de projeccio): ResolucioX = %d , ResolucioY = %d", ResX, ResY);
|
---|
1199 | printf("\nRes2(Lightmap): ResolucioX = %d , ResolucioY = %d", Res2X, Res2Y);
|
---|
1200 | dadesf = new float[Res2X*Res2Y*4];
|
---|
1201 | dadesi = new int[Res2X*Res2Y*4];
|
---|
1202 |
|
---|
1203 | init2(argv);
|
---|
1204 |
|
---|
1205 | //->g_pMesh.FreeMem();
|
---|
1206 |
|
---|
1207 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
1208 | glReadPixels(0,0,Res2X,Res2Y,GL_RGBA,GL_FLOAT,dadesf);
|
---|
1209 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
1210 |
|
---|
1211 | /* for(int i=0; i<Res2X*Res2Y; i++)
|
---|
1212 | {
|
---|
1213 | divisor = dadesf[4*i+3];
|
---|
1214 | if(divisor>15)
|
---|
1215 | {
|
---|
1216 | dadesf[4*i] = (dadesf[4*i]/divisor);
|
---|
1217 | dadesf[4*i+1] = (dadesf[4*i+1]/divisor);
|
---|
1218 | dadesf[4*i+2] = (dadesf[4*i+2]/divisor);
|
---|
1219 | dadesf[4*i+3] = 1.0;
|
---|
1220 | }
|
---|
1221 | else
|
---|
1222 | {
|
---|
1223 | a=0;
|
---|
1224 | aux[0] = 0.0;
|
---|
1225 | aux[1] = 0.0;
|
---|
1226 | aux[2] = 0.0;
|
---|
1227 | aux[3] = 0.0;
|
---|
1228 | if(i/Res2X > 0)
|
---|
1229 | {
|
---|
1230 | if(i/Res2X < Res2Y-1)
|
---|
1231 | {
|
---|
1232 | if(i%Res2X !=0)
|
---|
1233 | {
|
---|
1234 | if(dadesf[4*(i-Res2X)-1]>15) //ul();
|
---|
1235 | {
|
---|
1236 | aux[0] += (dadesf[4*(i-Res2X)-4]/dadesf[4*(i-Res2X)-1]);
|
---|
1237 | aux[1] += (dadesf[4*(i-Res2X)-3]/dadesf[4*(i-Res2X)-1]);
|
---|
1238 | aux[2] += (dadesf[4*(i-Res2X)-2]/dadesf[4*(i-Res2X)-1]);
|
---|
1239 | aux[3] += 1.0;
|
---|
1240 | a =+ 1.0;;
|
---|
1241 | }
|
---|
1242 | if(dadesf[4*i-1]>15) //l();
|
---|
1243 | {
|
---|
1244 | aux[0] += (dadesf[4*i-4]/dadesf[4*i-1]);
|
---|
1245 | aux[1] += (dadesf[4*i-3]/dadesf[4*i-1]);
|
---|
1246 | aux[2] += (dadesf[4*i-2]/dadesf[4*i-1]);
|
---|
1247 | aux[3] += 1.0;
|
---|
1248 | a =+ 1.0;;
|
---|
1249 | }
|
---|
1250 | if(dadesf[4*(i+Res2X)-1]>15) //dl();
|
---|
1251 | {
|
---|
1252 | aux[0] += (dadesf[4*(i+Res2X)-4]/dadesf[4*(i+Res2X)-1]);
|
---|
1253 | aux[1] += (dadesf[4*(i+Res2X)-3]/dadesf[4*(i+Res2X)-1]);
|
---|
1254 | aux[2] += (dadesf[4*(i+Res2X)-2]/dadesf[4*(i+Res2X)-1]);
|
---|
1255 | aux[3] += 1.0;
|
---|
1256 | a =+ 1.0;;
|
---|
1257 | }
|
---|
1258 | }
|
---|
1259 | if(dadesf[4*(i+Res2X)+3]>15) //d();
|
---|
1260 | {
|
---|
1261 | aux[0] += (dadesf[4*(i+Res2X)]/dadesf[4*(i+Res2X)+3]);
|
---|
1262 | aux[1] += (dadesf[4*(i+Res2X)+1]/dadesf[4*(i+Res2X)+3]);
|
---|
1263 | aux[2] += (dadesf[4*(i+Res2X)+2]/dadesf[4*(i+Res2X)+3]);
|
---|
1264 | aux[3] += 1.0;
|
---|
1265 | a =+ 1.0;;
|
---|
1266 | }
|
---|
1267 | if(i%Res2X!=Res2X-1)
|
---|
1268 | {
|
---|
1269 | if(dadesf[4*(i-Res2X)+7]>15) //ur();
|
---|
1270 | {
|
---|
1271 | aux[0] += (dadesf[4*(i-Res2X)+4]/dadesf[4*(i-Res2X)+7]);
|
---|
1272 | aux[1] += (dadesf[4*(i-Res2X)+5]/dadesf[4*(i-Res2X)+7]);
|
---|
1273 | aux[2] += (dadesf[4*(i-Res2X)+6]/dadesf[4*(i-Res2X)+7]);
|
---|
1274 | aux[3] += 1.0;
|
---|
1275 | a =+ 1.0;;
|
---|
1276 | }
|
---|
1277 | if(dadesf[4*i+7]>15) //r();
|
---|
1278 | {
|
---|
1279 | aux[0] += (dadesf[4*i+4]/dadesf[4*i+7]);
|
---|
1280 | aux[1] += (dadesf[4*i+5]/dadesf[4*i+7]);
|
---|
1281 | aux[2] += (dadesf[4*i+6]/dadesf[4*i+7]);
|
---|
1282 | aux[3] += 1.0;
|
---|
1283 | a =+ 1.0;;
|
---|
1284 | }
|
---|
1285 | if(dadesf[4*(i+Res2X)+7]>15) //dr();
|
---|
1286 | {
|
---|
1287 | aux[0] += (dadesf[4*(i+Res2X)+4]/dadesf[4*(i+Res2X)+7]);
|
---|
1288 | aux[1] += (dadesf[4*(i+Res2X)+5]/dadesf[4*(i+Res2X)+7]);
|
---|
1289 | aux[2] += (dadesf[4*(i+Res2X)+6]/dadesf[4*(i+Res2X)+7]);
|
---|
1290 | aux[3] += 1.0;
|
---|
1291 | a =+ 1.0;;
|
---|
1292 | }
|
---|
1293 | }
|
---|
1294 | if(dadesf[4*(i-Res2X)+3]>15) //u();
|
---|
1295 | {
|
---|
1296 | aux[0] += (dadesf[4*(i-Res2X)]/dadesf[4*(i-Res2X)+3]);
|
---|
1297 | aux[1] += (dadesf[4*(i-Res2X)+1]/dadesf[4*(i-Res2X)+3]);
|
---|
1298 | aux[2] += (dadesf[4*(i-Res2X)+2]/dadesf[4*(i-Res2X)+3]);
|
---|
1299 | aux[3] += 1.0;
|
---|
1300 | a =+ 1.0;;
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 | }
|
---|
1304 | else
|
---|
1305 | {
|
---|
1306 | if(i%Res2X !=0)
|
---|
1307 | {
|
---|
1308 | if(dadesf[4*i-1]>15) //l();
|
---|
1309 | {
|
---|
1310 | aux[0] += (dadesf[4*i-4]/dadesf[4*i-1]);
|
---|
1311 | aux[1] += (dadesf[4*i-3]/dadesf[4*i-1]);
|
---|
1312 | aux[2] += (dadesf[4*i-2]/dadesf[4*i-1]);
|
---|
1313 | aux[3] += 1.0;
|
---|
1314 | a =+ 1.0;;
|
---|
1315 | }
|
---|
1316 | if(dadesf[4*(i+Res2X)-1]>15) //dl();
|
---|
1317 | {
|
---|
1318 | aux[0] += (dadesf[4*(i+Res2X)-4]/dadesf[4*(i+Res2X)-1]);
|
---|
1319 | aux[1] += (dadesf[4*(i+Res2X)-3]/dadesf[4*(i+Res2X)-1]);
|
---|
1320 | aux[2] += (dadesf[4*(i+Res2X)-2]/dadesf[4*(i+Res2X)-1]);
|
---|
1321 | aux[3] = 1.0;
|
---|
1322 | a =+ 1.0;;
|
---|
1323 | }
|
---|
1324 | }
|
---|
1325 | if(i%Res2X!=Res2X-1)
|
---|
1326 | {
|
---|
1327 | if(dadesf[4*i+7]>15) //r();
|
---|
1328 | {
|
---|
1329 | aux[0] += (dadesf[4*i+4]/dadesf[4*i+7]);
|
---|
1330 | aux[1] += (dadesf[4*i+5]/dadesf[4*i+7]);
|
---|
1331 | aux[2] += (dadesf[4*i+6]/dadesf[4*i+7]);
|
---|
1332 | aux[3] += 1.0;
|
---|
1333 | a =+ 1.0;;
|
---|
1334 | }
|
---|
1335 | if(dadesf[4*(i+Res2X)+7]>15) //dr();
|
---|
1336 | {
|
---|
1337 | aux[0] += (dadesf[4*(i+Res2X)+4]/dadesf[4*(i+Res2X)+7]);
|
---|
1338 | aux[1] += (dadesf[4*(i+Res2X)+5]/dadesf[4*(i+Res2X)+7]);
|
---|
1339 | aux[2] += (dadesf[4*(i+Res2X)+6]/dadesf[4*(i+Res2X)+7]);
|
---|
1340 | aux[3] += 1.0;
|
---|
1341 | a =+ 1.0;;
|
---|
1342 | }
|
---|
1343 | }
|
---|
1344 | if(dadesf[4*(i+Res2X)+3]>15) //d();
|
---|
1345 | {
|
---|
1346 | aux[0] += (dadesf[4*(i+Res2X)]/dadesf[4*(i+Res2X)+3]);
|
---|
1347 | aux[1] += (dadesf[4*(i+Res2X)+1]/dadesf[4*(i+Res2X)+3]);
|
---|
1348 | aux[2] += (dadesf[4*(i+Res2X)+2]/dadesf[4*(i+Res2X)+3]);
|
---|
1349 | aux[3] += 1.0;
|
---|
1350 | a =+ 1.0;;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | if(a > 0.0)
|
---|
1354 | {
|
---|
1355 | dadesf[4*i] = aux[0]/aux[3];
|
---|
1356 | dadesf[4*i+1] = aux[1]/aux[3];
|
---|
1357 | dadesf[4*i+2] = aux[2]/aux[3];
|
---|
1358 | dadesf[4*i+3] = 1.0;
|
---|
1359 | }
|
---|
1360 | else
|
---|
1361 | {
|
---|
1362 | dadesf[4*i] = 0.0;
|
---|
1363 | dadesf[4*i+1] = 0.0;
|
---|
1364 | dadesf[4*i+2] = 0.0;
|
---|
1365 | dadesf[4*i+3] = 0.0;
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 | dadesi[4*i] = (int)(255.0*dadesf[4*i]);
|
---|
1369 | dadesi[4*i+1] = (int)(255.0*dadesf[4*i+1]);
|
---|
1370 | dadesi[4*i+2] = (int)(255.0*dadesf[4*i+2]);
|
---|
1371 | dadesi[4*i+3] = (int)(255.0*dadesf[4*i+3]);
|
---|
1372 |
|
---|
1373 | }
|
---|
1374 | */
|
---|
1375 | *imgRgbBuffer = dadesf;
|
---|
1376 |
|
---|
1377 | geom = g_pMesh;
|
---|
1378 |
|
---|
1379 | return 0;
|
---|
1380 |
|
---|
1381 | } |
---|