[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"
|
---|
[2857] | 34 | #include "FrameBufferObject.h"
|
---|
[2896] | 35 | #include "DeferredRenderer.h"
|
---|
[2887] | 36 | #include "ShadowMapping.h"
|
---|
| 37 | #include "Light.h"
|
---|
[2642] | 38 |
|
---|
| 39 |
|
---|
[2756] | 40 | using namespace std;
|
---|
[2776] | 41 | using namespace CHCDemoEngine;
|
---|
[2642] | 42 |
|
---|
| 43 |
|
---|
[2828] | 44 | static Environment env;
|
---|
| 45 |
|
---|
[2911] | 46 | #define MAX_DEPTH_CONST 10.0f
|
---|
[2828] | 47 |
|
---|
[2884] | 48 | // fbo
|
---|
[2879] | 49 | FrameBufferObject *fbo = NULL;
|
---|
[2756] | 50 |
|
---|
[2826] | 51 | GLuint fontTex;
|
---|
[2810] | 52 |
|
---|
[2756] | 53 | /// the renderable scene geometry
|
---|
| 54 | SceneEntityContainer sceneEntities;
|
---|
| 55 | // traverses and renders the hierarchy
|
---|
[2767] | 56 | RenderTraverser *traverser = NULL;
|
---|
[2756] | 57 | /// the hierarchy
|
---|
[2767] | 58 | Bvh *bvh = NULL;
|
---|
[2793] | 59 | /// handles scene loading
|
---|
[2795] | 60 | ResourceManager *loader = NULL;
|
---|
[2756] | 61 | /// the scene camera
|
---|
[2767] | 62 | Camera *camera = NULL;
|
---|
[2795] | 63 | /// the scene camera
|
---|
| 64 | Camera *visCamera = NULL;
|
---|
[2767] | 65 | /// the visualization
|
---|
| 66 | Visualization *visualization = NULL;
|
---|
[2760] | 67 | /// the current render state
|
---|
| 68 | RenderState state;
|
---|
[2764] | 69 | /// the rendering algorithm
|
---|
[2795] | 70 | int renderMode = RenderTraverser::CHCPLUSPLUS;
|
---|
[2756] | 71 | // eye near plane distance
|
---|
[2822] | 72 | float nearDist = 0.2f;
|
---|
[2856] | 73 | /// the field of view
|
---|
| 74 | float fov = 50.0f;
|
---|
[2771] | 75 | /// the pixel threshold where a node is still considered invisible
|
---|
| 76 | int threshold;
|
---|
[2764] | 77 |
|
---|
[2776] | 78 | int assumedVisibleFrames = 10;
|
---|
| 79 | int maxBatchSize = 50;
|
---|
[2771] | 80 |
|
---|
[2800] | 81 | int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES;
|
---|
[2767] | 82 |
|
---|
[2796] | 83 | SceneQuery *sceneQuery = NULL;
|
---|
[2801] | 84 | RenderQueue *renderQueue = NULL;
|
---|
[2796] | 85 |
|
---|
[2897] | 86 | // traverses and renders the hierarchy
|
---|
| 87 | RenderTraverser *shadowTraverser = NULL;
|
---|
| 88 |
|
---|
| 89 |
|
---|
[2809] | 90 | /// these values get scaled with the frame rate
|
---|
[2828] | 91 | static float keyForwardMotion = 30.0f;
|
---|
| 92 | static float keyRotation = 1.5f;
|
---|
[2801] | 93 |
|
---|
[2826] | 94 | /// elapsed time in milliseconds
|
---|
| 95 | double elapsedTime = 1000.0f;
|
---|
| 96 | double algTime = 1000.0f;
|
---|
[2795] | 97 |
|
---|
[2809] | 98 | static int winWidth = 1024;
|
---|
[2813] | 99 | static int winHeight = 768;
|
---|
[2892] | 100 |
|
---|
| 101 |
|
---|
[2809] | 102 | static float winAspectRatio = 1.0f;
|
---|
[2642] | 103 |
|
---|
[2776] | 104 | double accumulatedTime = 1000;
|
---|
[2770] | 105 | float fps = 1e3f;
|
---|
| 106 |
|
---|
[2809] | 107 | float myfar = 0;
|
---|
| 108 |
|
---|
[2826] | 109 | glfont::GLFont myfont;
|
---|
| 110 |
|
---|
[2809] | 111 | // rendertexture
|
---|
[2828] | 112 | static int texWidth = 1024;
|
---|
| 113 | static int texHeight = 768;
|
---|
[2866] | 114 |
|
---|
| 115 |
|
---|
[2770] | 116 | int renderedObjects = 0;
|
---|
[2773] | 117 | int renderedNodes = 0;
|
---|
| 118 | int renderedTriangles = 0;
|
---|
| 119 |
|
---|
[2770] | 120 | int issuedQueries = 0;
|
---|
| 121 | int traversedNodes = 0;
|
---|
| 122 | int frustumCulledNodes = 0;
|
---|
| 123 | int queryCulledNodes = 0;
|
---|
| 124 | int stateChanges = 0;
|
---|
[2800] | 125 | int numBatches = 0;
|
---|
[2770] | 126 |
|
---|
[2642] | 127 | bool showHelp = false;
|
---|
[2826] | 128 | bool showStatistics = false;
|
---|
[2865] | 129 | bool showOptions = true;
|
---|
[2642] | 130 | bool showBoundingVolumes = false;
|
---|
| 131 | bool visMode = false;
|
---|
| 132 |
|
---|
[2792] | 133 | // mouse navigation state
|
---|
[2809] | 134 | int xEyeBegin = 0;
|
---|
| 135 | int yEyeBegin = 0;
|
---|
| 136 | int yMotionBegin = 0;
|
---|
| 137 | int verticalMotionBegin = 0;
|
---|
| 138 | int horizontalMotionBegin = 0;
|
---|
[2642] | 139 |
|
---|
[2770] | 140 | bool useOptimization = false;
|
---|
[2786] | 141 | bool useTightBounds = true;
|
---|
[2795] | 142 | bool useRenderQueue = true;
|
---|
[2786] | 143 | bool useMultiQueries = true;
|
---|
[2800] | 144 | bool flyMode = true;
|
---|
| 145 |
|
---|
[2795] | 146 | SceneEntityContainer skyGeometry;
|
---|
| 147 |
|
---|
[2792] | 148 | bool leftKeyPressed = false;
|
---|
| 149 | bool rightKeyPressed = false;
|
---|
| 150 | bool upKeyPressed = false;
|
---|
| 151 | bool downKeyPressed = false;
|
---|
[2837] | 152 | bool descendKeyPressed = false;
|
---|
| 153 | bool ascendKeyPressed = false;
|
---|
[2787] | 154 |
|
---|
[2875] | 155 | bool useGlobIllum = false;
|
---|
| 156 | bool useTemporalCoherence = true;
|
---|
| 157 |
|
---|
[2901] | 158 | static float ssaoTempCohFactor = 255.0;
|
---|
[2821] | 159 |
|
---|
[2826] | 160 | bool showAlgorithmTime = false;
|
---|
| 161 |
|
---|
[2820] | 162 | GLubyte *randomNormals = NULL;
|
---|
[2809] | 163 |
|
---|
[2819] | 164 | PerfTimer frameTimer, algTimer;
|
---|
| 165 |
|
---|
[2825] | 166 | int renderType = RenderState::FIXED;
|
---|
[2820] | 167 |
|
---|
[2827] | 168 | PerformanceGraph *perfGraph = NULL;
|
---|
[2825] | 169 |
|
---|
[2828] | 170 | bool useFullScreen = false;
|
---|
[2827] | 171 |
|
---|
[2865] | 172 | bool useLODs = true;
|
---|
| 173 |
|
---|
[2896] | 174 | DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::POISSON;
|
---|
[2865] | 175 |
|
---|
[2903] | 176 | bool useAdvancedShading = false;
|
---|
| 177 |
|
---|
[2895] | 178 | bool showShadowMap = false;
|
---|
| 179 | bool shadowChanged = true;
|
---|
| 180 |
|
---|
[2834] | 181 | static Matrix4x4 matProjectionView = IdentityMatrix();
|
---|
[2820] | 182 |
|
---|
[2894] | 183 |
|
---|
| 184 | ShadowMap *shadowMap = NULL;
|
---|
[2887] | 185 | Light *light = NULL;
|
---|
[2834] | 186 |
|
---|
[2895] | 187 |
|
---|
[2809] | 188 | // function forward declarations
|
---|
[2759] | 189 | void InitExtensions();
|
---|
[2756] | 190 | void DisplayVisualization();
|
---|
[2759] | 191 | void InitGLstate();
|
---|
[2809] | 192 | void InitRenderTexture();
|
---|
| 193 | void InitCg();
|
---|
[2759] | 194 | void CleanUp();
|
---|
| 195 | void SetupEyeView();
|
---|
| 196 | void UpdateEyeMtx();
|
---|
| 197 | void SetupLighting();
|
---|
[2764] | 198 | void DisplayStats();
|
---|
[2769] | 199 | void Output(int x, int y, const char *string);
|
---|
[2786] | 200 | void DrawHelpMessage();
|
---|
[2796] | 201 | void RenderSky();
|
---|
[2801] | 202 | void RenderVisibleObjects();
|
---|
[2756] | 203 |
|
---|
[2792] | 204 | void Begin2D();
|
---|
| 205 | void End2D();
|
---|
| 206 | void KeyBoard(unsigned char c, int x, int y);
|
---|
| 207 | void DrawStatistics();
|
---|
| 208 | void Display();
|
---|
| 209 | void Special(int c, int x, int y);
|
---|
| 210 | void KeyUp(unsigned char c, int x, int y);
|
---|
| 211 | void SpecialKeyUp(int c, int x, int y);
|
---|
| 212 | void Reshape(int w, int h);
|
---|
| 213 | void Mouse(int button, int state, int x, int y);
|
---|
| 214 | void LeftMotion(int x, int y);
|
---|
| 215 | void RightMotion(int x, int y);
|
---|
| 216 | void MiddleMotion(int x, int y);
|
---|
| 217 | void CalcDecimalPoint(string &str, int d);
|
---|
[2642] | 218 |
|
---|
[2897] | 219 | RenderTraverser *CreateTraverser(Camera *cam);
|
---|
| 220 |
|
---|
[2792] | 221 | void KeyHorizontalMotion(float shift);
|
---|
[2794] | 222 | void KeyVerticalMotion(float shift);
|
---|
[2642] | 223 |
|
---|
[2801] | 224 | void PlaceViewer(const Vector3 &oldPos);
|
---|
[2642] | 225 |
|
---|
[2801] | 226 |
|
---|
[2887] | 227 |
|
---|
[2826] | 228 | inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; }
|
---|
| 229 | inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; }
|
---|
[2792] | 230 |
|
---|
[2809] | 231 | void InitFBO();
|
---|
[2795] | 232 |
|
---|
[2896] | 233 | DeferredRenderer *ssaoShader = NULL;
|
---|
[2810] | 234 |
|
---|
[2809] | 235 |
|
---|
| 236 |
|
---|
[2810] | 237 | /////////
|
---|
| 238 | //-- cg stuff
|
---|
| 239 |
|
---|
[2808] | 240 | static CGcontext sCgContext = NULL;
|
---|
[2810] | 241 | static CGprogram sCgMrtVertexProgram = NULL;
|
---|
[2795] | 242 |
|
---|
[2818] | 243 | static CGparameter sModelViewProjMatrixParam;
|
---|
[2809] | 244 | static CGparameter sMaxDepthParam;
|
---|
[2819] | 245 | static CGparameter sMaxDepthParamTex;
|
---|
[2808] | 246 |
|
---|
[2861] | 247 | static Matrix4x4 oldViewProjMatrix;
|
---|
[2820] | 248 |
|
---|
[2837] | 249 |
|
---|
[2809] | 250 | static void cgErrorCallback()
|
---|
| 251 | {
|
---|
| 252 | CGerror lastError = cgGetError();
|
---|
| 253 |
|
---|
| 254 | if(lastError)
|
---|
| 255 | {
|
---|
| 256 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
| 257 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
[2861] | 258 |
|
---|
[2809] | 259 | printf("Cg error, exiting...\n");
|
---|
| 260 |
|
---|
| 261 | exit(0);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | static void PrintGLerror(char *msg)
|
---|
| 267 | {
|
---|
| 268 | GLenum errCode;
|
---|
| 269 | const GLubyte *errStr;
|
---|
| 270 |
|
---|
| 271 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 272 | {
|
---|
| 273 | errStr = gluErrorString(errCode);
|
---|
| 274 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
[2642] | 279 | int main(int argc, char* argv[])
|
---|
| 280 | {
|
---|
[2781] | 281 | int returnCode = 0;
|
---|
| 282 |
|
---|
[2837] | 283 | Vector3 camPos(.0f, .0f, .0f);
|
---|
[2838] | 284 | Vector3 camDir(.0f, 1.0f, .0f);
|
---|
[2837] | 285 |
|
---|
[2873] | 286 | cout << "=== reading environment file ===" << endl << endl;
|
---|
[2830] | 287 |
|
---|
[2837] | 288 | string envFileName = "default.env";
|
---|
| 289 | if (!env.Read(envFileName))
|
---|
| 290 | {
|
---|
| 291 | cerr << "loading environment " << envFileName << " failed!" << endl;
|
---|
| 292 | }
|
---|
| 293 | else
|
---|
| 294 | {
|
---|
| 295 | env.GetIntParam(string("assumedVisibleFrames"), assumedVisibleFrames);
|
---|
| 296 | env.GetIntParam(string("maxBatchSize"), maxBatchSize);
|
---|
| 297 | env.GetIntParam(string("trianglesPerVirtualLeaf"), trianglesPerVirtualLeaf);
|
---|
[2828] | 298 |
|
---|
[2837] | 299 | env.GetFloatParam(string("keyForwardMotion"), keyForwardMotion);
|
---|
| 300 | env.GetFloatParam(string("keyRotation"), keyRotation);
|
---|
[2828] | 301 |
|
---|
[2837] | 302 | env.GetIntParam(string("winWidth"), winWidth);
|
---|
| 303 | env.GetIntParam(string("winHeight"), winHeight);
|
---|
[2828] | 304 |
|
---|
[2837] | 305 | env.GetBoolParam(string("useFullScreen"), useFullScreen);
|
---|
[2901] | 306 | env.GetFloatParam(string("tempCohFactor"), ssaoTempCohFactor);
|
---|
[2837] | 307 | env.GetVectorParam(string("camPosition"), camPos);
|
---|
[2838] | 308 | env.GetVectorParam(string("camDirection"), camDir);
|
---|
[2828] | 309 |
|
---|
[2865] | 310 | env.GetBoolParam(string("useLODs"), useLODs);
|
---|
| 311 |
|
---|
[2846] | 312 | //env.GetStringParam(string("modelPath"), model_path);
|
---|
[2838] | 313 | //env.GetIntParam(string("numSssaoSamples"), numSsaoSamples);
|
---|
| 314 |
|
---|
[2867] | 315 |
|
---|
[2837] | 316 | cout << "assumedVisibleFrames: " << assumedVisibleFrames << endl;
|
---|
| 317 | cout << "maxBatchSize: " << maxBatchSize << endl;
|
---|
| 318 | cout << "trianglesPerVirtualLeaf: " << trianglesPerVirtualLeaf << endl;
|
---|
[2828] | 319 |
|
---|
[2837] | 320 | cout << "keyForwardMotion: " << keyForwardMotion << endl;
|
---|
| 321 | cout << "keyRotation: " << keyRotation << endl;
|
---|
| 322 | cout << "winWidth: " << winWidth << endl;
|
---|
| 323 | cout << "winHeight: " << winHeight << endl;
|
---|
| 324 | cout << "useFullScreen: " << useFullScreen << endl;
|
---|
[2865] | 325 | cout << "useLODs: " << useLODs << endl;
|
---|
[2837] | 326 | cout << "camPosition: " << camPos << endl;
|
---|
[2901] | 327 | cout << "temporal coherence: " << ssaoTempCohFactor << endl;
|
---|
[2873] | 328 |
|
---|
[2846] | 329 | //cout << "model path: " << model_path << endl;
|
---|
[2873] | 330 |
|
---|
[2881] | 331 | cout << "**** end parameters ****" << endl << endl;
|
---|
[2837] | 332 | }
|
---|
[2829] | 333 |
|
---|
[2828] | 334 | ///////////////////////////
|
---|
| 335 |
|
---|
[2914] | 336 | camera = new Camera(winWidth, winHeight, fov);
|
---|
[2795] | 337 | camera->SetNear(nearDist);
|
---|
[2913] | 338 | camera->SetFar(1000);
|
---|
[2888] | 339 |
|
---|
[2838] | 340 | camera->SetDirection(camDir);
|
---|
[2829] | 341 | camera->SetPosition(camPos);
|
---|
| 342 |
|
---|
[2806] | 343 | visCamera = new Camera(winWidth, winHeight, fov);
|
---|
[2781] | 344 |
|
---|
[2796] | 345 | visCamera->SetNear(0.0f);
|
---|
| 346 | visCamera->Yaw(.5 * M_PI);
|
---|
[2781] | 347 |
|
---|
[2802] | 348 | renderQueue = new RenderQueue(&state, camera);
|
---|
[2801] | 349 |
|
---|
[2760] | 350 | glutInitWindowSize(winWidth, winHeight);
|
---|
[2756] | 351 | glutInit(&argc, argv);
|
---|
[2853] | 352 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
|
---|
| 353 | //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
|
---|
[2850] | 354 | //glutInitDisplayString("samples=2");
|
---|
| 355 |
|
---|
[2867] | 356 | SceneEntity::SetUseLODs(useLODs);
|
---|
| 357 |
|
---|
| 358 |
|
---|
[2828] | 359 | if (!useFullScreen)
|
---|
[2856] | 360 | {
|
---|
[2828] | 361 | glutCreateWindow("FriendlyCulling");
|
---|
[2856] | 362 | }
|
---|
[2828] | 363 | else
|
---|
| 364 | {
|
---|
| 365 | glutGameModeString( "1024x768:32@75" );
|
---|
| 366 | glutEnterGameMode();
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[2792] | 369 | glutDisplayFunc(Display);
|
---|
| 370 | glutKeyboardFunc(KeyBoard);
|
---|
| 371 | glutSpecialFunc(Special);
|
---|
| 372 | glutReshapeFunc(Reshape);
|
---|
| 373 | glutMouseFunc(Mouse);
|
---|
| 374 | glutIdleFunc(Display);
|
---|
| 375 | glutKeyboardUpFunc(KeyUp);
|
---|
| 376 | glutSpecialUpFunc(SpecialKeyUp);
|
---|
| 377 | glutIgnoreKeyRepeat(true);
|
---|
| 378 |
|
---|
[2829] | 379 | // initialise gl graphics
|
---|
[2756] | 380 | InitExtensions();
|
---|
| 381 | InitGLstate();
|
---|
[2850] | 382 |
|
---|
[2854] | 383 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 384 | glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
|
---|
[2850] | 385 |
|
---|
[2792] | 386 | LeftMotion(0, 0);
|
---|
| 387 | MiddleMotion(0, 0);
|
---|
[2756] | 388 |
|
---|
[2829] | 389 | perfGraph = new PerformanceGraph(1000);
|
---|
[2756] | 390 |
|
---|
[2795] | 391 | loader = new ResourceManager();
|
---|
[2793] | 392 |
|
---|
[2784] | 393 | //const string filename("data/city/model/city.dem");
|
---|
| 394 | const string filename = string(model_path + "city.dem");
|
---|
[2756] | 395 |
|
---|
[2793] | 396 | if (loader->Load(filename, sceneEntities))
|
---|
[2756] | 397 | cout << "scene " << filename << " loaded" << endl;
|
---|
| 398 | else
|
---|
[2784] | 399 | {
|
---|
[2756] | 400 | cerr << "loading scene " << filename << " failed" << endl;
|
---|
[2792] | 401 | CleanUp();
|
---|
[2784] | 402 | exit(0);
|
---|
| 403 | }
|
---|
[2756] | 404 |
|
---|
[2795] | 405 | SceneEntityContainer dummy;
|
---|
| 406 |
|
---|
| 407 | const string envname = string(model_path + "env.dem");
|
---|
| 408 |
|
---|
| 409 | if (loader->Load(envname, skyGeometry))
|
---|
| 410 | cout << "sky box " << filename << " loaded" << endl;
|
---|
| 411 | else
|
---|
| 412 | {
|
---|
| 413 | cerr << "loading sky box " << filename << " failed" << endl;
|
---|
| 414 | CleanUp();
|
---|
| 415 | exit(0);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[2784] | 418 | const string bvh_filename = string(model_path + "city.bvh");
|
---|
[2760] | 419 | BvhLoader bvhLoader;
|
---|
[2784] | 420 | bvh = bvhLoader.Load(bvh_filename, sceneEntities);
|
---|
[2762] | 421 |
|
---|
[2784] | 422 | if (!bvh)
|
---|
| 423 | {
|
---|
| 424 | cerr << "loading bvh " << bvh_filename << " failed" << endl;
|
---|
[2792] | 425 | CleanUp();
|
---|
[2784] | 426 | exit(0);
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[2829] | 429 | // set far plane based on scene extent
|
---|
| 430 | myfar = 10.0f * Magnitude(bvh->GetBox().Diagonal());
|
---|
| 431 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 432 |
|
---|
[2913] | 433 | camera->SetFar(Magnitude(bvh->GetBox().Diagonal()));
|
---|
[2829] | 434 |
|
---|
[2809] | 435 | InitCg();
|
---|
[2806] | 436 |
|
---|
[2896] | 437 | DeferredRenderer::Init(sCgContext);
|
---|
[2847] | 438 |
|
---|
[2861] | 439 |
|
---|
[2857] | 440 | // initialize the render traverser
|
---|
[2897] | 441 | traverser = CreateTraverser(camera);
|
---|
[2756] | 442 |
|
---|
[2787] | 443 | visualization = new Visualization(bvh, camera, NULL, &state);
|
---|
[2897] | 444 |
|
---|
| 445 | state.SetRenderType(RenderState::FIXED);
|
---|
[2843] | 446 | sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
|
---|
[2796] | 447 |
|
---|
[2895] | 448 | //Vector3 lightDir = Normalize(Vector3(0, 1, -1));
|
---|
| 449 | Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f));
|
---|
| 450 |
|
---|
[2893] | 451 | light = new Light(lightDir, RgbaColor(1, 1, 1, 1));
|
---|
[2887] | 452 |
|
---|
[2895] | 453 |
|
---|
[2847] | 454 | // frame time is restarted every frame
|
---|
| 455 | frameTimer.Start();
|
---|
[2800] | 456 |
|
---|
[2857] | 457 | // the rendering loop
|
---|
[2642] | 458 | glutMainLoop();
|
---|
| 459 |
|
---|
| 460 | // clean up
|
---|
[2756] | 461 | CleanUp();
|
---|
| 462 |
|
---|
[2642] | 463 | return 0;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[2756] | 466 |
|
---|
[2809] | 467 | void InitCg(void)
|
---|
| 468 | {
|
---|
| 469 | // Setup Cg
|
---|
| 470 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 471 |
|
---|
| 472 | // Create cgContext.
|
---|
| 473 | sCgContext = cgCreateContext();
|
---|
| 474 |
|
---|
| 475 | // get the best profile for this hardware
|
---|
[2818] | 476 | RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
| 477 | cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
|
---|
[2809] | 478 |
|
---|
[2818] | 479 | RenderState::sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
| 480 | cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
|
---|
[2809] | 481 |
|
---|
[2810] | 482 | sCgMrtVertexProgram =
|
---|
[2809] | 483 | cgCreateProgramFromFile(sCgContext,
|
---|
| 484 | CG_SOURCE,
|
---|
[2810] | 485 | "src/shaders/mrt.cg",
|
---|
[2818] | 486 | RenderState::sCgVertexProfile,
|
---|
[2810] | 487 | "vtx",
|
---|
[2809] | 488 | NULL);
|
---|
| 489 |
|
---|
[2821] | 490 | if (sCgMrtVertexProgram != NULL)
|
---|
[2809] | 491 | {
|
---|
[2810] | 492 | cgGLLoadProgram(sCgMrtVertexProgram);
|
---|
[2818] | 493 |
|
---|
[2840] | 494 | Transform3::sModelMatrixParam = cgGetNamedParameter(sCgMrtVertexProgram, "ModelMatrix");
|
---|
[2818] | 495 | sModelViewProjMatrixParam = cgGetNamedParameter(sCgMrtVertexProgram, "ModelViewProj");
|
---|
[2809] | 496 | }
|
---|
| 497 |
|
---|
[2819] | 498 | RenderState::sCgMrtFragmentTexProgram =
|
---|
[2809] | 499 | cgCreateProgramFromFile(sCgContext,
|
---|
| 500 | CG_SOURCE,
|
---|
[2810] | 501 | "src/shaders/mrt.cg",
|
---|
[2818] | 502 | RenderState::sCgFragmentProfile,
|
---|
[2819] | 503 | "fragtex",
|
---|
| 504 | NULL);
|
---|
| 505 |
|
---|
| 506 | if (RenderState::sCgMrtFragmentTexProgram != NULL)
|
---|
| 507 | {
|
---|
| 508 | cgGLLoadProgram(RenderState::sCgMrtFragmentTexProgram);
|
---|
| 509 |
|
---|
| 510 | sMaxDepthParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "maxDepth");
|
---|
| 511 | Material::sDiffuseTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "diffuse");
|
---|
[2822] | 512 | Material::sAmbientTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "ambient");
|
---|
[2819] | 513 |
|
---|
[2905] | 514 | cgGLSetParameter1f(sMaxDepthParamTex, MAX_DEPTH_CONST / myfar);
|
---|
| 515 | //Debug << "maxdepth: " << MAX_DEPTH_CONST / myfar << endl;
|
---|
[2819] | 516 | }
|
---|
| 517 | else
|
---|
| 518 | cerr << "fragment tex program failed to load" << endl;
|
---|
| 519 |
|
---|
| 520 | RenderState::sCgMrtFragmentProgram =
|
---|
| 521 | cgCreateProgramFromFile(sCgContext,
|
---|
| 522 | CG_SOURCE,
|
---|
| 523 | "src/shaders/mrt.cg",
|
---|
| 524 | RenderState::sCgFragmentProfile,
|
---|
[2810] | 525 | "frag",
|
---|
[2809] | 526 | NULL);
|
---|
| 527 |
|
---|
[2819] | 528 | if (RenderState::sCgMrtFragmentProgram != NULL)
|
---|
[2809] | 529 | {
|
---|
[2819] | 530 | cgGLLoadProgram(RenderState::sCgMrtFragmentProgram);
|
---|
[2809] | 531 |
|
---|
[2819] | 532 | sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
|
---|
| 533 | Material::sDiffuseParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "diffuse");
|
---|
[2822] | 534 | Material::sAmbientParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "ambient");
|
---|
[2818] | 535 |
|
---|
[2905] | 536 | cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / myfar);
|
---|
[2809] | 537 | }
|
---|
| 538 | else
|
---|
[2819] | 539 | cerr << "fragment program failed to load" << endl;
|
---|
[2809] | 540 |
|
---|
| 541 | PrintGLerror("init");
|
---|
[2818] | 542 |
|
---|
[2834] | 543 | cout << "cg initialization successful" << endl;
|
---|
[2809] | 544 | }
|
---|
| 545 |
|
---|
| 546 |
|
---|
| 547 | void InitFBO()
|
---|
[2810] | 548 | {
|
---|
[2857] | 549 | // this fbo basicly stores the scene information we get from standard rendering of a frame
|
---|
| 550 | // we store colors, normals, positions (for the ssao)
|
---|
[2884] | 551 | fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32);
|
---|
[2857] | 552 |
|
---|
[2859] | 553 | // the diffuse color buffer
|
---|
[2891] | 554 | //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true);
|
---|
| 555 | fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
|
---|
[2868] | 556 |
|
---|
[2859] | 557 | // the positions buffer
|
---|
[2867] | 558 | fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true);
|
---|
[2873] | 559 | //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
|
---|
[2868] | 560 |
|
---|
[2859] | 561 | // the normals buffer
|
---|
[2867] | 562 | //fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
|
---|
| 563 | fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
|
---|
[2857] | 564 |
|
---|
[2879] | 565 | // another color buffer
|
---|
[2884] | 566 | fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
|
---|
[2879] | 567 |
|
---|
[2810] | 568 | PrintGLerror("fbo");
|
---|
[2809] | 569 | }
|
---|
| 570 |
|
---|
| 571 |
|
---|
[2827] | 572 | bool InitFont(void)
|
---|
[2642] | 573 | {
|
---|
[2826] | 574 | glEnable(GL_TEXTURE_2D);
|
---|
| 575 |
|
---|
| 576 | glGenTextures(1, &fontTex);
|
---|
| 577 | glBindTexture(GL_TEXTURE_2D, fontTex);
|
---|
[2829] | 578 | if (!myfont.Create("data/fonts/verdana.glf", fontTex))
|
---|
[2826] | 579 | return false;
|
---|
| 580 |
|
---|
| 581 | glDisable(GL_TEXTURE_2D);
|
---|
[2827] | 582 |
|
---|
[2826] | 583 | return true;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 |
|
---|
| 587 | void InitGLstate()
|
---|
| 588 | {
|
---|
[2879] | 589 | glClearColor(0.0f, 0.0, 0.2f, 0.0f);
|
---|
[2642] | 590 |
|
---|
| 591 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
---|
| 592 | glPixelStorei(GL_PACK_ALIGNMENT,1);
|
---|
| 593 |
|
---|
| 594 | glDepthFunc(GL_LESS);
|
---|
[2762] | 595 | glEnable(GL_DEPTH_TEST);
|
---|
[2642] | 596 |
|
---|
[2759] | 597 | SetupLighting();
|
---|
[2642] | 598 |
|
---|
[2760] | 599 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2642] | 600 | glShadeModel(GL_SMOOTH);
|
---|
| 601 |
|
---|
| 602 | glMaterialf(GL_FRONT, GL_SHININESS, 64);
|
---|
| 603 | glEnable(GL_NORMALIZE);
|
---|
[2767] | 604 |
|
---|
| 605 | //glEnable(GL_ALPHA_TEST);
|
---|
| 606 | glDisable(GL_ALPHA_TEST);
|
---|
[2844] | 607 | glAlphaFunc(GL_GEQUAL, 0.8f);
|
---|
[2767] | 608 |
|
---|
[2642] | 609 | glFrontFace(GL_CCW);
|
---|
| 610 | glCullFace(GL_BACK);
|
---|
[2851] | 611 | glEnable(GL_CULL_FACE);
|
---|
| 612 |
|
---|
| 613 | //glDisable(GL_CULL_FACE);
|
---|
[2800] | 614 | glDisable(GL_TEXTURE_2D);
|
---|
[2762] | 615 |
|
---|
[2756] | 616 | GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};
|
---|
| 617 | GLfloat diffuseColor[] = {1.0, 0.0, 0.0, 1.0};
|
---|
[2759] | 618 | GLfloat specularColor[] = {0.0, 0.0, 0.0, 1.0};
|
---|
[2642] | 619 |
|
---|
[2756] | 620 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambientColor);
|
---|
| 621 | glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor);
|
---|
| 622 | glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
|
---|
[2801] | 623 |
|
---|
| 624 | glDepthFunc(GL_LESS);
|
---|
[2826] | 625 |
|
---|
[2827] | 626 | if (!InitFont())
|
---|
[2826] | 627 | cerr << "font creation failed" << endl;
|
---|
| 628 | else
|
---|
| 629 | cout << "successfully created font" << endl;
|
---|
[2642] | 630 | }
|
---|
| 631 |
|
---|
| 632 |
|
---|
[2827] | 633 | void DrawHelpMessage()
|
---|
[2826] | 634 | {
|
---|
[2642] | 635 | const char *message[] =
|
---|
| 636 | {
|
---|
| 637 | "Help information",
|
---|
| 638 | "",
|
---|
| 639 | "'F1' - shows/dismisses this message",
|
---|
[2795] | 640 | "'F2' - shows/hides bird eye view",
|
---|
[2790] | 641 | "'F3' - shows/hides bounds (boxes or tight bounds)",
|
---|
[2827] | 642 | "'F4', - shows/hides parameters",
|
---|
| 643 | "'F5' - shows/hides statistics",
|
---|
| 644 | "'F6', - toggles between fly/walkmode",
|
---|
[2826] | 645 | "'F7', - cycles throw render modes",
|
---|
| 646 | "'F8', - enables/disables ambient occlusion (only deferred)",
|
---|
| 647 | "'F9', - shows pure algorithm render time (using glFinish)",
|
---|
[2790] | 648 | "'SPACE' - cycles through occlusion culling algorithms",
|
---|
[2642] | 649 | "",
|
---|
[2827] | 650 | "'MOUSE LEFT' - turn left/right, move forward/backward",
|
---|
| 651 | "'MOUSE RIGHT' - turn left/right, move forward/backward",
|
---|
| 652 | "'MOUSE MIDDLE' - move up/down, left/right",
|
---|
| 653 | "'CURSOR UP/DOWN' - move forward/backward",
|
---|
| 654 | "'CURSOR LEFT/RIGHT' - turn left/right",
|
---|
[2642] | 655 | "",
|
---|
[2827] | 656 | "'-'/'+' - decreases/increases max batch size",
|
---|
[2837] | 657 | "'1'/'2' - downward/upward motion",
|
---|
| 658 | "'3'/'4' - decreases/increases triangles per virtual bvh leaf (sets bvh depth)",
|
---|
| 659 | "'5'/'6' - decreases/increases assumed visible frames",
|
---|
[2776] | 660 | "",
|
---|
[2786] | 661 | "'R' - use render queue",
|
---|
[2790] | 662 | "'B' - use tight bounds",
|
---|
| 663 | "'M' - use multiqueries",
|
---|
[2792] | 664 | "'O' - use CHC optimization (geometry queries for leaves)",
|
---|
[2642] | 665 | 0,
|
---|
| 666 | };
|
---|
| 667 |
|
---|
[2756] | 668 |
|
---|
[2827] | 669 | glColor4f(0.0f, 1.0f , 0.0f, 0.2f); // 20% green.
|
---|
[2756] | 670 |
|
---|
[2827] | 671 | glRecti(30, 30, winWidth - 30, winHeight - 30);
|
---|
[2642] | 672 |
|
---|
[2827] | 673 | glEnd();
|
---|
| 674 |
|
---|
[2756] | 675 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 676 |
|
---|
[2829] | 677 | glEnable(GL_TEXTURE_2D);
|
---|
[2827] | 678 | myfont.Begin();
|
---|
| 679 |
|
---|
| 680 | int x = 40, y = 30;
|
---|
| 681 |
|
---|
| 682 | for(int i = 0; message[i] != 0; ++ i)
|
---|
[2756] | 683 | {
|
---|
| 684 | if(message[i][0] == '\0')
|
---|
| 685 | {
|
---|
[2786] | 686 | y += 15;
|
---|
[2756] | 687 | }
|
---|
| 688 | else
|
---|
| 689 | {
|
---|
[2827] | 690 | myfont.DrawString(message[i], x, winHeight - y);
|
---|
| 691 | y += 25;
|
---|
[2642] | 692 | }
|
---|
| 693 | }
|
---|
[2829] | 694 | glDisable(GL_TEXTURE_2D);
|
---|
[2642] | 695 | }
|
---|
| 696 |
|
---|
| 697 |
|
---|
[2897] | 698 | RenderTraverser *CreateTraverser(Camera *cam)
|
---|
[2764] | 699 | {
|
---|
[2897] | 700 | RenderTraverser *tr;
|
---|
| 701 |
|
---|
[2771] | 702 | bvh->ResetNodeClassifications();
|
---|
| 703 |
|
---|
[2764] | 704 | switch (renderMode)
|
---|
| 705 | {
|
---|
| 706 | case RenderTraverser::CULL_FRUSTUM:
|
---|
[2897] | 707 | tr = new FrustumCullingTraverser();
|
---|
[2764] | 708 | break;
|
---|
| 709 | case RenderTraverser::STOP_AND_WAIT:
|
---|
[2897] | 710 | tr = new StopAndWaitTraverser();
|
---|
[2764] | 711 | break;
|
---|
| 712 | case RenderTraverser::CHC:
|
---|
[2897] | 713 | tr = new CHCTraverser();
|
---|
[2764] | 714 | break;
|
---|
[2767] | 715 | case RenderTraverser::CHCPLUSPLUS:
|
---|
[2897] | 716 | tr = new CHCPlusPlusTraverser();
|
---|
[2767] | 717 | break;
|
---|
| 718 |
|
---|
[2764] | 719 | default:
|
---|
[2897] | 720 | tr = new FrustumCullingTraverser();
|
---|
[2764] | 721 | }
|
---|
| 722 |
|
---|
[2897] | 723 | tr->SetCamera(cam);
|
---|
| 724 | tr->SetHierarchy(bvh);
|
---|
| 725 | tr->SetRenderQueue(renderQueue);
|
---|
| 726 | tr->SetRenderState(&state);
|
---|
| 727 | tr->SetUseOptimization(useOptimization);
|
---|
| 728 | tr->SetUseRenderQueue(useRenderQueue);
|
---|
| 729 | tr->SetVisibilityThreshold(threshold);
|
---|
| 730 | tr->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 731 | tr->SetMaxBatchSize(maxBatchSize);
|
---|
| 732 | tr->SetUseMultiQueries(useMultiQueries);
|
---|
| 733 | tr->SetUseTightBounds(useTightBounds);
|
---|
| 734 | tr->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
|
---|
| 735 | tr->SetRenderQueue(renderQueue);
|
---|
| 736 |
|
---|
| 737 | return tr;
|
---|
[2764] | 738 | }
|
---|
| 739 |
|
---|
| 740 |
|
---|
[2759] | 741 | void SetupLighting()
|
---|
[2642] | 742 | {
|
---|
[2759] | 743 | glEnable(GL_LIGHTING);
|
---|
| 744 | glEnable(GL_LIGHT0);
|
---|
[2825] | 745 | glEnable(GL_LIGHT1);
|
---|
| 746 |
|
---|
[2759] | 747 | GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
|
---|
| 748 | GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 749 | GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 750 |
|
---|
[2825] | 751 | GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
|
---|
[2759] | 752 |
|
---|
| 753 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
|
---|
| 754 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
|
---|
| 755 | glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
|
---|
| 756 |
|
---|
| 757 |
|
---|
| 758 | ////////////
|
---|
| 759 | //-- second light
|
---|
| 760 |
|
---|
[2825] | 761 | GLfloat ambient1[] = {0.2, 0.2, 0.2, 1.0};
|
---|
| 762 | GLfloat diffuse1[] = {1.0, 1.0, 1.0, 1.0};
|
---|
| 763 | //GLfloat diffuse1[] = {0.5, 0.5, 0.5, 1.0};
|
---|
| 764 | GLfloat specular1[] = {0.0, 0.0, 0.0, 1.0};
|
---|
[2759] | 765 |
|
---|
| 766 | glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
|
---|
| 767 | glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
|
---|
| 768 | glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
|
---|
[2825] | 769 |
|
---|
[2759] | 770 |
|
---|
[2825] | 771 | //////////////////////////////
|
---|
| 772 |
|
---|
| 773 | GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
|
---|
| 774 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
| 775 |
|
---|
| 776 | GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
|
---|
[2759] | 777 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
| 778 |
|
---|
| 779 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
[2801] | 780 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
[2759] | 781 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SINGLE_COLOR_EXT);
|
---|
[2642] | 782 | }
|
---|
| 783 |
|
---|
[2800] | 784 |
|
---|
[2795] | 785 | void SetupEyeView()
|
---|
[2642] | 786 | {
|
---|
[2861] | 787 | // store matrix of last frame
|
---|
| 788 | oldViewProjMatrix = matProjectionView;
|
---|
[2834] | 789 |
|
---|
[2759] | 790 | glMatrixMode(GL_PROJECTION);
|
---|
| 791 | glLoadIdentity();
|
---|
| 792 |
|
---|
[2833] | 793 | gluPerspective(fov, winAspectRatio, nearDist, myfar);
|
---|
[2759] | 794 |
|
---|
[2756] | 795 | glMatrixMode(GL_MODELVIEW);
|
---|
[2795] | 796 | glLoadIdentity();
|
---|
[2864] | 797 |
|
---|
| 798 | // set up the camera view
|
---|
[2760] | 799 | camera->SetupCameraView();
|
---|
| 800 |
|
---|
[2892] | 801 |
|
---|
[2864] | 802 | /////////////////
|
---|
| 803 |
|
---|
[2894] | 804 | Matrix4x4 matViewing, matProjection;
|
---|
[2864] | 805 |
|
---|
[2834] | 806 | camera->GetModelViewMatrix(matViewing);
|
---|
| 807 | camera->GetProjectionMatrix(matProjection);
|
---|
| 808 |
|
---|
[2864] | 809 | // store matrix for later use
|
---|
[2834] | 810 | matProjectionView = matViewing * matProjection;
|
---|
| 811 |
|
---|
[2825] | 812 | if (renderType == RenderState::DEFERRED)
|
---|
| 813 | {
|
---|
| 814 | // set modelview matrix for shaders
|
---|
| 815 | cgGLSetStateMatrixParameter(sModelViewProjMatrixParam,
|
---|
| 816 | CG_GL_MODELVIEW_PROJECTION_MATRIX,
|
---|
| 817 | CG_GL_MATRIX_IDENTITY);
|
---|
| 818 |
|
---|
| 819 | static Matrix4x4 identity = IdentityMatrix();
|
---|
[2840] | 820 | cgGLSetMatrixParameterfc(Transform3::sModelMatrixParam, (const float *)identity.x);
|
---|
[2834] | 821 | }
|
---|
[2642] | 822 | }
|
---|
| 823 |
|
---|
| 824 |
|
---|
[2792] | 825 | void KeyHorizontalMotion(float shift)
|
---|
| 826 | {
|
---|
[2888] | 827 | Vector3 hvec = -camera->GetDirection();
|
---|
[2792] | 828 | hvec.z = 0;
|
---|
| 829 |
|
---|
| 830 | Vector3 pos = camera->GetPosition();
|
---|
| 831 | pos += hvec * shift;
|
---|
| 832 |
|
---|
| 833 | camera->SetPosition(pos);
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 |
|
---|
[2794] | 837 | void KeyVerticalMotion(float shift)
|
---|
| 838 | {
|
---|
| 839 | Vector3 uvec = Vector3(0, 0, shift);
|
---|
| 840 |
|
---|
| 841 | Vector3 pos = camera->GetPosition();
|
---|
| 842 | pos += uvec;
|
---|
| 843 |
|
---|
| 844 | camera->SetPosition(pos);
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 |
|
---|
[2857] | 848 | // the main rendering loop
|
---|
[2792] | 849 | void Display()
|
---|
[2801] | 850 | {
|
---|
[2800] | 851 | Vector3 oldPos = camera->GetPosition();
|
---|
| 852 |
|
---|
[2792] | 853 | if (leftKeyPressed)
|
---|
[2795] | 854 | camera->Pitch(KeyRotationAngle());
|
---|
[2792] | 855 | if (rightKeyPressed)
|
---|
[2795] | 856 | camera->Pitch(-KeyRotationAngle());
|
---|
[2792] | 857 | if (upKeyPressed)
|
---|
[2887] | 858 | KeyHorizontalMotion(-KeyShift());
|
---|
| 859 | if (downKeyPressed)
|
---|
[2795] | 860 | KeyHorizontalMotion(KeyShift());
|
---|
[2837] | 861 | if (ascendKeyPressed)
|
---|
| 862 | KeyVerticalMotion(KeyShift());
|
---|
| 863 | if (descendKeyPressed)
|
---|
[2795] | 864 | KeyVerticalMotion(-KeyShift());
|
---|
[2792] | 865 |
|
---|
[2801] | 866 | // place view on ground
|
---|
| 867 | if (!flyMode) PlaceViewer(oldPos);
|
---|
[2800] | 868 |
|
---|
[2826] | 869 | if (showAlgorithmTime)
|
---|
| 870 | {
|
---|
| 871 | glFinish();
|
---|
| 872 | algTimer.Start();
|
---|
| 873 | }
|
---|
[2809] | 874 |
|
---|
[2895] | 875 |
|
---|
[2825] | 876 | // render without shading
|
---|
| 877 | switch (renderType)
|
---|
| 878 | {
|
---|
| 879 | case RenderState::FIXED:
|
---|
| 880 |
|
---|
[2851] | 881 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
[2879] | 882 |
|
---|
[2825] | 883 | state.SetRenderType(RenderState::FIXED);
|
---|
| 884 | glEnable(GL_LIGHTING);
|
---|
[2809] | 885 |
|
---|
[2825] | 886 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 887 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
[2809] | 888 |
|
---|
[2829] | 889 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 890 |
|
---|
[2825] | 891 | break;
|
---|
| 892 |
|
---|
| 893 | case RenderState::DEPTH_PASS:
|
---|
[2851] | 894 |
|
---|
| 895 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
[2879] | 896 |
|
---|
[2857] | 897 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 898 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 899 |
|
---|
[2825] | 900 | state.SetRenderType(RenderState::DEPTH_PASS);
|
---|
[2857] | 901 |
|
---|
| 902 | // the scene is rendered withouth any shading
|
---|
[2825] | 903 | glDisable(GL_LIGHTING);
|
---|
[2809] | 904 |
|
---|
[2829] | 905 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 906 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 907 |
|
---|
[2825] | 908 | break;
|
---|
| 909 |
|
---|
| 910 | case RenderState::DEFERRED:
|
---|
[2851] | 911 |
|
---|
[2897] | 912 | if (showShadowMap)// && shadowChanged)
|
---|
| 913 | {
|
---|
| 914 | if (!shadowMap)
|
---|
| 915 | {
|
---|
| 916 | const float shadowSize = 4096;
|
---|
| 917 | shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera);
|
---|
| 918 | }
|
---|
[2892] | 919 |
|
---|
[2897] | 920 | // change CHC state (must be done for each change of camera)
|
---|
| 921 | BvhNode::SetCurrentState(1);
|
---|
| 922 |
|
---|
| 923 | if (!shadowTraverser)
|
---|
| 924 | shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());
|
---|
| 925 |
|
---|
[2895] | 926 | shadowChanged = false;
|
---|
| 927 |
|
---|
| 928 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 929 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 930 |
|
---|
| 931 | state.SetRenderType(RenderState::DEPTH_PASS);
|
---|
| 932 |
|
---|
| 933 | // the scene is rendered withouth any shading
|
---|
[2911] | 934 | shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView);
|
---|
[2895] | 935 |
|
---|
| 936 | // change back state
|
---|
| 937 | BvhNode::SetCurrentState(0);
|
---|
| 938 |
|
---|
| 939 | glEnable(GL_LIGHTING);
|
---|
| 940 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 941 | }
|
---|
| 942 |
|
---|
[2879] | 943 | if (!fbo) InitFBO();
|
---|
| 944 |
|
---|
[2895] | 945 |
|
---|
[2878] | 946 | // multisampling does not work with deferred shading
|
---|
[2851] | 947 | glDisable(GL_MULTISAMPLE_ARB);
|
---|
| 948 |
|
---|
[2825] | 949 | state.SetRenderType(RenderState::DEFERRED);
|
---|
| 950 |
|
---|
[2861] | 951 | fbo->Bind();
|
---|
[2825] | 952 |
|
---|
| 953 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 954 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 955 |
|
---|
| 956 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 957 | cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
|
---|
| 958 |
|
---|
| 959 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
| 960 | cgGLBindProgram(sCgMrtVertexProgram);
|
---|
| 961 |
|
---|
[2861] | 962 | /// draw to 3 color buffers
|
---|
[2825] | 963 | glDrawBuffers(3, mrt);
|
---|
| 964 |
|
---|
[2829] | 965 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 966 |
|
---|
[2825] | 967 | break;
|
---|
| 968 | }
|
---|
| 969 |
|
---|
[2642] | 970 |
|
---|
[2801] | 971 | glDepthFunc(GL_LESS);
|
---|
[2825] | 972 |
|
---|
[2801] | 973 | glDisable(GL_TEXTURE_2D);
|
---|
| 974 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
[2825] | 975 |
|
---|
[2801] | 976 |
|
---|
[2861] | 977 | // reset lod levels for current frame
|
---|
[2847] | 978 | LODLevel::InitFrame(camera->GetPosition());
|
---|
| 979 |
|
---|
| 980 |
|
---|
[2756] | 981 | // bring eye modelview matrix up-to-date
|
---|
| 982 | SetupEyeView();
|
---|
[2642] | 983 |
|
---|
[2892] | 984 | // set up lights
|
---|
| 985 | GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
|
---|
| 986 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
[2795] | 987 |
|
---|
[2892] | 988 | GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
|
---|
| 989 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
[2801] | 990 |
|
---|
[2892] | 991 | // actually render the scene geometry using the specified algorithm
|
---|
| 992 | traverser->RenderScene();
|
---|
[2826] | 993 |
|
---|
[2892] | 994 |
|
---|
[2825] | 995 | /////////
|
---|
[2809] | 996 | //-- do the rest of the rendering
|
---|
[2801] | 997 |
|
---|
| 998 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 999 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 1000 |
|
---|
[2893] | 1001 |
|
---|
[2801] | 1002 | // reset depth pass and render visible objects
|
---|
[2825] | 1003 | if (renderType == RenderState::DEPTH_PASS)
|
---|
[2801] | 1004 | {
|
---|
[2829] | 1005 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
[2801] | 1006 | RenderVisibleObjects();
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
[2893] | 1009 |
|
---|
[2796] | 1010 | ///////////////
|
---|
| 1011 | //-- render sky
|
---|
[2795] | 1012 |
|
---|
[2894] | 1013 | // q: should we render sky after deferred shading?
|
---|
| 1014 | // this would conveniently solves some issues (e.g, skys without shadows)
|
---|
[2893] | 1015 |
|
---|
[2796] | 1016 | RenderSky();
|
---|
[2893] | 1017 |
|
---|
[2801] | 1018 |
|
---|
| 1019 |
|
---|
[2825] | 1020 | if (renderType == RenderState::DEFERRED)
|
---|
| 1021 | {
|
---|
[2881] | 1022 | FrameBufferObject::Release();
|
---|
[2810] | 1023 |
|
---|
[2825] | 1024 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1025 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2809] | 1026 |
|
---|
[2905] | 1027 | if (!ssaoShader) ssaoShader = new DeferredRenderer(texWidth, texHeight, camera, myfar / MAX_DEPTH_CONST);
|
---|
[2895] | 1028 |
|
---|
[2903] | 1029 | DeferredRenderer::SHADING_METHOD shadingMethod;
|
---|
| 1030 |
|
---|
| 1031 | if (useAdvancedShading)
|
---|
| 1032 | {
|
---|
| 1033 | if (useGlobIllum)
|
---|
| 1034 | shadingMethod = DeferredRenderer::GI;
|
---|
| 1035 | else
|
---|
| 1036 | shadingMethod = DeferredRenderer::SSAO;
|
---|
| 1037 | }
|
---|
| 1038 | else
|
---|
| 1039 | shadingMethod = DeferredRenderer::DEFAULT;
|
---|
| 1040 |
|
---|
| 1041 |
|
---|
[2895] | 1042 | ssaoShader->SetShadingMethod(shadingMethod);
|
---|
| 1043 | ssaoShader->SetSamplingMethod(samplingMethod);
|
---|
| 1044 | ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);
|
---|
[2879] | 1045 |
|
---|
[2895] | 1046 | ShadowMap *sm = showShadowMap ? shadowMap : NULL;
|
---|
[2901] | 1047 | ssaoShader->Render(fbo, oldViewProjMatrix, matProjectionView, ssaoTempCohFactor, sm);
|
---|
[2825] | 1048 | }
|
---|
[2827] | 1049 |
|
---|
[2893] | 1050 |
|
---|
[2827] | 1051 | state.SetRenderType(RenderState::FIXED);
|
---|
[2893] | 1052 | state.Reset();
|
---|
[2827] | 1053 |
|
---|
[2893] | 1054 |
|
---|
| 1055 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 1056 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 1057 |
|
---|
| 1058 |
|
---|
| 1059 |
|
---|
| 1060 | ///////////
|
---|
| 1061 |
|
---|
| 1062 |
|
---|
[2826] | 1063 | if (showAlgorithmTime)
|
---|
| 1064 | {
|
---|
| 1065 | glFinish();
|
---|
[2827] | 1066 |
|
---|
[2826] | 1067 | algTime = algTimer.Elapsedms();
|
---|
[2827] | 1068 | perfGraph->AddData(algTime);
|
---|
[2828] | 1069 |
|
---|
[2827] | 1070 | perfGraph->Draw();
|
---|
[2826] | 1071 | }
|
---|
[2827] | 1072 | else
|
---|
| 1073 | {
|
---|
| 1074 | if (visMode) DisplayVisualization();
|
---|
| 1075 | }
|
---|
[2825] | 1076 |
|
---|
[2884] | 1077 | glFlush();
|
---|
[2847] | 1078 |
|
---|
| 1079 | const bool restart = true;
|
---|
| 1080 | elapsedTime = frameTimer.Elapsedms(restart);
|
---|
| 1081 |
|
---|
[2764] | 1082 | DisplayStats();
|
---|
[2767] | 1083 |
|
---|
[2642] | 1084 | glutSwapBuffers();
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 |
|
---|
| 1088 | #pragma warning( disable : 4100 )
|
---|
[2792] | 1089 | void KeyBoard(unsigned char c, int x, int y)
|
---|
[2642] | 1090 | {
|
---|
| 1091 | switch(c)
|
---|
| 1092 | {
|
---|
| 1093 | case 27:
|
---|
[2792] | 1094 | CleanUp();
|
---|
[2642] | 1095 | exit(0);
|
---|
| 1096 | break;
|
---|
| 1097 | case 32: //space
|
---|
[2800] | 1098 | renderMode = (renderMode + 1) % RenderTraverser::NUM_TRAVERSAL_TYPES;
|
---|
[2897] | 1099 |
|
---|
| 1100 | DEL_PTR(traverser);
|
---|
| 1101 | traverser = CreateTraverser(camera);
|
---|
| 1102 |
|
---|
| 1103 | if (shadowTraverser && shadowMap)
|
---|
| 1104 | {
|
---|
| 1105 | DEL_PTR(shadowTraverser);
|
---|
| 1106 | //shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());
|
---|
| 1107 | }
|
---|
| 1108 |
|
---|
[2642] | 1109 | break;
|
---|
| 1110 | case 'h':
|
---|
| 1111 | case 'H':
|
---|
| 1112 | showHelp = !showHelp;
|
---|
| 1113 | break;
|
---|
| 1114 | case '+':
|
---|
[2867] | 1115 | if (maxBatchSize < 10)
|
---|
| 1116 | maxBatchSize = 10;
|
---|
| 1117 | else
|
---|
| 1118 | maxBatchSize += 10;
|
---|
| 1119 |
|
---|
[2776] | 1120 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
[2642] | 1121 | break;
|
---|
| 1122 | case '-':
|
---|
[2776] | 1123 | maxBatchSize -= 10;
|
---|
| 1124 | if (maxBatchSize < 0) maxBatchSize = 1;
|
---|
| 1125 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
[2642] | 1126 | break;
|
---|
[2837] | 1127 | case 'M':
|
---|
| 1128 | case 'm':
|
---|
| 1129 | useMultiQueries = !useMultiQueries;
|
---|
| 1130 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
| 1131 | break;
|
---|
| 1132 | case '1':
|
---|
| 1133 | descendKeyPressed = true;
|
---|
| 1134 | break;
|
---|
| 1135 | case '2':
|
---|
| 1136 | ascendKeyPressed = true;
|
---|
| 1137 | break;
|
---|
| 1138 | case '3':
|
---|
| 1139 | if (trianglesPerVirtualLeaf >= 100)
|
---|
| 1140 | trianglesPerVirtualLeaf -= 100;
|
---|
| 1141 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1142 | break;
|
---|
| 1143 | case '4':
|
---|
| 1144 | trianglesPerVirtualLeaf += 100;
|
---|
| 1145 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1146 | break;
|
---|
| 1147 | case '5':
|
---|
[2776] | 1148 | assumedVisibleFrames -= 1;
|
---|
| 1149 | if (assumedVisibleFrames < 1) assumedVisibleFrames = 1;
|
---|
| 1150 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1151 | break;
|
---|
[2837] | 1152 | case '6':
|
---|
[2776] | 1153 | assumedVisibleFrames += 1;
|
---|
| 1154 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1155 | break;
|
---|
[2837] | 1156 | case '7':
|
---|
[2901] | 1157 | ssaoTempCohFactor *= 0.5f;
|
---|
[2776] | 1158 | break;
|
---|
[2767] | 1159 | case '8':
|
---|
[2901] | 1160 | ssaoTempCohFactor *= 2.0f;
|
---|
| 1161 | //if (ssaoTempCohFactor > 1.0f) ssaoExpFactor = 1.0f;
|
---|
[2837] | 1162 | break;
|
---|
[2865] | 1163 | case '9':
|
---|
| 1164 | useLODs = !useLODs;
|
---|
| 1165 | SceneEntity::SetUseLODs(useLODs);
|
---|
| 1166 | break;
|
---|
[2887] | 1167 | case 'P':
|
---|
| 1168 | case 'p':
|
---|
[2896] | 1169 | if (samplingMethod == DeferredRenderer::GAUSS)
|
---|
| 1170 | samplingMethod = DeferredRenderer::POISSON;
|
---|
[2887] | 1171 | else
|
---|
[2896] | 1172 | samplingMethod = DeferredRenderer::GAUSS;
|
---|
[2887] | 1173 | break;
|
---|
[2895] | 1174 | case 'Y':
|
---|
| 1175 | case 'y':
|
---|
| 1176 | showShadowMap = !showShadowMap;
|
---|
| 1177 | break;
|
---|
[2875] | 1178 | case 'g':
|
---|
[2903] | 1179 | case 'G':
|
---|
| 1180 | useGlobIllum = !useGlobIllum;
|
---|
| 1181 | break;
|
---|
[2875] | 1182 | case 't':
|
---|
| 1183 | case 'T':
|
---|
| 1184 | useTemporalCoherence = !useTemporalCoherence;
|
---|
| 1185 | break;
|
---|
[2792] | 1186 | case 'o':
|
---|
| 1187 | case 'O':
|
---|
[2642] | 1188 | useOptimization = !useOptimization;
|
---|
[2764] | 1189 | traverser->SetUseOptimization(useOptimization);
|
---|
[2767] | 1190 | break;
|
---|
| 1191 | case 'a':
|
---|
| 1192 | case 'A':
|
---|
[2792] | 1193 | leftKeyPressed = true;
|
---|
[2767] | 1194 | break;
|
---|
| 1195 | case 'd':
|
---|
| 1196 | case 'D':
|
---|
[2792] | 1197 | rightKeyPressed = true;
|
---|
[2767] | 1198 | break;
|
---|
| 1199 | case 'w':
|
---|
| 1200 | case 'W':
|
---|
[2792] | 1201 | upKeyPressed = true;
|
---|
[2767] | 1202 | break;
|
---|
[2829] | 1203 | case 's':
|
---|
| 1204 | case 'S':
|
---|
[2792] | 1205 | downKeyPressed = true;
|
---|
[2767] | 1206 | break;
|
---|
| 1207 | case 'r':
|
---|
| 1208 | case 'R':
|
---|
| 1209 | {
|
---|
| 1210 | useRenderQueue = !useRenderQueue;
|
---|
| 1211 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
| 1212 | }
|
---|
[2790] | 1213 | break;
|
---|
[2786] | 1214 | case 'b':
|
---|
| 1215 | case 'B':
|
---|
| 1216 | {
|
---|
| 1217 | useTightBounds = !useTightBounds;
|
---|
| 1218 | traverser->SetUseTightBounds(useTightBounds);
|
---|
| 1219 | }
|
---|
[2790] | 1220 | break;
|
---|
[2642] | 1221 | default:
|
---|
| 1222 | return;
|
---|
| 1223 | }
|
---|
| 1224 |
|
---|
| 1225 | glutPostRedisplay();
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 |
|
---|
[2792] | 1229 | void SpecialKeyUp(int c, int x, int y)
|
---|
[2642] | 1230 | {
|
---|
[2792] | 1231 | switch (c)
|
---|
| 1232 | {
|
---|
| 1233 | case GLUT_KEY_LEFT:
|
---|
| 1234 | leftKeyPressed = false;
|
---|
| 1235 | break;
|
---|
| 1236 | case GLUT_KEY_RIGHT:
|
---|
| 1237 | rightKeyPressed = false;
|
---|
| 1238 | break;
|
---|
| 1239 | case GLUT_KEY_UP:
|
---|
| 1240 | upKeyPressed = false;
|
---|
| 1241 | break;
|
---|
| 1242 | case GLUT_KEY_DOWN:
|
---|
| 1243 | downKeyPressed = false;
|
---|
| 1244 | break;
|
---|
| 1245 | default:
|
---|
| 1246 | return;
|
---|
| 1247 | }
|
---|
| 1248 | //glutPostRedisplay();
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
| 1251 |
|
---|
| 1252 | void KeyUp(unsigned char c, int x, int y)
|
---|
| 1253 | {
|
---|
| 1254 | switch (c)
|
---|
| 1255 | {
|
---|
[2879] | 1256 |
|
---|
[2792] | 1257 | case 'A':
|
---|
| 1258 | case 'a':
|
---|
| 1259 | leftKeyPressed = false;
|
---|
| 1260 | break;
|
---|
| 1261 | case 'D':
|
---|
| 1262 | case 'd':
|
---|
| 1263 | rightKeyPressed = false;
|
---|
| 1264 | break;
|
---|
| 1265 | case 'W':
|
---|
| 1266 | case 'w':
|
---|
| 1267 | upKeyPressed = false;
|
---|
| 1268 | break;
|
---|
[2829] | 1269 | case 'S':
|
---|
| 1270 | case 's':
|
---|
[2792] | 1271 | downKeyPressed = false;
|
---|
| 1272 | break;
|
---|
[2837] | 1273 | case '1':
|
---|
| 1274 | descendKeyPressed = false;
|
---|
[2794] | 1275 | break;
|
---|
[2837] | 1276 | case '2':
|
---|
| 1277 | ascendKeyPressed = false;
|
---|
[2794] | 1278 | break;
|
---|
| 1279 |
|
---|
[2792] | 1280 | default:
|
---|
| 1281 | return;
|
---|
| 1282 | }
|
---|
| 1283 | //glutPostRedisplay();
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 |
|
---|
| 1287 | void Special(int c, int x, int y)
|
---|
| 1288 | {
|
---|
[2642] | 1289 | switch(c)
|
---|
| 1290 | {
|
---|
| 1291 | case GLUT_KEY_F1:
|
---|
| 1292 | showHelp = !showHelp;
|
---|
| 1293 | break;
|
---|
[2790] | 1294 | case GLUT_KEY_F2:
|
---|
[2795] | 1295 | visMode = !visMode;
|
---|
[2790] | 1296 | break;
|
---|
| 1297 | case GLUT_KEY_F3:
|
---|
| 1298 | showBoundingVolumes = !showBoundingVolumes;
|
---|
| 1299 | traverser->SetShowBounds(showBoundingVolumes);
|
---|
| 1300 | break;
|
---|
| 1301 | case GLUT_KEY_F4:
|
---|
[2827] | 1302 | showOptions = !showOptions;
|
---|
[2790] | 1303 | break;
|
---|
| 1304 | case GLUT_KEY_F5:
|
---|
[2827] | 1305 | showStatistics = !showStatistics;
|
---|
[2790] | 1306 | break;
|
---|
[2800] | 1307 | case GLUT_KEY_F6:
|
---|
| 1308 | flyMode = !flyMode;
|
---|
| 1309 | break;
|
---|
[2801] | 1310 | case GLUT_KEY_F7:
|
---|
[2825] | 1311 |
|
---|
| 1312 | renderType = (renderType + 1) % 3;
|
---|
| 1313 | traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
|
---|
| 1314 |
|
---|
[2801] | 1315 | break;
|
---|
[2803] | 1316 | case GLUT_KEY_F8:
|
---|
[2903] | 1317 |
|
---|
| 1318 | useAdvancedShading = !useAdvancedShading;
|
---|
| 1319 | //shadingMethod = (DeferredRenderer::SHADING_METHOD)((shadingMethod + 1) % 2);
|
---|
| 1320 |
|
---|
[2821] | 1321 | break;
|
---|
[2826] | 1322 | case GLUT_KEY_F9:
|
---|
| 1323 | showAlgorithmTime = !showAlgorithmTime;
|
---|
| 1324 | break;
|
---|
| 1325 |
|
---|
[2642] | 1326 | case GLUT_KEY_LEFT:
|
---|
[2767] | 1327 | {
|
---|
[2792] | 1328 | leftKeyPressed = true;
|
---|
[2795] | 1329 | camera->Pitch(KeyRotationAngle());
|
---|
[2767] | 1330 | }
|
---|
[2642] | 1331 | break;
|
---|
| 1332 | case GLUT_KEY_RIGHT:
|
---|
[2767] | 1333 | {
|
---|
[2792] | 1334 | rightKeyPressed = true;
|
---|
[2795] | 1335 | camera->Pitch(-KeyRotationAngle());
|
---|
[2767] | 1336 | }
|
---|
[2642] | 1337 | break;
|
---|
| 1338 | case GLUT_KEY_UP:
|
---|
[2767] | 1339 | {
|
---|
[2792] | 1340 | upKeyPressed = true;
|
---|
[2795] | 1341 | KeyHorizontalMotion(KeyShift());
|
---|
[2767] | 1342 | }
|
---|
[2642] | 1343 | break;
|
---|
| 1344 | case GLUT_KEY_DOWN:
|
---|
[2767] | 1345 | {
|
---|
[2792] | 1346 | downKeyPressed = true;
|
---|
[2795] | 1347 | KeyHorizontalMotion(-KeyShift());
|
---|
[2767] | 1348 | }
|
---|
[2642] | 1349 | break;
|
---|
| 1350 | default:
|
---|
| 1351 | return;
|
---|
| 1352 |
|
---|
| 1353 | }
|
---|
| 1354 |
|
---|
| 1355 | glutPostRedisplay();
|
---|
| 1356 | }
|
---|
[2767] | 1357 |
|
---|
[2642] | 1358 | #pragma warning( default : 4100 )
|
---|
| 1359 |
|
---|
| 1360 |
|
---|
[2792] | 1361 | void Reshape(int w, int h)
|
---|
[2642] | 1362 | {
|
---|
[2759] | 1363 | winAspectRatio = 1.0f;
|
---|
[2642] | 1364 |
|
---|
| 1365 | glViewport(0, 0, w, h);
|
---|
| 1366 |
|
---|
| 1367 | winWidth = w;
|
---|
| 1368 | winHeight = h;
|
---|
| 1369 |
|
---|
[2833] | 1370 | if (w) winAspectRatio = (float) w / (float) h;
|
---|
[2642] | 1371 |
|
---|
[2758] | 1372 | glMatrixMode(GL_PROJECTION);
|
---|
| 1373 | glLoadIdentity();
|
---|
[2642] | 1374 |
|
---|
[2833] | 1375 | gluPerspective(fov, winAspectRatio, nearDist, myfar);
|
---|
[2788] | 1376 |
|
---|
[2758] | 1377 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1378 |
|
---|
[2642] | 1379 | glutPostRedisplay();
|
---|
| 1380 | }
|
---|
| 1381 |
|
---|
| 1382 |
|
---|
[2792] | 1383 | void Mouse(int button, int state, int x, int y)
|
---|
[2642] | 1384 | {
|
---|
[2758] | 1385 | if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
|
---|
[2642] | 1386 | {
|
---|
| 1387 | xEyeBegin = x;
|
---|
| 1388 | yMotionBegin = y;
|
---|
| 1389 |
|
---|
[2792] | 1390 | glutMotionFunc(LeftMotion);
|
---|
[2642] | 1391 | }
|
---|
[2758] | 1392 | else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
|
---|
[2642] | 1393 | {
|
---|
[2829] | 1394 | xEyeBegin = x;
|
---|
[2758] | 1395 | yEyeBegin = y;
|
---|
| 1396 | yMotionBegin = y;
|
---|
| 1397 |
|
---|
[2792] | 1398 | glutMotionFunc(RightMotion);
|
---|
[2758] | 1399 | }
|
---|
| 1400 | else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
|
---|
| 1401 | {
|
---|
[2642] | 1402 | horizontalMotionBegin = x;
|
---|
| 1403 | verticalMotionBegin = y;
|
---|
[2792] | 1404 | glutMotionFunc(MiddleMotion);
|
---|
[2642] | 1405 | }
|
---|
| 1406 |
|
---|
| 1407 | glutPostRedisplay();
|
---|
| 1408 | }
|
---|
| 1409 |
|
---|
[2758] | 1410 |
|
---|
| 1411 | /** rotation for left/right mouse drag
|
---|
[2642] | 1412 | motion for up/down mouse drag
|
---|
| 1413 | */
|
---|
[2792] | 1414 | void LeftMotion(int x, int y)
|
---|
[2642] | 1415 | {
|
---|
[2758] | 1416 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1417 | Vector3 pos = camera->GetPosition();
|
---|
| 1418 |
|
---|
| 1419 | // don't move in the vertical direction
|
---|
[2764] | 1420 | Vector3 horView(viewDir[0], viewDir[1], 0);
|
---|
[2642] | 1421 |
|
---|
[2795] | 1422 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
[2756] | 1423 |
|
---|
[2795] | 1424 | camera->Pitch(eyeXAngle);
|
---|
[2787] | 1425 |
|
---|
[2888] | 1426 | pos += horView * (yMotionBegin - y) * 0.2f;
|
---|
[2795] | 1427 |
|
---|
[2758] | 1428 | camera->SetPosition(pos);
|
---|
[2642] | 1429 |
|
---|
| 1430 | xEyeBegin = x;
|
---|
| 1431 | yMotionBegin = y;
|
---|
[2758] | 1432 |
|
---|
[2642] | 1433 | glutPostRedisplay();
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
[2758] | 1436 |
|
---|
[2767] | 1437 | /** rotation for left / right mouse drag
|
---|
| 1438 | motion for up / down mouse drag
|
---|
[2758] | 1439 | */
|
---|
[2792] | 1440 | void RightMotion(int x, int y)
|
---|
[2758] | 1441 | {
|
---|
[2829] | 1442 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
[2795] | 1443 | float eyeYAngle = -0.2f * M_PI * (yEyeBegin - y) / 180.0;
|
---|
[2758] | 1444 |
|
---|
[2795] | 1445 | camera->Yaw(eyeYAngle);
|
---|
[2829] | 1446 | camera->Pitch(eyeXAngle);
|
---|
[2780] | 1447 |
|
---|
[2829] | 1448 | xEyeBegin = x;
|
---|
[2758] | 1449 | yEyeBegin = y;
|
---|
[2829] | 1450 |
|
---|
[2758] | 1451 | glutPostRedisplay();
|
---|
| 1452 | }
|
---|
| 1453 |
|
---|
| 1454 |
|
---|
[2642] | 1455 | // strafe
|
---|
[2792] | 1456 | void MiddleMotion(int x, int y)
|
---|
[2642] | 1457 | {
|
---|
[2758] | 1458 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1459 | Vector3 pos = camera->GetPosition();
|
---|
| 1460 |
|
---|
[2642] | 1461 | // the 90 degree rotated view vector
|
---|
| 1462 | // y zero so we don't move in the vertical
|
---|
[2764] | 1463 | Vector3 rVec(viewDir[0], viewDir[1], 0);
|
---|
[2642] | 1464 |
|
---|
[2764] | 1465 | Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f);
|
---|
[2758] | 1466 | rVec = rot * rVec;
|
---|
[2642] | 1467 |
|
---|
[2888] | 1468 | pos -= rVec * (x - horizontalMotionBegin) * 0.1f;
|
---|
[2764] | 1469 | pos[2] += (verticalMotionBegin - y) * 0.1f;
|
---|
[2642] | 1470 |
|
---|
[2758] | 1471 | camera->SetPosition(pos);
|
---|
| 1472 |
|
---|
[2642] | 1473 | horizontalMotionBegin = x;
|
---|
| 1474 | verticalMotionBegin = y;
|
---|
[2758] | 1475 |
|
---|
[2642] | 1476 | glutPostRedisplay();
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
| 1479 |
|
---|
[2756] | 1480 | void InitExtensions(void)
|
---|
[2642] | 1481 | {
|
---|
| 1482 | GLenum err = glewInit();
|
---|
[2756] | 1483 |
|
---|
[2642] | 1484 | if (GLEW_OK != err)
|
---|
| 1485 | {
|
---|
| 1486 | // problem: glewInit failed, something is seriously wrong
|
---|
| 1487 | fprintf(stderr,"Error: %s\n", glewGetErrorString(err));
|
---|
| 1488 | exit(1);
|
---|
| 1489 | }
|
---|
[2756] | 1490 | if (!GLEW_ARB_occlusion_query)
|
---|
[2642] | 1491 | {
|
---|
[2756] | 1492 | printf("I require the GL_ARB_occlusion_query to work.\n");
|
---|
[2642] | 1493 | exit(1);
|
---|
| 1494 | }
|
---|
| 1495 | }
|
---|
| 1496 |
|
---|
| 1497 |
|
---|
[2826] | 1498 | void Begin2D()
|
---|
[2642] | 1499 | {
|
---|
| 1500 | glDisable(GL_LIGHTING);
|
---|
| 1501 | glDisable(GL_DEPTH_TEST);
|
---|
| 1502 |
|
---|
[2826] | 1503 | glMatrixMode(GL_PROJECTION);
|
---|
[2642] | 1504 | glPushMatrix();
|
---|
| 1505 | glLoadIdentity();
|
---|
[2834] | 1506 |
|
---|
[2826] | 1507 | gluOrtho2D(0, winWidth, 0, winHeight);
|
---|
[2642] | 1508 |
|
---|
[2826] | 1509 | glMatrixMode(GL_MODELVIEW);
|
---|
[2642] | 1510 | glPushMatrix();
|
---|
| 1511 | glLoadIdentity();
|
---|
| 1512 | }
|
---|
| 1513 |
|
---|
| 1514 |
|
---|
[2826] | 1515 | void End2D()
|
---|
[2642] | 1516 | {
|
---|
[2834] | 1517 | glMatrixMode(GL_PROJECTION);
|
---|
[2642] | 1518 | glPopMatrix();
|
---|
[2834] | 1519 |
|
---|
[2642] | 1520 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1521 | glPopMatrix();
|
---|
| 1522 |
|
---|
| 1523 | glEnable(GL_LIGHTING);
|
---|
| 1524 | glEnable(GL_DEPTH_TEST);
|
---|
| 1525 | }
|
---|
| 1526 |
|
---|
| 1527 |
|
---|
[2787] | 1528 | // displays the visualisation of culling algorithm
|
---|
| 1529 | void DisplayVisualization()
|
---|
[2796] | 1530 | {
|
---|
[2787] | 1531 | visualization->SetFrameId(traverser->GetCurrentFrameId());
|
---|
| 1532 |
|
---|
[2792] | 1533 | Begin2D();
|
---|
[2642] | 1534 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 1535 | glEnable(GL_BLEND);
|
---|
[2827] | 1536 | glColor4f(0.0f ,0.0f, 0.0f, 0.5f);
|
---|
[2642] | 1537 |
|
---|
[2827] | 1538 | glRecti(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth, winHeight);
|
---|
[2642] | 1539 | glDisable(GL_BLEND);
|
---|
[2792] | 1540 | End2D();
|
---|
[2788] | 1541 |
|
---|
| 1542 |
|
---|
| 1543 | AxisAlignedBox3 box = bvh->GetBox();
|
---|
| 1544 |
|
---|
[2838] | 1545 | // hack: set far plane for viz
|
---|
[2911] | 1546 | //camera->SetFar(0.35f * Magnitude(box.Diagonal()));
|
---|
[2913] | 1547 | //camera->SetFar(1000);
|
---|
[2796] | 1548 |
|
---|
[2807] | 1549 | const float offs = box.Size().x * 0.3f;
|
---|
[2834] | 1550 |
|
---|
[2838] | 1551 | Vector3 vizpos = Vector3(box.Min().x, box.Min().y - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
|
---|
[2806] | 1552 |
|
---|
[2795] | 1553 | visCamera->SetPosition(vizpos);
|
---|
[2838] | 1554 | visCamera->ResetPitchAndYaw();
|
---|
[2892] | 1555 |
|
---|
[2834] | 1556 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2806] | 1557 | glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3);
|
---|
[2796] | 1558 |
|
---|
| 1559 | glMatrixMode(GL_PROJECTION);
|
---|
[2834] | 1560 | glPushMatrix();
|
---|
| 1561 |
|
---|
[2787] | 1562 | glLoadIdentity();
|
---|
[2796] | 1563 |
|
---|
[2807] | 1564 | glOrtho(-offs, offs, -offs, offs, 0.0f, box.Size().z * 100.0f);
|
---|
[2787] | 1565 |
|
---|
[2796] | 1566 | glMatrixMode(GL_MODELVIEW);
|
---|
[2834] | 1567 | glPushMatrix();
|
---|
[2787] | 1568 |
|
---|
[2796] | 1569 | visCamera->SetupCameraView();
|
---|
[2806] | 1570 |
|
---|
[2838] | 1571 | //Matrix4x4 rotX = RotationXMatrix(camera->GetYaw());
|
---|
| 1572 | //glMultMatrixf((float *)rotX.x);
|
---|
[2806] | 1573 | Matrix4x4 rotZ = RotationZMatrix(-camera->GetPitch());
|
---|
| 1574 | glMultMatrixf((float *)rotZ.x);
|
---|
| 1575 |
|
---|
[2887] | 1576 | // inverse translation in order to fix current position
|
---|
[2838] | 1577 | Vector3 pos = camera->GetPosition();
|
---|
[2806] | 1578 | glTranslatef(-pos.x, -pos.y, -pos.z);
|
---|
| 1579 |
|
---|
| 1580 |
|
---|
[2788] | 1581 | GLfloat position[] = {0.8f, 1.0f, 1.5f, 0.0f};
|
---|
| 1582 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
[2787] | 1583 |
|
---|
[2788] | 1584 | GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f};
|
---|
| 1585 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
[2787] | 1586 |
|
---|
[2642] | 1587 | glClear(GL_DEPTH_BUFFER_BIT);
|
---|
| 1588 |
|
---|
[2888] | 1589 |
|
---|
[2767] | 1590 | ////////////
|
---|
[2787] | 1591 | //-- visualization of the occlusion culling
|
---|
| 1592 |
|
---|
[2767] | 1593 | visualization->Render();
|
---|
[2887] | 1594 |
|
---|
[2767] | 1595 |
|
---|
[2834] | 1596 | // reset previous settings
|
---|
| 1597 | glPopAttrib();
|
---|
| 1598 |
|
---|
| 1599 | glMatrixMode(GL_PROJECTION);
|
---|
| 1600 | glPopMatrix();
|
---|
| 1601 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1602 | glPopMatrix();
|
---|
[2642] | 1603 | }
|
---|
| 1604 |
|
---|
[2767] | 1605 |
|
---|
[2642] | 1606 | // cleanup routine after the main loop
|
---|
[2756] | 1607 | void CleanUp()
|
---|
[2642] | 1608 | {
|
---|
[2756] | 1609 | DEL_PTR(traverser);
|
---|
[2796] | 1610 | DEL_PTR(sceneQuery);
|
---|
[2756] | 1611 | DEL_PTR(bvh);
|
---|
[2767] | 1612 | DEL_PTR(visualization);
|
---|
[2787] | 1613 | DEL_PTR(camera);
|
---|
[2793] | 1614 | DEL_PTR(loader);
|
---|
[2801] | 1615 | DEL_PTR(renderQueue);
|
---|
[2827] | 1616 | DEL_PTR(perfGraph);
|
---|
[2809] | 1617 |
|
---|
[2879] | 1618 | DEL_PTR(fbo);
|
---|
| 1619 | DEL_PTR(ssaoShader);
|
---|
| 1620 |
|
---|
[2810] | 1621 | if (sCgMrtVertexProgram)
|
---|
| 1622 | cgDestroyProgram(sCgMrtVertexProgram);
|
---|
[2827] | 1623 | if (RenderState::sCgMrtFragmentProgram)
|
---|
| 1624 | cgDestroyProgram(RenderState::sCgMrtFragmentProgram);
|
---|
| 1625 | if (RenderState::sCgMrtFragmentTexProgram)
|
---|
| 1626 | cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
|
---|
[2861] | 1627 |
|
---|
[2809] | 1628 | if (sCgContext)
|
---|
| 1629 | cgDestroyContext(sCgContext);
|
---|
[2642] | 1630 | }
|
---|
| 1631 |
|
---|
| 1632 |
|
---|
| 1633 | // this function inserts a dezimal point after each 1000
|
---|
[2829] | 1634 | void CalcDecimalPoint(string &str, int d, int len)
|
---|
[2642] | 1635 | {
|
---|
[2827] | 1636 | static vector<int> numbers;
|
---|
| 1637 | numbers.clear();
|
---|
| 1638 |
|
---|
[2829] | 1639 | static string shortStr;
|
---|
| 1640 | shortStr.clear();
|
---|
[2642] | 1641 |
|
---|
[2829] | 1642 | static char hstr[100];
|
---|
| 1643 |
|
---|
[2642] | 1644 | while (d != 0)
|
---|
| 1645 | {
|
---|
| 1646 | numbers.push_back(d % 1000);
|
---|
| 1647 | d /= 1000;
|
---|
| 1648 | }
|
---|
| 1649 |
|
---|
| 1650 | // first element without leading zeros
|
---|
| 1651 | if (numbers.size() > 0)
|
---|
| 1652 | {
|
---|
[2800] | 1653 | sprintf(hstr, "%d", numbers.back());
|
---|
[2829] | 1654 | shortStr.append(hstr);
|
---|
[2642] | 1655 | }
|
---|
| 1656 |
|
---|
[2764] | 1657 | for (int i = (int)numbers.size() - 2; i >= 0; i--)
|
---|
[2642] | 1658 | {
|
---|
[2800] | 1659 | sprintf(hstr, ",%03d", numbers[i]);
|
---|
[2829] | 1660 | shortStr.append(hstr);
|
---|
[2642] | 1661 | }
|
---|
[2829] | 1662 |
|
---|
| 1663 | int dif = len - (int)shortStr.size();
|
---|
| 1664 |
|
---|
| 1665 | for (int i = 0; i < dif; ++ i)
|
---|
| 1666 | {
|
---|
| 1667 | str += " ";
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
| 1670 | str.append(shortStr);
|
---|
[2764] | 1671 | }
|
---|
| 1672 |
|
---|
| 1673 |
|
---|
| 1674 | void DisplayStats()
|
---|
| 1675 | {
|
---|
[2826] | 1676 | static char msg[9][300];
|
---|
[2764] | 1677 |
|
---|
[2826] | 1678 | static double frameTime = elapsedTime;
|
---|
| 1679 | static double renderTime = algTime;
|
---|
| 1680 |
|
---|
[2818] | 1681 | const float expFactor = 0.1f;
|
---|
[2767] | 1682 |
|
---|
[2802] | 1683 | // if some strange render time spike happened in this frame => don't count
|
---|
[2826] | 1684 | if (elapsedTime < 500) frameTime = elapsedTime * expFactor + (1.0f - expFactor) * elapsedTime;
|
---|
| 1685 |
|
---|
| 1686 | static float rTime = 1000.0f;
|
---|
[2764] | 1687 |
|
---|
[2826] | 1688 | if (showAlgorithmTime)
|
---|
| 1689 | {
|
---|
| 1690 | if (algTime < 500) renderTime = algTime * expFactor + (1.0f - expFactor) * renderTime;
|
---|
| 1691 | }
|
---|
[2802] | 1692 |
|
---|
[2826] | 1693 | accumulatedTime += elapsedTime;
|
---|
| 1694 |
|
---|
[2776] | 1695 | if (accumulatedTime > 500) // update every fraction of a second
|
---|
[2770] | 1696 | {
|
---|
| 1697 | accumulatedTime = 0;
|
---|
| 1698 |
|
---|
[2826] | 1699 | if (frameTime) fps = 1e3f / (float)frameTime;
|
---|
| 1700 |
|
---|
| 1701 | rTime = renderTime;
|
---|
[2770] | 1702 | renderedObjects = traverser->GetStats().mNumRenderedGeometry;
|
---|
[2773] | 1703 | renderedNodes = traverser->GetStats().mNumRenderedNodes;
|
---|
| 1704 | renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
|
---|
| 1705 |
|
---|
[2770] | 1706 | traversedNodes = traverser->GetStats().mNumTraversedNodes;
|
---|
| 1707 | frustumCulledNodes = traverser->GetStats().mNumFrustumCulledNodes;
|
---|
| 1708 | queryCulledNodes = traverser->GetStats().mNumQueryCulledNodes;
|
---|
| 1709 | issuedQueries = traverser->GetStats().mNumIssuedQueries;
|
---|
| 1710 | stateChanges = traverser->GetStats().mNumStateChanges;
|
---|
[2800] | 1711 | numBatches = traverser->GetStats().mNumBatches;
|
---|
[2770] | 1712 | }
|
---|
| 1713 |
|
---|
[2764] | 1714 |
|
---|
[2826] | 1715 | Begin2D();
|
---|
[2808] | 1716 |
|
---|
[2826] | 1717 | glEnable(GL_BLEND);
|
---|
| 1718 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
[2764] | 1719 |
|
---|
[2826] | 1720 | if (showHelp)
|
---|
| 1721 | {
|
---|
| 1722 | DrawHelpMessage();
|
---|
| 1723 | }
|
---|
| 1724 | else
|
---|
| 1725 | {
|
---|
[2829] | 1726 | if (showOptions)
|
---|
| 1727 | {
|
---|
| 1728 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1729 | glRecti(5, winHeight - 95, winWidth * 2 / 3 - 5, winHeight - 5);
|
---|
| 1730 | }
|
---|
| 1731 |
|
---|
| 1732 | if (showStatistics)
|
---|
| 1733 | {
|
---|
| 1734 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1735 | glRecti(5, winHeight - 165, winWidth * 2 / 3 - 5, winHeight - 100);
|
---|
| 1736 | }
|
---|
| 1737 |
|
---|
| 1738 | glEnable(GL_TEXTURE_2D);
|
---|
| 1739 |
|
---|
[2827] | 1740 | myfont.Begin();
|
---|
[2769] | 1741 |
|
---|
[2826] | 1742 | if (showOptions)
|
---|
| 1743 | {
|
---|
[2829] | 1744 | glColor3f(0.0f, 1.0f, 0.0f);
|
---|
| 1745 |
|
---|
[2826] | 1746 | int i = 0;
|
---|
| 1747 |
|
---|
| 1748 | static char *renderTypeStr[] = {"fixed function", "fixed function + depth pass", "deferred shading"};
|
---|
[2825] | 1749 |
|
---|
[2826] | 1750 | sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d",
|
---|
| 1751 | useMultiQueries, useTightBounds, useRenderQueue);
|
---|
[2792] | 1752 |
|
---|
[2903] | 1753 | sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderTypeStr[renderType], useAdvancedShading);
|
---|
[2808] | 1754 |
|
---|
[2826] | 1755 | sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
|
---|
[2808] | 1756 |
|
---|
[2826] | 1757 | sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d",
|
---|
| 1758 | assumedVisibleFrames, maxBatchSize);
|
---|
[2808] | 1759 |
|
---|
[2826] | 1760 | for (int j = 0; j < 4; ++ j)
|
---|
[2829] | 1761 | myfont.DrawString(msg[j], 10.0f, winHeight - 5 - j * 20);
|
---|
[2826] | 1762 | }
|
---|
[2808] | 1763 |
|
---|
[2786] | 1764 | if (showStatistics)
|
---|
[2764] | 1765 | {
|
---|
[2829] | 1766 | glColor3f(1.0f, 1.0f, 0.0f);
|
---|
| 1767 |
|
---|
[2826] | 1768 | string str;
|
---|
| 1769 | string str2;
|
---|
| 1770 |
|
---|
[2829] | 1771 | int len = 10;
|
---|
| 1772 | CalcDecimalPoint(str, renderedTriangles, len);
|
---|
| 1773 | CalcDecimalPoint(str2, bvh->GetBvhStats().mTriangles, len);
|
---|
[2826] | 1774 |
|
---|
| 1775 | int i = 4;
|
---|
| 1776 |
|
---|
[2829] | 1777 | sprintf(msg[i ++], "rendered: %6d of %6d nodes, %s of %s triangles",
|
---|
[2826] | 1778 | renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str());
|
---|
| 1779 |
|
---|
| 1780 | sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d",
|
---|
| 1781 | traversedNodes, frustumCulledNodes, queryCulledNodes);
|
---|
| 1782 |
|
---|
| 1783 | sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d",
|
---|
| 1784 | issuedQueries, stateChanges, numBatches);
|
---|
| 1785 |
|
---|
| 1786 | for (int j = 4; j < 7; ++ j)
|
---|
[2829] | 1787 | myfont.DrawString(msg[j], 10.0f, winHeight - (j + 1) * 20);
|
---|
[2764] | 1788 | }
|
---|
[2790] | 1789 |
|
---|
[2826] | 1790 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 1791 | static char *alg_str[] = {"Frustum Cull", "Stop and Wait", "CHC", "CHC ++"};
|
---|
[2827] | 1792 |
|
---|
| 1793 | if (!showAlgorithmTime)
|
---|
| 1794 | {
|
---|
| 1795 | sprintf(msg[7], "%s: %6.1f fps", alg_str[renderMode], fps);
|
---|
| 1796 | }
|
---|
| 1797 | else
|
---|
| 1798 | {
|
---|
| 1799 | sprintf(msg[7], "%s: %6.1f ms", alg_str[renderMode], rTime);
|
---|
| 1800 | }
|
---|
[2826] | 1801 |
|
---|
[2829] | 1802 | myfont.DrawString(msg[7], 1.3f, 690.0f, 760.0f);//, top_color, bottom_color);
|
---|
[2827] | 1803 |
|
---|
| 1804 | //sprintf(msg[8], "algorithm time: %6.1f ms", rTime);
|
---|
| 1805 | //myfont.DrawString(msg[8], 720.0f, 730.0f);
|
---|
[2764] | 1806 | }
|
---|
| 1807 |
|
---|
[2826] | 1808 | glDisable(GL_BLEND);
|
---|
| 1809 | glDisable(GL_TEXTURE_2D);
|
---|
| 1810 |
|
---|
[2792] | 1811 | End2D();
|
---|
[2764] | 1812 | }
|
---|
[2796] | 1813 |
|
---|
| 1814 |
|
---|
| 1815 | void RenderSky()
|
---|
| 1816 | {
|
---|
| 1817 | SceneEntityContainer::const_iterator sit, sit_end = skyGeometry.end();
|
---|
| 1818 |
|
---|
| 1819 | for (sit = skyGeometry.begin(); sit != sit_end; ++ sit)
|
---|
| 1820 | (*sit)->Render(&state);
|
---|
[2801] | 1821 | }
|
---|
[2796] | 1822 |
|
---|
[2895] | 1823 | // render visible object from depth pass
|
---|
[2801] | 1824 | void RenderVisibleObjects()
|
---|
| 1825 | {
|
---|
[2825] | 1826 | state.SetRenderType(RenderState::FIXED);
|
---|
[2796] | 1827 | state.Reset();
|
---|
[2801] | 1828 |
|
---|
| 1829 | glEnable(GL_LIGHTING);
|
---|
| 1830 | glDepthFunc(GL_LEQUAL);
|
---|
| 1831 |
|
---|
| 1832 | //cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
|
---|
| 1833 |
|
---|
| 1834 | SceneEntityContainer::const_iterator sit,
|
---|
| 1835 | sit_end = traverser->GetVisibleObjects().end();
|
---|
| 1836 |
|
---|
| 1837 | for (sit = traverser->GetVisibleObjects().begin(); sit != sit_end; ++ sit)
|
---|
| 1838 | renderQueue->Enqueue(*sit);
|
---|
| 1839 |
|
---|
| 1840 | renderQueue->Apply();
|
---|
| 1841 |
|
---|
| 1842 | glDepthFunc(GL_LESS);
|
---|
| 1843 | }
|
---|
| 1844 |
|
---|
| 1845 |
|
---|
| 1846 | void PlaceViewer(const Vector3 &oldPos)
|
---|
| 1847 | {
|
---|
| 1848 | Vector3 playerPos = camera->GetPosition();
|
---|
| 1849 |
|
---|
[2843] | 1850 | bool validIntersect = sceneQuery->CalcIntersection(playerPos);
|
---|
[2801] | 1851 |
|
---|
[2853] | 1852 | if (validIntersect)
|
---|
[2848] | 1853 | // && ((playerPos.z - oldPos.z) < bvh->GetBox().Size(2) * 1e-1f))
|
---|
[2801] | 1854 | {
|
---|
| 1855 | camera->SetPosition(playerPos);
|
---|
| 1856 | }
|
---|
[2809] | 1857 | }
|
---|