[2642] | 1 | // occquery.cpp : Defines the entry point for the console application.
|
---|
| 2 | //
|
---|
[2818] | 3 | #include "glInterface.h"
|
---|
[2746] | 4 | #include <math.h>
|
---|
| 5 | #include <time.h>
|
---|
[2756] | 6 | #include "common.h"
|
---|
[2642] | 7 | #include "RenderTraverser.h"
|
---|
[2756] | 8 | #include "SceneEntity.h"
|
---|
| 9 | #include "Vector3.h"
|
---|
| 10 | #include "Matrix4x4.h"
|
---|
[2795] | 11 | #include "ResourceManager.h"
|
---|
[2756] | 12 | #include "Bvh.h"
|
---|
| 13 | #include "Camera.h"
|
---|
| 14 | #include "Geometry.h"
|
---|
[2760] | 15 | #include "BvhLoader.h"
|
---|
| 16 | #include "FrustumCullingTraverser.h"
|
---|
[2763] | 17 | #include "StopAndWaitTraverser.h"
|
---|
[2764] | 18 | #include "CHCTraverser.h"
|
---|
[2767] | 19 | #include "CHCPlusPlusTraverser.h"
|
---|
| 20 | #include "Visualization.h"
|
---|
[2769] | 21 | #include "RenderState.h"
|
---|
[2795] | 22 | #include "Timer/PerfTimer.h"
|
---|
[2796] | 23 | #include "SceneQuery.h"
|
---|
[2801] | 24 | #include "RenderQueue.h"
|
---|
[2819] | 25 | #include "Material.h"
|
---|
[2808] | 26 | #include <Cg/cg.h>
|
---|
| 27 | #include <Cg/cgGL.h>
|
---|
[2826] | 28 | #include "glfont2.h"
|
---|
[2827] | 29 | #include "PerformanceGraph.h"
|
---|
[2828] | 30 | #include "Environment.h"
|
---|
[2837] | 31 | #include "Halton.h"
|
---|
[2840] | 32 | #include "Transform3.h"
|
---|
[2853] | 33 | #include "SampleGenerator.h"
|
---|
[2642] | 34 |
|
---|
| 35 |
|
---|
[2756] | 36 | using namespace std;
|
---|
[2776] | 37 | using namespace CHCDemoEngine;
|
---|
[2642] | 38 |
|
---|
| 39 |
|
---|
[2828] | 40 | static Environment env;
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | /////////////
|
---|
[2834] | 44 | //-- fbos
|
---|
| 45 |
|
---|
[2809] | 46 | GLuint fbo;
|
---|
[2834] | 47 | GLuint fbo1;
|
---|
| 48 | GLuint fbo2;
|
---|
[2756] | 49 |
|
---|
[2834] | 50 | bool isFirstTexture = true;
|
---|
| 51 |
|
---|
| 52 | /////////////
|
---|
| 53 | //-- renderbuffers
|
---|
| 54 |
|
---|
| 55 |
|
---|
[2810] | 56 | GLuint depthBuffer;
|
---|
[2809] | 57 |
|
---|
[2810] | 58 | GLuint positionsBuffer;
|
---|
| 59 | GLuint colorsBuffer;
|
---|
[2834] | 60 | GLuint colorsBuffer1;
|
---|
| 61 | GLuint colorsBuffer2;
|
---|
[2810] | 62 | GLuint normalsBuffer;
|
---|
[2809] | 63 |
|
---|
[2834] | 64 |
|
---|
| 65 | /////////////
|
---|
| 66 | //-- textures
|
---|
| 67 |
|
---|
| 68 |
|
---|
[2810] | 69 | GLuint positionsTex;
|
---|
| 70 | GLuint colorsTex;
|
---|
[2834] | 71 | GLuint colorsTex1;
|
---|
| 72 | GLuint colorsTex2;
|
---|
| 73 |
|
---|
[2810] | 74 | GLuint normalsTex;
|
---|
[2809] | 75 |
|
---|
[2810] | 76 | GLuint noiseTex;
|
---|
| 77 |
|
---|
[2826] | 78 | GLuint fontTex;
|
---|
[2810] | 79 |
|
---|
[2756] | 80 | /// the renderable scene geometry
|
---|
| 81 | SceneEntityContainer sceneEntities;
|
---|
| 82 | // traverses and renders the hierarchy
|
---|
[2767] | 83 | RenderTraverser *traverser = NULL;
|
---|
[2756] | 84 | /// the hierarchy
|
---|
[2767] | 85 | Bvh *bvh = NULL;
|
---|
[2793] | 86 | /// handles scene loading
|
---|
[2795] | 87 | ResourceManager *loader = NULL;
|
---|
[2756] | 88 | /// the scene camera
|
---|
[2767] | 89 | Camera *camera = NULL;
|
---|
[2795] | 90 | /// the scene camera
|
---|
| 91 | Camera *visCamera = NULL;
|
---|
[2767] | 92 | /// the visualization
|
---|
| 93 | Visualization *visualization = NULL;
|
---|
[2760] | 94 | /// the current render state
|
---|
| 95 | RenderState state;
|
---|
[2764] | 96 | /// the rendering algorithm
|
---|
[2795] | 97 | int renderMode = RenderTraverser::CHCPLUSPLUS;
|
---|
[2756] | 98 | // eye near plane distance
|
---|
[2822] | 99 | float nearDist = 0.2f;
|
---|
[2771] | 100 | /// the pixel threshold where a node is still considered invisible
|
---|
| 101 | int threshold;
|
---|
[2764] | 102 |
|
---|
[2795] | 103 | float fov = 50.0f;
|
---|
| 104 |
|
---|
[2776] | 105 | int assumedVisibleFrames = 10;
|
---|
| 106 | int maxBatchSize = 50;
|
---|
[2771] | 107 |
|
---|
[2800] | 108 | int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES;
|
---|
[2767] | 109 |
|
---|
[2796] | 110 | SceneQuery *sceneQuery = NULL;
|
---|
[2801] | 111 | RenderQueue *renderQueue = NULL;
|
---|
[2796] | 112 |
|
---|
[2809] | 113 | /// these values get scaled with the frame rate
|
---|
[2828] | 114 | static float keyForwardMotion = 30.0f;
|
---|
| 115 | static float keyRotation = 1.5f;
|
---|
[2801] | 116 |
|
---|
[2826] | 117 | /// elapsed time in milliseconds
|
---|
| 118 | double elapsedTime = 1000.0f;
|
---|
| 119 | double algTime = 1000.0f;
|
---|
[2795] | 120 |
|
---|
[2809] | 121 | static int winWidth = 1024;
|
---|
[2813] | 122 | static int winHeight = 768;
|
---|
[2809] | 123 | static float winAspectRatio = 1.0f;
|
---|
[2642] | 124 |
|
---|
[2776] | 125 | double accumulatedTime = 1000;
|
---|
[2770] | 126 | float fps = 1e3f;
|
---|
| 127 |
|
---|
[2809] | 128 | float myfar = 0;
|
---|
| 129 |
|
---|
[2826] | 130 | glfont::GLFont myfont;
|
---|
| 131 |
|
---|
[2809] | 132 | // rendertexture
|
---|
[2828] | 133 | static int texWidth = 1024;
|
---|
| 134 | static int texHeight = 768;
|
---|
| 135 | //static int texWidth = 2048;
|
---|
| 136 | //static int texHeight = 2048;
|
---|
[2809] | 137 |
|
---|
[2770] | 138 | int renderedObjects = 0;
|
---|
[2773] | 139 | int renderedNodes = 0;
|
---|
| 140 | int renderedTriangles = 0;
|
---|
| 141 |
|
---|
[2770] | 142 | int issuedQueries = 0;
|
---|
| 143 | int traversedNodes = 0;
|
---|
| 144 | int frustumCulledNodes = 0;
|
---|
| 145 | int queryCulledNodes = 0;
|
---|
| 146 | int stateChanges = 0;
|
---|
[2800] | 147 | int numBatches = 0;
|
---|
[2770] | 148 |
|
---|
[2642] | 149 | bool showHelp = false;
|
---|
[2826] | 150 | bool showStatistics = false;
|
---|
| 151 | bool showOptions = false;
|
---|
[2642] | 152 | bool showBoundingVolumes = false;
|
---|
| 153 | bool visMode = false;
|
---|
| 154 |
|
---|
[2792] | 155 | // mouse navigation state
|
---|
[2809] | 156 | int xEyeBegin = 0;
|
---|
| 157 | int yEyeBegin = 0;
|
---|
| 158 | int yMotionBegin = 0;
|
---|
| 159 | int verticalMotionBegin = 0;
|
---|
| 160 | int horizontalMotionBegin = 0;
|
---|
[2642] | 161 |
|
---|
[2770] | 162 | bool useOptimization = false;
|
---|
[2786] | 163 | bool useTightBounds = true;
|
---|
[2795] | 164 | bool useRenderQueue = true;
|
---|
[2786] | 165 | bool useMultiQueries = true;
|
---|
[2800] | 166 | bool flyMode = true;
|
---|
| 167 |
|
---|
[2795] | 168 | SceneEntityContainer skyGeometry;
|
---|
| 169 |
|
---|
[2792] | 170 | bool leftKeyPressed = false;
|
---|
| 171 | bool rightKeyPressed = false;
|
---|
| 172 | bool upKeyPressed = false;
|
---|
| 173 | bool downKeyPressed = false;
|
---|
[2837] | 174 | bool descendKeyPressed = false;
|
---|
| 175 | bool ascendKeyPressed = false;
|
---|
[2787] | 176 |
|
---|
[2821] | 177 | bool useSsao = false;
|
---|
| 178 |
|
---|
[2826] | 179 | bool showAlgorithmTime = false;
|
---|
| 180 |
|
---|
[2820] | 181 | GLubyte *randomNormals = NULL;
|
---|
[2809] | 182 |
|
---|
[2819] | 183 | PerfTimer frameTimer, algTimer;
|
---|
| 184 |
|
---|
[2825] | 185 | int renderType = RenderState::FIXED;
|
---|
[2820] | 186 |
|
---|
[2827] | 187 | PerformanceGraph *perfGraph = NULL;
|
---|
[2825] | 188 |
|
---|
[2828] | 189 | bool useFullScreen = false;
|
---|
[2827] | 190 |
|
---|
[2853] | 191 | // exp factor for ssao
|
---|
| 192 | float expFactor = 0.05f;
|
---|
[2828] | 193 |
|
---|
| 194 | // ssao number of samples
|
---|
[2834] | 195 | //#define NUM_SAMPLES 8
|
---|
| 196 | #define NUM_SAMPLES 16
|
---|
[2820] | 197 |
|
---|
[2828] | 198 | // ssao random spherical samples
|
---|
[2820] | 199 | static float samples[NUM_SAMPLES * 2];
|
---|
| 200 |
|
---|
[2834] | 201 | static Matrix4x4 matProjectionView = IdentityMatrix();
|
---|
[2820] | 202 |
|
---|
[2834] | 203 |
|
---|
[2809] | 204 | // function forward declarations
|
---|
[2759] | 205 | void InitExtensions();
|
---|
[2756] | 206 | void DisplayVisualization();
|
---|
[2759] | 207 | void InitGLstate();
|
---|
[2809] | 208 | void InitRenderTexture();
|
---|
| 209 | void InitCg();
|
---|
[2759] | 210 | void CleanUp();
|
---|
| 211 | void SetupEyeView();
|
---|
| 212 | void UpdateEyeMtx();
|
---|
| 213 | void SetupLighting();
|
---|
[2764] | 214 | void DisplayStats();
|
---|
[2769] | 215 | void Output(int x, int y, const char *string);
|
---|
[2786] | 216 | void DrawHelpMessage();
|
---|
[2796] | 217 | void RenderSky();
|
---|
[2801] | 218 | void RenderVisibleObjects();
|
---|
[2756] | 219 |
|
---|
[2792] | 220 | void Begin2D();
|
---|
| 221 | void End2D();
|
---|
| 222 | void KeyBoard(unsigned char c, int x, int y);
|
---|
| 223 | void DrawStatistics();
|
---|
| 224 | void Display();
|
---|
| 225 | void Special(int c, int x, int y);
|
---|
| 226 | void KeyUp(unsigned char c, int x, int y);
|
---|
| 227 | void SpecialKeyUp(int c, int x, int y);
|
---|
| 228 | void Reshape(int w, int h);
|
---|
| 229 | void Mouse(int button, int state, int x, int y);
|
---|
| 230 | void LeftMotion(int x, int y);
|
---|
| 231 | void RightMotion(int x, int y);
|
---|
| 232 | void MiddleMotion(int x, int y);
|
---|
| 233 | void CalcDecimalPoint(string &str, int d);
|
---|
[2764] | 234 | void ResetTraverser();
|
---|
[2642] | 235 |
|
---|
[2792] | 236 | void KeyHorizontalMotion(float shift);
|
---|
[2794] | 237 | void KeyVerticalMotion(float shift);
|
---|
[2642] | 238 |
|
---|
[2801] | 239 | void PlaceViewer(const Vector3 &oldPos);
|
---|
[2809] | 240 | void DisplayRenderTexture();
|
---|
[2642] | 241 |
|
---|
[2801] | 242 |
|
---|
[2826] | 243 | inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; }
|
---|
| 244 | inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; }
|
---|
[2792] | 245 |
|
---|
[2809] | 246 | void InitFBO();
|
---|
[2795] | 247 |
|
---|
[2810] | 248 | void CreateNoiseTex2D();
|
---|
| 249 |
|
---|
[2809] | 250 | void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
|
---|
| 251 |
|
---|
[2820] | 252 | void GenerateSamples();
|
---|
| 253 |
|
---|
| 254 |
|
---|
[2810] | 255 | GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
|
---|
[2809] | 256 |
|
---|
| 257 |
|
---|
[2810] | 258 | /////////
|
---|
| 259 | //-- cg stuff
|
---|
| 260 |
|
---|
[2808] | 261 | static CGcontext sCgContext = NULL;
|
---|
[2810] | 262 | static CGprogram sCgMrtVertexProgram = NULL;
|
---|
[2821] | 263 | static CGprogram sCgSsaoProgram = NULL;
|
---|
| 264 | static CGprogram sCgDeferredProgram = NULL;
|
---|
[2795] | 265 |
|
---|
[2810] | 266 | static CGparameter sColorsTexParam;
|
---|
| 267 | static CGparameter sPositionsTexParam;
|
---|
| 268 | static CGparameter sNormalsTexParam;
|
---|
[2808] | 269 |
|
---|
[2821] | 270 | static CGparameter sColorsTexParamSsao;
|
---|
| 271 | static CGparameter sPositionsTexParamSsao;
|
---|
| 272 | static CGparameter sNormalsTexParamSsao;
|
---|
| 273 | static CGparameter sNoiseTexParamSsao;
|
---|
| 274 |
|
---|
[2818] | 275 | static CGparameter sModelViewProjMatrixParam;
|
---|
[2834] | 276 | static CGparameter sOldModelViewProjMatrixParam;
|
---|
[2809] | 277 | static CGparameter sMaxDepthParam;
|
---|
[2835] | 278 | static CGparameter sMaxDepthParamSsao;
|
---|
[2819] | 279 | static CGparameter sMaxDepthParamTex;
|
---|
[2808] | 280 |
|
---|
[2821] | 281 | static CGparameter sSamplesParamSsao;
|
---|
[2834] | 282 | static CGparameter sOldTexParamSsao;
|
---|
| 283 | static CGparameter sNoiseMultiplierParam;
|
---|
[2837] | 284 | static CGparameter sExpFactorParamSsao;
|
---|
[2820] | 285 |
|
---|
[2837] | 286 |
|
---|
[2809] | 287 | static void cgErrorCallback()
|
---|
| 288 | {
|
---|
| 289 | CGerror lastError = cgGetError();
|
---|
| 290 |
|
---|
| 291 | if(lastError)
|
---|
| 292 | {
|
---|
| 293 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
| 294 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
| 295 | printf("Cg error, exiting...\n");
|
---|
| 296 |
|
---|
| 297 | exit(0);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | static void PrintGLerror(char *msg)
|
---|
| 303 | {
|
---|
| 304 | GLenum errCode;
|
---|
| 305 | const GLubyte *errStr;
|
---|
| 306 |
|
---|
| 307 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 308 | {
|
---|
| 309 | errStr = gluErrorString(errCode);
|
---|
| 310 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 |
|
---|
[2642] | 315 | int main(int argc, char* argv[])
|
---|
| 316 | {
|
---|
[2781] | 317 | int returnCode = 0;
|
---|
| 318 |
|
---|
[2837] | 319 | Vector3 camPos(.0f, .0f, .0f);
|
---|
[2838] | 320 | Vector3 camDir(.0f, 1.0f, .0f);
|
---|
[2837] | 321 |
|
---|
[2830] | 322 | cout << "=== reading environment file === " << endl;
|
---|
| 323 |
|
---|
[2837] | 324 | string envFileName = "default.env";
|
---|
| 325 | if (!env.Read(envFileName))
|
---|
| 326 | {
|
---|
| 327 | cerr << "loading environment " << envFileName << " failed!" << endl;
|
---|
| 328 | }
|
---|
| 329 | else
|
---|
| 330 | {
|
---|
| 331 | env.GetIntParam(string("assumedVisibleFrames"), assumedVisibleFrames);
|
---|
| 332 | env.GetIntParam(string("maxBatchSize"), maxBatchSize);
|
---|
| 333 | env.GetIntParam(string("trianglesPerVirtualLeaf"), trianglesPerVirtualLeaf);
|
---|
[2828] | 334 |
|
---|
[2837] | 335 | env.GetFloatParam(string("keyForwardMotion"), keyForwardMotion);
|
---|
| 336 | env.GetFloatParam(string("keyRotation"), keyRotation);
|
---|
[2828] | 337 |
|
---|
[2837] | 338 | env.GetIntParam(string("winWidth"), winWidth);
|
---|
| 339 | env.GetIntParam(string("winHeight"), winHeight);
|
---|
[2828] | 340 |
|
---|
[2837] | 341 | env.GetBoolParam(string("useFullScreen"), useFullScreen);
|
---|
| 342 | env.GetFloatParam(string("expFactor"), expFactor);
|
---|
| 343 | env.GetVectorParam(string("camPosition"), camPos);
|
---|
[2838] | 344 | env.GetVectorParam(string("camDirection"), camDir);
|
---|
[2828] | 345 |
|
---|
[2846] | 346 | //env.GetStringParam(string("modelPath"), model_path);
|
---|
[2838] | 347 | //env.GetIntParam(string("numSssaoSamples"), numSsaoSamples);
|
---|
| 348 |
|
---|
[2837] | 349 | cout << "assumedVisibleFrames: " << assumedVisibleFrames << endl;
|
---|
| 350 | cout << "maxBatchSize: " << maxBatchSize << endl;
|
---|
| 351 | cout << "trianglesPerVirtualLeaf: " << trianglesPerVirtualLeaf << endl;
|
---|
[2828] | 352 |
|
---|
[2837] | 353 | cout << "keyForwardMotion: " << keyForwardMotion << endl;
|
---|
| 354 | cout << "keyRotation: " << keyRotation << endl;
|
---|
| 355 | cout << "winWidth: " << winWidth << endl;
|
---|
| 356 | cout << "winHeight: " << winHeight << endl;
|
---|
| 357 | cout << "useFullScreen: " << useFullScreen << endl;
|
---|
| 358 | cout << "camPosition: " << camPos << endl;
|
---|
| 359 | cout << "expFactor: " << expFactor << endl;
|
---|
[2846] | 360 | //cout << "model path: " << model_path << endl;
|
---|
[2837] | 361 | }
|
---|
[2829] | 362 |
|
---|
[2828] | 363 | ///////////////////////////
|
---|
| 364 |
|
---|
[2795] | 365 | camera = new Camera(winWidth, winHeight, fov);
|
---|
| 366 | camera->SetNear(nearDist);
|
---|
[2806] | 367 |
|
---|
[2838] | 368 | camera->SetDirection(camDir);
|
---|
[2829] | 369 | camera->SetPosition(camPos);
|
---|
| 370 |
|
---|
[2806] | 371 | visCamera = new Camera(winWidth, winHeight, fov);
|
---|
[2781] | 372 |
|
---|
[2796] | 373 | visCamera->SetNear(0.0f);
|
---|
| 374 | visCamera->Yaw(.5 * M_PI);
|
---|
[2781] | 375 |
|
---|
[2802] | 376 | renderQueue = new RenderQueue(&state, camera);
|
---|
[2801] | 377 |
|
---|
[2760] | 378 | glutInitWindowSize(winWidth, winHeight);
|
---|
[2756] | 379 | glutInit(&argc, argv);
|
---|
[2853] | 380 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
|
---|
| 381 | //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
|
---|
[2756] | 382 |
|
---|
[2850] | 383 | //glutInitDisplayString("samples=2");
|
---|
| 384 |
|
---|
[2828] | 385 | if (!useFullScreen)
|
---|
| 386 | glutCreateWindow("FriendlyCulling");
|
---|
| 387 | else
|
---|
| 388 | {
|
---|
| 389 | glutGameModeString( "1024x768:32@75" );
|
---|
| 390 | glutEnterGameMode();
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[2792] | 393 | glutDisplayFunc(Display);
|
---|
| 394 | glutKeyboardFunc(KeyBoard);
|
---|
| 395 | glutSpecialFunc(Special);
|
---|
| 396 | glutReshapeFunc(Reshape);
|
---|
| 397 | glutMouseFunc(Mouse);
|
---|
| 398 | glutIdleFunc(Display);
|
---|
| 399 | glutKeyboardUpFunc(KeyUp);
|
---|
| 400 | glutSpecialUpFunc(SpecialKeyUp);
|
---|
| 401 | glutIgnoreKeyRepeat(true);
|
---|
| 402 |
|
---|
[2829] | 403 | // initialise gl graphics
|
---|
[2756] | 404 | InitExtensions();
|
---|
| 405 | InitGLstate();
|
---|
[2850] | 406 |
|
---|
[2854] | 407 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 408 | glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
|
---|
[2850] | 409 |
|
---|
[2809] | 410 | InitFBO();
|
---|
[2810] | 411 |
|
---|
[2792] | 412 | LeftMotion(0, 0);
|
---|
| 413 | MiddleMotion(0, 0);
|
---|
[2756] | 414 |
|
---|
[2829] | 415 | perfGraph = new PerformanceGraph(1000);
|
---|
[2756] | 416 |
|
---|
[2795] | 417 | loader = new ResourceManager();
|
---|
[2793] | 418 |
|
---|
[2784] | 419 | //const string filename("data/city/model/city.dem");
|
---|
| 420 | const string filename = string(model_path + "city.dem");
|
---|
[2756] | 421 |
|
---|
[2793] | 422 | if (loader->Load(filename, sceneEntities))
|
---|
[2756] | 423 | cout << "scene " << filename << " loaded" << endl;
|
---|
| 424 | else
|
---|
[2784] | 425 | {
|
---|
[2756] | 426 | cerr << "loading scene " << filename << " failed" << endl;
|
---|
[2792] | 427 | CleanUp();
|
---|
[2784] | 428 | exit(0);
|
---|
| 429 | }
|
---|
[2756] | 430 |
|
---|
[2795] | 431 | SceneEntityContainer dummy;
|
---|
| 432 |
|
---|
| 433 | const string envname = string(model_path + "env.dem");
|
---|
| 434 |
|
---|
| 435 | if (loader->Load(envname, skyGeometry))
|
---|
| 436 | cout << "sky box " << filename << " loaded" << endl;
|
---|
| 437 | else
|
---|
| 438 | {
|
---|
| 439 | cerr << "loading sky box " << filename << " failed" << endl;
|
---|
| 440 | CleanUp();
|
---|
| 441 | exit(0);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[2784] | 444 | const string bvh_filename = string(model_path + "city.bvh");
|
---|
[2760] | 445 | BvhLoader bvhLoader;
|
---|
[2784] | 446 | bvh = bvhLoader.Load(bvh_filename, sceneEntities);
|
---|
[2762] | 447 |
|
---|
[2784] | 448 | if (!bvh)
|
---|
| 449 | {
|
---|
| 450 | cerr << "loading bvh " << bvh_filename << " failed" << endl;
|
---|
[2792] | 451 | CleanUp();
|
---|
[2784] | 452 | exit(0);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[2829] | 455 | // set far plane based on scene extent
|
---|
| 456 | myfar = 10.0f * Magnitude(bvh->GetBox().Diagonal());
|
---|
| 457 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 458 |
|
---|
| 459 | bvh->SetCamera(camera);
|
---|
| 460 |
|
---|
[2809] | 461 | InitCg();
|
---|
[2806] | 462 |
|
---|
[2825] | 463 | // create noise texture for ssao
|
---|
| 464 | CreateNoiseTex2D();
|
---|
[2847] | 465 |
|
---|
[2825] | 466 | // init render traverser
|
---|
[2764] | 467 | ResetTraverser();
|
---|
[2756] | 468 |
|
---|
[2787] | 469 | visualization = new Visualization(bvh, camera, NULL, &state);
|
---|
| 470 |
|
---|
[2843] | 471 | sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
|
---|
[2796] | 472 |
|
---|
[2847] | 473 | // frame time is restarted every frame
|
---|
| 474 | frameTimer.Start();
|
---|
[2800] | 475 |
|
---|
[2847] | 476 |
|
---|
[2642] | 477 | glutMainLoop();
|
---|
| 478 |
|
---|
| 479 | // clean up
|
---|
[2756] | 480 | CleanUp();
|
---|
| 481 |
|
---|
[2642] | 482 | return 0;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[2756] | 485 |
|
---|
[2809] | 486 | void InitCg(void)
|
---|
| 487 | {
|
---|
| 488 | // Setup Cg
|
---|
| 489 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 490 |
|
---|
| 491 | // Create cgContext.
|
---|
| 492 | sCgContext = cgCreateContext();
|
---|
| 493 |
|
---|
| 494 | // get the best profile for this hardware
|
---|
[2818] | 495 | RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
[2809] | 496 | //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
|
---|
[2818] | 497 | cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
|
---|
[2809] | 498 |
|
---|
[2818] | 499 | RenderState::sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
| 500 | cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
|
---|
[2809] | 501 |
|
---|
[2810] | 502 | sCgMrtVertexProgram =
|
---|
[2809] | 503 | cgCreateProgramFromFile(sCgContext,
|
---|
| 504 | CG_SOURCE,
|
---|
[2810] | 505 | "src/shaders/mrt.cg",
|
---|
[2818] | 506 | RenderState::sCgVertexProfile,
|
---|
[2810] | 507 | "vtx",
|
---|
[2809] | 508 | NULL);
|
---|
| 509 |
|
---|
[2821] | 510 | if (sCgMrtVertexProgram != NULL)
|
---|
[2809] | 511 | {
|
---|
[2810] | 512 | cgGLLoadProgram(sCgMrtVertexProgram);
|
---|
[2818] | 513 |
|
---|
[2840] | 514 | Transform3::sModelMatrixParam = cgGetNamedParameter(sCgMrtVertexProgram, "ModelMatrix");
|
---|
[2818] | 515 | sModelViewProjMatrixParam = cgGetNamedParameter(sCgMrtVertexProgram, "ModelViewProj");
|
---|
[2809] | 516 | }
|
---|
| 517 |
|
---|
[2819] | 518 | RenderState::sCgMrtFragmentTexProgram =
|
---|
[2809] | 519 | cgCreateProgramFromFile(sCgContext,
|
---|
| 520 | CG_SOURCE,
|
---|
[2810] | 521 | "src/shaders/mrt.cg",
|
---|
[2818] | 522 | RenderState::sCgFragmentProfile,
|
---|
[2819] | 523 | "fragtex",
|
---|
| 524 | NULL);
|
---|
| 525 |
|
---|
| 526 | if (RenderState::sCgMrtFragmentTexProgram != NULL)
|
---|
| 527 | {
|
---|
| 528 | cgGLLoadProgram(RenderState::sCgMrtFragmentTexProgram);
|
---|
| 529 |
|
---|
| 530 | sMaxDepthParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "maxDepth");
|
---|
| 531 | Material::sDiffuseTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "diffuse");
|
---|
[2822] | 532 | Material::sAmbientTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "ambient");
|
---|
[2819] | 533 |
|
---|
| 534 | cgGLSetParameter1f(sMaxDepthParamTex, 10.0f / myfar);
|
---|
[2834] | 535 | Debug << "maxdepth: " << 10.0f / myfar << endl;
|
---|
[2819] | 536 | }
|
---|
| 537 | else
|
---|
| 538 | cerr << "fragment tex program failed to load" << endl;
|
---|
| 539 |
|
---|
| 540 | RenderState::sCgMrtFragmentProgram =
|
---|
| 541 | cgCreateProgramFromFile(sCgContext,
|
---|
| 542 | CG_SOURCE,
|
---|
| 543 | "src/shaders/mrt.cg",
|
---|
| 544 | RenderState::sCgFragmentProfile,
|
---|
[2810] | 545 | "frag",
|
---|
[2809] | 546 | NULL);
|
---|
| 547 |
|
---|
[2819] | 548 | if (RenderState::sCgMrtFragmentProgram != NULL)
|
---|
[2809] | 549 | {
|
---|
[2819] | 550 | cgGLLoadProgram(RenderState::sCgMrtFragmentProgram);
|
---|
[2809] | 551 |
|
---|
[2819] | 552 | sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
|
---|
| 553 | Material::sDiffuseParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "diffuse");
|
---|
[2822] | 554 | Material::sAmbientParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "ambient");
|
---|
[2818] | 555 |
|
---|
| 556 | cgGLSetParameter1f(sMaxDepthParam, 10.0f / myfar);
|
---|
[2809] | 557 | }
|
---|
| 558 | else
|
---|
[2819] | 559 | cerr << "fragment program failed to load" << endl;
|
---|
[2809] | 560 |
|
---|
| 561 | PrintGLerror("test");
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 | ///////////////
|
---|
| 565 |
|
---|
[2821] | 566 | sCgSsaoProgram =
|
---|
[2809] | 567 | cgCreateProgramFromFile(sCgContext,
|
---|
| 568 | CG_SOURCE,
|
---|
[2810] | 569 | "src/shaders/deferred.cg",
|
---|
[2818] | 570 | RenderState::sCgFragmentProfile,
|
---|
[2821] | 571 | "main_ssao",
|
---|
[2809] | 572 | NULL);
|
---|
| 573 |
|
---|
[2821] | 574 | if (sCgSsaoProgram != NULL)
|
---|
[2809] | 575 | {
|
---|
[2821] | 576 | cgGLLoadProgram(sCgSsaoProgram);
|
---|
[2809] | 577 |
|
---|
| 578 | // we need size of texture for scaling
|
---|
[2821] | 579 | sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");
|
---|
| 580 | sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");
|
---|
| 581 | sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");
|
---|
| 582 | sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
|
---|
[2834] | 583 | sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
|
---|
| 584 | sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
|
---|
[2835] | 585 | sMaxDepthParamSsao = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
|
---|
[2837] | 586 | sExpFactorParamSsao = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
|
---|
[2820] | 587 |
|
---|
[2835] | 588 | cgGLSetParameter1f(sMaxDepthParamSsao, myfar / 10.0f);
|
---|
[2837] | 589 | cgGLSetParameter1f(sExpFactorParamSsao, expFactor);
|
---|
[2835] | 590 |
|
---|
[2821] | 591 | sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");
|
---|
[2834] | 592 | sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");
|
---|
| 593 |
|
---|
[2835] | 594 | GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
|
---|
[2809] | 595 | }
|
---|
| 596 | else
|
---|
| 597 | cerr << "ssao program failed to load" << endl;
|
---|
| 598 |
|
---|
[2821] | 599 |
|
---|
| 600 | sCgDeferredProgram =
|
---|
| 601 | cgCreateProgramFromFile(sCgContext,
|
---|
| 602 | CG_SOURCE,
|
---|
| 603 | "src/shaders/deferred.cg",
|
---|
| 604 | RenderState::sCgFragmentProfile,
|
---|
| 605 | "main",
|
---|
| 606 | NULL);
|
---|
| 607 |
|
---|
| 608 | if (sCgDeferredProgram != NULL)
|
---|
| 609 | {
|
---|
| 610 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
| 611 |
|
---|
| 612 | // we need size of texture for scaling
|
---|
| 613 | sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
| 614 | sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
[2834] | 615 | sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
[2821] | 616 | }
|
---|
| 617 | else
|
---|
| 618 | cerr << "deferred program failed to load" << endl;
|
---|
| 619 |
|
---|
| 620 |
|
---|
[2809] | 621 | PrintGLerror("init");
|
---|
[2818] | 622 |
|
---|
[2834] | 623 | cout << "cg initialization successful" << endl;
|
---|
[2809] | 624 | }
|
---|
| 625 |
|
---|
| 626 |
|
---|
| 627 | void PrintFBOStatus(GLenum status)
|
---|
| 628 | {
|
---|
| 629 | switch(status)
|
---|
| 630 | {
|
---|
| 631 | case GL_FRAMEBUFFER_COMPLETE_EXT:
|
---|
| 632 | cout << "frame buffer object created successfully" << endl;
|
---|
| 633 | break;
|
---|
| 634 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
---|
| 635 | cerr << "incomplete attachment" << endl;
|
---|
| 636 | break;
|
---|
| 637 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
---|
| 638 | cerr << "missing attachment" << endl;
|
---|
| 639 | break;
|
---|
| 640 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
---|
| 641 | cerr << "incomplete dimensions" << endl;
|
---|
| 642 | break;
|
---|
| 643 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
|
---|
| 644 | cerr << "incomplete formats" << endl;
|
---|
| 645 | break;
|
---|
| 646 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
---|
| 647 | cerr << "incomplete draw buffer" << endl;
|
---|
| 648 | break;
|
---|
| 649 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
---|
| 650 | cerr << "incomplete read buffer" << endl;
|
---|
| 651 | break;
|
---|
| 652 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
---|
| 653 | cerr << "framebuffer unsupported" << endl;
|
---|
| 654 | break;
|
---|
| 655 | default:
|
---|
| 656 | cerr << "unknown status code " << status << endl;
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
| 661 | void InitFBO()
|
---|
[2810] | 662 | {
|
---|
[2809] | 663 | glGenFramebuffersEXT(1, &fbo);
|
---|
| 664 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
|
---|
| 665 |
|
---|
[2822] | 666 |
|
---|
[2810] | 667 | ////////////
|
---|
| 668 | //-- colors buffer
|
---|
[2809] | 669 |
|
---|
[2810] | 670 | glGenRenderbuffersEXT(1, &colorsBuffer);
|
---|
| 671 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer);
|
---|
| 672 |
|
---|
[2838] | 673 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
|
---|
[2850] | 674 |
|
---|
[2822] | 675 | int samples = 8;
|
---|
| 676 | //glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 677 | //glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_RGBA8, texWidth, texHeight);
|
---|
[2810] | 678 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer);
|
---|
| 679 |
|
---|
| 680 | glGenTextures(1, &colorsTex);
|
---|
| 681 | glBindTexture(GL_TEXTURE_2D, colorsTex);
|
---|
| 682 |
|
---|
[2838] | 683 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
[2835] | 684 | //glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2810] | 685 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0);
|
---|
[2809] | 686 |
|
---|
| 687 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
[2835] | 688 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
[2809] | 689 |
|
---|
| 690 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 691 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 692 |
|
---|
[2822] | 693 |
|
---|
| 694 | //////////
|
---|
| 695 | //-- positions buffer
|
---|
| 696 |
|
---|
[2809] | 697 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
[2810] | 698 | glGenRenderbuffersEXT(1, &positionsBuffer);
|
---|
| 699 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, positionsBuffer);
|
---|
[2822] | 700 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
|
---|
| 701 |
|
---|
[2810] | 702 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, positionsBuffer);
|
---|
[2809] | 703 |
|
---|
[2810] | 704 | glGenTextures(1, &positionsTex);
|
---|
| 705 | glBindTexture(GL_TEXTURE_2D, positionsTex);
|
---|
[2834] | 706 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
[2817] | 707 | //glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2810] | 708 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0);
|
---|
[2809] | 709 |
|
---|
[2817] | 710 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 711 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
[2809] | 712 |
|
---|
[2817] | 713 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 714 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
[2810] | 715 |
|
---|
| 716 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 717 |
|
---|
| 718 |
|
---|
| 719 | ////////
|
---|
| 720 | //-- normals buffer
|
---|
| 721 |
|
---|
| 722 | glGenRenderbuffersEXT(1, &normalsBuffer);
|
---|
| 723 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, normalsBuffer);
|
---|
[2822] | 724 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight);
|
---|
[2810] | 725 |
|
---|
| 726 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, normalsBuffer);
|
---|
| 727 |
|
---|
| 728 | glGenTextures(1, &normalsTex);
|
---|
| 729 | glBindTexture(GL_TEXTURE_2D, normalsTex);
|
---|
[2834] | 730 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
---|
[2810] | 731 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0);
|
---|
| 732 |
|
---|
[2817] | 733 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 734 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
[2810] | 735 |
|
---|
[2809] | 736 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 737 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 738 |
|
---|
| 739 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 740 |
|
---|
[2810] | 741 |
|
---|
[2809] | 742 | ///////////
|
---|
[2810] | 743 | //-- create depth buffer
|
---|
[2809] | 744 |
|
---|
[2810] | 745 | glGenRenderbuffersEXT(1, &depthBuffer);
|
---|
| 746 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
|
---|
[2815] | 747 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);
|
---|
[2810] | 748 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);
|
---|
[2809] | 749 |
|
---|
| 750 |
|
---|
| 751 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 752 |
|
---|
[2834] | 753 |
|
---|
| 754 |
|
---|
| 755 | //////////////////////////////////
|
---|
| 756 |
|
---|
| 757 |
|
---|
| 758 |
|
---|
| 759 | glGenFramebuffersEXT(1, &fbo1);
|
---|
| 760 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);
|
---|
| 761 |
|
---|
| 762 |
|
---|
| 763 | ////////////
|
---|
| 764 | //-- colors buffer
|
---|
| 765 |
|
---|
| 766 | glGenRenderbuffersEXT(1, &colorsBuffer1);
|
---|
| 767 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1);
|
---|
| 768 |
|
---|
[2838] | 769 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
|
---|
[2834] | 770 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1);
|
---|
| 771 |
|
---|
| 772 | glGenTextures(1, &colorsTex1);
|
---|
| 773 | glBindTexture(GL_TEXTURE_2D, colorsTex1);
|
---|
| 774 |
|
---|
[2838] | 775 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
[2835] | 776 | //glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2834] | 777 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0);
|
---|
| 778 |
|
---|
[2835] | 779 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 780 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
---|
[2834] | 781 |
|
---|
| 782 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 783 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 784 |
|
---|
[2835] | 785 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 786 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 787 |
|
---|
[2834] | 788 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 789 |
|
---|
| 790 |
|
---|
| 791 |
|
---|
| 792 | /////////////////////////
|
---|
| 793 |
|
---|
| 794 | glGenFramebuffersEXT(1, &fbo2);
|
---|
| 795 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);
|
---|
| 796 |
|
---|
| 797 |
|
---|
| 798 | ////////////
|
---|
| 799 | //-- colors buffer
|
---|
| 800 |
|
---|
| 801 | glGenRenderbuffersEXT(1, &colorsBuffer2);
|
---|
| 802 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2);
|
---|
| 803 |
|
---|
[2838] | 804 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
|
---|
[2834] | 805 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2);
|
---|
| 806 |
|
---|
| 807 | glGenTextures(1, &colorsTex2);
|
---|
| 808 | glBindTexture(GL_TEXTURE_2D, colorsTex2);
|
---|
| 809 |
|
---|
[2838] | 810 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
|
---|
[2835] | 811 | //glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2834] | 812 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0);
|
---|
| 813 |
|
---|
[2835] | 814 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 815 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
---|
[2838] | 816 |
|
---|
[2835] | 817 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 818 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
[2837] | 819 |
|
---|
[2834] | 820 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
[2835] | 821 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
[2834] | 822 |
|
---|
| 823 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 824 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 825 |
|
---|
| 826 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 827 |
|
---|
[2809] | 828 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 829 |
|
---|
[2810] | 830 | PrintGLerror("fbo");
|
---|
[2809] | 831 | }
|
---|
| 832 |
|
---|
| 833 |
|
---|
[2827] | 834 | bool InitFont(void)
|
---|
[2642] | 835 | {
|
---|
[2826] | 836 | glEnable(GL_TEXTURE_2D);
|
---|
| 837 |
|
---|
| 838 | glGenTextures(1, &fontTex);
|
---|
| 839 | glBindTexture(GL_TEXTURE_2D, fontTex);
|
---|
[2829] | 840 | if (!myfont.Create("data/fonts/verdana.glf", fontTex))
|
---|
[2826] | 841 | return false;
|
---|
| 842 |
|
---|
| 843 | glDisable(GL_TEXTURE_2D);
|
---|
[2827] | 844 |
|
---|
[2826] | 845 | return true;
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 | void InitGLstate()
|
---|
| 850 | {
|
---|
[2767] | 851 | glClearColor(0.5f, 0.5f, 0.8f, 0.0f);
|
---|
[2642] | 852 |
|
---|
| 853 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
---|
| 854 | glPixelStorei(GL_PACK_ALIGNMENT,1);
|
---|
| 855 |
|
---|
| 856 | glDepthFunc(GL_LESS);
|
---|
[2762] | 857 | glEnable(GL_DEPTH_TEST);
|
---|
[2642] | 858 |
|
---|
[2759] | 859 | SetupLighting();
|
---|
[2642] | 860 |
|
---|
[2760] | 861 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2642] | 862 | glShadeModel(GL_SMOOTH);
|
---|
| 863 |
|
---|
| 864 | glMaterialf(GL_FRONT, GL_SHININESS, 64);
|
---|
| 865 | glEnable(GL_NORMALIZE);
|
---|
[2767] | 866 |
|
---|
| 867 | //glEnable(GL_ALPHA_TEST);
|
---|
| 868 | glDisable(GL_ALPHA_TEST);
|
---|
[2844] | 869 | glAlphaFunc(GL_GEQUAL, 0.8f);
|
---|
[2767] | 870 |
|
---|
[2642] | 871 | glFrontFace(GL_CCW);
|
---|
| 872 | glCullFace(GL_BACK);
|
---|
[2851] | 873 | glEnable(GL_CULL_FACE);
|
---|
| 874 |
|
---|
| 875 | //glDisable(GL_CULL_FACE);
|
---|
[2800] | 876 | glDisable(GL_TEXTURE_2D);
|
---|
[2762] | 877 |
|
---|
[2756] | 878 | GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};
|
---|
| 879 | GLfloat diffuseColor[] = {1.0, 0.0, 0.0, 1.0};
|
---|
[2759] | 880 | GLfloat specularColor[] = {0.0, 0.0, 0.0, 1.0};
|
---|
[2642] | 881 |
|
---|
[2756] | 882 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambientColor);
|
---|
| 883 | glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor);
|
---|
| 884 | glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
|
---|
[2801] | 885 |
|
---|
| 886 | glDepthFunc(GL_LESS);
|
---|
[2826] | 887 |
|
---|
[2827] | 888 | if (!InitFont())
|
---|
[2826] | 889 | cerr << "font creation failed" << endl;
|
---|
| 890 | else
|
---|
| 891 | cout << "successfully created font" << endl;
|
---|
[2642] | 892 | }
|
---|
| 893 |
|
---|
| 894 |
|
---|
[2827] | 895 | void DrawHelpMessage()
|
---|
[2826] | 896 | {
|
---|
[2642] | 897 | const char *message[] =
|
---|
| 898 | {
|
---|
| 899 | "Help information",
|
---|
| 900 | "",
|
---|
| 901 | "'F1' - shows/dismisses this message",
|
---|
[2795] | 902 | "'F2' - shows/hides bird eye view",
|
---|
[2790] | 903 | "'F3' - shows/hides bounds (boxes or tight bounds)",
|
---|
[2827] | 904 | "'F4', - shows/hides parameters",
|
---|
| 905 | "'F5' - shows/hides statistics",
|
---|
| 906 | "'F6', - toggles between fly/walkmode",
|
---|
[2826] | 907 | "'F7', - cycles throw render modes",
|
---|
| 908 | "'F8', - enables/disables ambient occlusion (only deferred)",
|
---|
| 909 | "'F9', - shows pure algorithm render time (using glFinish)",
|
---|
[2790] | 910 | "'SPACE' - cycles through occlusion culling algorithms",
|
---|
[2642] | 911 | "",
|
---|
[2827] | 912 | "'MOUSE LEFT' - turn left/right, move forward/backward",
|
---|
| 913 | "'MOUSE RIGHT' - turn left/right, move forward/backward",
|
---|
| 914 | "'MOUSE MIDDLE' - move up/down, left/right",
|
---|
| 915 | "'CURSOR UP/DOWN' - move forward/backward",
|
---|
| 916 | "'CURSOR LEFT/RIGHT' - turn left/right",
|
---|
[2642] | 917 | "",
|
---|
[2827] | 918 | "'-'/'+' - decreases/increases max batch size",
|
---|
[2837] | 919 | "'1'/'2' - downward/upward motion",
|
---|
| 920 | "'3'/'4' - decreases/increases triangles per virtual bvh leaf (sets bvh depth)",
|
---|
| 921 | "'5'/'6' - decreases/increases assumed visible frames",
|
---|
[2776] | 922 | "",
|
---|
[2786] | 923 | "'R' - use render queue",
|
---|
[2790] | 924 | "'B' - use tight bounds",
|
---|
| 925 | "'M' - use multiqueries",
|
---|
[2792] | 926 | "'O' - use CHC optimization (geometry queries for leaves)",
|
---|
[2642] | 927 | 0,
|
---|
| 928 | };
|
---|
| 929 |
|
---|
[2756] | 930 |
|
---|
[2827] | 931 | glColor4f(0.0f, 1.0f , 0.0f, 0.2f); // 20% green.
|
---|
[2756] | 932 |
|
---|
[2827] | 933 | glRecti(30, 30, winWidth - 30, winHeight - 30);
|
---|
[2642] | 934 |
|
---|
[2827] | 935 | glEnd();
|
---|
| 936 |
|
---|
[2756] | 937 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 938 |
|
---|
[2829] | 939 | glEnable(GL_TEXTURE_2D);
|
---|
[2827] | 940 | myfont.Begin();
|
---|
| 941 |
|
---|
| 942 | int x = 40, y = 30;
|
---|
| 943 |
|
---|
| 944 | for(int i = 0; message[i] != 0; ++ i)
|
---|
[2756] | 945 | {
|
---|
| 946 | if(message[i][0] == '\0')
|
---|
| 947 | {
|
---|
[2786] | 948 | y += 15;
|
---|
[2756] | 949 | }
|
---|
| 950 | else
|
---|
| 951 | {
|
---|
[2827] | 952 | myfont.DrawString(message[i], x, winHeight - y);
|
---|
| 953 | y += 25;
|
---|
[2642] | 954 | }
|
---|
| 955 | }
|
---|
[2829] | 956 | glDisable(GL_TEXTURE_2D);
|
---|
[2642] | 957 | }
|
---|
| 958 |
|
---|
| 959 |
|
---|
[2764] | 960 | void ResetTraverser()
|
---|
| 961 | {
|
---|
| 962 | DEL_PTR(traverser);
|
---|
| 963 |
|
---|
[2771] | 964 | bvh->ResetNodeClassifications();
|
---|
| 965 |
|
---|
[2764] | 966 | switch (renderMode)
|
---|
| 967 | {
|
---|
| 968 | case RenderTraverser::CULL_FRUSTUM:
|
---|
| 969 | traverser = new FrustumCullingTraverser();
|
---|
| 970 | break;
|
---|
| 971 | case RenderTraverser::STOP_AND_WAIT:
|
---|
| 972 | traverser = new StopAndWaitTraverser();
|
---|
| 973 | break;
|
---|
| 974 | case RenderTraverser::CHC:
|
---|
| 975 | traverser = new CHCTraverser();
|
---|
| 976 | break;
|
---|
[2767] | 977 | case RenderTraverser::CHCPLUSPLUS:
|
---|
| 978 | traverser = new CHCPlusPlusTraverser();
|
---|
| 979 | break;
|
---|
| 980 |
|
---|
[2764] | 981 | default:
|
---|
| 982 | traverser = new FrustumCullingTraverser();
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[2767] | 985 | traverser->SetCamera(camera);
|
---|
[2764] | 986 | traverser->SetHierarchy(bvh);
|
---|
[2801] | 987 | traverser->SetRenderQueue(renderQueue);
|
---|
[2764] | 988 | traverser->SetRenderState(&state);
|
---|
[2770] | 989 | traverser->SetUseOptimization(useOptimization);
|
---|
| 990 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
| 991 | traverser->SetVisibilityThreshold(threshold);
|
---|
[2776] | 992 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 993 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
| 994 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
[2786] | 995 | traverser->SetUseTightBounds(useTightBounds);
|
---|
[2825] | 996 | traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
|
---|
[2801] | 997 | traverser->SetRenderQueue(renderQueue);
|
---|
[2764] | 998 | }
|
---|
| 999 |
|
---|
| 1000 |
|
---|
[2759] | 1001 | void SetupLighting()
|
---|
[2642] | 1002 | {
|
---|
[2759] | 1003 | glEnable(GL_LIGHTING);
|
---|
| 1004 | glEnable(GL_LIGHT0);
|
---|
[2825] | 1005 | glEnable(GL_LIGHT1);
|
---|
| 1006 |
|
---|
[2759] | 1007 | GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
|
---|
| 1008 | GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 1009 | GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 1010 |
|
---|
[2825] | 1011 | GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
|
---|
[2759] | 1012 |
|
---|
| 1013 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
|
---|
| 1014 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
|
---|
| 1015 | glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
|
---|
| 1016 |
|
---|
| 1017 |
|
---|
| 1018 | ////////////
|
---|
| 1019 | //-- second light
|
---|
| 1020 |
|
---|
[2825] | 1021 | GLfloat ambient1[] = {0.2, 0.2, 0.2, 1.0};
|
---|
| 1022 | GLfloat diffuse1[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 1023 | //GLfloat diffuse1[] = {0.5, 0.5, 0.5, 1.0};
|
---|
| 1024 | GLfloat specular1[] = {0.0, 0.0, 0.0, 1.0};
|
---|
[2759] | 1025 |
|
---|
| 1026 | glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
|
---|
| 1027 | glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
|
---|
| 1028 | glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
|
---|
[2825] | 1029 |
|
---|
[2759] | 1030 |
|
---|
[2825] | 1031 | //////////////////////////////
|
---|
| 1032 |
|
---|
| 1033 | GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
|
---|
| 1034 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
| 1035 |
|
---|
| 1036 | GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
|
---|
[2759] | 1037 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
| 1038 |
|
---|
| 1039 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
[2801] | 1040 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
[2759] | 1041 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SINGLE_COLOR_EXT);
|
---|
[2642] | 1042 | }
|
---|
| 1043 |
|
---|
[2800] | 1044 |
|
---|
[2795] | 1045 | void SetupEyeView()
|
---|
[2642] | 1046 | {
|
---|
[2834] | 1047 | Matrix4x4 matViewing, matProjection;
|
---|
| 1048 |
|
---|
| 1049 | if (renderType == RenderState::DEFERRED)
|
---|
| 1050 | {
|
---|
| 1051 | cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x);
|
---|
| 1052 | }
|
---|
| 1053 |
|
---|
[2759] | 1054 | glMatrixMode(GL_PROJECTION);
|
---|
| 1055 | glLoadIdentity();
|
---|
| 1056 |
|
---|
[2833] | 1057 | gluPerspective(fov, winAspectRatio, nearDist, myfar);
|
---|
[2759] | 1058 |
|
---|
[2756] | 1059 | glMatrixMode(GL_MODELVIEW);
|
---|
[2795] | 1060 | glLoadIdentity();
|
---|
[2760] | 1061 | camera->SetupCameraView();
|
---|
| 1062 |
|
---|
[2825] | 1063 | GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
|
---|
[2759] | 1064 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
| 1065 |
|
---|
[2825] | 1066 | GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
|
---|
[2759] | 1067 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
[2825] | 1068 |
|
---|
| 1069 |
|
---|
[2834] | 1070 | camera->GetModelViewMatrix(matViewing);
|
---|
| 1071 | camera->GetProjectionMatrix(matProjection);
|
---|
| 1072 |
|
---|
| 1073 | matProjectionView = matViewing * matProjection;
|
---|
| 1074 |
|
---|
[2825] | 1075 | if (renderType == RenderState::DEFERRED)
|
---|
| 1076 | {
|
---|
| 1077 | // set modelview matrix for shaders
|
---|
| 1078 | cgGLSetStateMatrixParameter(sModelViewProjMatrixParam,
|
---|
| 1079 | CG_GL_MODELVIEW_PROJECTION_MATRIX,
|
---|
| 1080 | CG_GL_MATRIX_IDENTITY);
|
---|
| 1081 |
|
---|
| 1082 | static Matrix4x4 identity = IdentityMatrix();
|
---|
[2840] | 1083 | cgGLSetMatrixParameterfc(Transform3::sModelMatrixParam, (const float *)identity.x);
|
---|
[2834] | 1084 | }
|
---|
[2642] | 1085 | }
|
---|
| 1086 |
|
---|
| 1087 |
|
---|
[2792] | 1088 | void KeyHorizontalMotion(float shift)
|
---|
| 1089 | {
|
---|
| 1090 | Vector3 hvec = camera->GetDirection();
|
---|
| 1091 | hvec.z = 0;
|
---|
| 1092 |
|
---|
| 1093 | Vector3 pos = camera->GetPosition();
|
---|
| 1094 | pos += hvec * shift;
|
---|
| 1095 |
|
---|
| 1096 | camera->SetPosition(pos);
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 |
|
---|
[2794] | 1100 | void KeyVerticalMotion(float shift)
|
---|
| 1101 | {
|
---|
| 1102 | Vector3 uvec = Vector3(0, 0, shift);
|
---|
| 1103 |
|
---|
| 1104 | Vector3 pos = camera->GetPosition();
|
---|
| 1105 | pos += uvec;
|
---|
| 1106 |
|
---|
| 1107 | camera->SetPosition(pos);
|
---|
| 1108 | }
|
---|
| 1109 |
|
---|
| 1110 |
|
---|
[2792] | 1111 | void Display()
|
---|
[2801] | 1112 | {
|
---|
[2800] | 1113 | Vector3 oldPos = camera->GetPosition();
|
---|
| 1114 |
|
---|
[2792] | 1115 | if (leftKeyPressed)
|
---|
[2795] | 1116 | camera->Pitch(KeyRotationAngle());
|
---|
[2792] | 1117 | if (rightKeyPressed)
|
---|
[2795] | 1118 | camera->Pitch(-KeyRotationAngle());
|
---|
[2792] | 1119 | if (upKeyPressed)
|
---|
[2795] | 1120 | KeyHorizontalMotion(KeyShift());
|
---|
[2792] | 1121 | if (downKeyPressed)
|
---|
[2795] | 1122 | KeyHorizontalMotion(-KeyShift());
|
---|
[2837] | 1123 | if (ascendKeyPressed)
|
---|
| 1124 | KeyVerticalMotion(KeyShift());
|
---|
| 1125 | if (descendKeyPressed)
|
---|
[2795] | 1126 | KeyVerticalMotion(-KeyShift());
|
---|
[2792] | 1127 |
|
---|
[2801] | 1128 | // place view on ground
|
---|
| 1129 | if (!flyMode) PlaceViewer(oldPos);
|
---|
[2800] | 1130 |
|
---|
[2826] | 1131 | if (showAlgorithmTime)
|
---|
| 1132 | {
|
---|
| 1133 | glFinish();
|
---|
| 1134 | algTimer.Start();
|
---|
| 1135 | }
|
---|
[2809] | 1136 |
|
---|
[2825] | 1137 | // render without shading
|
---|
| 1138 | switch (renderType)
|
---|
| 1139 | {
|
---|
| 1140 | case RenderState::FIXED:
|
---|
| 1141 |
|
---|
[2851] | 1142 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 1143 | //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
| 1144 |
|
---|
[2825] | 1145 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
[2809] | 1146 |
|
---|
[2825] | 1147 | state.SetRenderType(RenderState::FIXED);
|
---|
| 1148 | glEnable(GL_LIGHTING);
|
---|
[2809] | 1149 |
|
---|
[2825] | 1150 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1151 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
[2809] | 1152 |
|
---|
[2825] | 1153 | glDrawBuffers(1, mrt);
|
---|
| 1154 |
|
---|
[2829] | 1155 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1156 |
|
---|
[2825] | 1157 | break;
|
---|
| 1158 |
|
---|
| 1159 | case RenderState::DEPTH_PASS:
|
---|
[2851] | 1160 |
|
---|
| 1161 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 1162 | //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
| 1163 |
|
---|
[2825] | 1164 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 1165 |
|
---|
| 1166 | state.SetRenderType(RenderState::DEPTH_PASS);
|
---|
| 1167 | glDisable(GL_LIGHTING);
|
---|
| 1168 |
|
---|
| 1169 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1170 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
[2809] | 1171 |
|
---|
[2829] | 1172 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1173 |
|
---|
| 1174 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 1175 |
|
---|
[2825] | 1176 | glDrawBuffers(1, mrt);
|
---|
| 1177 |
|
---|
| 1178 | break;
|
---|
| 1179 |
|
---|
| 1180 | case RenderState::DEFERRED:
|
---|
[2851] | 1181 |
|
---|
| 1182 | // multisampling not working with deferred shading
|
---|
| 1183 | //glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
| 1184 | glDisable(GL_MULTISAMPLE_ARB);
|
---|
| 1185 |
|
---|
[2825] | 1186 | state.SetRenderType(RenderState::DEFERRED);
|
---|
| 1187 |
|
---|
| 1188 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
|
---|
| 1189 |
|
---|
| 1190 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1191 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 1192 |
|
---|
| 1193 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1194 | cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
|
---|
| 1195 |
|
---|
| 1196 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
| 1197 | cgGLBindProgram(sCgMrtVertexProgram);
|
---|
| 1198 |
|
---|
| 1199 | glDrawBuffers(3, mrt);
|
---|
| 1200 |
|
---|
[2829] | 1201 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1202 |
|
---|
[2825] | 1203 | break;
|
---|
| 1204 | }
|
---|
| 1205 |
|
---|
[2642] | 1206 |
|
---|
[2801] | 1207 | glDepthFunc(GL_LESS);
|
---|
[2825] | 1208 |
|
---|
[2801] | 1209 | glDisable(GL_TEXTURE_2D);
|
---|
| 1210 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
[2825] | 1211 |
|
---|
[2801] | 1212 |
|
---|
[2847] | 1213 | LODLevel::InitFrame(camera->GetPosition());
|
---|
| 1214 |
|
---|
| 1215 |
|
---|
[2756] | 1216 | // bring eye modelview matrix up-to-date
|
---|
| 1217 | SetupEyeView();
|
---|
[2642] | 1218 |
|
---|
[2819] | 1219 |
|
---|
[2844] | 1220 | /*state.Reset();
|
---|
| 1221 | state.SetState(RenderState::RENDER);
|
---|
| 1222 |
|
---|
| 1223 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 1224 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 1225 |
|
---|
| 1226 | for (int i = 0; i < loader->mSceneEntities.size(); ++ i)
|
---|
| 1227 | {
|
---|
| 1228 | SceneEntity *ent = loader->mSceneEntities[i];
|
---|
| 1229 |
|
---|
| 1230 | ent->UpdateLODs(camera);
|
---|
| 1231 | ent->Render(&state);
|
---|
| 1232 | }*/
|
---|
| 1233 |
|
---|
[2767] | 1234 | // actually render the scene geometry using one of the specified algorithms
|
---|
| 1235 | traverser->RenderScene();
|
---|
[2795] | 1236 |
|
---|
[2801] | 1237 |
|
---|
[2826] | 1238 |
|
---|
[2825] | 1239 | /////////
|
---|
[2809] | 1240 | //-- do the rest of the rendering
|
---|
[2801] | 1241 |
|
---|
| 1242 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 1243 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 1244 |
|
---|
| 1245 |
|
---|
| 1246 | // reset depth pass and render visible objects
|
---|
[2825] | 1247 | if (renderType == RenderState::DEPTH_PASS)
|
---|
[2801] | 1248 | {
|
---|
[2829] | 1249 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
[2801] | 1250 | RenderVisibleObjects();
|
---|
| 1251 | }
|
---|
[2764] | 1252 |
|
---|
[2801] | 1253 |
|
---|
[2796] | 1254 | ///////////////
|
---|
| 1255 | //-- render sky
|
---|
[2795] | 1256 |
|
---|
[2796] | 1257 | RenderSky();
|
---|
[2801] | 1258 |
|
---|
| 1259 | state.Reset();
|
---|
| 1260 |
|
---|
| 1261 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 1262 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 1263 |
|
---|
[2825] | 1264 | if (renderType == RenderState::DEFERRED)
|
---|
| 1265 | {
|
---|
| 1266 | glPopAttrib();
|
---|
| 1267 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
[2810] | 1268 |
|
---|
[2825] | 1269 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1270 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2809] | 1271 |
|
---|
[2825] | 1272 | glDrawBuffers(1, mrt);
|
---|
[2809] | 1273 |
|
---|
[2825] | 1274 | DisplayRenderTexture();
|
---|
| 1275 | }
|
---|
[2801] | 1276 |
|
---|
[2827] | 1277 |
|
---|
| 1278 | ///////////
|
---|
| 1279 |
|
---|
| 1280 | state.SetRenderType(RenderState::FIXED);
|
---|
| 1281 |
|
---|
[2826] | 1282 | if (showAlgorithmTime)
|
---|
| 1283 | {
|
---|
| 1284 | glFinish();
|
---|
[2827] | 1285 |
|
---|
[2826] | 1286 | algTime = algTimer.Elapsedms();
|
---|
[2827] | 1287 | perfGraph->AddData(algTime);
|
---|
[2828] | 1288 |
|
---|
[2827] | 1289 | perfGraph->Draw();
|
---|
[2826] | 1290 | }
|
---|
[2827] | 1291 | else
|
---|
| 1292 | {
|
---|
| 1293 | if (visMode) DisplayVisualization();
|
---|
| 1294 | }
|
---|
[2825] | 1295 |
|
---|
[2847] | 1296 | glFlush();
|
---|
| 1297 |
|
---|
| 1298 | const bool restart = true;
|
---|
| 1299 | elapsedTime = frameTimer.Elapsedms(restart);
|
---|
| 1300 |
|
---|
[2764] | 1301 | DisplayStats();
|
---|
[2767] | 1302 |
|
---|
[2834] | 1303 | isFirstTexture = !isFirstTexture;
|
---|
| 1304 |
|
---|
[2642] | 1305 | glutSwapBuffers();
|
---|
| 1306 | }
|
---|
| 1307 |
|
---|
| 1308 |
|
---|
| 1309 | #pragma warning( disable : 4100 )
|
---|
[2792] | 1310 | void KeyBoard(unsigned char c, int x, int y)
|
---|
[2642] | 1311 | {
|
---|
| 1312 | switch(c)
|
---|
| 1313 | {
|
---|
| 1314 | case 27:
|
---|
[2792] | 1315 | CleanUp();
|
---|
[2642] | 1316 | exit(0);
|
---|
| 1317 | break;
|
---|
| 1318 | case 32: //space
|
---|
[2800] | 1319 | renderMode = (renderMode + 1) % RenderTraverser::NUM_TRAVERSAL_TYPES;
|
---|
[2764] | 1320 | ResetTraverser();
|
---|
[2642] | 1321 | break;
|
---|
| 1322 | case 'h':
|
---|
| 1323 | case 'H':
|
---|
| 1324 | showHelp = !showHelp;
|
---|
| 1325 | break;
|
---|
| 1326 | case '+':
|
---|
[2776] | 1327 | maxBatchSize += 10;
|
---|
| 1328 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
[2642] | 1329 | break;
|
---|
| 1330 | case '-':
|
---|
[2776] | 1331 | maxBatchSize -= 10;
|
---|
| 1332 | if (maxBatchSize < 0) maxBatchSize = 1;
|
---|
| 1333 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
[2642] | 1334 | break;
|
---|
[2837] | 1335 | case 'M':
|
---|
| 1336 | case 'm':
|
---|
| 1337 | useMultiQueries = !useMultiQueries;
|
---|
| 1338 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
| 1339 | break;
|
---|
| 1340 | case '1':
|
---|
| 1341 | descendKeyPressed = true;
|
---|
| 1342 | break;
|
---|
| 1343 | case '2':
|
---|
| 1344 | ascendKeyPressed = true;
|
---|
| 1345 | break;
|
---|
| 1346 | case '3':
|
---|
| 1347 | if (trianglesPerVirtualLeaf >= 100)
|
---|
| 1348 | trianglesPerVirtualLeaf -= 100;
|
---|
| 1349 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1350 | break;
|
---|
| 1351 | case '4':
|
---|
| 1352 | trianglesPerVirtualLeaf += 100;
|
---|
| 1353 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1354 | break;
|
---|
| 1355 | case '5':
|
---|
[2776] | 1356 | assumedVisibleFrames -= 1;
|
---|
| 1357 | if (assumedVisibleFrames < 1) assumedVisibleFrames = 1;
|
---|
| 1358 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1359 | break;
|
---|
[2837] | 1360 | case '6':
|
---|
[2776] | 1361 | assumedVisibleFrames += 1;
|
---|
| 1362 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1363 | break;
|
---|
[2837] | 1364 | case '7':
|
---|
| 1365 | expFactor *= 0.5f;
|
---|
[2776] | 1366 | break;
|
---|
[2767] | 1367 | case '8':
|
---|
[2837] | 1368 | expFactor *= 2.0f;
|
---|
| 1369 | if (expFactor > 1.0f) expFactor = 1.0f;
|
---|
| 1370 | break;
|
---|
[2792] | 1371 | case 'o':
|
---|
| 1372 | case 'O':
|
---|
[2642] | 1373 | useOptimization = !useOptimization;
|
---|
[2764] | 1374 | traverser->SetUseOptimization(useOptimization);
|
---|
[2767] | 1375 | break;
|
---|
| 1376 | case 'a':
|
---|
| 1377 | case 'A':
|
---|
[2792] | 1378 | leftKeyPressed = true;
|
---|
[2767] | 1379 | break;
|
---|
| 1380 | case 'd':
|
---|
| 1381 | case 'D':
|
---|
[2792] | 1382 | rightKeyPressed = true;
|
---|
[2767] | 1383 | break;
|
---|
| 1384 | case 'w':
|
---|
| 1385 | case 'W':
|
---|
[2792] | 1386 | upKeyPressed = true;
|
---|
[2767] | 1387 | break;
|
---|
[2829] | 1388 | case 's':
|
---|
| 1389 | case 'S':
|
---|
[2792] | 1390 | downKeyPressed = true;
|
---|
[2767] | 1391 | break;
|
---|
| 1392 | case 'r':
|
---|
| 1393 | case 'R':
|
---|
| 1394 | {
|
---|
| 1395 | useRenderQueue = !useRenderQueue;
|
---|
| 1396 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
| 1397 | }
|
---|
[2790] | 1398 | break;
|
---|
[2786] | 1399 | case 'b':
|
---|
| 1400 | case 'B':
|
---|
| 1401 | {
|
---|
| 1402 | useTightBounds = !useTightBounds;
|
---|
| 1403 | traverser->SetUseTightBounds(useTightBounds);
|
---|
| 1404 | }
|
---|
[2790] | 1405 | break;
|
---|
[2642] | 1406 | default:
|
---|
| 1407 | return;
|
---|
| 1408 | }
|
---|
| 1409 |
|
---|
| 1410 | glutPostRedisplay();
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
| 1413 |
|
---|
[2792] | 1414 | void SpecialKeyUp(int c, int x, int y)
|
---|
[2642] | 1415 | {
|
---|
[2792] | 1416 | switch (c)
|
---|
| 1417 | {
|
---|
| 1418 | case GLUT_KEY_LEFT:
|
---|
| 1419 | leftKeyPressed = false;
|
---|
| 1420 | break;
|
---|
| 1421 | case GLUT_KEY_RIGHT:
|
---|
| 1422 | rightKeyPressed = false;
|
---|
| 1423 | break;
|
---|
| 1424 | case GLUT_KEY_UP:
|
---|
| 1425 | upKeyPressed = false;
|
---|
| 1426 | break;
|
---|
| 1427 | case GLUT_KEY_DOWN:
|
---|
| 1428 | downKeyPressed = false;
|
---|
| 1429 | break;
|
---|
| 1430 | default:
|
---|
| 1431 | return;
|
---|
| 1432 | }
|
---|
| 1433 | //glutPostRedisplay();
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
| 1436 |
|
---|
| 1437 | void KeyUp(unsigned char c, int x, int y)
|
---|
| 1438 | {
|
---|
| 1439 | switch (c)
|
---|
| 1440 | {
|
---|
| 1441 | case 'A':
|
---|
| 1442 | case 'a':
|
---|
| 1443 | leftKeyPressed = false;
|
---|
| 1444 | break;
|
---|
| 1445 | case 'D':
|
---|
| 1446 | case 'd':
|
---|
| 1447 | rightKeyPressed = false;
|
---|
| 1448 | break;
|
---|
| 1449 | case 'W':
|
---|
| 1450 | case 'w':
|
---|
| 1451 | upKeyPressed = false;
|
---|
| 1452 | break;
|
---|
[2829] | 1453 | case 'S':
|
---|
| 1454 | case 's':
|
---|
[2792] | 1455 | downKeyPressed = false;
|
---|
| 1456 | break;
|
---|
[2837] | 1457 | case '1':
|
---|
| 1458 | descendKeyPressed = false;
|
---|
[2794] | 1459 | break;
|
---|
[2837] | 1460 | case '2':
|
---|
| 1461 | ascendKeyPressed = false;
|
---|
[2794] | 1462 | break;
|
---|
| 1463 |
|
---|
[2792] | 1464 | default:
|
---|
| 1465 | return;
|
---|
| 1466 | }
|
---|
| 1467 | //glutPostRedisplay();
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 |
|
---|
| 1471 | void Special(int c, int x, int y)
|
---|
| 1472 | {
|
---|
[2642] | 1473 | switch(c)
|
---|
| 1474 | {
|
---|
| 1475 | case GLUT_KEY_F1:
|
---|
| 1476 | showHelp = !showHelp;
|
---|
| 1477 | break;
|
---|
[2790] | 1478 | case GLUT_KEY_F2:
|
---|
[2795] | 1479 | visMode = !visMode;
|
---|
[2790] | 1480 | break;
|
---|
| 1481 | case GLUT_KEY_F3:
|
---|
| 1482 | showBoundingVolumes = !showBoundingVolumes;
|
---|
| 1483 | traverser->SetShowBounds(showBoundingVolumes);
|
---|
| 1484 | break;
|
---|
| 1485 | case GLUT_KEY_F4:
|
---|
[2827] | 1486 | showOptions = !showOptions;
|
---|
[2790] | 1487 | break;
|
---|
| 1488 | case GLUT_KEY_F5:
|
---|
[2827] | 1489 | showStatistics = !showStatistics;
|
---|
[2790] | 1490 | break;
|
---|
[2800] | 1491 | case GLUT_KEY_F6:
|
---|
| 1492 | flyMode = !flyMode;
|
---|
| 1493 | break;
|
---|
[2801] | 1494 | case GLUT_KEY_F7:
|
---|
[2825] | 1495 |
|
---|
| 1496 | renderType = (renderType + 1) % 3;
|
---|
| 1497 | traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
|
---|
| 1498 |
|
---|
[2801] | 1499 | break;
|
---|
[2803] | 1500 | case GLUT_KEY_F8:
|
---|
[2821] | 1501 | useSsao = !useSsao;
|
---|
| 1502 | break;
|
---|
[2826] | 1503 | case GLUT_KEY_F9:
|
---|
| 1504 | showAlgorithmTime = !showAlgorithmTime;
|
---|
| 1505 | break;
|
---|
| 1506 |
|
---|
[2642] | 1507 | case GLUT_KEY_LEFT:
|
---|
[2767] | 1508 | {
|
---|
[2792] | 1509 | leftKeyPressed = true;
|
---|
[2795] | 1510 | camera->Pitch(KeyRotationAngle());
|
---|
[2767] | 1511 | }
|
---|
[2642] | 1512 | break;
|
---|
| 1513 | case GLUT_KEY_RIGHT:
|
---|
[2767] | 1514 | {
|
---|
[2792] | 1515 | rightKeyPressed = true;
|
---|
[2795] | 1516 | camera->Pitch(-KeyRotationAngle());
|
---|
[2767] | 1517 | }
|
---|
[2642] | 1518 | break;
|
---|
| 1519 | case GLUT_KEY_UP:
|
---|
[2767] | 1520 | {
|
---|
[2792] | 1521 | upKeyPressed = true;
|
---|
[2795] | 1522 | KeyHorizontalMotion(KeyShift());
|
---|
[2767] | 1523 | }
|
---|
[2642] | 1524 | break;
|
---|
| 1525 | case GLUT_KEY_DOWN:
|
---|
[2767] | 1526 | {
|
---|
[2792] | 1527 | downKeyPressed = true;
|
---|
[2795] | 1528 | KeyHorizontalMotion(-KeyShift());
|
---|
[2767] | 1529 | }
|
---|
[2642] | 1530 | break;
|
---|
| 1531 | default:
|
---|
| 1532 | return;
|
---|
| 1533 |
|
---|
| 1534 | }
|
---|
| 1535 |
|
---|
| 1536 | glutPostRedisplay();
|
---|
| 1537 | }
|
---|
[2767] | 1538 |
|
---|
[2642] | 1539 | #pragma warning( default : 4100 )
|
---|
| 1540 |
|
---|
| 1541 |
|
---|
[2792] | 1542 | void Reshape(int w, int h)
|
---|
[2642] | 1543 | {
|
---|
[2759] | 1544 | winAspectRatio = 1.0f;
|
---|
[2642] | 1545 |
|
---|
| 1546 | glViewport(0, 0, w, h);
|
---|
| 1547 |
|
---|
| 1548 | winWidth = w;
|
---|
| 1549 | winHeight = h;
|
---|
| 1550 |
|
---|
[2833] | 1551 | if (w) winAspectRatio = (float) w / (float) h;
|
---|
[2642] | 1552 |
|
---|
[2758] | 1553 | glMatrixMode(GL_PROJECTION);
|
---|
| 1554 | glLoadIdentity();
|
---|
[2642] | 1555 |
|
---|
[2833] | 1556 | gluPerspective(fov, winAspectRatio, nearDist, myfar);
|
---|
[2788] | 1557 |
|
---|
[2758] | 1558 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1559 |
|
---|
[2642] | 1560 | glutPostRedisplay();
|
---|
| 1561 | }
|
---|
| 1562 |
|
---|
| 1563 |
|
---|
[2792] | 1564 | void Mouse(int button, int state, int x, int y)
|
---|
[2642] | 1565 | {
|
---|
[2758] | 1566 | if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
|
---|
[2642] | 1567 | {
|
---|
| 1568 | xEyeBegin = x;
|
---|
| 1569 | yMotionBegin = y;
|
---|
| 1570 |
|
---|
[2792] | 1571 | glutMotionFunc(LeftMotion);
|
---|
[2642] | 1572 | }
|
---|
[2758] | 1573 | else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
|
---|
[2642] | 1574 | {
|
---|
[2829] | 1575 | xEyeBegin = x;
|
---|
[2758] | 1576 | yEyeBegin = y;
|
---|
| 1577 | yMotionBegin = y;
|
---|
| 1578 |
|
---|
[2792] | 1579 | glutMotionFunc(RightMotion);
|
---|
[2758] | 1580 | }
|
---|
| 1581 | else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
|
---|
| 1582 | {
|
---|
[2642] | 1583 | horizontalMotionBegin = x;
|
---|
| 1584 | verticalMotionBegin = y;
|
---|
[2792] | 1585 | glutMotionFunc(MiddleMotion);
|
---|
[2642] | 1586 | }
|
---|
| 1587 |
|
---|
| 1588 | glutPostRedisplay();
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
[2758] | 1591 |
|
---|
| 1592 | /** rotation for left/right mouse drag
|
---|
[2642] | 1593 | motion for up/down mouse drag
|
---|
| 1594 | */
|
---|
[2792] | 1595 | void LeftMotion(int x, int y)
|
---|
[2642] | 1596 | {
|
---|
[2758] | 1597 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1598 | Vector3 pos = camera->GetPosition();
|
---|
| 1599 |
|
---|
| 1600 | // don't move in the vertical direction
|
---|
[2764] | 1601 | Vector3 horView(viewDir[0], viewDir[1], 0);
|
---|
[2642] | 1602 |
|
---|
[2795] | 1603 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
[2756] | 1604 |
|
---|
[2795] | 1605 | camera->Pitch(eyeXAngle);
|
---|
[2787] | 1606 |
|
---|
[2780] | 1607 | pos += horView * (yMotionBegin - y) * 0.2f;
|
---|
[2795] | 1608 |
|
---|
[2758] | 1609 | camera->SetPosition(pos);
|
---|
[2642] | 1610 |
|
---|
| 1611 | xEyeBegin = x;
|
---|
| 1612 | yMotionBegin = y;
|
---|
[2758] | 1613 |
|
---|
[2642] | 1614 | glutPostRedisplay();
|
---|
| 1615 | }
|
---|
| 1616 |
|
---|
[2758] | 1617 |
|
---|
[2767] | 1618 | /** rotation for left / right mouse drag
|
---|
| 1619 | motion for up / down mouse drag
|
---|
[2758] | 1620 | */
|
---|
[2792] | 1621 | void RightMotion(int x, int y)
|
---|
[2758] | 1622 | {
|
---|
[2829] | 1623 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
[2795] | 1624 | float eyeYAngle = -0.2f * M_PI * (yEyeBegin - y) / 180.0;
|
---|
[2758] | 1625 |
|
---|
[2795] | 1626 | camera->Yaw(eyeYAngle);
|
---|
[2829] | 1627 | camera->Pitch(eyeXAngle);
|
---|
[2780] | 1628 |
|
---|
[2829] | 1629 | xEyeBegin = x;
|
---|
[2758] | 1630 | yEyeBegin = y;
|
---|
[2829] | 1631 |
|
---|
[2758] | 1632 | glutPostRedisplay();
|
---|
| 1633 | }
|
---|
| 1634 |
|
---|
| 1635 |
|
---|
[2642] | 1636 | // strafe
|
---|
[2792] | 1637 | void MiddleMotion(int x, int y)
|
---|
[2642] | 1638 | {
|
---|
[2758] | 1639 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1640 | Vector3 pos = camera->GetPosition();
|
---|
| 1641 |
|
---|
[2642] | 1642 | // the 90 degree rotated view vector
|
---|
| 1643 | // y zero so we don't move in the vertical
|
---|
[2764] | 1644 | Vector3 rVec(viewDir[0], viewDir[1], 0);
|
---|
[2642] | 1645 |
|
---|
[2764] | 1646 | Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f);
|
---|
[2758] | 1647 | rVec = rot * rVec;
|
---|
[2642] | 1648 |
|
---|
[2780] | 1649 | pos -= rVec * (x - horizontalMotionBegin) * 0.1f;
|
---|
[2764] | 1650 | pos[2] += (verticalMotionBegin - y) * 0.1f;
|
---|
[2642] | 1651 |
|
---|
[2758] | 1652 | camera->SetPosition(pos);
|
---|
| 1653 |
|
---|
[2642] | 1654 | horizontalMotionBegin = x;
|
---|
| 1655 | verticalMotionBegin = y;
|
---|
[2758] | 1656 |
|
---|
[2642] | 1657 | glutPostRedisplay();
|
---|
| 1658 | }
|
---|
| 1659 |
|
---|
| 1660 |
|
---|
[2756] | 1661 | void InitExtensions(void)
|
---|
[2642] | 1662 | {
|
---|
| 1663 | GLenum err = glewInit();
|
---|
[2756] | 1664 |
|
---|
[2642] | 1665 | if (GLEW_OK != err)
|
---|
| 1666 | {
|
---|
| 1667 | // problem: glewInit failed, something is seriously wrong
|
---|
| 1668 | fprintf(stderr,"Error: %s\n", glewGetErrorString(err));
|
---|
| 1669 | exit(1);
|
---|
| 1670 | }
|
---|
[2756] | 1671 | if (!GLEW_ARB_occlusion_query)
|
---|
[2642] | 1672 | {
|
---|
[2756] | 1673 | printf("I require the GL_ARB_occlusion_query to work.\n");
|
---|
[2642] | 1674 | exit(1);
|
---|
| 1675 | }
|
---|
| 1676 | }
|
---|
| 1677 |
|
---|
| 1678 |
|
---|
[2826] | 1679 | void Begin2D()
|
---|
[2642] | 1680 | {
|
---|
| 1681 | glDisable(GL_LIGHTING);
|
---|
| 1682 | glDisable(GL_DEPTH_TEST);
|
---|
| 1683 |
|
---|
[2826] | 1684 | glMatrixMode(GL_PROJECTION);
|
---|
[2642] | 1685 | glPushMatrix();
|
---|
| 1686 | glLoadIdentity();
|
---|
[2834] | 1687 |
|
---|
[2826] | 1688 | gluOrtho2D(0, winWidth, 0, winHeight);
|
---|
[2642] | 1689 |
|
---|
[2826] | 1690 | glMatrixMode(GL_MODELVIEW);
|
---|
[2642] | 1691 | glPushMatrix();
|
---|
| 1692 | glLoadIdentity();
|
---|
| 1693 | }
|
---|
| 1694 |
|
---|
| 1695 |
|
---|
[2826] | 1696 | void End2D()
|
---|
[2642] | 1697 | {
|
---|
[2834] | 1698 | glMatrixMode(GL_PROJECTION);
|
---|
[2642] | 1699 | glPopMatrix();
|
---|
[2834] | 1700 |
|
---|
[2642] | 1701 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1702 | glPopMatrix();
|
---|
| 1703 |
|
---|
| 1704 | glEnable(GL_LIGHTING);
|
---|
| 1705 | glEnable(GL_DEPTH_TEST);
|
---|
| 1706 | }
|
---|
| 1707 |
|
---|
| 1708 |
|
---|
[2787] | 1709 | // displays the visualisation of culling algorithm
|
---|
| 1710 | void DisplayVisualization()
|
---|
[2796] | 1711 | {
|
---|
[2787] | 1712 | visualization->SetFrameId(traverser->GetCurrentFrameId());
|
---|
| 1713 |
|
---|
[2792] | 1714 | Begin2D();
|
---|
[2642] | 1715 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 1716 | glEnable(GL_BLEND);
|
---|
[2827] | 1717 | glColor4f(0.0f ,0.0f, 0.0f, 0.5f);
|
---|
[2642] | 1718 |
|
---|
[2827] | 1719 | glRecti(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth, winHeight);
|
---|
[2642] | 1720 | glDisable(GL_BLEND);
|
---|
[2792] | 1721 | End2D();
|
---|
[2788] | 1722 |
|
---|
| 1723 |
|
---|
| 1724 | AxisAlignedBox3 box = bvh->GetBox();
|
---|
| 1725 |
|
---|
[2838] | 1726 | // hack: set far plane for viz
|
---|
[2807] | 1727 | camera->SetFar(0.35f * Magnitude(box.Diagonal()));
|
---|
[2796] | 1728 |
|
---|
[2807] | 1729 | const float offs = box.Size().x * 0.3f;
|
---|
[2834] | 1730 |
|
---|
[2838] | 1731 | //Vector3 vizpos = Vector3(box.Min().x, box.Min().y + box.Size().y * 0.5f, box.Min().z + box.Size().z * 50);
|
---|
| 1732 | Vector3 vizpos = Vector3(box.Min().x, box.Min().y - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
|
---|
[2806] | 1733 |
|
---|
[2795] | 1734 | visCamera->SetPosition(vizpos);
|
---|
[2838] | 1735 | visCamera->ResetPitchAndYaw();
|
---|
| 1736 | //visCamera->Pitch(M_PI);
|
---|
| 1737 |
|
---|
[2834] | 1738 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2806] | 1739 | glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3);
|
---|
[2796] | 1740 |
|
---|
| 1741 | glMatrixMode(GL_PROJECTION);
|
---|
[2834] | 1742 | glPushMatrix();
|
---|
| 1743 |
|
---|
[2787] | 1744 | glLoadIdentity();
|
---|
[2796] | 1745 |
|
---|
[2807] | 1746 | glOrtho(-offs, offs, -offs, offs, 0.0f, box.Size().z * 100.0f);
|
---|
[2787] | 1747 |
|
---|
[2796] | 1748 | glMatrixMode(GL_MODELVIEW);
|
---|
[2834] | 1749 | glPushMatrix();
|
---|
[2787] | 1750 |
|
---|
[2796] | 1751 | visCamera->SetupCameraView();
|
---|
[2806] | 1752 |
|
---|
[2838] | 1753 | //Matrix4x4 rotX = RotationXMatrix(camera->GetYaw());
|
---|
| 1754 | //glMultMatrixf((float *)rotX.x);
|
---|
[2806] | 1755 | Matrix4x4 rotZ = RotationZMatrix(-camera->GetPitch());
|
---|
| 1756 | glMultMatrixf((float *)rotZ.x);
|
---|
| 1757 |
|
---|
[2838] | 1758 | Vector3 pos = camera->GetPosition();
|
---|
[2806] | 1759 | glTranslatef(-pos.x, -pos.y, -pos.z);
|
---|
| 1760 |
|
---|
| 1761 |
|
---|
[2788] | 1762 | GLfloat position[] = {0.8f, 1.0f, 1.5f, 0.0f};
|
---|
| 1763 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
[2787] | 1764 |
|
---|
[2788] | 1765 | GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f};
|
---|
| 1766 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
[2787] | 1767 |
|
---|
[2642] | 1768 | glClear(GL_DEPTH_BUFFER_BIT);
|
---|
| 1769 |
|
---|
[2806] | 1770 |
|
---|
[2767] | 1771 | ////////////
|
---|
[2787] | 1772 | //-- visualization of the occlusion culling
|
---|
| 1773 |
|
---|
[2767] | 1774 | visualization->Render();
|
---|
| 1775 |
|
---|
[2834] | 1776 |
|
---|
| 1777 | // reset previous settings
|
---|
| 1778 | glPopAttrib();
|
---|
| 1779 |
|
---|
| 1780 | glMatrixMode(GL_PROJECTION);
|
---|
| 1781 | glPopMatrix();
|
---|
| 1782 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1783 | glPopMatrix();
|
---|
[2642] | 1784 | }
|
---|
| 1785 |
|
---|
[2767] | 1786 |
|
---|
[2642] | 1787 | // cleanup routine after the main loop
|
---|
[2756] | 1788 | void CleanUp()
|
---|
[2642] | 1789 | {
|
---|
[2756] | 1790 | DEL_PTR(traverser);
|
---|
[2796] | 1791 | DEL_PTR(sceneQuery);
|
---|
[2756] | 1792 | DEL_PTR(bvh);
|
---|
[2767] | 1793 | DEL_PTR(visualization);
|
---|
[2787] | 1794 | DEL_PTR(camera);
|
---|
[2793] | 1795 | DEL_PTR(loader);
|
---|
[2801] | 1796 | DEL_PTR(renderQueue);
|
---|
[2827] | 1797 | DEL_PTR(perfGraph);
|
---|
[2809] | 1798 |
|
---|
[2810] | 1799 | if (sCgMrtVertexProgram)
|
---|
| 1800 | cgDestroyProgram(sCgMrtVertexProgram);
|
---|
[2827] | 1801 | if (RenderState::sCgMrtFragmentProgram)
|
---|
| 1802 | cgDestroyProgram(RenderState::sCgMrtFragmentProgram);
|
---|
| 1803 | if (RenderState::sCgMrtFragmentTexProgram)
|
---|
| 1804 | cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
|
---|
[2821] | 1805 | if (sCgSsaoProgram)
|
---|
| 1806 | cgDestroyProgram(sCgSsaoProgram);
|
---|
| 1807 | if (sCgDeferredProgram)
|
---|
| 1808 | cgDestroyProgram(sCgDeferredProgram);
|
---|
[2809] | 1809 | if (sCgContext)
|
---|
| 1810 | cgDestroyContext(sCgContext);
|
---|
[2817] | 1811 |
|
---|
| 1812 | glDeleteFramebuffersEXT(1, &fbo);
|
---|
| 1813 | glDeleteRenderbuffersEXT(1, &depthBuffer);
|
---|
| 1814 | glDeleteRenderbuffersEXT(1, &colorsBuffer);
|
---|
| 1815 | glDeleteRenderbuffersEXT(1, &normalsBuffer);
|
---|
| 1816 | glDeleteRenderbuffersEXT(1, &positionsBuffer);
|
---|
| 1817 |
|
---|
| 1818 | glDeleteTextures(1, &colorsTex);
|
---|
| 1819 | glDeleteTextures(1, &normalsTex);
|
---|
| 1820 | glDeleteTextures(1, &positionsTex);
|
---|
| 1821 | glDeleteTextures(1, &noiseTex);
|
---|
[2827] | 1822 | glDeleteTextures(1, &fontTex);
|
---|
[2642] | 1823 | }
|
---|
| 1824 |
|
---|
| 1825 |
|
---|
| 1826 | // this function inserts a dezimal point after each 1000
|
---|
[2829] | 1827 | void CalcDecimalPoint(string &str, int d, int len)
|
---|
[2642] | 1828 | {
|
---|
[2827] | 1829 | static vector<int> numbers;
|
---|
| 1830 | numbers.clear();
|
---|
| 1831 |
|
---|
[2829] | 1832 | static string shortStr;
|
---|
| 1833 | shortStr.clear();
|
---|
[2642] | 1834 |
|
---|
[2829] | 1835 | static char hstr[100];
|
---|
| 1836 |
|
---|
[2642] | 1837 | while (d != 0)
|
---|
| 1838 | {
|
---|
| 1839 | numbers.push_back(d % 1000);
|
---|
| 1840 | d /= 1000;
|
---|
| 1841 | }
|
---|
| 1842 |
|
---|
| 1843 | // first element without leading zeros
|
---|
| 1844 | if (numbers.size() > 0)
|
---|
| 1845 | {
|
---|
[2800] | 1846 | sprintf(hstr, "%d", numbers.back());
|
---|
[2829] | 1847 | shortStr.append(hstr);
|
---|
[2642] | 1848 | }
|
---|
| 1849 |
|
---|
[2764] | 1850 | for (int i = (int)numbers.size() - 2; i >= 0; i--)
|
---|
[2642] | 1851 | {
|
---|
[2800] | 1852 | sprintf(hstr, ",%03d", numbers[i]);
|
---|
[2829] | 1853 | shortStr.append(hstr);
|
---|
[2642] | 1854 | }
|
---|
[2829] | 1855 |
|
---|
| 1856 | int dif = len - (int)shortStr.size();
|
---|
| 1857 |
|
---|
| 1858 | for (int i = 0; i < dif; ++ i)
|
---|
| 1859 | {
|
---|
| 1860 | str += " ";
|
---|
| 1861 | }
|
---|
| 1862 |
|
---|
| 1863 | str.append(shortStr);
|
---|
[2764] | 1864 | }
|
---|
| 1865 |
|
---|
| 1866 |
|
---|
| 1867 | void DisplayStats()
|
---|
| 1868 | {
|
---|
[2826] | 1869 | static char msg[9][300];
|
---|
[2764] | 1870 |
|
---|
[2826] | 1871 | static double frameTime = elapsedTime;
|
---|
| 1872 | static double renderTime = algTime;
|
---|
| 1873 |
|
---|
[2818] | 1874 | const float expFactor = 0.1f;
|
---|
[2767] | 1875 |
|
---|
[2802] | 1876 | // if some strange render time spike happened in this frame => don't count
|
---|
[2826] | 1877 | if (elapsedTime < 500) frameTime = elapsedTime * expFactor + (1.0f - expFactor) * elapsedTime;
|
---|
| 1878 |
|
---|
| 1879 | static float rTime = 1000.0f;
|
---|
[2764] | 1880 |
|
---|
[2826] | 1881 | if (showAlgorithmTime)
|
---|
| 1882 | {
|
---|
| 1883 | if (algTime < 500) renderTime = algTime * expFactor + (1.0f - expFactor) * renderTime;
|
---|
| 1884 | }
|
---|
[2802] | 1885 |
|
---|
[2826] | 1886 | accumulatedTime += elapsedTime;
|
---|
| 1887 |
|
---|
[2776] | 1888 | if (accumulatedTime > 500) // update every fraction of a second
|
---|
[2770] | 1889 | {
|
---|
| 1890 | accumulatedTime = 0;
|
---|
| 1891 |
|
---|
[2826] | 1892 | if (frameTime) fps = 1e3f / (float)frameTime;
|
---|
| 1893 |
|
---|
| 1894 | rTime = renderTime;
|
---|
[2770] | 1895 | renderedObjects = traverser->GetStats().mNumRenderedGeometry;
|
---|
[2773] | 1896 | renderedNodes = traverser->GetStats().mNumRenderedNodes;
|
---|
| 1897 | renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
|
---|
| 1898 |
|
---|
[2770] | 1899 | traversedNodes = traverser->GetStats().mNumTraversedNodes;
|
---|
| 1900 | frustumCulledNodes = traverser->GetStats().mNumFrustumCulledNodes;
|
---|
| 1901 | queryCulledNodes = traverser->GetStats().mNumQueryCulledNodes;
|
---|
| 1902 | issuedQueries = traverser->GetStats().mNumIssuedQueries;
|
---|
| 1903 | stateChanges = traverser->GetStats().mNumStateChanges;
|
---|
[2800] | 1904 | numBatches = traverser->GetStats().mNumBatches;
|
---|
[2770] | 1905 | }
|
---|
| 1906 |
|
---|
[2764] | 1907 |
|
---|
[2826] | 1908 | Begin2D();
|
---|
[2808] | 1909 |
|
---|
[2826] | 1910 | glEnable(GL_BLEND);
|
---|
| 1911 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
[2764] | 1912 |
|
---|
[2826] | 1913 | if (showHelp)
|
---|
| 1914 | {
|
---|
| 1915 | DrawHelpMessage();
|
---|
| 1916 | }
|
---|
| 1917 | else
|
---|
| 1918 | {
|
---|
[2829] | 1919 | if (showOptions)
|
---|
| 1920 | {
|
---|
| 1921 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1922 | glRecti(5, winHeight - 95, winWidth * 2 / 3 - 5, winHeight - 5);
|
---|
| 1923 | }
|
---|
| 1924 |
|
---|
| 1925 | if (showStatistics)
|
---|
| 1926 | {
|
---|
| 1927 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1928 | glRecti(5, winHeight - 165, winWidth * 2 / 3 - 5, winHeight - 100);
|
---|
| 1929 | }
|
---|
| 1930 |
|
---|
| 1931 | glEnable(GL_TEXTURE_2D);
|
---|
| 1932 |
|
---|
[2827] | 1933 | myfont.Begin();
|
---|
[2769] | 1934 |
|
---|
[2826] | 1935 | if (showOptions)
|
---|
| 1936 | {
|
---|
[2829] | 1937 | glColor3f(0.0f, 1.0f, 0.0f);
|
---|
| 1938 |
|
---|
[2826] | 1939 | int i = 0;
|
---|
| 1940 |
|
---|
| 1941 | static char *renderTypeStr[] = {"fixed function", "fixed function + depth pass", "deferred shading"};
|
---|
[2825] | 1942 |
|
---|
[2826] | 1943 | sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d",
|
---|
| 1944 | useMultiQueries, useTightBounds, useRenderQueue);
|
---|
[2792] | 1945 |
|
---|
[2826] | 1946 | sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderTypeStr[renderType], useSsao);
|
---|
[2808] | 1947 |
|
---|
[2826] | 1948 | sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
|
---|
[2808] | 1949 |
|
---|
[2826] | 1950 | sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d",
|
---|
| 1951 | assumedVisibleFrames, maxBatchSize);
|
---|
[2808] | 1952 |
|
---|
[2826] | 1953 | for (int j = 0; j < 4; ++ j)
|
---|
[2829] | 1954 | myfont.DrawString(msg[j], 10.0f, winHeight - 5 - j * 20);
|
---|
[2826] | 1955 | }
|
---|
[2808] | 1956 |
|
---|
[2786] | 1957 | if (showStatistics)
|
---|
[2764] | 1958 | {
|
---|
[2829] | 1959 | glColor3f(1.0f, 1.0f, 0.0f);
|
---|
| 1960 |
|
---|
[2826] | 1961 | string str;
|
---|
| 1962 | string str2;
|
---|
| 1963 |
|
---|
[2829] | 1964 | int len = 10;
|
---|
| 1965 | CalcDecimalPoint(str, renderedTriangles, len);
|
---|
| 1966 | CalcDecimalPoint(str2, bvh->GetBvhStats().mTriangles, len);
|
---|
[2826] | 1967 |
|
---|
| 1968 | int i = 4;
|
---|
| 1969 |
|
---|
[2829] | 1970 | sprintf(msg[i ++], "rendered: %6d of %6d nodes, %s of %s triangles",
|
---|
[2826] | 1971 | renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str());
|
---|
| 1972 |
|
---|
| 1973 | sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d",
|
---|
| 1974 | traversedNodes, frustumCulledNodes, queryCulledNodes);
|
---|
| 1975 |
|
---|
| 1976 | sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d",
|
---|
| 1977 | issuedQueries, stateChanges, numBatches);
|
---|
| 1978 |
|
---|
| 1979 | for (int j = 4; j < 7; ++ j)
|
---|
[2829] | 1980 | myfont.DrawString(msg[j], 10.0f, winHeight - (j + 1) * 20);
|
---|
[2764] | 1981 | }
|
---|
[2790] | 1982 |
|
---|
[2826] | 1983 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 1984 | static char *alg_str[] = {"Frustum Cull", "Stop and Wait", "CHC", "CHC ++"};
|
---|
[2827] | 1985 |
|
---|
| 1986 | if (!showAlgorithmTime)
|
---|
| 1987 | {
|
---|
| 1988 | sprintf(msg[7], "%s: %6.1f fps", alg_str[renderMode], fps);
|
---|
| 1989 | }
|
---|
| 1990 | else
|
---|
| 1991 | {
|
---|
| 1992 | sprintf(msg[7], "%s: %6.1f ms", alg_str[renderMode], rTime);
|
---|
| 1993 | }
|
---|
[2826] | 1994 |
|
---|
[2829] | 1995 | myfont.DrawString(msg[7], 1.3f, 690.0f, 760.0f);//, top_color, bottom_color);
|
---|
[2827] | 1996 |
|
---|
| 1997 | //sprintf(msg[8], "algorithm time: %6.1f ms", rTime);
|
---|
| 1998 | //myfont.DrawString(msg[8], 720.0f, 730.0f);
|
---|
[2764] | 1999 | }
|
---|
| 2000 |
|
---|
[2826] | 2001 | glDisable(GL_BLEND);
|
---|
| 2002 | glDisable(GL_TEXTURE_2D);
|
---|
| 2003 |
|
---|
[2792] | 2004 | End2D();
|
---|
[2764] | 2005 | }
|
---|
[2796] | 2006 |
|
---|
| 2007 |
|
---|
| 2008 | void RenderSky()
|
---|
| 2009 | {
|
---|
| 2010 | SceneEntityContainer::const_iterator sit, sit_end = skyGeometry.end();
|
---|
| 2011 |
|
---|
| 2012 | for (sit = skyGeometry.begin(); sit != sit_end; ++ sit)
|
---|
| 2013 | (*sit)->Render(&state);
|
---|
[2801] | 2014 | }
|
---|
[2796] | 2015 |
|
---|
| 2016 |
|
---|
[2801] | 2017 | void RenderVisibleObjects()
|
---|
| 2018 | {
|
---|
[2825] | 2019 | state.SetRenderType(RenderState::FIXED);
|
---|
[2796] | 2020 | state.Reset();
|
---|
[2801] | 2021 |
|
---|
| 2022 | glEnable(GL_LIGHTING);
|
---|
| 2023 | glDepthFunc(GL_LEQUAL);
|
---|
| 2024 |
|
---|
| 2025 | //cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
|
---|
| 2026 |
|
---|
| 2027 | SceneEntityContainer::const_iterator sit,
|
---|
| 2028 | sit_end = traverser->GetVisibleObjects().end();
|
---|
| 2029 |
|
---|
| 2030 | for (sit = traverser->GetVisibleObjects().begin(); sit != sit_end; ++ sit)
|
---|
| 2031 | renderQueue->Enqueue(*sit);
|
---|
| 2032 |
|
---|
| 2033 | renderQueue->Apply();
|
---|
| 2034 |
|
---|
| 2035 | glDepthFunc(GL_LESS);
|
---|
| 2036 | }
|
---|
| 2037 |
|
---|
| 2038 |
|
---|
| 2039 | void PlaceViewer(const Vector3 &oldPos)
|
---|
| 2040 | {
|
---|
| 2041 | Vector3 playerPos = camera->GetPosition();
|
---|
| 2042 |
|
---|
[2843] | 2043 | bool validIntersect = sceneQuery->CalcIntersection(playerPos);
|
---|
[2801] | 2044 |
|
---|
[2853] | 2045 | if (validIntersect)
|
---|
[2848] | 2046 | // && ((playerPos.z - oldPos.z) < bvh->GetBox().Size(2) * 1e-1f))
|
---|
[2801] | 2047 | {
|
---|
| 2048 | camera->SetPosition(playerPos);
|
---|
| 2049 | }
|
---|
[2809] | 2050 | }
|
---|
| 2051 |
|
---|
| 2052 |
|
---|
[2834] | 2053 | void DisplayRenderTexture3()
|
---|
| 2054 | {
|
---|
| 2055 | glEnable(GL_TEXTURE_2D);
|
---|
| 2056 |
|
---|
| 2057 | if (isFirstTexture)
|
---|
| 2058 | glBindTexture(GL_TEXTURE_2D, colorsTex1);
|
---|
| 2059 | else
|
---|
| 2060 | glBindTexture(GL_TEXTURE_2D, colorsTex2);
|
---|
| 2061 |
|
---|
| 2062 | glDisable(GL_LIGHTING);
|
---|
| 2063 |
|
---|
| 2064 | glMatrixMode(GL_PROJECTION);
|
---|
| 2065 | glPushMatrix();
|
---|
| 2066 | glLoadIdentity();
|
---|
| 2067 |
|
---|
| 2068 | glMatrixMode(GL_MODELVIEW);
|
---|
| 2069 | glPushMatrix();
|
---|
| 2070 | glLoadIdentity();
|
---|
| 2071 |
|
---|
| 2072 | const float offs = 0.5f;
|
---|
| 2073 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 2074 |
|
---|
| 2075 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 2076 |
|
---|
| 2077 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
| 2078 | glBegin(GL_QUADS);
|
---|
| 2079 |
|
---|
| 2080 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
| 2081 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
| 2082 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
| 2083 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
| 2084 |
|
---|
| 2085 | glEnd();
|
---|
| 2086 |
|
---|
| 2087 | glEnable(GL_LIGHTING);
|
---|
| 2088 | glDisable(GL_TEXTURE_2D);
|
---|
| 2089 |
|
---|
| 2090 | glMatrixMode(GL_PROJECTION);
|
---|
| 2091 | glPopMatrix();
|
---|
| 2092 |
|
---|
| 2093 | glMatrixMode(GL_MODELVIEW);
|
---|
| 2094 | glPopMatrix();
|
---|
| 2095 |
|
---|
| 2096 | PrintGLerror("displaytexture3");
|
---|
| 2097 | }
|
---|
| 2098 |
|
---|
| 2099 |
|
---|
[2809] | 2100 | void DisplayRenderTexture()
|
---|
| 2101 | {
|
---|
[2834] | 2102 | GLuint oldTex;
|
---|
| 2103 |
|
---|
| 2104 | if (isFirstTexture)
|
---|
| 2105 | {
|
---|
| 2106 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);
|
---|
| 2107 | oldTex = colorsTex2;
|
---|
| 2108 | }
|
---|
| 2109 | else
|
---|
| 2110 | {
|
---|
| 2111 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);
|
---|
| 2112 | oldTex = colorsTex1;
|
---|
| 2113 | }
|
---|
| 2114 |
|
---|
| 2115 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 2116 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 2117 |
|
---|
| 2118 | glDrawBuffers(1, mrt);
|
---|
| 2119 |
|
---|
[2829] | 2120 | glDisable(GL_ALPHA_TEST);
|
---|
[2809] | 2121 | glDisable(GL_TEXTURE_2D);
|
---|
| 2122 | glDisable(GL_LIGHTING);
|
---|
[2818] | 2123 |
|
---|
[2809] | 2124 | glMatrixMode(GL_PROJECTION);
|
---|
| 2125 | glPushMatrix();
|
---|
| 2126 | glLoadIdentity();
|
---|
| 2127 |
|
---|
| 2128 | glMatrixMode(GL_MODELVIEW);
|
---|
| 2129 | glPushMatrix();
|
---|
| 2130 | glLoadIdentity();
|
---|
[2810] | 2131 |
|
---|
[2817] | 2132 | const float offs = 0.5f;
|
---|
[2818] | 2133 |
|
---|
[2817] | 2134 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
[2809] | 2135 |
|
---|
| 2136 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 2137 |
|
---|
[2818] | 2138 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
[2809] | 2139 |
|
---|
[2821] | 2140 | if (useSsao)
|
---|
| 2141 | {
|
---|
| 2142 | cgGLBindProgram(sCgSsaoProgram);
|
---|
| 2143 |
|
---|
[2834] | 2144 | cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);
|
---|
[2821] | 2145 | cgGLEnableTextureParameter(sPositionsTexParamSsao);
|
---|
[2834] | 2146 |
|
---|
| 2147 | cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);
|
---|
[2821] | 2148 | cgGLEnableTextureParameter(sColorsTexParamSsao);
|
---|
| 2149 |
|
---|
[2834] | 2150 | cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);
|
---|
[2821] | 2151 | cgGLEnableTextureParameter(sNormalsTexParamSsao);
|
---|
| 2152 |
|
---|
[2834] | 2153 | cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);
|
---|
[2821] | 2154 | cgGLEnableTextureParameter(sNoiseTexParamSsao);
|
---|
[2832] | 2155 |
|
---|
[2834] | 2156 | cgGLSetTextureParameter(sOldTexParamSsao, oldTex);
|
---|
| 2157 | cgGLEnableTextureParameter(sOldTexParamSsao);
|
---|
| 2158 |
|
---|
| 2159 | cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
|
---|
[2837] | 2160 | cgGLSetParameter1f(sExpFactorParamSsao, expFactor);
|
---|
[2834] | 2161 |
|
---|
[2836] | 2162 | GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
|
---|
[2821] | 2163 | }
|
---|
| 2164 | else
|
---|
| 2165 | {
|
---|
| 2166 | cgGLBindProgram(sCgDeferredProgram);
|
---|
| 2167 |
|
---|
[2834] | 2168 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
[2821] | 2169 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
[2834] | 2170 |
|
---|
| 2171 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
[2821] | 2172 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
[2809] | 2173 |
|
---|
[2834] | 2174 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
[2821] | 2175 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
| 2176 | }
|
---|
[2810] | 2177 |
|
---|
[2809] | 2178 | Vector3 tl, tr, bl, br;
|
---|
| 2179 | ComputeViewVectors(tl, tr, bl, br);
|
---|
| 2180 |
|
---|
| 2181 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 2182 |
|
---|
| 2183 | glBegin(GL_QUADS);
|
---|
[2818] | 2184 |
|
---|
[2829] | 2185 | // note: slightly larger texture hides ambient occlusion error on border but costs resolution
|
---|
[2818] | 2186 | //float offs2 = 0.55f;
|
---|
| 2187 | float offs2 = 0.5f;
|
---|
[2817] | 2188 |
|
---|
| 2189 | glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 2190 | glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 2191 | glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 2192 | glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
[2818] | 2193 |
|
---|
[2809] | 2194 | glEnd();
|
---|
| 2195 |
|
---|
[2829] | 2196 |
|
---|
[2821] | 2197 | if (useSsao)
|
---|
[2834] | 2198 | {
|
---|
| 2199 | cgGLDisableTextureParameter(sColorsTexParamSsao);
|
---|
| 2200 | cgGLDisableTextureParameter(sPositionsTexParamSsao);
|
---|
| 2201 | cgGLDisableTextureParameter(sNormalsTexParamSsao);
|
---|
[2821] | 2202 | cgGLDisableTextureParameter(sNoiseTexParamSsao);
|
---|
[2834] | 2203 | cgGLDisableTextureParameter(sOldTexParamSsao);
|
---|
| 2204 | }
|
---|
| 2205 | else
|
---|
| 2206 | {
|
---|
| 2207 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
| 2208 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
| 2209 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
| 2210 | }
|
---|
[2821] | 2211 |
|
---|
[2818] | 2212 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2809] | 2213 |
|
---|
| 2214 | glEnable(GL_LIGHTING);
|
---|
| 2215 | glDisable(GL_TEXTURE_2D);
|
---|
| 2216 |
|
---|
| 2217 | glMatrixMode(GL_PROJECTION);
|
---|
| 2218 | glPopMatrix();
|
---|
| 2219 |
|
---|
| 2220 | glMatrixMode(GL_MODELVIEW);
|
---|
| 2221 | glPopMatrix();
|
---|
| 2222 |
|
---|
[2834] | 2223 | glPopAttrib();
|
---|
| 2224 |
|
---|
[2809] | 2225 | PrintGLerror("displaytexture");
|
---|
[2834] | 2226 |
|
---|
| 2227 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 2228 |
|
---|
| 2229 | DisplayRenderTexture3();
|
---|
[2809] | 2230 | }
|
---|
| 2231 |
|
---|
[2830] | 2232 | #if 0
|
---|
| 2233 | // kustls magic sample positions
|
---|
[2809] | 2234 | void GenerateSamples()
|
---|
| 2235 | {
|
---|
[2830] | 2236 | static const float mysamples[] =
|
---|
| 2237 | {
|
---|
| 2238 | -0.326212f, -0.405805f,
|
---|
| 2239 | -0.840144f, -0.07358f,
|
---|
| 2240 | -0.695914f, 0.457137f,
|
---|
| 2241 | -0.203345f, 0.620716,
|
---|
| 2242 | 0.96234f, -0.194983f,
|
---|
| 2243 | 0.473434f, -0.480026f,
|
---|
| 2244 | 0.519456, 0.767022f,
|
---|
| 2245 | 0.185461f, -0.893124f,
|
---|
| 2246 | 0.507431f, 0.064425f,
|
---|
| 2247 | 0.89642f, 0.412458f,
|
---|
| 2248 | -0.32194f, -0.932615f,
|
---|
| 2249 | -0.791559f, -0.597705f,
|
---|
| 2250 | 0.326212f, 0.405805f,
|
---|
| 2251 | 0.840144f, 0.07358f,
|
---|
| 2252 | 0.695914f, -0.457137f,
|
---|
| 2253 | 0.203345f, -0.620716,
|
---|
| 2254 | -0.96234f, 0.194983f,
|
---|
| 2255 | -0.473434f, 0.480026f,
|
---|
| 2256 | -0.519456, -0.767022f,
|
---|
| 2257 | -0.185461f, 0.893124f,
|
---|
| 2258 | -0.507431f, -0.064425f,
|
---|
| 2259 | -0.89642f, -0.412458f,
|
---|
| 2260 | 0.32194f, 0.932615f,
|
---|
| 2261 | 0.791559f, 0.597705f
|
---|
| 2262 | };
|
---|
| 2263 |
|
---|
| 2264 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 2265 | {
|
---|
| 2266 | samples[i] = mysamples[i];
|
---|
| 2267 | }
|
---|
| 2268 | }
|
---|
| 2269 |
|
---|
| 2270 | #else
|
---|
| 2271 |
|
---|
[2820] | 2272 |
|
---|
[2830] | 2273 | void GenerateSamples()
|
---|
| 2274 | {
|
---|
[2853] | 2275 | /*static HaltonSequence halton;
|
---|
[2837] | 2276 |
|
---|
| 2277 | float r[2];
|
---|
| 2278 |
|
---|
[2830] | 2279 | // generates poisson distribution on disc
|
---|
[2834] | 2280 | float minDist = 2.0f / sqrt((float)NUM_SAMPLES);
|
---|
[2830] | 2281 |
|
---|
[2834] | 2282 | //cout << "minDist before= " << minDist << endl;
|
---|
[2830] | 2283 |
|
---|
[2833] | 2284 | for (int i = 0; i < NUM_SAMPLES * 2; i += 2)
|
---|
[2830] | 2285 | {
|
---|
[2833] | 2286 | int tries = 0, totalTries = 0;
|
---|
[2830] | 2287 |
|
---|
| 2288 | // repeat until valid sample was found
|
---|
| 2289 | while (1)
|
---|
| 2290 | {
|
---|
| 2291 | ++ tries;
|
---|
[2833] | 2292 | ++ totalTries;
|
---|
[2830] | 2293 |
|
---|
[2837] | 2294 | halton.GetNext(2, r);
|
---|
[2830] | 2295 |
|
---|
[2837] | 2296 | //const float rx = RandomValue(-1, 1);
|
---|
| 2297 | //const float ry = RandomValue(-1, 1);
|
---|
| 2298 |
|
---|
| 2299 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
| 2300 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
| 2301 |
|
---|
[2830] | 2302 | // check if in disk, else exit early
|
---|
| 2303 | if (rx * rx + ry * ry > 1)
|
---|
| 2304 | continue;
|
---|
| 2305 |
|
---|
| 2306 | bool sampleValid = true;
|
---|
| 2307 |
|
---|
| 2308 | // check poisson property
|
---|
| 2309 | for (int j = 0; ((j < i) && sampleValid); j += 2)
|
---|
| 2310 | {
|
---|
| 2311 | const float dist =
|
---|
| 2312 | sqrt((samples[j] - rx) * (samples[j] - rx) +
|
---|
| 2313 | (samples[j + 1] - ry) * (samples[j + 1] - ry));
|
---|
| 2314 |
|
---|
| 2315 | if (dist < minDist)
|
---|
| 2316 | sampleValid = false;
|
---|
| 2317 | }
|
---|
| 2318 |
|
---|
| 2319 | if (sampleValid)
|
---|
| 2320 | {
|
---|
| 2321 | samples[i] = rx;
|
---|
| 2322 | samples[i + 1]= ry;
|
---|
| 2323 | break;
|
---|
| 2324 | }
|
---|
| 2325 |
|
---|
[2833] | 2326 | if (tries > 2000)
|
---|
[2830] | 2327 | {
|
---|
| 2328 | minDist *= 0.9f;
|
---|
| 2329 | tries = 0;
|
---|
| 2330 | }
|
---|
| 2331 | }
|
---|
| 2332 | }
|
---|
[2853] | 2333 | */
|
---|
[2830] | 2334 |
|
---|
[2853] | 2335 | static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
|
---|
| 2336 | poisson.Generate((Sample2 *)samples);
|
---|
[2826] | 2337 | }
|
---|
| 2338 |
|
---|
| 2339 |
|
---|
[2830] | 2340 | #endif
|
---|
| 2341 |
|
---|
| 2342 |
|
---|
[2809] | 2343 | void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
|
---|
| 2344 | {
|
---|
| 2345 | float myfov = fov * M_PI / 180.0f;
|
---|
| 2346 |
|
---|
[2833] | 2347 | const float w_far = 2.0f * tan(myfov / 2.0f);
|
---|
| 2348 | const float aspect = texWidth / texHeight;
|
---|
| 2349 |
|
---|
| 2350 | const float h_far = w_far / aspect;
|
---|
| 2351 |
|
---|
[2809] | 2352 | float t1 = h_far * 0.5f;
|
---|
| 2353 | float t2 = w_far * 0.5f;
|
---|
| 2354 |
|
---|
[2834] | 2355 | #if 0
|
---|
[2833] | 2356 | bl = Normalize(-Vector3(-t1, -t2, 1.0f));
|
---|
| 2357 | br = Normalize(-Vector3( t1, -t2, 1.0f));
|
---|
| 2358 | tl = Normalize(-Vector3(-t1, t2, 1.0f));
|
---|
| 2359 | tr = Normalize(-Vector3( t1, t2, 1.0f));
|
---|
[2834] | 2360 | #else
|
---|
| 2361 | bl = -Normalize(camera->GetDirection());
|
---|
| 2362 | br = -Normalize(camera->GetDirection());
|
---|
| 2363 | tl = -Normalize(camera->GetDirection());
|
---|
| 2364 | tr = -Normalize(camera->GetDirection());
|
---|
| 2365 | #endif
|
---|
[2809] | 2366 | // normalize to 0 .. 1
|
---|
| 2367 | bl = bl * 0.5f + 0.5f;
|
---|
| 2368 | br = br * 0.5f + 0.5f;
|
---|
| 2369 | tl = tl * 0.5f + 0.5f;
|
---|
| 2370 | tr = tr * 0.5f + 0.5f;
|
---|
| 2371 | }
|
---|
| 2372 |
|
---|
| 2373 |
|
---|
[2810] | 2374 | void CreateNoiseTex2D()
|
---|
| 2375 | {
|
---|
[2825] | 2376 | randomNormals = new GLubyte[texWidth * texHeight * 3];
|
---|
[2810] | 2377 |
|
---|
[2825] | 2378 | for (int i = 0; i < texWidth * texHeight * 3; i += 3)
|
---|
[2810] | 2379 | {
|
---|
| 2380 | // create random samples over sphere
|
---|
| 2381 | const float rx = RandomValue(0, 1);
|
---|
| 2382 | const float theta = 2.0f * acos(sqrt(1.0f - rx));
|
---|
| 2383 |
|
---|
[2817] | 2384 | randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
|
---|
| 2385 | randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
|
---|
| 2386 | randomNormals[i + 2] = 0;
|
---|
[2810] | 2387 | }
|
---|
| 2388 |
|
---|
| 2389 | glEnable(GL_TEXTURE_2D);
|
---|
| 2390 | glGenTextures(1, &noiseTex);
|
---|
| 2391 | glBindTexture(GL_TEXTURE_2D, noiseTex);
|
---|
[2825] | 2392 |
|
---|
[2817] | 2393 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 2394 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
[2810] | 2395 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
| 2396 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
| 2397 |
|
---|
[2835] | 2398 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
|
---|
| 2399 | //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
|
---|
[2825] | 2400 |
|
---|
[2810] | 2401 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 2402 | glDisable(GL_TEXTURE_2D);
|
---|
| 2403 |
|
---|
| 2404 | cout << "created noise texture" << endl;
|
---|
| 2405 | PrintGLerror("noisetexture");
|
---|
| 2406 | }
|
---|