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 | #include "glew.h"
|
---|
10 | #include <glut.h>
|
---|
11 | #include "stdlib.h"
|
---|
12 |
|
---|
13 | // Includes
|
---|
14 | #include "CMesh.h"
|
---|
15 | #include "vcObscuranceMap.h"
|
---|
16 |
|
---|
17 | //progress bar
|
---|
18 | #include <wx/progdlg.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 |
|
---|
60 | // Global variables
|
---|
61 | CVert Vertex, Direccio, Increment, Primer, Ultim, Origin;
|
---|
62 |
|
---|
63 | static GLuint texName[3];
|
---|
64 |
|
---|
65 | static int ResX; //Resolution
|
---|
66 | static int ResY;
|
---|
67 | static int Res2X; //Resolution
|
---|
68 | static int Res2Y; //Resolution
|
---|
69 |
|
---|
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 |
|
---|
124 | // Returns a random point in the surface of the sphere
|
---|
125 | CVert ranpoint(float r, CVert c)
|
---|
126 | {
|
---|
127 | double ale1, ale2;
|
---|
128 | CVert p;
|
---|
129 | float alfa,beta;
|
---|
130 |
|
---|
131 | ale1= (float)rand()/(float)RAND_MAX;
|
---|
132 | ale2= (float)rand()/(float)RAND_MAX;
|
---|
133 |
|
---|
134 | alfa=1.0-2.0*ale1; /* real between -1 and 1 */
|
---|
135 | alfa=acos(alfa); /* angle between 0 and PI */
|
---|
136 |
|
---|
137 | beta=2*M_PI*ale2; /* angle between 0 and 2*PI */
|
---|
138 |
|
---|
139 | p.x=c.x+r*sin(alfa)*cos(beta);
|
---|
140 | p.y=c.y+r*sin(alfa)*sin(beta);
|
---|
141 | p.z=c.z+r*cos(alfa);
|
---|
142 |
|
---|
143 | return p;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Function to find a point in the sphere in the direction that goes from the point in the surface of the sphere
|
---|
147 | // to the center of the sphere. First it gets the random point in the surface.
|
---|
148 | void BuscarDireccio()
|
---|
149 | {
|
---|
150 | Esfera.center.x = 0.0;
|
---|
151 | Esfera.center.y = 0.0;
|
---|
152 | Esfera.center.z = 0.0;
|
---|
153 | Esfera.radius = sqrt(12.0)/2.0;
|
---|
154 |
|
---|
155 | Primer = ranpoint(Esfera.radius,Esfera.center);
|
---|
156 | Vertex.x = Primer.x + (Esfera.center.x-Primer.x) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
157 | Vertex.y = Primer.y + (Esfera.center.y-Primer.y) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
158 | Vertex.z = Primer.z + (Esfera.center.z-Primer.z) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Function that renders the depth peeling geometry planes and stores them in PBOs.
|
---|
162 | void RenderProjeccions(void)
|
---|
163 | {
|
---|
164 | glUseProgram(pProjection);
|
---|
165 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
166 |
|
---|
167 | int i, j, e;
|
---|
168 | changeSize(ResX, ResY);
|
---|
169 | glClearColor(0.0,0.0,1.0,1.0);
|
---|
170 | glClearDepth(1.0);
|
---|
171 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
172 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
173 | glDepthFunc(GL_LESS);
|
---|
174 |
|
---|
175 | glDisable(GL_CULL_FACE);
|
---|
176 |
|
---|
177 | glMatrixMode(GL_PROJECTION);
|
---|
178 | glLoadIdentity();
|
---|
179 | glOrtho(-Esfera.radius,Esfera.radius,-Esfera.radius,Esfera.radius,0,Esfera.radius * 2.0);
|
---|
180 |
|
---|
181 | glMatrixMode(GL_MODELVIEW);
|
---|
182 | glLoadIdentity();
|
---|
183 | gluLookAt(Primer.x,Primer.y,Primer.z,
|
---|
184 | Esfera.center.x,Esfera.center.y,Esfera.center.z,
|
---|
185 | 0.0f,1.0f,0.0f);
|
---|
186 |
|
---|
187 | glViewport(0,0,ResX,ResY);
|
---|
188 |
|
---|
189 | glUniform1f(SizeProjection, (float)ResX);
|
---|
190 | glUniform1f(FirstProjection, 1.0);
|
---|
191 |
|
---|
192 | // Enable Pointers
|
---|
193 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
194 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
195 | glEnableClientState( GL_NORMAL_ARRAY ); // Enable Texture Coord Arrays
|
---|
196 |
|
---|
197 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
198 | {
|
---|
199 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
200 | {
|
---|
201 | // Set Pointers To Our Data
|
---|
202 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
203 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
204 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
205 | glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
206 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals );
|
---|
207 | glNormalPointer( GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
208 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
209 |
|
---|
210 | // Render
|
---|
211 | 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
|
---|
212 |
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | //Copy the color buffer to PBO.
|
---|
217 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[1] ); // Bind The Buffer
|
---|
218 | glReadPixels(0, 0, ResX, ResY, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
219 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
220 |
|
---|
221 | //Render the following layers.
|
---|
222 | glUniform1f(FirstProjection, 0.0);
|
---|
223 |
|
---|
224 | e = 1;
|
---|
225 |
|
---|
226 | for(e = 1; e < MAX_LAYERS; e++)
|
---|
227 | {
|
---|
228 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[e] ); // Bind The Buffer
|
---|
229 | glBindTexture(GL_TEXTURE_2D,texName[0]);
|
---|
230 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ResX, ResY);
|
---|
231 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
232 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
233 |
|
---|
234 | glActiveTexture(GL_TEXTURE0);
|
---|
235 | glBindTexture(GL_TEXTURE_2D, texName[0]);
|
---|
236 | glUniform1i(TexturaDepthProjection, 0);
|
---|
237 |
|
---|
238 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
239 | {
|
---|
240 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
241 | {
|
---|
242 | // Set Pointers To Our Data
|
---|
243 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
244 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
245 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
246 | glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
247 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals );
|
---|
248 | glNormalPointer( GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
249 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
250 |
|
---|
251 | 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
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[e+1] ); // Bind The Buffer
|
---|
256 | glReadPixels(0, 0, ResX, ResY, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
257 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
261 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
262 | glDisableClientState( GL_NORMAL_ARRAY ); // Disable Texture Coord Arrays
|
---|
263 |
|
---|
264 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
265 | glUseProgram(0);
|
---|
266 | }
|
---|
267 |
|
---|
268 | // Function that calculates the lightmap using the neighbouring projections.
|
---|
269 | void RenderTransfer(void)
|
---|
270 | {
|
---|
271 | glUseProgram(pTransfer);
|
---|
272 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
273 |
|
---|
274 | glBlendFunc(GL_ONE, GL_ONE);
|
---|
275 |
|
---|
276 | glEnable(GL_BLEND);
|
---|
277 |
|
---|
278 | glMatrixMode(GL_PROJECTION);
|
---|
279 | glLoadIdentity();
|
---|
280 | gluOrtho2D(-1,1,-1,1);
|
---|
281 |
|
---|
282 | glMatrixMode(GL_MODELVIEW);
|
---|
283 | glLoadIdentity();
|
---|
284 | glPointSize(1.0);
|
---|
285 |
|
---|
286 | glViewport(0, 0, Res2X, Res2Y);
|
---|
287 |
|
---|
288 | glUniform1f(DmaxTransfer, (GLfloat)0.3);
|
---|
289 | glUniform1f(DirectionTransfer, 0.0);
|
---|
290 | glActiveTexture(GL_TEXTURE0);
|
---|
291 | glBindTexture(GL_TEXTURE_2D, texReflect[0]);
|
---|
292 | glUniform1i(TexturaReflectTransfer, 0);
|
---|
293 |
|
---|
294 | //First Render
|
---|
295 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
296 | glUniform1f(DirectionTransfer, 1.0);
|
---|
297 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[1] );
|
---|
298 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
299 |
|
---|
300 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
301 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[0] );
|
---|
302 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
303 |
|
---|
304 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
305 |
|
---|
306 | for(int e=0;e<MAX_LAYERS-1;e++)
|
---|
307 | {
|
---|
308 | glUniform1f(DirectionTransfer, 0.0);
|
---|
309 |
|
---|
310 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[e+1] );
|
---|
311 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
312 |
|
---|
313 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[e+2] );
|
---|
314 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
315 |
|
---|
316 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
317 |
|
---|
318 | glUniform1f(DirectionTransfer, 1.0);
|
---|
319 |
|
---|
320 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[e+2] );
|
---|
321 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
322 |
|
---|
323 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[e+1] );
|
---|
324 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
325 |
|
---|
326 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
327 | }
|
---|
328 |
|
---|
329 | glUniform1f(DirectionTransfer, 0.0);
|
---|
330 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
331 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[MAX_LAYERS] );
|
---|
332 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
333 |
|
---|
334 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
335 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[MAX_LAYERS+1] );
|
---|
336 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
337 |
|
---|
338 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
339 |
|
---|
340 |
|
---|
341 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
342 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
343 |
|
---|
344 | glDisable(GL_BLEND);
|
---|
345 | glUseProgram(0);
|
---|
346 |
|
---|
347 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
348 |
|
---|
349 | }
|
---|
350 |
|
---|
351 | // Function that copies the 16-bit fp RGBA buffer of the lightmap to a 32-bit RGBA fb buffer.
|
---|
352 | void CopyTexture(void)
|
---|
353 | {
|
---|
354 | glUseProgram(pCopy);
|
---|
355 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
356 | void *texturePBO;
|
---|
357 |
|
---|
358 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
359 | texturePBO = glMapBuffer(GL_PIXEL_PACK_BUFFER_EXT, GL_READ_ONLY);
|
---|
360 | glBindTexture(GL_TEXTURE_2D,texName[1]);
|
---|
361 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, texturePBO);
|
---|
362 | glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
|
---|
363 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
364 |
|
---|
365 | glEnable(GL_CULL_FACE);
|
---|
366 |
|
---|
367 | glMatrixMode(GL_PROJECTION);
|
---|
368 | glLoadIdentity();
|
---|
369 | gluOrtho2D(-Res2X,Res2Y,-Res2X,Res2Y);
|
---|
370 |
|
---|
371 | glMatrixMode(GL_MODELVIEW);
|
---|
372 | glLoadIdentity();
|
---|
373 |
|
---|
374 | glViewport(0, 0, Res2X, Res2Y);
|
---|
375 |
|
---|
376 | glActiveTexture(GL_TEXTURE0);
|
---|
377 | glBindTexture(GL_TEXTURE_2D, texTransfer[0]);
|
---|
378 | glUniform1i(TextureCopy, 0);
|
---|
379 | glActiveTexture(GL_TEXTURE1);
|
---|
380 | glBindTexture(GL_TEXTURE_2D, texName[1]);
|
---|
381 | glUniform1i(Texture2Copy, 1);
|
---|
382 | glActiveTexture(GL_TEXTURE0);
|
---|
383 |
|
---|
384 |
|
---|
385 | //Draw the geometry.
|
---|
386 | glBegin(GL_QUADS);
|
---|
387 | {
|
---|
388 | glTexCoord2f(0, 0); glVertex3f(-Res2X, -Res2Y, -0.5f);
|
---|
389 | glTexCoord2f(1,0); glVertex3f(Res2X, -Res2Y, -0.5f);
|
---|
390 | glTexCoord2f(1,1); glVertex3f(Res2X, Res2Y, -0.5f);
|
---|
391 | glTexCoord2f(0, 1); glVertex3f(-Res2X, Res2Y, -0.5f);
|
---|
392 | }
|
---|
393 | glEnd();
|
---|
394 |
|
---|
395 | //Copy Buffer to PBO.
|
---|
396 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT,ObscurancesPBO ); // Bind The Buffer
|
---|
397 | glReadPixels(0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
398 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
399 |
|
---|
400 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
401 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
402 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
403 |
|
---|
404 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
405 | glUseProgram(0);
|
---|
406 | }
|
---|
407 |
|
---|
408 | // Function that calculates the reflectivity map from the scene data.
|
---|
409 | void RenderReflect(void)
|
---|
410 | {
|
---|
411 | glUseProgram(pReflect);
|
---|
412 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
413 |
|
---|
414 | int i, j;
|
---|
415 |
|
---|
416 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
417 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
418 |
|
---|
419 | glMatrixMode(GL_PROJECTION);
|
---|
420 | glLoadIdentity();
|
---|
421 | gluOrtho2D(0,1,0,1);
|
---|
422 |
|
---|
423 | glMatrixMode(GL_MODELVIEW);
|
---|
424 | glLoadIdentity();
|
---|
425 |
|
---|
426 | glViewport(0, 0, Res2X, Res2Y);
|
---|
427 |
|
---|
428 | // Enable Pointers
|
---|
429 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
430 | glEnableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
431 |
|
---|
432 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
433 | {
|
---|
434 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
435 | {
|
---|
436 | // Set Pointers To Our Data
|
---|
437 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
438 | glVertexPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
439 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors );
|
---|
440 | glColorPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
441 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
442 |
|
---|
443 | // Render
|
---|
444 | 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
|
---|
445 |
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | glDisableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
450 | glDisableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
451 |
|
---|
452 | glUseProgram(0);
|
---|
453 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
454 | }
|
---|
455 |
|
---|
456 | // Function that copies the 16-bit fp RGBA buffer of the lightmap to a 32-bit RGBA fb buffer.
|
---|
457 | void NormalizeLightmap(void)
|
---|
458 | {
|
---|
459 | glUseProgram(pNormalize);
|
---|
460 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
461 |
|
---|
462 | glEnable(GL_CULL_FACE);
|
---|
463 |
|
---|
464 | glMatrixMode(GL_PROJECTION);
|
---|
465 | glLoadIdentity();
|
---|
466 | gluOrtho2D(-Res2X,Res2Y,-Res2X,Res2Y);
|
---|
467 |
|
---|
468 | glMatrixMode(GL_MODELVIEW);
|
---|
469 | glLoadIdentity();
|
---|
470 |
|
---|
471 | glViewport(0, 0, Res2X, Res2Y);
|
---|
472 |
|
---|
473 | glActiveTexture(GL_TEXTURE0);
|
---|
474 | glBindTexture(GL_TEXTURE_2D, texCopy[0]);
|
---|
475 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
476 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
477 | glUniform1i(TextureCopy, 0);
|
---|
478 |
|
---|
479 | //Draw the geometry.
|
---|
480 | glBegin(GL_QUADS);
|
---|
481 | {
|
---|
482 | glTexCoord2f(0, 0); glVertex3f(-Res2X, -Res2Y, -0.5f);
|
---|
483 | glTexCoord2f(1,0); glVertex3f(Res2X, -Res2Y, -0.5f);
|
---|
484 | glTexCoord2f(1,1); glVertex3f(Res2X, Res2Y, -0.5f);
|
---|
485 | glTexCoord2f(0, 1); glVertex3f(-Res2X, Res2Y, -0.5f);
|
---|
486 | }
|
---|
487 | glEnd();
|
---|
488 |
|
---|
489 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
490 | glUseProgram(0);
|
---|
491 | }
|
---|
492 |
|
---|
493 | // Function that updates the lightmap.
|
---|
494 | // The number of directions used to calculate the lightmap is iterations * steps
|
---|
495 | void UpdateTexture(int iterations, int steps)
|
---|
496 | {
|
---|
497 | const int range = ((steps * 100) / steps) + ((iterations) * 100)/(steps * iterations);
|
---|
498 |
|
---|
499 | //progress bar
|
---|
500 | wxProgressDialog dialog(wxT("Calculating GPUObscurances"),
|
---|
501 | wxT("Progress..."),
|
---|
502 | range, //range
|
---|
503 | 0, //parent
|
---|
504 | wxPD_APP_MODAL|wxPD_AUTO_HIDE|
|
---|
505 | wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME|wxPD_CAN_ABORT);
|
---|
506 |
|
---|
507 | bool cont = true;
|
---|
508 |
|
---|
509 | printf("\nIteracions: %d => Steps: %d\n", iterations, steps);
|
---|
510 | for(int i = 0; (i < steps) && cont; i++)
|
---|
511 | {
|
---|
512 | for(int j = 0; (j < iterations) && cont; j++)
|
---|
513 | {
|
---|
514 | cont = dialog.Update(i*100/steps + j*100/(steps*iterations));
|
---|
515 | BuscarDireccio(); // Find a random direction
|
---|
516 | RenderProjeccions();
|
---|
517 | RenderTransfer();
|
---|
518 | }
|
---|
519 | CopyTexture();
|
---|
520 | }
|
---|
521 | printf("\r100%% Done!!!!");
|
---|
522 | NormalizeLightmap();
|
---|
523 | }
|
---|
524 |
|
---|
525 | // Function that generates the VBOs with the geometric data of the scene.
|
---|
526 | // It also initializes the constant textures used.
|
---|
527 | void InitGeometry(void)
|
---|
528 | {
|
---|
529 | int i, j;
|
---|
530 | m_pBlau = new float[ResX * ResY * 4];
|
---|
531 | //Initialize a blue buffer.
|
---|
532 | for(i = 0; i<ResX; i++)
|
---|
533 | {
|
---|
534 | for(j = 0; j<ResY; j++)
|
---|
535 | {
|
---|
536 | m_pBlau[4 * (i * ResY + j)] = 0.0;
|
---|
537 | m_pBlau[4 * (i * ResY + j) + 1] = 0.0;
|
---|
538 | m_pBlau[4 * (i * ResY + j) + 2] = 1.0;
|
---|
539 | m_pBlau[4 * (i * ResY + j) + 3] = 1.0;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | //Initialize a black buffer
|
---|
544 | for(i = 0; i<Res2X; i++)
|
---|
545 | {
|
---|
546 | for(j = 0; j<Res2Y; j++)
|
---|
547 | {
|
---|
548 | m_pNegre[4*(i*Res2Y+j)] = 0.0;
|
---|
549 | m_pNegre[4*(i*Res2Y+j)+1] = 0.0;
|
---|
550 | m_pNegre[4*(i*Res2Y+j)+2] = 0.0;
|
---|
551 | m_pNegre[4*(i*Res2Y+j)+3] = 0.0;
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | for(i = 0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
556 | {
|
---|
557 | for(j = 0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
558 | {
|
---|
559 | //Aqui creeem els VBOs.
|
---|
560 |
|
---|
561 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs = 0;
|
---|
562 | // Generate And Bind The Index Buffer
|
---|
563 | glGenBuffersARB( 1, (unsigned int *)&g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Get A Valid Name
|
---|
564 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Bind The Buffer
|
---|
565 | // Load The Data
|
---|
566 | 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 );
|
---|
567 |
|
---|
568 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices = 0;
|
---|
569 | // Generate And Bind The Vertex Buffer
|
---|
570 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Get A Valid Name
|
---|
571 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Bind The Buffer
|
---|
572 | // Load The Data
|
---|
573 | 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 );
|
---|
574 |
|
---|
575 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals = 0;
|
---|
576 | // Generate And Bind The Normal Buffer
|
---|
577 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Get A Valid Name
|
---|
578 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Bind The Buffer
|
---|
579 | // Load The Data
|
---|
580 | 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 );
|
---|
581 |
|
---|
582 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords = 0;
|
---|
583 | // Generate And Bind The TexCoord Buffer
|
---|
584 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Get A Valid Name
|
---|
585 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Bind The Buffer
|
---|
586 | // Load The Data
|
---|
587 | 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 );
|
---|
588 |
|
---|
589 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors = 0;
|
---|
590 | // Generate And Bind The Color Buffer
|
---|
591 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Get A Valid Name
|
---|
592 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Bind The Buffer
|
---|
593 | // Load The Data
|
---|
594 | //*12
|
---|
595 | 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 );
|
---|
596 | }
|
---|
597 | }
|
---|
598 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); // Bind The Buffer
|
---|
599 | }
|
---|
600 |
|
---|
601 | // Functions that initializes the RenderTextures, the shaders and so on...
|
---|
602 | void init2(char *argv)
|
---|
603 | {
|
---|
604 | g_pMesh.LoadMesh(argv);
|
---|
605 |
|
---|
606 | m_pTextureImage = new float[Res2X*Res2Y*4];// Texture of diferent colors.
|
---|
607 | m_pTextureReflect = new float[Res2X*Res2Y*4]; //Texture of reflectivitat.
|
---|
608 | m_pNegre = new float[Res2X*Res2Y*4];
|
---|
609 |
|
---|
610 | //Create Textures.
|
---|
611 | glGenTextures(3, &texName[0]); // Create The Textures
|
---|
612 |
|
---|
613 | InitGeometry();
|
---|
614 |
|
---|
615 | //Texture
|
---|
616 | // Binding
|
---|
617 | glBindTexture( GL_TEXTURE_2D, texName[0] );
|
---|
618 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
619 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
620 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
621 |
|
---|
622 | //Texture
|
---|
623 | // Binding
|
---|
624 | glBindTexture( GL_TEXTURE_2D, texName[1] );
|
---|
625 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
626 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
627 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
628 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
629 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
630 |
|
---|
631 |
|
---|
632 | //Texture
|
---|
633 | // Binding
|
---|
634 | glBindTexture( GL_TEXTURE_2D, texName[2] );
|
---|
635 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, m_pTextureReflect);
|
---|
636 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
637 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
638 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
639 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
640 |
|
---|
641 | //FBOs
|
---|
642 | glGenFramebuffersEXT(1, &fb1);
|
---|
643 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
644 |
|
---|
645 | glGenTextures(1,&texReflect[0]);
|
---|
646 |
|
---|
647 | glBindTexture( GL_TEXTURE_2D, texReflect[0] );
|
---|
648 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
649 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
650 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
651 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
652 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
653 |
|
---|
654 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texReflect[0],0);
|
---|
655 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
656 |
|
---|
657 | glGenFramebuffersEXT(1, &fb2);
|
---|
658 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
659 |
|
---|
660 | glGenTextures(1,&texProjeccions[0]);
|
---|
661 |
|
---|
662 | glBindTexture( GL_TEXTURE_2D, texProjeccions[0] );
|
---|
663 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
664 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
665 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
666 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
667 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
668 |
|
---|
669 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texProjeccions[0],0);
|
---|
670 |
|
---|
671 | glGenRenderbuffersEXT(1, &texProjeccions[1]); |
---|
672 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, texProjeccions[1]); |
---|
673 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, ResX, ResY ); |
---|
674 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, texProjeccions[1]);
|
---|
675 |
|
---|
676 | glGenFramebuffersEXT(1, &fb3);
|
---|
677 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
678 |
|
---|
679 | glGenTextures(1,&texTransfer[0]);
|
---|
680 |
|
---|
681 | glBindTexture( GL_TEXTURE_2D, texTransfer[0] );
|
---|
682 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT16_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
683 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
684 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
685 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
686 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
687 |
|
---|
688 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texTransfer[0],0);
|
---|
689 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
690 |
|
---|
691 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
692 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
693 |
|
---|
694 | glGenFramebuffersEXT(1, &fb4);
|
---|
695 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
696 |
|
---|
697 | glGenTextures(1,&texCopy[0]);
|
---|
698 |
|
---|
699 | glBindTexture( GL_TEXTURE_2D, texCopy[0] );
|
---|
700 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
701 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
702 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
703 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
704 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
705 |
|
---|
706 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texCopy[0],0);
|
---|
707 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
708 |
|
---|
709 | glGenFramebuffersEXT(1, &fb5);
|
---|
710 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
711 |
|
---|
712 | glGenTextures(1,&texNormalize[0]);
|
---|
713 |
|
---|
714 | glBindTexture( GL_TEXTURE_2D, texNormalize[0] );
|
---|
715 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
716 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
717 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
718 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
719 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
720 |
|
---|
721 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texNormalize[0],0);
|
---|
722 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
723 |
|
---|
724 | //Create PBO
|
---|
725 | glGenBuffersARB( MAX_LAYERS+2, &ProjectionsPBO[0] );
|
---|
726 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[0] ); // Bind The Buffer
|
---|
727 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
728 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[MAX_LAYERS+1] ); // Bind The Buffer
|
---|
729 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
730 | for (int e = 1; e < MAX_LAYERS+1; e++ )
|
---|
731 | {
|
---|
732 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[e] ); // Bind The Buffer
|
---|
733 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), NULL, GL_STREAM_COPY );
|
---|
734 | }
|
---|
735 |
|
---|
736 | glGenBuffersARB( 1, &ObscurancesPBO );
|
---|
737 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
738 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, Res2X*Res2Y*4*sizeof(float), m_pNegre, GL_STREAM_COPY );
|
---|
739 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
740 |
|
---|
741 | //GLSL
|
---|
742 | //Projection Shaders
|
---|
743 |
|
---|
744 | vProjection = glCreateShader(GL_VERTEX_SHADER);
|
---|
745 | fProjection = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
746 |
|
---|
747 | static const char *vvProjection =
|
---|
748 | " \n"
|
---|
749 | " varying float cosine; \n"
|
---|
750 | " varying vec2 texCoord; \n"
|
---|
751 | " \n"
|
---|
752 | " void main() \n"
|
---|
753 | " { \n"
|
---|
754 | " gl_Position = ftransform(); \n"
|
---|
755 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
756 | " \n"
|
---|
757 | " cosine = (gl_ModelViewMatrixInverseTranspose * vec4(gl_Normal,1.0)).z; \n"
|
---|
758 | " } \n"
|
---|
759 | " \n";
|
---|
760 |
|
---|
761 | static const char *ffProjection =
|
---|
762 | " \n"
|
---|
763 | " uniform float res; \n"
|
---|
764 | " uniform float first; \n"
|
---|
765 | " uniform sampler2D ztex; \n"
|
---|
766 | " \n"
|
---|
767 | " varying float cosine; \n"
|
---|
768 | " varying vec2 texCoord; \n"
|
---|
769 | " \n"
|
---|
770 | " void main() \n"
|
---|
771 | " { \n"
|
---|
772 | " if( first == 0.0) \n"
|
---|
773 | " { \n"
|
---|
774 | " float depth = tex2D(ztex,gl_FragCoord.xy/res).a; \n"
|
---|
775 | " if (gl_FragCoord.z < (depth + 0.000001)) discard; \n"
|
---|
776 | " } \n"
|
---|
777 | " gl_FragColor.rg = texCoord+0.5/512.0; \n"
|
---|
778 | " gl_FragColor.b = cosine; \n"
|
---|
779 | " gl_FragColor.a = gl_FragCoord.z; \n"
|
---|
780 | " } \n";
|
---|
781 |
|
---|
782 |
|
---|
783 | glShaderSource(vProjection, 1, &vvProjection, NULL);
|
---|
784 | glShaderSource(fProjection, 1, &ffProjection, NULL);
|
---|
785 |
|
---|
786 | glCompileShader(vProjection);
|
---|
787 | glCompileShader(fProjection);
|
---|
788 |
|
---|
789 | pProjection = glCreateProgram();
|
---|
790 | glAttachShader(pProjection, vProjection);
|
---|
791 | glAttachShader(pProjection, fProjection);
|
---|
792 |
|
---|
793 | glLinkProgram(pProjection);
|
---|
794 |
|
---|
795 | glUseProgram(pProjection);
|
---|
796 | TexturaDepthProjection = glGetUniformLocation(pProjection, "ztex");
|
---|
797 | SizeProjection = glGetUniformLocation(pProjection, "res");
|
---|
798 | FirstProjection = glGetUniformLocation(pProjection, "first");
|
---|
799 |
|
---|
800 | glUseProgram(0);
|
---|
801 |
|
---|
802 |
|
---|
803 | //Reflect Shaders
|
---|
804 |
|
---|
805 | vReflect = glCreateShader(GL_VERTEX_SHADER);
|
---|
806 | fReflect = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
807 |
|
---|
808 | static const char *vvReflect =
|
---|
809 | " \n"
|
---|
810 | " varying vec4 color; \n"
|
---|
811 | " \n"
|
---|
812 | " void main() \n"
|
---|
813 | " { \n"
|
---|
814 | " vec4 posicio = vec4(gl_Vertex.xy, -0.5, 1.0); \n"
|
---|
815 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
816 | " \n"
|
---|
817 | " color = vec4(gl_Color.xyz,1.0); \n"
|
---|
818 | " } \n";
|
---|
819 |
|
---|
820 | static const char *ffReflect =
|
---|
821 | " \n"
|
---|
822 | " varying vec4 color; \n"
|
---|
823 | " \n"
|
---|
824 | " void main() \n"
|
---|
825 | " { \n"
|
---|
826 | " gl_FragColor = color; \n"
|
---|
827 | " } \n"
|
---|
828 | " \n";
|
---|
829 |
|
---|
830 | glShaderSource(vReflect, 1, &vvReflect, NULL);
|
---|
831 | glShaderSource(fReflect, 1, &ffReflect, NULL);
|
---|
832 |
|
---|
833 | glCompileShader(vReflect);
|
---|
834 | glCompileShader(fReflect);
|
---|
835 |
|
---|
836 | pReflect = glCreateProgram();
|
---|
837 | glAttachShader(pReflect, vReflect);
|
---|
838 | glAttachShader(pReflect, fReflect);
|
---|
839 |
|
---|
840 | glLinkProgram(pReflect);
|
---|
841 |
|
---|
842 | glUseProgram(0);
|
---|
843 |
|
---|
844 | //Texture Copy Shaders
|
---|
845 |
|
---|
846 | vCopy = glCreateShader(GL_VERTEX_SHADER);
|
---|
847 | fCopy = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
848 |
|
---|
849 | static const char *vvCopy =
|
---|
850 | " \n"
|
---|
851 | " varying vec2 texCoord; \n"
|
---|
852 | " \n"
|
---|
853 | " void main() \n"
|
---|
854 | " { \n"
|
---|
855 | " vec4 posicio = vec4(gl_Vertex.xyz, 1.0); \n"
|
---|
856 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
857 | " \n"
|
---|
858 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
859 | " } \n";
|
---|
860 |
|
---|
861 | static const char *ffCopy =
|
---|
862 | " \n"
|
---|
863 | " uniform sampler2D texture; \n"
|
---|
864 | " uniform sampler2D texture2; \n"
|
---|
865 | " \n"
|
---|
866 | " varying vec2 texCoord; \n"
|
---|
867 | " \n"
|
---|
868 | " void main() \n"
|
---|
869 | " { \n"
|
---|
870 | " gl_FragColor = tex2D(texture,texCoord) + tex2D(texture2,texCoord); \n"
|
---|
871 | " } \n";
|
---|
872 |
|
---|
873 | glShaderSource(vCopy, 1, &vvCopy, NULL);
|
---|
874 | glShaderSource(fCopy, 1, &ffCopy, NULL);
|
---|
875 |
|
---|
876 | glCompileShader(vCopy);
|
---|
877 | glCompileShader(fCopy);
|
---|
878 |
|
---|
879 | pCopy = glCreateProgram();
|
---|
880 | glAttachShader(pCopy, vCopy);
|
---|
881 | glAttachShader(pCopy, fCopy);
|
---|
882 |
|
---|
883 | glLinkProgram(pCopy);
|
---|
884 |
|
---|
885 | glUseProgram(pCopy);
|
---|
886 | TextureCopy = glGetUniformLocation(pCopy, "texture");
|
---|
887 | Texture2Copy = glGetUniformLocation(pCopy, "texture2");
|
---|
888 | glUseProgram(0);
|
---|
889 |
|
---|
890 | //Lightmap Normalization Shaders
|
---|
891 |
|
---|
892 | vNormalize = glCreateShader(GL_VERTEX_SHADER);
|
---|
893 | fNormalize = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
894 |
|
---|
895 | static const char *vvNormalize =
|
---|
896 | " \n"
|
---|
897 | " varying vec2 texCoord; \n"
|
---|
898 | " \n"
|
---|
899 | " void main() \n"
|
---|
900 | " { \n"
|
---|
901 | " vec4 posicio = vec4(gl_Vertex.xyz, 1.0); \n"
|
---|
902 | " gl_Position = gl_ModelViewProjectionMatrix * posicio; \n"
|
---|
903 | " \n"
|
---|
904 | " texCoord = gl_MultiTexCoord0.st; \n"
|
---|
905 | " } \n";
|
---|
906 |
|
---|
907 | static const char *ffNormalize =
|
---|
908 | " \n"
|
---|
909 | " uniform sampler2D texture; \n"
|
---|
910 | " \n"
|
---|
911 | " varying vec2 texCoord; \n"
|
---|
912 | " \n"
|
---|
913 | " void main() \n"
|
---|
914 | " { \n"
|
---|
915 | " float res = 256.0; \n"
|
---|
916 | " float dif = 1.0/res; \n"
|
---|
917 | " vec4 aux1 = tex2D(texture,texCoord); \n"
|
---|
918 | " vec4 aux2 = tex2D(texture,vec2(texCoord.r+dif,texCoord.g)); \n"
|
---|
919 | " vec4 aux3 = tex2D(texture,vec2(texCoord.r+dif,texCoord.g+dif)); \n"
|
---|
920 | " vec4 aux4 = tex2D(texture,vec2(texCoord.r,texCoord.g+dif)); \n"
|
---|
921 | " float counter = 0.0; \n"
|
---|
922 | " vec4 aux = vec4(0.0,0.0,0.0,0.0); \n"
|
---|
923 | " if(aux1.a>0.0) \n"
|
---|
924 | " { \n"
|
---|
925 | " aux += vec4(aux1.rgb/aux1.a,1.0); \n"
|
---|
926 | " counter += 1.0; \n"
|
---|
927 | " } \n"
|
---|
928 | " if(aux2.a>0.0) \n"
|
---|
929 | " { \n"
|
---|
930 | " aux += vec4(aux2.rgb/aux2.a,1.0); \n"
|
---|
931 | " counter += 1.0; \n"
|
---|
932 | " } \n"
|
---|
933 | " if(aux3.a>0.0) \n"
|
---|
934 | " { \n"
|
---|
935 | " aux += vec4(aux3.rgb/aux3.a,1.0); \n"
|
---|
936 | " counter += 1.0; \n"
|
---|
937 | " } \n"
|
---|
938 | " if(aux4.a>0.0) \n"
|
---|
939 | " { \n"
|
---|
940 | " aux += vec4(aux4.rgb/aux4.a,1.0); \n"
|
---|
941 | " counter += 1.0; \n"
|
---|
942 | " } \n"
|
---|
943 | " if(counter < 1.0) gl_FragColor = vec4(0.0,0.0,0.0,0.0); \n"
|
---|
944 | " else \n"
|
---|
945 | " { \n"
|
---|
946 | " aux /= counter; \n"
|
---|
947 | " gl_FragColor = aux; \n"
|
---|
948 | " } \n"
|
---|
949 | " } \n";
|
---|
950 |
|
---|
951 | glShaderSource(vNormalize, 1, &vvNormalize, NULL);
|
---|
952 | glShaderSource(fNormalize, 1, &ffNormalize, NULL);
|
---|
953 |
|
---|
954 | glCompileShader(vNormalize);
|
---|
955 | glCompileShader(fNormalize);
|
---|
956 |
|
---|
957 | pNormalize = glCreateProgram();
|
---|
958 | glAttachShader(pNormalize, vNormalize);
|
---|
959 | glAttachShader(pNormalize, fNormalize);
|
---|
960 |
|
---|
961 | glLinkProgram(pNormalize);
|
---|
962 |
|
---|
963 | glUseProgram(pNormalize);
|
---|
964 | TexturaNormalize = glGetUniformLocation(pNormalize, "texture");
|
---|
965 | glUseProgram(0);
|
---|
966 |
|
---|
967 | //Energy Transfer Shaders
|
---|
968 |
|
---|
969 | vTransfer = glCreateShader(GL_VERTEX_SHADER);
|
---|
970 | fTransfer = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
971 |
|
---|
972 | static const char *vvTransfer =
|
---|
973 | " \n"
|
---|
974 | " uniform float direction; \n"
|
---|
975 | " \n"
|
---|
976 | " varying vec2 p1; \n"
|
---|
977 | " varying vec2 p2; \n"
|
---|
978 | " varying float factor; \n"
|
---|
979 | " \n"
|
---|
980 | " void main() \n"
|
---|
981 | " { \n"
|
---|
982 | " vec4 position = gl_Vertex; \n"
|
---|
983 | " vec4 texCoord = gl_MultiTexCoord0; \n"
|
---|
984 | " if(((direction == 0) && (position.b != 1.0) && (position.b < 0.0) && ((texCoord.b > 0.0) || (texCoord.b == 1))) \n"
|
---|
985 | " || ((direction == 1) && (position.b != 1.0) && (position.b > 0.0) && ((texCoord.b < 0.0) || (texCoord.b == 1)))) \n"
|
---|
986 | " { \n"
|
---|
987 | " \n"
|
---|
988 | " p1 = vec2(position.r, position.g); \n"
|
---|
989 | " \n"
|
---|
990 | " p2 = vec2(texCoord.r, texCoord.g); \n"
|
---|
991 | " \n"
|
---|
992 | " gl_Position = vec4((p1 * 2.0) - vec2(1.0,1.0), 0.0,1.0); \n"
|
---|
993 | " \n"
|
---|
994 | " if (texCoord.b == 1) factor = 1; \n"
|
---|
995 | " else factor = abs(texCoord.a - position.a); \n"
|
---|
996 | " } \n"
|
---|
997 | " else \n"
|
---|
998 | " { \n"
|
---|
999 | " gl_Position = vec4( 0.0,0.0, 2.0, 1.0 ); \n"
|
---|
1000 | " p1 = p2 = vec2(1.0,1.0); \n"
|
---|
1001 | " factor = 0.5; \n"
|
---|
1002 | " } \n"
|
---|
1003 | " } \n";
|
---|
1004 |
|
---|
1005 | static const char *ffTransfer =
|
---|
1006 | " \n"
|
---|
1007 | " uniform sampler2D reflectivity; \n"
|
---|
1008 | " uniform float dmax; \n"
|
---|
1009 | " \n"
|
---|
1010 | " varying vec2 p1; \n"
|
---|
1011 | " varying vec2 p2; \n"
|
---|
1012 | " varying float factor; \n"
|
---|
1013 | " \n"
|
---|
1014 | " void main() \n"
|
---|
1015 | " { \n"
|
---|
1016 | " if(factor>=dmax) gl_FragColor.rgb = vec3(0.5,0.5,0.5); \n"
|
---|
1017 | " else gl_FragColor.rgb = tex2D(reflectivity,p2).rgb * sqrt(factor/dmax); \n"
|
---|
1018 | " gl_FragColor.a = 1.0; \n"
|
---|
1019 | " } \n";
|
---|
1020 |
|
---|
1021 | glShaderSource(vTransfer, 1, &vvTransfer, NULL);
|
---|
1022 | glShaderSource(fTransfer, 1, &ffTransfer, NULL);
|
---|
1023 |
|
---|
1024 | glCompileShader(vTransfer);
|
---|
1025 | glCompileShader(fTransfer);
|
---|
1026 |
|
---|
1027 | pTransfer = glCreateProgram();
|
---|
1028 | glAttachShader(pTransfer, vTransfer);
|
---|
1029 | glAttachShader(pTransfer, fTransfer);
|
---|
1030 |
|
---|
1031 | glLinkProgram(pTransfer);
|
---|
1032 |
|
---|
1033 | glUseProgram(pTransfer);
|
---|
1034 | TexturaReflectTransfer = glGetUniformLocation(pTransfer, "reflectivity");
|
---|
1035 | DirectionTransfer = glGetUniformLocation(pTransfer, "direction");
|
---|
1036 | DmaxTransfer = glGetUniformLocation(pTransfer, "dmax");
|
---|
1037 |
|
---|
1038 | glUseProgram(0);
|
---|
1039 |
|
---|
1040 | // Setup GL States
|
---|
1041 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
1042 |
|
---|
1043 | //Generate the reflectivity texture.
|
---|
1044 | RenderReflect();
|
---|
1045 |
|
---|
1046 | UpdateTexture(60,3);
|
---|
1047 |
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | // Function that interfaces with the VisualCAD.
|
---|
1051 | int vcObscurerGenerateImage(CMesh &geom, char *argv, int quality, int *imgUsize, int *imgVsize, float **imgRgbBuffer)
|
---|
1052 | {
|
---|
1053 | // float divisor;
|
---|
1054 | int *DadesTemp = new int[4];
|
---|
1055 | float *dadesf=NULL;
|
---|
1056 | int *dadesi=NULL;
|
---|
1057 | int qual;
|
---|
1058 | float aux[4];
|
---|
1059 | float a;
|
---|
1060 |
|
---|
1061 |
|
---|
1062 | glutCreateWindow("T");
|
---|
1063 |
|
---|
1064 | glerror = glewInit();
|
---|
1065 |
|
---|
1066 | FILE *file = NULL;
|
---|
1067 |
|
---|
1068 | file = fopen("tmp.txt", "w");
|
---|
1069 |
|
---|
1070 | if (glerror != GLEW_OK) fprintf(file, "\nError initializing glew library, %s", glewGetErrorString(glerror));
|
---|
1071 | else fprintf(file, "\nTot Correcte!\n");
|
---|
1072 |
|
---|
1073 | fclose(file);
|
---|
1074 |
|
---|
1075 | memset(&aux, 0.0, 4);
|
---|
1076 |
|
---|
1077 | qual = quality;
|
---|
1078 |
|
---|
1079 | if (qual < 1) qual = 1;
|
---|
1080 |
|
---|
1081 | switch (qual)
|
---|
1082 | {
|
---|
1083 | case 1: *imgVsize = *imgUsize = 256;
|
---|
1084 | break;
|
---|
1085 | case 2: *imgVsize = *imgUsize = 512;
|
---|
1086 | break;
|
---|
1087 | case 3: *imgVsize = *imgUsize = 1024;
|
---|
1088 | break;
|
---|
1089 | case 4: *imgVsize = *imgUsize = 2048;
|
---|
1090 | break;
|
---|
1091 | default: printf("\nResolution not supported");
|
---|
1092 | };
|
---|
1093 |
|
---|
1094 | Res2X = *imgUsize;
|
---|
1095 | Res2Y = *imgVsize;
|
---|
1096 |
|
---|
1097 | ResX = Res2X * 2.0;
|
---|
1098 | ResY = Res2Y * 2.0;
|
---|
1099 |
|
---|
1100 | if(ResX == 4096) ResX = 2048;
|
---|
1101 | if(ResY == 4096) ResY = 2048;
|
---|
1102 |
|
---|
1103 | printf("\nRes(Plans de projeccio): ResolucioX = %d , ResolucioY = %d", ResX, ResY);
|
---|
1104 | printf("\nRes2(Lightmap): ResolucioX = %d , ResolucioY = %d", Res2X, Res2Y);
|
---|
1105 | dadesf = new float[Res2X*Res2Y*4];
|
---|
1106 | dadesi = new int[Res2X*Res2Y*4];
|
---|
1107 |
|
---|
1108 | init2(argv);
|
---|
1109 |
|
---|
1110 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb5);
|
---|
1111 | glReadPixels(0,0,Res2X,Res2Y,GL_RGBA,GL_FLOAT,dadesf);
|
---|
1112 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
1113 |
|
---|
1114 | *imgRgbBuffer = dadesf;
|
---|
1115 |
|
---|
1116 | geom = g_pMesh;
|
---|
1117 |
|
---|
1118 | return 0;
|
---|
1119 |
|
---|
1120 | } |
---|