[930] | 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 "textfile.h"
|
---|
| 14 |
|
---|
| 15 | // Includes
|
---|
| 16 | #include "CMesh.h"
|
---|
| 17 | #include "vcObscuranceMap.h"
|
---|
| 18 | #include <imdebug.h>
|
---|
| 19 | #include <imdebuggl.h>
|
---|
| 20 |
|
---|
| 21 | #ifndef M_PI
|
---|
| 22 | #define M_PI 3.14159265358979323846
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
| 25 | // DepthPeeling Layers.
|
---|
| 26 | #define MAX_LAYERS 12
|
---|
| 27 |
|
---|
| 28 | // To use Offsets in the PBOs.
|
---|
| 29 | #define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
---|
| 30 |
|
---|
| 31 | //GLSL
|
---|
| 32 | // Handlers
|
---|
| 33 | GLuint vProjection,fProjection,pProjection;
|
---|
| 34 | GLuint vReflect, fReflect, pReflect;
|
---|
| 35 | GLuint vCopy, fCopy, pCopy;
|
---|
| 36 | GLuint vTransfer, fTransfer, pTransfer;
|
---|
| 37 |
|
---|
| 38 | //Uniform Parameters
|
---|
| 39 | //Projection Step
|
---|
| 40 | GLint TexturaDepthProjection;
|
---|
| 41 | GLint SizeProjection;
|
---|
| 42 | GLint FirstProjection;
|
---|
| 43 |
|
---|
| 44 | //Reflectivity texture generation Step.
|
---|
| 45 |
|
---|
| 46 | //Texture Copy Step.
|
---|
| 47 | GLint TextureCopy;
|
---|
| 48 | GLint Texture2Copy;
|
---|
| 49 |
|
---|
| 50 | //Energy Transfer Step.
|
---|
| 51 | GLint TexturaReflectTransfer;
|
---|
| 52 | GLint DirectionTransfer;
|
---|
| 53 | GLint DmaxTransfer;
|
---|
| 54 |
|
---|
| 55 | // Global variables
|
---|
| 56 | CVert Vertex, Direccio, Increment, Primer, Ultim, Origin;
|
---|
| 57 |
|
---|
| 58 | static GLuint texName[2];
|
---|
| 59 |
|
---|
| 60 | static int ResX; //Resolution
|
---|
| 61 | static int ResY;
|
---|
| 62 | static int Res2X; //Resolution
|
---|
| 63 | static int Res2Y; //Resolution
|
---|
| 64 |
|
---|
| 65 | static GLuint texProjeccions[2]; //Texture name
|
---|
| 66 | static GLuint texTransfer[1];
|
---|
| 67 | static GLuint texReflect[1];
|
---|
| 68 | static GLuint texCopy[1];
|
---|
| 69 | static GLuint fb1; // Framebuffer Reflectivity
|
---|
| 70 | static GLuint fb2; // Framebuffer Projeccions
|
---|
| 71 | static GLuint fb3; // Framebuffer Transfer
|
---|
| 72 | static GLuint fb4; // Framebuffer Copy
|
---|
| 73 |
|
---|
| 74 | GLenum glerror;
|
---|
| 75 |
|
---|
| 76 | GLfloat *m_pTextureImage;// Texture of diferent colors.
|
---|
| 77 | GLfloat *m_pTextureReflect; //Texture of reflectivitat.
|
---|
| 78 | GLfloat *m_pBlau;
|
---|
| 79 | GLfloat *m_pNegre;
|
---|
| 80 | int obs_global_cancelar;
|
---|
| 81 |
|
---|
| 82 | CMesh g_pMesh; // Mesh Data
|
---|
| 83 | static CSphere Esfera;
|
---|
| 84 |
|
---|
| 85 | static GLuint ProjectionsPBO[MAX_LAYERS+2]; // PBOs to store the projections.
|
---|
| 86 | static GLuint ObscurancesPBO; // PBOs to store the projections.
|
---|
| 87 | static GLuint ztex; //Texture with the depth.
|
---|
| 88 |
|
---|
| 89 | // Function to use when the window is resized
|
---|
| 90 | void changeSize(int w, int h)
|
---|
| 91 | {
|
---|
| 92 |
|
---|
| 93 | if(h == 0)
|
---|
| 94 | h = 1;
|
---|
| 95 |
|
---|
| 96 | float ratio = 1.0 * (float)w / (float)h;
|
---|
| 97 |
|
---|
| 98 | //Camera Set up
|
---|
| 99 | glMatrixMode(GL_PROJECTION);
|
---|
| 100 | glLoadIdentity();
|
---|
| 101 | gluOrtho2D(-w,w,-h,h);
|
---|
| 102 |
|
---|
| 103 | glMatrixMode(GL_MODELVIEW);
|
---|
| 104 | glLoadIdentity();
|
---|
| 105 |
|
---|
| 106 | glViewport(0, 0, w, h);
|
---|
| 107 |
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | // Returns a random point in the surface of the sphere
|
---|
| 112 | CVert ranpoint(float r, CVert c)
|
---|
| 113 | {
|
---|
| 114 | double ale1, ale2;
|
---|
| 115 | CVert p;
|
---|
| 116 | float alfa,beta;
|
---|
| 117 |
|
---|
| 118 | ale1= (float)rand()/(float)RAND_MAX;
|
---|
| 119 | ale2= (float)rand()/(float)RAND_MAX;
|
---|
| 120 |
|
---|
| 121 | alfa=1.0-2.0*ale1; /* real between -1 and 1 */
|
---|
| 122 | alfa=acos(alfa); /* angle between 0 and PI */
|
---|
| 123 |
|
---|
| 124 | beta=2*M_PI*ale2; /* angle between 0 and 2*PI */
|
---|
| 125 |
|
---|
| 126 | p.x=c.x+r*sin(alfa)*cos(beta);
|
---|
| 127 | p.y=c.y+r*sin(alfa)*sin(beta);
|
---|
| 128 | p.z=c.z+r*cos(alfa);
|
---|
| 129 |
|
---|
| 130 | return p;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // Function to find a point in the sphere in the direction that goes from the point in the surface of the sphere
|
---|
| 134 | // to the center of the sphere. First it gets the random point in the surface.
|
---|
| 135 | void BuscarDireccio()
|
---|
| 136 | {
|
---|
| 137 | Esfera.center.x = 0.0;
|
---|
| 138 | Esfera.center.y = 0.0;
|
---|
| 139 | Esfera.center.z = 0.0;
|
---|
| 140 | Esfera.radius = sqrt(12.0)/2.0;
|
---|
| 141 |
|
---|
| 142 | Primer = ranpoint(Esfera.radius,Esfera.center);
|
---|
| 143 | Vertex.x = Primer.x + (Esfera.center.x-Primer.x) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
| 144 | Vertex.y = Primer.y + (Esfera.center.y-Primer.y) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
| 145 | Vertex.z = Primer.z + (Esfera.center.z-Primer.z) * ((float)rand()/(float)RAND_MAX)*2.0;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | // Function that renders the depth peeling geometry planes and stores them in PBOs.
|
---|
| 149 | void RenderProjeccions(int buffer, int first)
|
---|
| 150 | {
|
---|
| 151 | int i, j, e;
|
---|
| 152 |
|
---|
| 153 | glUseProgram(pProjection);
|
---|
| 154 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
| 155 |
|
---|
| 156 | changeSize(ResX, ResY);
|
---|
| 157 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
| 158 | glDepthFunc(GL_LESS);
|
---|
| 159 |
|
---|
| 160 | glDisable(GL_CULL_FACE);
|
---|
| 161 |
|
---|
| 162 | glMatrixMode(GL_PROJECTION);
|
---|
| 163 | glLoadIdentity();
|
---|
| 164 | glOrtho(-Esfera.radius,Esfera.radius,-Esfera.radius,Esfera.radius,0,Esfera.radius * 2.0);
|
---|
| 165 |
|
---|
| 166 | glMatrixMode(GL_MODELVIEW);
|
---|
| 167 | glLoadIdentity();
|
---|
| 168 | gluLookAt(Primer.x,Primer.y,Primer.z,
|
---|
| 169 | Esfera.center.x,Esfera.center.y,Esfera.center.z,
|
---|
| 170 | 0.0f,1.0f,0.0f);
|
---|
| 171 |
|
---|
| 172 | glViewport(0,0,ResX,ResY);
|
---|
| 173 |
|
---|
| 174 | glUniform1f(SizeProjection, (float)ResX);
|
---|
| 175 | glUniform1f(FirstProjection, (float)first);
|
---|
| 176 |
|
---|
| 177 | int other = ((buffer == 1)? 0:1);
|
---|
| 178 | if(first == 0)
|
---|
| 179 | {
|
---|
| 180 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[other] ); // Bind The Buffer
|
---|
| 181 | glBindTexture(GL_TEXTURE_2D,texName[0]);
|
---|
| 182 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ResX, ResY);
|
---|
| 183 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
| 184 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 185 |
|
---|
| 186 | glActiveTexture(GL_TEXTURE0);
|
---|
| 187 | glBindTexture(GL_TEXTURE_2D, texName[0]);
|
---|
| 188 | glUniform1i(TexturaDepthProjection, 0);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | // Enable Pointers
|
---|
| 192 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 193 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 194 | glEnableClientState( GL_NORMAL_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 195 |
|
---|
| 196 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
| 197 | {
|
---|
| 198 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 199 | {
|
---|
| 200 | // Set Pointers To Our Data
|
---|
| 201 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
| 202 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 203 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
| 204 | glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 205 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals );
|
---|
| 206 | glNormalPointer( GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 207 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
| 208 |
|
---|
| 209 | // Render
|
---|
| 210 | 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
|
---|
| 211 |
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
| 216 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
| 217 | glDisableClientState( GL_NORMAL_ARRAY ); // Disable Texture Coord Arrays
|
---|
| 218 |
|
---|
| 219 | //Copy the color buffer to PBO.
|
---|
| 220 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[buffer] ); // Bind The Buffer
|
---|
| 221 | glReadPixels(0, 0, ResX, ResY, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
| 222 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
| 223 |
|
---|
| 224 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 225 | glUseProgram(0);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | // Function that calculates the lightmap using the neighbouring projections.
|
---|
| 229 | void RenderTransfer(int buffer, int first)
|
---|
| 230 | {
|
---|
| 231 | glUseProgram(pTransfer);
|
---|
| 232 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
| 233 |
|
---|
| 234 | int other = ((buffer == 1)? 0:1);
|
---|
| 235 |
|
---|
| 236 | glBlendFunc(GL_ONE, GL_ONE);
|
---|
| 237 |
|
---|
| 238 | glEnable(GL_BLEND);
|
---|
| 239 |
|
---|
| 240 | glMatrixMode(GL_PROJECTION);
|
---|
| 241 | glLoadIdentity();
|
---|
| 242 | gluOrtho2D(-1,1,-1,1);
|
---|
| 243 |
|
---|
| 244 | glMatrixMode(GL_MODELVIEW);
|
---|
| 245 | glLoadIdentity();
|
---|
| 246 | glPointSize(1.0);
|
---|
| 247 |
|
---|
| 248 | glViewport(0, 0, Res2X, Res2Y);
|
---|
| 249 |
|
---|
| 250 | glUniform1f(DmaxTransfer, (GLfloat)0.3);
|
---|
| 251 | glActiveTexture(GL_TEXTURE0);
|
---|
| 252 | glBindTexture(GL_TEXTURE_2D, texReflect[0]);
|
---|
| 253 | glUniform1i(TexturaReflectTransfer, 0);
|
---|
| 254 |
|
---|
| 255 | if(first == 1)
|
---|
| 256 | {
|
---|
| 257 | //First Render
|
---|
| 258 | glUniform1f(DirectionTransfer, (float)buffer);
|
---|
| 259 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 260 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
| 261 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 262 |
|
---|
| 263 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 264 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
| 265 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 266 |
|
---|
| 267 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
| 268 | }
|
---|
| 269 | else
|
---|
| 270 | {
|
---|
| 271 | glUniform1f(DirectionTransfer, 0.0);
|
---|
| 272 |
|
---|
| 273 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 274 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
| 275 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 276 |
|
---|
| 277 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 278 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
| 279 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 280 |
|
---|
| 281 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
| 282 |
|
---|
| 283 | glUniform1f(DirectionTransfer, 1.0);
|
---|
| 284 |
|
---|
| 285 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[other] );
|
---|
| 286 | glVertexPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 287 |
|
---|
| 288 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, ProjectionsPBO[buffer] );
|
---|
| 289 | glTexCoordPointer( 4, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 290 |
|
---|
| 291 | glDrawArrays(GL_POINTS, 0, ResX*ResY );
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
| 295 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
| 296 |
|
---|
| 297 | glDisable(GL_BLEND);
|
---|
| 298 | glUseProgram(0);
|
---|
| 299 |
|
---|
| 300 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 301 |
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | // Function that copies the 16-bit fp RGBA buffer of the lightmap to a 32-bit RGBA fb buffer.
|
---|
| 305 | void CopyTexture(void)
|
---|
| 306 | {
|
---|
| 307 | glUseProgram(pCopy);
|
---|
| 308 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
| 309 | void *texturePBO;
|
---|
| 310 |
|
---|
| 311 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
| 312 | texturePBO = glMapBuffer(GL_PIXEL_PACK_BUFFER_EXT, GL_READ_ONLY);
|
---|
| 313 | glBindTexture(GL_TEXTURE_2D,texName[1]);
|
---|
| 314 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, texturePBO);
|
---|
| 315 | glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
|
---|
| 316 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
| 317 |
|
---|
| 318 | glEnable(GL_CULL_FACE);
|
---|
| 319 |
|
---|
| 320 | glMatrixMode(GL_PROJECTION);
|
---|
| 321 | glLoadIdentity();
|
---|
| 322 | gluOrtho2D(-Res2X,Res2Y,-Res2X,Res2Y);
|
---|
| 323 |
|
---|
| 324 | glMatrixMode(GL_MODELVIEW);
|
---|
| 325 | glLoadIdentity();
|
---|
| 326 |
|
---|
| 327 | glViewport(0, 0, Res2X, Res2Y);
|
---|
| 328 |
|
---|
| 329 | glActiveTexture(GL_TEXTURE0);
|
---|
| 330 | glBindTexture(GL_TEXTURE_2D, texTransfer[0]);
|
---|
| 331 | glUniform1i(TextureCopy, 0);
|
---|
| 332 | glActiveTexture(GL_TEXTURE1);
|
---|
| 333 | glBindTexture(GL_TEXTURE_2D, texName[1]);
|
---|
| 334 | glUniform1i(Texture2Copy, 1);
|
---|
| 335 | glActiveTexture(GL_TEXTURE0);
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | //Draw the geometry.
|
---|
| 339 | glBegin(GL_QUADS);
|
---|
| 340 | {
|
---|
| 341 | glTexCoord2f(0, 0); glVertex3f(-Res2X, -Res2Y, -0.5f);
|
---|
| 342 | glTexCoord2f(1,0); glVertex3f(Res2X, -Res2Y, -0.5f);
|
---|
| 343 | glTexCoord2f(1,1); glVertex3f(Res2X, Res2Y, -0.5f);
|
---|
| 344 | glTexCoord2f(0, 1); glVertex3f(-Res2X, Res2Y, -0.5f);
|
---|
| 345 | }
|
---|
| 346 | glEnd();
|
---|
| 347 |
|
---|
| 348 | //Copy Buffer to PBO.
|
---|
| 349 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT,ObscurancesPBO ); // Bind The Buffer
|
---|
| 350 | glReadPixels(0, 0, Res2X, Res2Y, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));
|
---|
| 351 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
|
---|
| 352 |
|
---|
| 353 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
| 354 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
| 355 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
| 356 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 357 | glUseProgram(0);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | // Function that calculates the reflectivity map from the scene data.
|
---|
| 361 | void RenderReflect(void)
|
---|
| 362 | {
|
---|
| 363 | int i, j;
|
---|
| 364 | glUseProgram(pReflect);
|
---|
| 365 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
| 366 |
|
---|
| 367 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
| 368 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
| 369 |
|
---|
| 370 | glMatrixMode(GL_PROJECTION);
|
---|
| 371 | glLoadIdentity();
|
---|
| 372 | gluOrtho2D(0,1,0,1);
|
---|
| 373 |
|
---|
| 374 | glMatrixMode(GL_MODELVIEW);
|
---|
| 375 | glLoadIdentity();
|
---|
| 376 |
|
---|
| 377 | glViewport(0, 0, Res2X, Res2Y);
|
---|
| 378 |
|
---|
| 379 | // Enable Pointers
|
---|
| 380 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 381 | glEnableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 382 |
|
---|
| 383 | for(i=0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
| 384 | {
|
---|
| 385 | for(j=0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 386 | {
|
---|
| 387 | // Set Pointers To Our Data
|
---|
| 388 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
| 389 | glVertexPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 390 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors );
|
---|
| 391 | glColorPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 392 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
| 393 |
|
---|
| 394 | // Render
|
---|
| 395 | 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
|
---|
| 396 |
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | glDisableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 401 | glDisableClientState( GL_COLOR_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 402 |
|
---|
| 403 | glUseProgram(0);
|
---|
| 404 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | // Function that updates the lightmap.
|
---|
| 408 | // The number of directions used to calculate the lightmap is iterations * steps
|
---|
| 409 | void UpdateTexture(int iterations, int steps)
|
---|
| 410 | {
|
---|
| 411 | printf("\nIteracions: %d => Steps: %d\n", iterations, steps);
|
---|
| 412 | for(int i = 0; i < steps; i++)
|
---|
| 413 | {
|
---|
| 414 | for(int j = 0; j < iterations; j++)
|
---|
| 415 | {
|
---|
| 416 | printf("\rProgress: %d%%",i*100/steps + j*100/(steps*iterations));
|
---|
| 417 | BuscarDireccio(); // Find a random direction
|
---|
| 418 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
| 419 | glClearColor(0.0,0.0,1.0,1.0);
|
---|
| 420 | glClearDepth(1.0);
|
---|
| 421 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 422 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 423 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[0] ); // Bind The Buffer
|
---|
| 424 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
| 425 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
|
---|
| 426 | RenderProjeccions(1,1);
|
---|
| 427 | RenderTransfer(1, 1);
|
---|
| 428 | int l = 0;
|
---|
| 429 | for(int k=1; k < MAX_LAYERS; k++)
|
---|
| 430 | {
|
---|
| 431 | RenderProjeccions(l,0);
|
---|
| 432 | l = (l == 0)? 1:0;
|
---|
| 433 | RenderTransfer(l,0);
|
---|
| 434 | }
|
---|
| 435 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[l] ); // Bind The Buffer
|
---|
| 436 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
| 437 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
|
---|
| 438 | l = (l = 0)? 1:0;
|
---|
| 439 | RenderTransfer(l,1);
|
---|
| 440 | }
|
---|
| 441 | CopyTexture();
|
---|
| 442 | }
|
---|
| 443 | printf("\r100%% Done!!!!");
|
---|
| 444 |
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | // Function that generates the VBOs with the geometric data of the scene.
|
---|
| 448 | // It also initializes the constant textures used.
|
---|
| 449 | void InitGeometry(void)
|
---|
| 450 | {
|
---|
| 451 | int i, j;
|
---|
| 452 | m_pBlau = new float[ResX * ResY * 4];
|
---|
| 453 | //Initialize a blue buffer.
|
---|
| 454 | for(i = 0; i<ResX; i++)
|
---|
| 455 | {
|
---|
| 456 | for(j = 0; j<ResY; j++)
|
---|
| 457 | {
|
---|
| 458 | m_pBlau[4 * (i * ResY + j)] = 0.0;
|
---|
| 459 | m_pBlau[4 * (i * ResY + j) + 1] = 0.0;
|
---|
| 460 | m_pBlau[4 * (i * ResY + j) + 2] = 1.0;
|
---|
| 461 | m_pBlau[4 * (i * ResY + j) + 3] = 1.0;
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | //Initialize a black buffer
|
---|
| 466 | for(i = 0; i<Res2X; i++)
|
---|
| 467 | {
|
---|
| 468 | for(j = 0; j<Res2Y; j++)
|
---|
| 469 | {
|
---|
| 470 | m_pNegre[4*(i*Res2Y+j)] = 0.0;
|
---|
| 471 | m_pNegre[4*(i*Res2Y+j)+1] = 0.0;
|
---|
| 472 | m_pNegre[4*(i*Res2Y+j)+2] = 0.0;
|
---|
| 473 | m_pNegre[4*(i*Res2Y+j)+3] = 0.0;
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 |
|
---|
| 478 | for(i = 0; i<g_pMesh.m_pObject._numGeos;i++)
|
---|
| 479 | {
|
---|
| 480 | for(j = 0; j<g_pMesh.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 481 | {
|
---|
| 482 | //Aqui creeem els VBOs.
|
---|
| 483 |
|
---|
| 484 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs = 0;
|
---|
| 485 | // Generate And Bind The Index Buffer
|
---|
| 486 | glGenBuffersARB( 1, (unsigned int *)&g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Get A Valid Name
|
---|
| 487 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Bind The Buffer
|
---|
| 488 | // Load The Data
|
---|
| 489 | 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 );
|
---|
| 490 |
|
---|
| 491 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices = 0;
|
---|
| 492 | // Generate And Bind The Vertex Buffer
|
---|
| 493 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Get A Valid Name
|
---|
| 494 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Bind The Buffer
|
---|
| 495 | // Load The Data
|
---|
| 496 | 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 );
|
---|
| 497 |
|
---|
| 498 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals = 0;
|
---|
| 499 | // Generate And Bind The Normal Buffer
|
---|
| 500 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Get A Valid Name
|
---|
| 501 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Bind The Buffer
|
---|
| 502 | // Load The Data
|
---|
| 503 | 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 );
|
---|
| 504 |
|
---|
| 505 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords = 0;
|
---|
| 506 | // Generate And Bind The TexCoord Buffer
|
---|
| 507 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Get A Valid Name
|
---|
| 508 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Bind The Buffer
|
---|
| 509 | // Load The Data
|
---|
| 510 | 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 );
|
---|
| 511 |
|
---|
| 512 | g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors = 0;
|
---|
| 513 | // Generate And Bind The Color Buffer
|
---|
| 514 | glGenBuffersARB( 1, &g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Get A Valid Name
|
---|
| 515 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_pMesh.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Bind The Buffer
|
---|
| 516 | // Load The Data
|
---|
| 517 | //*12
|
---|
| 518 | 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 );
|
---|
| 519 | }
|
---|
| 520 | }
|
---|
| 521 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); // Bind The Buffer
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | // Functions that initializes the RenderTextures, the shaders and so on...
|
---|
| 525 | void init2(char *argv)
|
---|
| 526 | {
|
---|
| 527 |
|
---|
| 528 | g_pMesh.LoadMesh(argv);
|
---|
| 529 |
|
---|
| 530 | m_pTextureImage = new float[Res2X*Res2Y*4];// Texture of diferent colors.
|
---|
| 531 | m_pTextureReflect = new float[Res2X*Res2Y*4]; //Texture of reflectivitat.
|
---|
| 532 | m_pNegre = new float[Res2X*Res2Y*4];
|
---|
| 533 |
|
---|
| 534 | //Create Textures.
|
---|
| 535 | glGenTextures(2, &texName[0]); // Create The Textures
|
---|
| 536 |
|
---|
| 537 | InitGeometry();
|
---|
| 538 |
|
---|
| 539 | //Texture
|
---|
| 540 | // Binding
|
---|
| 541 | glBindTexture( GL_TEXTURE_2D, texName[0] );
|
---|
| 542 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
| 543 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 544 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 545 |
|
---|
| 546 | //Texture
|
---|
| 547 | // Binding
|
---|
| 548 | glBindTexture( GL_TEXTURE_2D, texName[1] );
|
---|
| 549 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
| 550 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 551 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 552 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 553 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 554 |
|
---|
| 555 | // Setup GL States
|
---|
| 556 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
| 557 |
|
---|
| 558 | // Setup GL States
|
---|
| 559 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
| 560 |
|
---|
| 561 | //FBOs
|
---|
| 562 | glGenFramebuffersEXT(1, &fb1);
|
---|
| 563 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb1);
|
---|
| 564 |
|
---|
| 565 | glGenTextures(1,&texReflect[0]);
|
---|
| 566 |
|
---|
| 567 | glBindTexture( GL_TEXTURE_2D, texReflect[0] );
|
---|
| 568 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT16_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
| 569 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 570 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 571 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 572 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 573 |
|
---|
| 574 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texReflect[0],0);
|
---|
| 575 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
| 576 |
|
---|
| 577 | glGenFramebuffersEXT(1, &fb2);
|
---|
| 578 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb2);
|
---|
| 579 |
|
---|
| 580 | glGenTextures(1,&texProjeccions[0]);
|
---|
| 581 |
|
---|
| 582 | glBindTexture( GL_TEXTURE_2D, texProjeccions[0] );
|
---|
| 583 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, ResX, ResY, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
| 584 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 585 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 586 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 587 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 588 |
|
---|
| 589 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texProjeccions[0],0);
|
---|
| 590 |
|
---|
| 591 | glGenRenderbuffersEXT(1, &texProjeccions[1]); |
---|
| 592 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, texProjeccions[1]); |
---|
| 593 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, ResX, ResY ); |
---|
| 594 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, texProjeccions[1]);
|
---|
| 595 |
|
---|
| 596 | glGenFramebuffersEXT(1, &fb3);
|
---|
| 597 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb3);
|
---|
| 598 |
|
---|
| 599 | glGenTextures(1,&texTransfer[0]);
|
---|
| 600 |
|
---|
| 601 | glBindTexture( GL_TEXTURE_2D, texTransfer[0] );
|
---|
| 602 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT16_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
| 603 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 604 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 605 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 606 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 607 |
|
---|
| 608 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texTransfer[0],0);
|
---|
| 609 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
| 610 |
|
---|
| 611 | glClearColor(0.0,0.0,0.0,0.0);
|
---|
| 612 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
| 613 |
|
---|
| 614 | glGenFramebuffersEXT(1, &fb4);
|
---|
| 615 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
| 616 |
|
---|
| 617 | glGenTextures(1,&texCopy[0]);
|
---|
| 618 |
|
---|
| 619 | glBindTexture( GL_TEXTURE_2D, texCopy[0] );
|
---|
| 620 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, Res2X, Res2Y, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
| 621 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 622 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 623 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 624 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 625 |
|
---|
| 626 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texCopy[0],0);
|
---|
| 627 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
|
---|
| 628 |
|
---|
| 629 | //Create PBO
|
---|
| 630 | glGenBuffersARB( 2, &ProjectionsPBO[0] );
|
---|
| 631 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[0] ); // Bind The Buffer
|
---|
| 632 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), m_pBlau, GL_STREAM_COPY );
|
---|
| 633 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ProjectionsPBO[1] ); // Bind The Buffer
|
---|
| 634 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, ResX*ResY*4*sizeof(float), NULL, GL_STREAM_COPY );
|
---|
| 635 |
|
---|
| 636 |
|
---|
| 637 | glGenBuffersARB( 1, &ObscurancesPBO );
|
---|
| 638 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, ObscurancesPBO ); // Bind The Buffer
|
---|
| 639 | glBufferDataARB( GL_PIXEL_PACK_BUFFER_EXT, Res2X*Res2Y*4*sizeof(float), m_pNegre, GL_STREAM_COPY );
|
---|
| 640 | glBindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 ); // Bind The Buffer
|
---|
| 641 |
|
---|
| 642 | //GLSL
|
---|
| 643 | //Projection Shaders
|
---|
| 644 | char *vsProjection = NULL, *fsProjection = NULL;
|
---|
| 645 |
|
---|
| 646 | vProjection = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 647 | fProjection = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 648 |
|
---|
| 649 | vsProjection = textFileRead("projection.vert");
|
---|
| 650 | fsProjection = textFileRead("projection.frag");
|
---|
| 651 |
|
---|
| 652 | const char *vvProjection = vsProjection;
|
---|
| 653 | const char *ffProjection = fsProjection;
|
---|
| 654 |
|
---|
| 655 | glShaderSource(vProjection, 1, &vvProjection, NULL);
|
---|
| 656 | glShaderSource(fProjection, 1, &ffProjection, NULL);
|
---|
| 657 |
|
---|
| 658 | free(vsProjection); free(fsProjection);
|
---|
| 659 |
|
---|
| 660 | glCompileShader(vProjection);
|
---|
| 661 | glCompileShader(fProjection);
|
---|
| 662 |
|
---|
| 663 | pProjection = glCreateProgram();
|
---|
| 664 | glAttachShader(pProjection, vProjection);
|
---|
| 665 | glAttachShader(pProjection, fProjection);
|
---|
| 666 |
|
---|
| 667 | glLinkProgram(pProjection);
|
---|
| 668 |
|
---|
| 669 | glUseProgram(pProjection);
|
---|
| 670 | TexturaDepthProjection = glGetUniformLocation(pProjection, "ztex");
|
---|
| 671 | SizeProjection = glGetUniformLocation(pProjection, "res");
|
---|
| 672 | FirstProjection = glGetUniformLocation(pProjection, "first");
|
---|
| 673 |
|
---|
| 674 | glUseProgram(0);
|
---|
| 675 |
|
---|
| 676 |
|
---|
| 677 | //Reflect Shaders
|
---|
| 678 | char *vsReflect = NULL, *fsReflect = NULL;
|
---|
| 679 |
|
---|
| 680 | vReflect = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 681 | fReflect = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 682 |
|
---|
| 683 | vsReflect = textFileRead("reflect.vert");
|
---|
| 684 | fsReflect = textFileRead("reflect.frag");
|
---|
| 685 |
|
---|
| 686 | const char *vvReflect = vsReflect;
|
---|
| 687 | const char *ffReflect = fsReflect;
|
---|
| 688 |
|
---|
| 689 | glShaderSource(vReflect, 1, &vvReflect, NULL);
|
---|
| 690 | glShaderSource(fReflect, 1, &ffReflect, NULL);
|
---|
| 691 |
|
---|
| 692 | free(vsReflect); free(fsReflect);
|
---|
| 693 |
|
---|
| 694 | glCompileShader(vReflect);
|
---|
| 695 | glCompileShader(fReflect);
|
---|
| 696 |
|
---|
| 697 | pReflect = glCreateProgram();
|
---|
| 698 | glAttachShader(pReflect, vReflect);
|
---|
| 699 | glAttachShader(pReflect, fReflect);
|
---|
| 700 |
|
---|
| 701 | glLinkProgram(pReflect);
|
---|
| 702 |
|
---|
| 703 | // glUseProgram(pReflect);
|
---|
| 704 | glUseProgram(0);
|
---|
| 705 |
|
---|
| 706 | //Texture Copy Shaders
|
---|
| 707 | char *vsCopy = NULL, *fsCopy = NULL;
|
---|
| 708 |
|
---|
| 709 | vCopy = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 710 | fCopy = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 711 |
|
---|
| 712 | vsCopy = textFileRead("copy.vert");
|
---|
| 713 | fsCopy = textFileRead("copy.frag");
|
---|
| 714 |
|
---|
| 715 | const char *vvCopy = vsCopy;
|
---|
| 716 | const char *ffCopy = fsCopy;
|
---|
| 717 |
|
---|
| 718 | glShaderSource(vCopy, 1, &vvCopy, NULL);
|
---|
| 719 | glShaderSource(fCopy, 1, &ffCopy, NULL);
|
---|
| 720 |
|
---|
| 721 | free(vsCopy); free(fsCopy);
|
---|
| 722 |
|
---|
| 723 | glCompileShader(vCopy);
|
---|
| 724 | glCompileShader(fCopy);
|
---|
| 725 |
|
---|
| 726 | pCopy = glCreateProgram();
|
---|
| 727 | glAttachShader(pCopy, vCopy);
|
---|
| 728 | glAttachShader(pCopy, fCopy);
|
---|
| 729 |
|
---|
| 730 | glLinkProgram(pCopy);
|
---|
| 731 |
|
---|
| 732 | glUseProgram(pCopy);
|
---|
| 733 | TextureCopy = glGetUniformLocation(pCopy, "texture");
|
---|
| 734 | Texture2Copy = glGetUniformLocation(pCopy, "texture2");
|
---|
| 735 | glUseProgram(0);
|
---|
| 736 |
|
---|
| 737 | //Energy Transfer Shaders
|
---|
| 738 | char *vsTransfer = NULL, *fsTransfer = NULL;
|
---|
| 739 |
|
---|
| 740 | vTransfer = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 741 | fTransfer = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 742 |
|
---|
| 743 | vsTransfer = textFileRead("transfer.vert");
|
---|
| 744 | fsTransfer = textFileRead("transfer.frag");
|
---|
| 745 |
|
---|
| 746 | const char *vvTransfer = vsTransfer;
|
---|
| 747 | const char *ffTransfer = fsTransfer;
|
---|
| 748 |
|
---|
| 749 | glShaderSource(vTransfer, 1, &vvTransfer, NULL);
|
---|
| 750 | glShaderSource(fTransfer, 1, &ffTransfer, NULL);
|
---|
| 751 |
|
---|
| 752 | free(vsTransfer); free(fsTransfer);
|
---|
| 753 |
|
---|
| 754 | glCompileShader(vTransfer);
|
---|
| 755 | glCompileShader(fTransfer);
|
---|
| 756 |
|
---|
| 757 | pTransfer = glCreateProgram();
|
---|
| 758 | glAttachShader(pTransfer, vTransfer);
|
---|
| 759 | glAttachShader(pTransfer, fTransfer);
|
---|
| 760 |
|
---|
| 761 | glLinkProgram(pTransfer);
|
---|
| 762 |
|
---|
| 763 | glUseProgram(pTransfer);
|
---|
| 764 | TexturaReflectTransfer = glGetUniformLocation(pTransfer, "reflectivity");
|
---|
| 765 | DirectionTransfer = glGetUniformLocation(pTransfer, "direction");
|
---|
| 766 | DmaxTransfer = glGetUniformLocation(pTransfer, "dmax");
|
---|
| 767 |
|
---|
| 768 | glUseProgram(0);
|
---|
| 769 | //Generate the reflectivity texture.
|
---|
| 770 |
|
---|
| 771 | RenderReflect();
|
---|
| 772 |
|
---|
| 773 | UpdateTexture(60,3);
|
---|
| 774 | // UpdateTexture(1,1);
|
---|
| 775 |
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | // Function that interfaces with the VisualCAD.
|
---|
| 779 | int vcObscurerGenerateImage(CMesh &geom, char *argv, int quality, int *imgUsize, int *imgVsize, float **imgRgbBuffer)
|
---|
| 780 | {
|
---|
| 781 | float divisor;
|
---|
| 782 | int *DadesTemp = new int[4];
|
---|
| 783 | float *dadesf=NULL;
|
---|
| 784 | float aux[4];
|
---|
| 785 | int *dadesi=NULL;
|
---|
| 786 | float a;
|
---|
| 787 | int qual;
|
---|
| 788 |
|
---|
| 789 | glutCreateWindow("T");
|
---|
| 790 |
|
---|
| 791 | FILE *file = NULL;
|
---|
| 792 |
|
---|
| 793 | file = fopen("L:\\Ogre3D\\ogrenew\\Tools\\GPUObscurancesOgre3D1\\bin\\Debug\\tmp.txt", "w");
|
---|
| 794 |
|
---|
| 795 | glerror = glewInit();
|
---|
| 796 |
|
---|
| 797 | if (glerror != GLEW_OK) fprintf(file, "\nError initializing glew library, %s", glewGetErrorString(glerror));
|
---|
| 798 | else fprintf(file, "\nTot Correcte!\n");
|
---|
| 799 |
|
---|
| 800 | fclose(file);
|
---|
| 801 |
|
---|
| 802 | memset(&aux, 0.0, 4);
|
---|
| 803 |
|
---|
| 804 | qual = quality;
|
---|
| 805 |
|
---|
| 806 | if (qual < 1) qual = 1;
|
---|
| 807 |
|
---|
| 808 | switch (qual)
|
---|
| 809 | {
|
---|
| 810 | case 1: *imgVsize = *imgUsize = 256;
|
---|
| 811 | break;
|
---|
| 812 | case 2: *imgVsize = *imgUsize = 512;
|
---|
| 813 | break;
|
---|
| 814 | case 3: *imgVsize = *imgUsize = 1024;
|
---|
| 815 | break;
|
---|
| 816 | case 4: *imgVsize = *imgUsize = 2048;
|
---|
| 817 | break;
|
---|
| 818 | default: printf("\nResolution not supported");
|
---|
| 819 | };
|
---|
| 820 |
|
---|
| 821 | Res2X = *imgUsize;
|
---|
| 822 | Res2Y = *imgVsize;
|
---|
| 823 |
|
---|
| 824 | ResX = Res2X * 2.0;
|
---|
| 825 | ResY = Res2Y * 2.0;
|
---|
| 826 |
|
---|
| 827 | if(ResX == 4096) ResX = 2048;
|
---|
| 828 | if(ResY == 4096) ResY = 2048;
|
---|
| 829 |
|
---|
| 830 | printf("\nRes(Plans de projeccio): ResolucioX = %d , ResolucioY = %d", ResX, ResY);
|
---|
| 831 | printf("\nRes2(Lightmap): ResolucioX = %d , ResolucioY = %d", Res2X, Res2Y);
|
---|
| 832 | dadesf = new float[Res2X*Res2Y*4];
|
---|
| 833 | dadesi = new int[Res2X*Res2Y*4];
|
---|
| 834 |
|
---|
| 835 |
|
---|
| 836 | init2(argv);
|
---|
| 837 |
|
---|
| 838 | //g_pMesh.FreeMem();
|
---|
| 839 |
|
---|
| 840 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb4);
|
---|
| 841 | glReadPixels(0,0,Res2X,Res2Y,GL_RGBA,GL_FLOAT,dadesf);
|
---|
| 842 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 843 |
|
---|
| 844 | for(int i=0; i<Res2X*Res2Y; i++)
|
---|
| 845 | {
|
---|
| 846 | divisor = dadesf[4*i+3];
|
---|
| 847 | if(divisor>15)
|
---|
| 848 | {
|
---|
| 849 | dadesf[4*i] = (dadesf[4*i]/divisor);
|
---|
| 850 | dadesf[4*i+1] = (dadesf[4*i+1]/divisor);
|
---|
| 851 | dadesf[4*i+2] = (dadesf[4*i+2]/divisor);
|
---|
| 852 | dadesf[4*i+3] = 1.0;
|
---|
| 853 | }
|
---|
| 854 | else
|
---|
| 855 | {
|
---|
| 856 | a=0;
|
---|
| 857 | aux[0] = 0.0;
|
---|
| 858 | aux[1] = 0.0;
|
---|
| 859 | aux[2] = 0.0;
|
---|
| 860 | aux[3] = 0.0;
|
---|
| 861 | if(i/Res2X > 0)
|
---|
| 862 | {
|
---|
| 863 | if(i/Res2X < Res2Y-1)
|
---|
| 864 | {
|
---|
| 865 | if(i%Res2X !=0)
|
---|
| 866 | {
|
---|
| 867 | if(dadesf[4*(i-Res2X)-1]>15) //ul();
|
---|
| 868 | {
|
---|
| 869 | aux[0] += (dadesf[4*(i-Res2X)-4]/dadesf[4*(i-Res2X)-1]);
|
---|
| 870 | aux[1] += (dadesf[4*(i-Res2X)-3]/dadesf[4*(i-Res2X)-1]);
|
---|
| 871 | aux[2] += (dadesf[4*(i-Res2X)-2]/dadesf[4*(i-Res2X)-1]);
|
---|
| 872 | aux[3] += 1.0;
|
---|
| 873 | a =+ 1.0;;
|
---|
| 874 | }
|
---|
| 875 | if(dadesf[4*i-1]>15) //l();
|
---|
| 876 | {
|
---|
| 877 | aux[0] += (dadesf[4*i-4]/dadesf[4*i-1]);
|
---|
| 878 | aux[1] += (dadesf[4*i-3]/dadesf[4*i-1]);
|
---|
| 879 | aux[2] += (dadesf[4*i-2]/dadesf[4*i-1]);
|
---|
| 880 | aux[3] += 1.0;
|
---|
| 881 | a =+ 1.0;;
|
---|
| 882 | }
|
---|
| 883 | if(dadesf[4*(i+Res2X)-1]>15) //dl();
|
---|
| 884 | {
|
---|
| 885 | aux[0] += (dadesf[4*(i+Res2X)-4]/dadesf[4*(i+Res2X)-1]);
|
---|
| 886 | aux[1] += (dadesf[4*(i+Res2X)-3]/dadesf[4*(i+Res2X)-1]);
|
---|
| 887 | aux[2] += (dadesf[4*(i+Res2X)-2]/dadesf[4*(i+Res2X)-1]);
|
---|
| 888 | aux[3] += 1.0;
|
---|
| 889 | a =+ 1.0;;
|
---|
| 890 | }
|
---|
| 891 | }
|
---|
| 892 | if(dadesf[4*(i+Res2X)+3]>15) //d();
|
---|
| 893 | {
|
---|
| 894 | aux[0] += (dadesf[4*(i+Res2X)]/dadesf[4*(i+Res2X)+3]);
|
---|
| 895 | aux[1] += (dadesf[4*(i+Res2X)+1]/dadesf[4*(i+Res2X)+3]);
|
---|
| 896 | aux[2] += (dadesf[4*(i+Res2X)+2]/dadesf[4*(i+Res2X)+3]);
|
---|
| 897 | aux[3] += 1.0;
|
---|
| 898 | a =+ 1.0;;
|
---|
| 899 | }
|
---|
| 900 | if(i%Res2X!=Res2X-1)
|
---|
| 901 | {
|
---|
| 902 | if(dadesf[4*(i-Res2X)+7]>15) //ur();
|
---|
| 903 | {
|
---|
| 904 | aux[0] += (dadesf[4*(i-Res2X)+4]/dadesf[4*(i-Res2X)+7]);
|
---|
| 905 | aux[1] += (dadesf[4*(i-Res2X)+5]/dadesf[4*(i-Res2X)+7]);
|
---|
| 906 | aux[2] += (dadesf[4*(i-Res2X)+6]/dadesf[4*(i-Res2X)+7]);
|
---|
| 907 | aux[3] += 1.0;
|
---|
| 908 | a =+ 1.0;;
|
---|
| 909 | }
|
---|
| 910 | if(dadesf[4*i+7]>15) //r();
|
---|
| 911 | {
|
---|
| 912 | aux[0] += (dadesf[4*i+4]/dadesf[4*i+7]);
|
---|
| 913 | aux[1] += (dadesf[4*i+5]/dadesf[4*i+7]);
|
---|
| 914 | aux[2] += (dadesf[4*i+6]/dadesf[4*i+7]);
|
---|
| 915 | aux[3] += 1.0;
|
---|
| 916 | a =+ 1.0;;
|
---|
| 917 | }
|
---|
| 918 | if(dadesf[4*(i+Res2X)+7]>15) //dr();
|
---|
| 919 | {
|
---|
| 920 | aux[0] += (dadesf[4*(i+Res2X)+4]/dadesf[4*(i+Res2X)+7]);
|
---|
| 921 | aux[1] += (dadesf[4*(i+Res2X)+5]/dadesf[4*(i+Res2X)+7]);
|
---|
| 922 | aux[2] += (dadesf[4*(i+Res2X)+6]/dadesf[4*(i+Res2X)+7]);
|
---|
| 923 | aux[3] += 1.0;
|
---|
| 924 | a =+ 1.0;;
|
---|
| 925 | }
|
---|
| 926 | }
|
---|
| 927 | if(dadesf[4*(i-Res2X)+3]>15) //u();
|
---|
| 928 | {
|
---|
| 929 | aux[0] += (dadesf[4*(i-Res2X)]/dadesf[4*(i-Res2X)+3]);
|
---|
| 930 | aux[1] += (dadesf[4*(i-Res2X)+1]/dadesf[4*(i-Res2X)+3]);
|
---|
| 931 | aux[2] += (dadesf[4*(i-Res2X)+2]/dadesf[4*(i-Res2X)+3]);
|
---|
| 932 | aux[3] += 1.0;
|
---|
| 933 | a =+ 1.0;;
|
---|
| 934 | }
|
---|
| 935 | }
|
---|
| 936 | }
|
---|
| 937 | else
|
---|
| 938 | {
|
---|
| 939 | if(i%Res2X !=0)
|
---|
| 940 | {
|
---|
| 941 | if(dadesf[4*i-1]>15) //l();
|
---|
| 942 | {
|
---|
| 943 | aux[0] += (dadesf[4*i-4]/dadesf[4*i-1]);
|
---|
| 944 | aux[1] += (dadesf[4*i-3]/dadesf[4*i-1]);
|
---|
| 945 | aux[2] += (dadesf[4*i-2]/dadesf[4*i-1]);
|
---|
| 946 | aux[3] += 1.0;
|
---|
| 947 | a =+ 1.0;;
|
---|
| 948 | }
|
---|
| 949 | if(dadesf[4*(i+Res2X)-1]>15) //dl();
|
---|
| 950 | {
|
---|
| 951 | aux[0] += (dadesf[4*(i+Res2X)-4]/dadesf[4*(i+Res2X)-1]);
|
---|
| 952 | aux[1] += (dadesf[4*(i+Res2X)-3]/dadesf[4*(i+Res2X)-1]);
|
---|
| 953 | aux[2] += (dadesf[4*(i+Res2X)-2]/dadesf[4*(i+Res2X)-1]);
|
---|
| 954 | aux[3] = 1.0;
|
---|
| 955 | a =+ 1.0;;
|
---|
| 956 | }
|
---|
| 957 | }
|
---|
| 958 | if(i%Res2X!=Res2X-1)
|
---|
| 959 | {
|
---|
| 960 | if(dadesf[4*i+7]>15) //r();
|
---|
| 961 | {
|
---|
| 962 | aux[0] += (dadesf[4*i+4]/dadesf[4*i+7]);
|
---|
| 963 | aux[1] += (dadesf[4*i+5]/dadesf[4*i+7]);
|
---|
| 964 | aux[2] += (dadesf[4*i+6]/dadesf[4*i+7]);
|
---|
| 965 | aux[3] += 1.0;
|
---|
| 966 | a =+ 1.0;;
|
---|
| 967 | }
|
---|
| 968 | if(dadesf[4*(i+Res2X)+7]>15) //dr();
|
---|
| 969 | {
|
---|
| 970 | aux[0] += (dadesf[4*(i+Res2X)+4]/dadesf[4*(i+Res2X)+7]);
|
---|
| 971 | aux[1] += (dadesf[4*(i+Res2X)+5]/dadesf[4*(i+Res2X)+7]);
|
---|
| 972 | aux[2] += (dadesf[4*(i+Res2X)+6]/dadesf[4*(i+Res2X)+7]);
|
---|
| 973 | aux[3] += 1.0;
|
---|
| 974 | a =+ 1.0;;
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
| 977 | if(dadesf[4*(i+Res2X)+3]>15) //d();
|
---|
| 978 | {
|
---|
| 979 | aux[0] += (dadesf[4*(i+Res2X)]/dadesf[4*(i+Res2X)+3]);
|
---|
| 980 | aux[1] += (dadesf[4*(i+Res2X)+1]/dadesf[4*(i+Res2X)+3]);
|
---|
| 981 | aux[2] += (dadesf[4*(i+Res2X)+2]/dadesf[4*(i+Res2X)+3]);
|
---|
| 982 | aux[3] += 1.0;
|
---|
| 983 | a =+ 1.0;;
|
---|
| 984 | }
|
---|
| 985 | }
|
---|
| 986 | if(a > 0.0)
|
---|
| 987 | {
|
---|
| 988 | dadesf[4*i] = aux[0]/aux[3];
|
---|
| 989 | dadesf[4*i+1] = aux[1]/aux[3];
|
---|
| 990 | dadesf[4*i+2] = aux[2]/aux[3];
|
---|
| 991 | dadesf[4*i+3] = 1.0;
|
---|
| 992 | }
|
---|
| 993 | else
|
---|
| 994 | {
|
---|
| 995 | dadesf[4*i] = 0.0;
|
---|
| 996 | dadesf[4*i+1] = 0.0;
|
---|
| 997 | dadesf[4*i+2] = 0.0;
|
---|
| 998 | dadesf[4*i+3] = 0.0;
|
---|
| 999 | }
|
---|
| 1000 | }
|
---|
| 1001 | dadesi[4*i] = (int)(255.0*dadesf[4*i]);
|
---|
| 1002 | dadesi[4*i+1] = (int)(255.0*dadesf[4*i+1]);
|
---|
| 1003 | dadesi[4*i+2] = (int)(255.0*dadesf[4*i+2]);
|
---|
| 1004 | dadesi[4*i+3] = (int)(255.0*dadesf[4*i+3]);
|
---|
| 1005 |
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | geom = g_pMesh;
|
---|
| 1009 |
|
---|
| 1010 | imdebug("rgba b=32f w=%d h=%d %p", Res2X, Res2Y, dadesf);
|
---|
| 1011 |
|
---|
| 1012 | *imgRgbBuffer = dadesf;
|
---|
| 1013 |
|
---|
| 1014 | return 0;
|
---|
| 1015 |
|
---|
| 1016 | } |
---|