[930] | 1 | // GPUObscurances.
|
---|
| 2 | // Sergi Funtané & Nicolau Sunyer.
|
---|
| 3 |
|
---|
| 4 | #define GLEW_STATIC 1
|
---|
| 5 | #include "glew.h"
|
---|
| 6 | #include <glut.h>
|
---|
| 7 | #include <iostream>
|
---|
| 8 | #include <windows.h>
|
---|
| 9 | #include "textfile.h"
|
---|
| 10 |
|
---|
| 11 | //Includes
|
---|
| 12 | #include "CMesh.h"
|
---|
| 13 | #include "Maths.h"
|
---|
| 14 |
|
---|
| 15 | #include "vcObscuranceMap.h"
|
---|
| 16 |
|
---|
| 17 | //GUI includes
|
---|
| 18 | #include "Main.h"
|
---|
| 19 | #include "MyFrame.h"
|
---|
| 20 | #include <wx/progdlg.h>
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | //To use Offsets in the PBOs.
|
---|
| 24 | #define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
---|
| 25 |
|
---|
| 26 | //GLSL
|
---|
| 27 | // Handlers
|
---|
| 28 | GLuint vRender,fRender,pRender;
|
---|
| 29 | GLuint vDepth, fDepth, pDepth;
|
---|
| 30 |
|
---|
| 31 | //Uniform Parameters
|
---|
| 32 | //Depth Step
|
---|
| 33 | GLint locAngle;
|
---|
| 34 |
|
---|
| 35 | //Render Step
|
---|
| 36 | GLint ProjParam0;
|
---|
| 37 | GLint texture0;
|
---|
| 38 | GLint texture20;
|
---|
| 39 | GLint texture30;
|
---|
| 40 | GLint kd0;
|
---|
| 41 | GLint lpos0;
|
---|
| 42 | GLint Time0;
|
---|
| 43 | GLint ModeDirect0;
|
---|
| 44 | GLint ModeIndirect0;
|
---|
| 45 | GLint ModeAmbient0;
|
---|
| 46 | GLint AmbientIntensity0;
|
---|
| 47 | GLint LightPower0;
|
---|
| 48 | GLint Factor0;
|
---|
| 49 | GLint LightMap0;
|
---|
| 50 | //End GLSL
|
---|
| 51 |
|
---|
| 52 | //FBO
|
---|
| 53 | GLuint fb;
|
---|
| 54 | static GLuint texFBO[2];
|
---|
| 55 |
|
---|
| 56 | //Textures
|
---|
| 57 | static GLuint texNam[2];
|
---|
| 58 |
|
---|
| 59 | int g_nFPS = 0, g_nFrames = 0; // FPS and FPS Counter
|
---|
| 60 | DWORD g_dwLastFPS = 0; // Last FPS Check Time
|
---|
| 61 |
|
---|
| 62 | float it = 0.0;
|
---|
| 63 |
|
---|
| 64 | float angle = 0.0;
|
---|
| 65 | float mode = 0.0;
|
---|
| 66 | float modeDirect = 0.0, modeIndirect = 0.0, modeAmbient = 0.0, modeLight = 0.0;
|
---|
| 67 | float intensityr = 0.5, intensityg = 0.5, intensityb = 0.5, intensityinc = 0.1;
|
---|
| 68 | float lightr = 0.5, lightg = 0.5, lightb = 0.5, lightinc = 0.1;
|
---|
| 69 | float factor = 0.5, factorinc = 0.1;
|
---|
| 70 | //int vsync = 0;
|
---|
| 71 |
|
---|
| 72 | GLfloat *m_pSpotlight=NULL;
|
---|
| 73 | float *ObscuranceMap;
|
---|
| 74 | static CSphere Esf;
|
---|
| 75 |
|
---|
| 76 | //Mouse control
|
---|
| 77 | float zoom = 5.0f;
|
---|
| 78 | float width, height;
|
---|
| 79 | float rotx = 0;
|
---|
| 80 | float roty = 0.001f;
|
---|
| 81 | float tx = 0;
|
---|
| 82 | float ty = 0;
|
---|
| 83 | int lastx=0;
|
---|
| 84 | int lasty=0;
|
---|
| 85 | unsigned char Buttons[3] = {0};
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | MATRIX4X4 project,mview,inverse;
|
---|
| 89 | static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
|
---|
| 90 | 0.0f, 0.5f, 0.0f, 0.0f,
|
---|
| 91 | 0.0f, 0.0f, 0.5f, 0.0f,
|
---|
| 92 | 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
|
---|
| 93 | MATRIX4X4 textureMatrix;
|
---|
| 94 | CMesh geom;
|
---|
| 95 | //End global variables
|
---|
| 96 |
|
---|
| 97 | //testing
|
---|
| 98 | void changeSize1(int w, int h) {
|
---|
| 99 |
|
---|
| 100 | // Prevent a divide by zero, when window is too short
|
---|
| 101 | // (you cant make a window of zero width).
|
---|
| 102 | if(h == 0)
|
---|
| 103 | h = 1;
|
---|
| 104 |
|
---|
| 105 | // we're storing these values for latter use in the
|
---|
| 106 | // mouse motion functions
|
---|
| 107 | width = w;
|
---|
| 108 | height = h;
|
---|
| 109 | float ratio = (GLfloat) w / (GLfloat)h;
|
---|
| 110 |
|
---|
| 111 | // Reset the coordinate system before modifying
|
---|
| 112 | glMatrixMode(GL_PROJECTION);
|
---|
| 113 | glLoadIdentity();
|
---|
| 114 |
|
---|
| 115 | // Set the viewport to be the entire window
|
---|
| 116 | glViewport(0, 0, w, h);
|
---|
| 117 |
|
---|
| 118 | // Set the correct perspective.
|
---|
| 119 | gluPerspective(30,ratio,0.1,100.0);
|
---|
| 120 | glMatrixMode(GL_MODELVIEW);
|
---|
| 121 | glLoadIdentity();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | //Functonal to use when the window is resized
|
---|
| 126 | void changeSize2(int w, int h)
|
---|
| 127 | {
|
---|
| 128 |
|
---|
| 129 | if(h == 0)
|
---|
| 130 | h = 1;
|
---|
| 131 |
|
---|
| 132 | float ratio = (GLfloat) w / (GLfloat)h;
|
---|
| 133 |
|
---|
| 134 | glMatrixMode(GL_PROJECTION);
|
---|
| 135 | glLoadIdentity();
|
---|
| 136 | gluPerspective(60,ratio,0.01,100);
|
---|
| 137 |
|
---|
| 138 | glMatrixMode(GL_MODELVIEW);
|
---|
| 139 | glLoadIdentity();
|
---|
| 140 |
|
---|
| 141 | glViewport(0, 0, w, h);
|
---|
| 142 |
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | //Keyboard Processing
|
---|
| 146 | void processNormalKeys(unsigned char key, int x, int y)
|
---|
| 147 | {
|
---|
| 148 |
|
---|
| 149 | if (key == 27) exit(0);
|
---|
| 150 | else if (key=='1') modeDirect = ((modeDirect == 1.0)? 0.0:1.0);
|
---|
| 151 | else if (key == '2') modeIndirect = ((modeIndirect == 1.0)? 0.0:1.0);
|
---|
| 152 | else if (key == '3') modeAmbient = ((modeAmbient == 1.0)? 0.0:1.0);
|
---|
| 153 | else if (key == '4') modeLight = ((modeLight == 1.0)? 0.0:1.0);
|
---|
| 154 | else if (key == 'z') factor -= factorinc;
|
---|
| 155 | else if (key == 'x') factor += factorinc;
|
---|
| 156 | else if (key == 'c') angle--;
|
---|
| 157 | else if (key == 'v') angle++;
|
---|
| 158 | else if (key == 'q') intensityr += intensityinc;
|
---|
| 159 | else if (key == 'w') intensityg += intensityinc;
|
---|
| 160 | else if (key == 'e') intensityb += intensityinc;
|
---|
| 161 | else if (key == 'a') intensityr -= intensityinc;
|
---|
| 162 | else if (key == 's') intensityg -= intensityinc;
|
---|
| 163 | else if (key == 'd') intensityb -= intensityinc;
|
---|
| 164 | else if (key == 'r')
|
---|
| 165 | {
|
---|
| 166 | intensityr += intensityinc;
|
---|
| 167 | intensityg += intensityinc;
|
---|
| 168 | intensityb += intensityinc;
|
---|
| 169 | }
|
---|
| 170 | else if (key == 'f')
|
---|
| 171 | {
|
---|
| 172 | intensityr -= intensityinc;
|
---|
| 173 | intensityg -= intensityinc;
|
---|
| 174 | intensityb -= intensityinc;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | else if (key == 'u') lightr += lightinc;
|
---|
| 178 | else if (key == 'h') lightg += lightinc;
|
---|
| 179 | else if (key == 'i') lightb += lightinc;
|
---|
| 180 | else if (key == 'j') lightr -= lightinc;
|
---|
| 181 | else if (key == 'o') lightg -= lightinc;
|
---|
| 182 | else if (key == 'k') lightb -= lightinc;
|
---|
| 183 | else if (key == 'p')
|
---|
| 184 | {
|
---|
| 185 | lightr += lightinc;
|
---|
| 186 | lightg += lightinc;
|
---|
| 187 | lightb += lightinc;
|
---|
| 188 | }
|
---|
| 189 | else if (key == 'l')
|
---|
| 190 | {
|
---|
| 191 | lightr -= lightinc;
|
---|
| 192 | lightg -= lightinc;
|
---|
| 193 | lightb -= lightinc;
|
---|
| 194 | };
|
---|
| 195 |
|
---|
| 196 | if(lightr < 0.0) lightr = 0.0;
|
---|
| 197 | if(lightg < 0.0) lightg = 0.0;
|
---|
| 198 | if(lightb < 0.0) lightb = 0.0;
|
---|
| 199 |
|
---|
| 200 | printf("\nDirect: %1.0f Indirect: %1.0f Ambient: %1.0f Factor: %1.1f Angle: %1.0f",modeDirect,modeIndirect,modeAmbient,factor,angle);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | void init(int numargs, char **argv)
|
---|
| 204 | {
|
---|
| 205 | //->vcVrmlLoaderLoadFile("tricicle.wrl", &vcO);
|
---|
| 206 |
|
---|
| 207 | int err, i, j;
|
---|
| 208 | int qualitat = 1;
|
---|
| 209 | int resX = 256 * pow(2,qualitat-1), resY = 256 * pow(2,qualitat-1);
|
---|
| 210 | int RX = 512;
|
---|
| 211 | int RY = 512;
|
---|
| 212 |
|
---|
| 213 | ObscuranceMap = new float[resX * resY * 4];
|
---|
| 214 | printf("\nAbans: ResolucioX = %d , ResolucioY = %d", resX, resY);
|
---|
| 215 |
|
---|
| 216 | err = vcObscurerGenerateImage(geom, numargs, argv, qualitat, &resX, &resY, &ObscuranceMap);
|
---|
| 217 |
|
---|
| 218 | printf("\nx = %f, y = %f, z = %f\n", geom.m_pObject._geo[0]._triSet[0]._ver[0], geom.m_pObject._geo[0]._triSet[0]._ver[1], geom.m_pObject._geo[0]._triSet[0]._ver[2]);
|
---|
| 219 |
|
---|
| 220 | printf("\nDespres: ResolucioX = %d , ResolucioY = %d", resX, resY);
|
---|
| 221 |
|
---|
| 222 | // GLubyte *Obscurance= new GLubyte[resX * resY * 4];
|
---|
| 223 | // for(int s = 0; s < resX * resY * 4; s++) Obscurance[s] = (GLubyte) ObscuranceMap[s];
|
---|
| 224 |
|
---|
| 225 | m_pSpotlight = new float[RX * RY * 4];
|
---|
| 226 |
|
---|
| 227 | for(i = 0; i<RX; i++)
|
---|
| 228 | {
|
---|
| 229 | for(j = 0; j<RY; j++)
|
---|
| 230 | {
|
---|
| 231 | if((i-RX/2.0)*(i-RX/2.0)+(j-RY/2.0)*(j-RY/2.0)>RX*RY/5.0)
|
---|
| 232 | {
|
---|
| 233 | m_pSpotlight[4*(i*RY+j)] = 0.0;
|
---|
| 234 | m_pSpotlight[4*(i*RY+j)+1] = 0.0;
|
---|
| 235 | m_pSpotlight[4*(i*RY+j)+2] = 0.0;
|
---|
| 236 | m_pSpotlight[4*(i*RY+j)+3] = 1.0;
|
---|
| 237 | }
|
---|
| 238 | else
|
---|
| 239 | {
|
---|
| 240 | m_pSpotlight[4*(i*RY+j)] = 1.0;
|
---|
| 241 | m_pSpotlight[4*(i*RY+j)+1] = 1.0;
|
---|
| 242 | m_pSpotlight[4*(i*RY+j)+2] = 1.0;
|
---|
| 243 | m_pSpotlight[4*(i*RY+j)+3] = 1.0;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | //Create Textures.
|
---|
| 249 | glGenTextures(2, &texNam[0]); // Create The Textures
|
---|
| 250 |
|
---|
| 251 | //Texture
|
---|
| 252 | // Binding
|
---|
| 253 | glBindTexture( GL_TEXTURE_2D, texNam[0] );
|
---|
| 254 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, resX, resY, 0, GL_RGBA, GL_FLOAT, ObscuranceMap);
|
---|
| 255 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 256 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 257 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 258 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 259 |
|
---|
| 260 | glBindTexture( GL_TEXTURE_2D, texNam[1] );
|
---|
| 261 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, RX, RY, 0, GL_RGBA, GL_FLOAT, m_pSpotlight);
|
---|
| 262 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 263 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 264 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 265 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 266 |
|
---|
| 267 | glGenFramebuffersEXT(1, &fb);
|
---|
| 268 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
|
---|
| 269 |
|
---|
| 270 | glGenTextures(1,&texFBO[0]);
|
---|
| 271 |
|
---|
| 272 | glBindTexture( GL_TEXTURE_2D, texFBO[0] );
|
---|
| 273 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, RX, RY, 0, GL_RGBA, GL_FLOAT, 0);
|
---|
| 274 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 275 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 276 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 277 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 278 |
|
---|
| 279 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texFBO[0],0);
|
---|
| 280 |
|
---|
| 281 | glGenRenderbuffersEXT(1, &texFBO[1]); |
---|
| 282 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, texFBO[1]); |
---|
| 283 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, RX, RY ); |
---|
| 284 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, texFBO[1]);
|
---|
| 285 |
|
---|
| 286 | //We unbind the texture.
|
---|
| 287 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 288 | //We set the rendering context to render to the backbuffer again
|
---|
| 289 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 290 |
|
---|
| 291 | //GLSL
|
---|
| 292 | //Depth Shaders
|
---|
| 293 | char *vsDepth = NULL, *fsDepth = NULL;
|
---|
| 294 |
|
---|
| 295 | vDepth = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 296 | fDepth = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 297 |
|
---|
| 298 |
|
---|
| 299 | vsDepth = textFileRead("depth.vert");
|
---|
| 300 | fsDepth = textFileRead("depth.frag");
|
---|
| 301 |
|
---|
| 302 | const char *vvDepth = vsDepth;
|
---|
| 303 | const char *ffDepth = fsDepth;
|
---|
| 304 |
|
---|
| 305 | glShaderSource(vDepth, 1, &vvDepth, NULL);
|
---|
| 306 | glShaderSource(fDepth, 1, &ffDepth, NULL);
|
---|
| 307 |
|
---|
| 308 | free(vsDepth); free(fsDepth);
|
---|
| 309 |
|
---|
| 310 | glCompileShader(vDepth);
|
---|
| 311 | glCompileShader(fDepth);
|
---|
| 312 |
|
---|
| 313 | pDepth = glCreateProgram();
|
---|
| 314 | glAttachShader(pDepth, vDepth);
|
---|
| 315 | glAttachShader(pDepth, fDepth);
|
---|
| 316 |
|
---|
| 317 | glLinkProgram(pDepth);
|
---|
| 318 |
|
---|
| 319 | glUseProgram(pDepth);
|
---|
| 320 | locAngle = glGetUniformLocation(pDepth, "angle");
|
---|
| 321 | glUseProgram(0);
|
---|
| 322 |
|
---|
| 323 | //Render Shaders
|
---|
| 324 | char *vsRender = NULL, *fsRender = NULL;
|
---|
| 325 |
|
---|
| 326 | vRender = glCreateShader(GL_VERTEX_SHADER);
|
---|
| 327 | fRender = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
| 328 |
|
---|
| 329 | vsRender = textFileRead("render.vert");
|
---|
| 330 | fsRender = textFileRead("render.frag");
|
---|
| 331 |
|
---|
| 332 | const char *vvRender = vsRender;
|
---|
| 333 | const char *ffRender = fsRender;
|
---|
| 334 |
|
---|
| 335 | glShaderSource(vRender, 1, &vvRender, NULL);
|
---|
| 336 | glShaderSource(fRender, 1, &ffRender, NULL);
|
---|
| 337 |
|
---|
| 338 | free(vsRender); free(fsRender);
|
---|
| 339 |
|
---|
| 340 | glCompileShader(vRender);
|
---|
| 341 | glCompileShader(fRender);
|
---|
| 342 |
|
---|
| 343 | pRender = glCreateProgram();
|
---|
| 344 | glAttachShader(pRender, vRender);
|
---|
| 345 | glAttachShader(pRender, fRender);
|
---|
| 346 |
|
---|
| 347 | glLinkProgram(pRender);
|
---|
| 348 |
|
---|
| 349 | glUseProgram(pRender);
|
---|
| 350 | ProjParam0 = glGetUniformLocation(pRender, "projMatrix");
|
---|
| 351 | Time0 = glGetUniformLocation(pRender, "time");
|
---|
| 352 | texture0 = glGetUniformLocation(pRender, "texture");
|
---|
| 353 | texture20 = glGetUniformLocation(pRender, "shadowMap");
|
---|
| 354 | texture30 = glGetUniformLocation(pRender, "spotlight");
|
---|
| 355 | kd0 = glGetUniformLocation(pRender, "Kd");
|
---|
| 356 | lpos0 = glGetUniformLocation(pRender, "LightPos");
|
---|
| 357 | ModeDirect0 = glGetUniformLocation(pRender, "modeDirect");
|
---|
| 358 | ModeIndirect0 = glGetUniformLocation(pRender, "modeIndirect");
|
---|
| 359 | ModeAmbient0 = glGetUniformLocation(pRender, "modeAmbient");
|
---|
| 360 | AmbientIntensity0 = glGetUniformLocation(pRender, "intensity");
|
---|
| 361 | LightPower0 = glGetUniformLocation(pRender, "lpower");
|
---|
| 362 | Factor0 = glGetUniformLocation(pRender, "factor");
|
---|
| 363 | LightMap0 = glGetUniformLocation(pRender, "modeLight");
|
---|
| 364 | glUseProgram(0);
|
---|
| 365 | //End GLSL
|
---|
| 366 |
|
---|
| 367 | //->geom.LoadMesh(numargs, argv);
|
---|
| 368 |
|
---|
| 369 | for(i = 0; i<geom.m_pObject._numGeos;i++)
|
---|
| 370 | {
|
---|
| 371 | for(j = 0; j<geom.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 372 | {
|
---|
| 373 | //Aqui creeem els VBOs.
|
---|
| 374 |
|
---|
| 375 | // Generate And Bind The Index Buffer
|
---|
| 376 | glGenBuffersARB( 1, (unsigned int *)&geom.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Get A Valid Name
|
---|
| 377 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOIndexs ); // Bind The Buffer
|
---|
| 378 | // Load The Data
|
---|
| 379 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j]._numTris * 3 * sizeof(int), geom.m_pObject._geo[i]._triSet[j]._index, GL_STATIC_DRAW_ARB );
|
---|
| 380 |
|
---|
| 381 | // Generate And Bind The Vertex Buffer
|
---|
| 382 | glGenBuffersARB( 1, &geom.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Get A Valid Name
|
---|
| 383 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOVertices ); // Bind The Buffer
|
---|
| 384 | // Load The Data
|
---|
| 385 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), geom.m_pObject._geo[i]._triSet[j]._ver, GL_STATIC_DRAW_ARB );
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | // Generate And Bind The Normal Buffer
|
---|
| 389 | glGenBuffersARB( 1, &geom.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Get A Valid Name
|
---|
| 390 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBONormals ); // Bind The Buffer
|
---|
| 391 | // Load The Data
|
---|
| 392 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), geom.m_pObject._geo[i]._triSet[j]._nor, GL_STATIC_DRAW_ARB );
|
---|
| 393 |
|
---|
| 394 | // Generate And Bind The TexCoord Buffer
|
---|
| 395 | glGenBuffersARB( 1, &geom.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Get A Valid Name
|
---|
| 396 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords ); // Bind The Buffer
|
---|
| 397 | // Load The Data
|
---|
| 398 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j]._numVer * 2 * sizeof(float), geom.m_pObject._geo[i]._triSet[j]._tex, GL_STATIC_DRAW_ARB );
|
---|
| 399 |
|
---|
| 400 | // Generate And Bind The Color Buffer
|
---|
| 401 | glGenBuffersARB( 1, &geom.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Get A Valid Name
|
---|
| 402 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOColors ); // Bind The Buffer
|
---|
| 403 | // Load The Data
|
---|
| 404 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j]._numVer * 3 * sizeof(float), geom.m_pObject._geo[i]._triSet[j]._refl, GL_STATIC_DRAW_ARB );
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); // Bind The Buffer
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | void renderDepth()
|
---|
| 411 | {
|
---|
| 412 | glUseProgram(pDepth);
|
---|
| 413 |
|
---|
| 414 | Esf.center.x = 0.0;
|
---|
| 415 | Esf.center.y = 0.0;
|
---|
| 416 | Esf.center.z = 0.0;
|
---|
| 417 | Esf.radius = sqrt(12.0)/2.0;
|
---|
| 418 |
|
---|
| 419 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
|
---|
| 420 |
|
---|
| 421 | glClearDepth(1.0);
|
---|
| 422 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 423 | glEnable (GL_DEPTH_TEST); // Enable Depth Testing
|
---|
| 424 | glDepthFunc(GL_LESS);
|
---|
| 425 |
|
---|
| 426 | glMatrixMode(GL_PROJECTION);
|
---|
| 427 | glLoadIdentity();
|
---|
| 428 | gluPerspective(40.0f, 1.0f, 1.0f, (Esf.radius * 2.0)+1.0);
|
---|
| 429 | glGetFloatv(GL_PROJECTION_MATRIX,project);
|
---|
| 430 |
|
---|
| 431 | glMatrixMode(GL_MODELVIEW);
|
---|
| 432 | glLoadIdentity();
|
---|
| 433 | gluLookAt( (Esf.radius+1.0) * cos(angle/50.0), (Esf.radius+1.0) * sin(angle/50.0) , 1.0,
|
---|
| 434 | Esf.center.x, Esf.center.y, Esf.center.z,
|
---|
| 435 | 0.0f, 1.0f, 0.0f);
|
---|
| 436 | glGetFloatv(GL_MODELVIEW_MATRIX,mview);
|
---|
| 437 |
|
---|
| 438 | glViewport(0,0,512.0,512.0);
|
---|
| 439 |
|
---|
| 440 | glUniform1f(locAngle, angle);
|
---|
| 441 |
|
---|
| 442 | // Enable Pointers
|
---|
| 443 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 444 |
|
---|
| 445 | for(int i=0; i<geom.m_pObject._numGeos;i++)
|
---|
| 446 | {
|
---|
| 447 | for(int j=0; j<geom.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 448 | {
|
---|
| 449 | // Set Pointers To Our Data
|
---|
| 450 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
| 451 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 452 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
| 453 |
|
---|
| 454 | glDrawElements( GL_TRIANGLES, geom.m_pObject._geo[i]._triSet[j]._numTris * 3,GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once
|
---|
| 455 | //->glDrawElements( GL_TRIANGLES, geom.m_pObject._geo[i]._triSet[j]._numTris*3,GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once
|
---|
| 456 | }
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 460 |
|
---|
| 461 | glUseProgram(0);
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | //Render the Scene
|
---|
| 465 | void renderScene(void)
|
---|
| 466 | {
|
---|
| 467 | renderDepth();
|
---|
| 468 |
|
---|
| 469 | glUseProgram(pRender);
|
---|
| 470 |
|
---|
| 471 | changeSize2(512, 512);
|
---|
| 472 |
|
---|
| 473 | glLoadIdentity(); |
---|
| 474 |
|
---|
| 475 | glTranslatef(0,0,-zoom);
|
---|
| 476 | glTranslatef(tx,ty,0);
|
---|
| 477 | glRotatef(rotx,1,0,0);
|
---|
| 478 | glRotatef(roty,0,1,0);
|
---|
| 479 | glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
|
---|
| 480 |
|
---|
| 481 | glClearColor(1.0,1.0,1.0,1.0);
|
---|
| 482 | glClearDepth(1.0);
|
---|
| 483 |
|
---|
| 484 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 485 |
|
---|
| 486 | glViewport(0,0,512*2,512*2);
|
---|
| 487 |
|
---|
| 488 | textureMatrix = biasMatrix * project * mview;
|
---|
| 489 |
|
---|
| 490 | glMatrixMode(GL_TEXTURE);
|
---|
| 491 | glLoadIdentity();
|
---|
| 492 | glMultMatrixf(textureMatrix);
|
---|
| 493 |
|
---|
| 494 | glEnable(GL_CULL_FACE);
|
---|
| 495 |
|
---|
| 496 | glUniform1f(Time0, angle);
|
---|
| 497 | glUniform1f(ModeDirect0, modeDirect);
|
---|
| 498 | glUniform1f(ModeIndirect0, modeIndirect);
|
---|
| 499 | glUniform1f(ModeAmbient0, modeAmbient);
|
---|
| 500 | glUniform1f(LightMap0, modeLight);
|
---|
| 501 | glUniform3f(kd0, 0.5,0.5,0.5);
|
---|
| 502 | glUniform3f(AmbientIntensity0, intensityr,intensityg,intensityb);
|
---|
| 503 | glUniform3f(LightPower0, lightr,lightg,lightb);
|
---|
| 504 | glUniform3f(Factor0, factor,factor,factor);
|
---|
| 505 | glUniform3f(lpos0, (Esf.radius+1.0) * cos(angle/50), (Esf.radius+1.0) * sin(angle/50), 1.0);
|
---|
| 506 | glUniformMatrix4fv(ProjParam0, 1, GL_FALSE, textureMatrix);
|
---|
| 507 | glActiveTexture(GL_TEXTURE0);
|
---|
| 508 | glBindTexture(GL_TEXTURE_2D, texNam[0]);
|
---|
| 509 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 510 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 511 | glUniform1i(texture0, 0);
|
---|
| 512 | glActiveTexture(GL_TEXTURE1);
|
---|
| 513 | glBindTexture(GL_TEXTURE_2D, texFBO[0]);
|
---|
| 514 | glUniform1i(texture20, 1);
|
---|
| 515 | glActiveTexture(GL_TEXTURE2);
|
---|
| 516 | glBindTexture(GL_TEXTURE_2D, texNam[1]);
|
---|
| 517 | glUniform1i(texture30,2);
|
---|
| 518 | glActiveTexture(GL_TEXTURE0);
|
---|
| 519 |
|
---|
| 520 | // Get FPS
|
---|
| 521 | if( GetTickCount() - g_dwLastFPS >= 1000 ) // When A Second Has Passed...
|
---|
| 522 | {
|
---|
| 523 | g_dwLastFPS = GetTickCount(); // Update Our Time Variable
|
---|
| 524 | g_nFPS = g_nFrames; // Save The FPS
|
---|
| 525 | g_nFrames = 0; // Reset The FPS Counter
|
---|
| 526 |
|
---|
| 527 | char szTitle[256]={0}; // Build The Title String
|
---|
| 528 | sprintf( szTitle, "Obscurances: Nicolau i Sergi - %d FPS", g_nFPS );
|
---|
| 529 | glutSetWindowTitle(szTitle);
|
---|
| 530 | }
|
---|
| 531 | g_nFrames++; // Increment Our FPS Counter
|
---|
| 532 |
|
---|
| 533 |
|
---|
| 534 | // Enable Pointers
|
---|
| 535 | glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
|
---|
| 536 | glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays
|
---|
| 537 | glEnableClientState( GL_COLOR_ARRAY );
|
---|
| 538 | glEnableClientState( GL_NORMAL_ARRAY );
|
---|
| 539 |
|
---|
| 540 | for(int i=0; i<geom.m_pObject._numGeos;i++)
|
---|
| 541 | {
|
---|
| 542 | for(int j=0; j<geom.m_pObject._geo[i]._numTriSets;j++)
|
---|
| 543 | {
|
---|
| 544 | // Set Pointers To Our Data
|
---|
| 545 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOVertices );
|
---|
| 546 | glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 547 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOColors );
|
---|
| 548 | glColorPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
|
---|
| 549 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOTexCoords );
|
---|
| 550 | glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 551 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBONormals );
|
---|
| 552 | glNormalPointer( GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
|
---|
| 553 | glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, geom.m_pObject._geo[i]._triSet[j].m_nVBOIndexs );
|
---|
| 554 |
|
---|
| 555 | glDrawElements( GL_TRIANGLES, geom.m_pObject._geo[i]._triSet[j]._numTris * 3, GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 | // Disable Pointers
|
---|
| 559 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
| 560 | glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
|
---|
| 561 | glDisableClientState( GL_COLOR_ARRAY );
|
---|
| 562 | glDisableClientState( GL_NORMAL_ARRAY );
|
---|
| 563 |
|
---|
| 564 | glUseProgram(0);
|
---|
| 565 |
|
---|
| 566 | glutSwapBuffers();
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | void Motion(int x,int y)
|
---|
| 571 | {
|
---|
| 572 | int diffx=x-lastx;
|
---|
| 573 | int diffy=y-lasty;
|
---|
| 574 | lastx=x;
|
---|
| 575 | lasty=y;
|
---|
| 576 |
|
---|
| 577 | if( Buttons[2] )
|
---|
| 578 | {
|
---|
| 579 | zoom -= (float) 0.05f * diffx;
|
---|
| 580 | }
|
---|
| 581 | else
|
---|
| 582 | if( Buttons[0] )
|
---|
| 583 | {
|
---|
| 584 | rotx += (float) 0.5f * diffy;
|
---|
| 585 | roty += (float) 0.5f * diffx;
|
---|
| 586 | }
|
---|
| 587 | else
|
---|
| 588 | if( Buttons[1] )
|
---|
| 589 | {
|
---|
| 590 | tx += (float) 0.05f * diffx;
|
---|
| 591 | ty -= (float) 0.05f * diffy;
|
---|
| 592 | }
|
---|
| 593 | glutPostRedisplay();
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | void Mouse(int b,int s,int x,int y)
|
---|
| 597 | {
|
---|
| 598 | lastx=x;
|
---|
| 599 | lasty=y;
|
---|
| 600 | switch(b)
|
---|
| 601 | {
|
---|
| 602 | case GLUT_LEFT_BUTTON:
|
---|
| 603 | if (GLUT_DOWN == s) Buttons[0] = 1;
|
---|
| 604 | else Buttons[0] = 0;
|
---|
| 605 | break;
|
---|
| 606 | case GLUT_MIDDLE_BUTTON:
|
---|
| 607 | if (GLUT_DOWN == s) Buttons[1] = 1;
|
---|
| 608 | else Buttons[1] = 0;
|
---|
| 609 | break;
|
---|
| 610 | case GLUT_RIGHT_BUTTON:
|
---|
| 611 | if (GLUT_DOWN == s) Buttons[2] = 1;
|
---|
| 612 | else Buttons[2] = 0;
|
---|
| 613 | break;
|
---|
| 614 | default:
|
---|
| 615 | break;
|
---|
| 616 | }
|
---|
| 617 | glutPostRedisplay();
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | IMPLEMENT_APP(Main)
|
---|
| 621 |
|
---|
| 622 | BEGIN_EVENT_TABLE(Main,wxApp)
|
---|
| 623 | EVT_IDLE(Main::OnIdle)
|
---|
| 624 | END_EVENT_TABLE()
|
---|
| 625 |
|
---|
| 626 | bool Main::OnInit()
|
---|
| 627 | {
|
---|
| 628 | //Inicializem els handlers per tractar amb imatges
|
---|
| 629 | wxInitAllImageHandlers();
|
---|
| 630 |
|
---|
| 631 | //Creem el Frame Principal del GUI
|
---|
| 632 | frame = new MyFrame("Generate GPU Obscurances",50,50,800,600);
|
---|
| 633 | frame->Show(true);
|
---|
| 634 | SetTopWindow(frame);
|
---|
| 635 |
|
---|
| 636 | return true;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | void Main::OnIdle(wxIdleEvent& event)
|
---|
| 640 | {
|
---|
| 641 | //Obtenim els resultats dels Views (estaran en threads?)
|
---|
| 642 | //... ids de textura
|
---|
| 643 |
|
---|
| 644 | //wake up idle:
|
---|
| 645 | wxWakeUpIdle();
|
---|
| 646 |
|
---|
| 647 | event.Skip(); // normal processing
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 |
|
---|
| 651 | //Main Program
|
---|
| 652 | /*->void main(int argc, char **argv)
|
---|
| 653 | {
|
---|
| 654 |
|
---|
| 655 | modeDirect = 0.0;
|
---|
| 656 | modeIndirect = 0.0;
|
---|
| 657 | modeAmbient = 0.0;
|
---|
| 658 |
|
---|
| 659 | glutInit(&argc, argv);
|
---|
| 660 |
|
---|
| 661 | glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
|
---|
| 662 |
|
---|
| 663 | glutInitWindowPosition(100,100);
|
---|
| 664 | glutInitWindowSize(1024,1024);
|
---|
| 665 | glutCreateWindow("Test Pbuffer");
|
---|
| 666 | glutReshapeFunc(changeSize1);
|
---|
| 667 |
|
---|
| 668 | GLenum err = glewInit();
|
---|
| 669 |
|
---|
| 670 | glutDisplayFunc(renderScene);
|
---|
| 671 |
|
---|
| 672 | glutIdleFunc(renderScene);
|
---|
| 673 |
|
---|
| 674 | glutMouseFunc(Mouse); |
---|
| 675 | glutMotionFunc(Motion);
|
---|
| 676 |
|
---|
| 677 | glutIgnoreKeyRepeat(1);
|
---|
| 678 | glutKeyboardFunc(processNormalKeys);
|
---|
| 679 |
|
---|
| 680 | init(argc, argv);
|
---|
| 681 |
|
---|
| 682 | glutMainLoop();
|
---|
| 683 | }*/
|
---|