[2764] | 1 | #include "SceneQuery.h"
|
---|
| 2 | #include "glInterface.h"
|
---|
| 3 | #include "Vector3.h"
|
---|
| 4 | #include "Camera.h"
|
---|
[2778] | 5 | #include "SceneQuery.h"
|
---|
| 6 | #include "RenderTraverser.h"
|
---|
[2887] | 7 | #include "FrameBufferObject.h"
|
---|
[3101] | 8 | #include "RenderState.h"
|
---|
[2887] | 9 |
|
---|
[2800] | 10 | #include <IL/il.h>
|
---|
| 11 | #include <assert.h>
|
---|
[2764] | 12 |
|
---|
| 13 |
|
---|
[3021] | 14 | #ifdef _CRT_SET
|
---|
| 15 | #define _CRTDBG_MAP_ALLOC
|
---|
| 16 | #include <stdlib.h>
|
---|
| 17 | #include <crtdbg.h>
|
---|
| 18 |
|
---|
| 19 | // redefine new operator
|
---|
| 20 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 21 | #define new DEBUG_NEW
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 |
|
---|
[2808] | 25 | using namespace std;
|
---|
| 26 |
|
---|
[2848] | 27 | const static int texWidth = 2048;
|
---|
| 28 | const static int texHeight = 2048;
|
---|
[2808] | 29 |
|
---|
| 30 |
|
---|
| 31 | static void PrintGLerror(char *msg)
|
---|
| 32 | {
|
---|
| 33 | GLenum errCode;
|
---|
| 34 | const GLubyte *errStr;
|
---|
| 35 |
|
---|
| 36 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 37 | {
|
---|
| 38 | errStr = gluErrorString(errCode);
|
---|
| 39 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
[2887] | 44 | static void startil()
|
---|
[2800] | 45 | {
|
---|
| 46 | ilInit();
|
---|
| 47 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 48 | }
|
---|
[2764] | 49 |
|
---|
| 50 |
|
---|
[2887] | 51 | static void stopil()
|
---|
[2800] | 52 | {
|
---|
| 53 | ilShutDown();
|
---|
| 54 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[2776] | 58 | namespace CHCDemoEngine
|
---|
[2764] | 59 | {
|
---|
| 60 |
|
---|
| 61 |
|
---|
[2887] | 62 | static void GrabDepthBuffer(float *data, GLuint depthTexture)
|
---|
[2808] | 63 | {
|
---|
[2877] | 64 | glEnable(GL_TEXTURE_2D);
|
---|
| 65 | glBindTexture(GL_TEXTURE_2D, depthTexture);
|
---|
[2764] | 66 |
|
---|
[2877] | 67 | glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
|
---|
[2764] | 68 |
|
---|
[2877] | 69 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 70 | glDisable(GL_TEXTURE_2D);
|
---|
[2808] | 71 | }
|
---|
[2764] | 72 |
|
---|
| 73 |
|
---|
[2887] | 74 | static void ExportDepthBuffer(float *data)
|
---|
[2808] | 75 | {
|
---|
| 76 | startil();
|
---|
| 77 |
|
---|
[3101] | 78 | ILstring filename = ILstring("sceneQuery.tga");
|
---|
[2808] | 79 | ilRegisterType(IL_FLOAT);
|
---|
| 80 |
|
---|
| 81 | const int depth = 1;
|
---|
| 82 | const int bpp = 1;
|
---|
| 83 |
|
---|
| 84 | if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
|
---|
| 85 | {
|
---|
| 86 | cerr << "IL error " << ilGetError() << endl;
|
---|
| 87 | stopil();
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | if (!ilSaveImage(filename))
|
---|
| 92 | {
|
---|
| 93 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | stopil();
|
---|
| 97 |
|
---|
| 98 | cout << "exported depth buffer" << endl;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
[2887] | 102 | SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox,
|
---|
[3101] | 103 | RenderTraverser *renderer, RenderState *state):
|
---|
| 104 | mSceneBox(sceneBox), mDepth(NULL), mRenderState(state)
|
---|
[2764] | 105 | {
|
---|
[2778] | 106 | Prepare(renderer);
|
---|
[2764] | 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
| 111 | {
|
---|
[2888] | 112 | const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
|
---|
| 113 | const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
|
---|
[2764] | 114 |
|
---|
[3101] | 115 | const float d = mDepth[px + py * texHeight];
|
---|
[2764] | 116 |
|
---|
[2808] | 117 | static float depth = d;
|
---|
[2800] | 118 |
|
---|
[3101] | 119 | if (d > .0f)
|
---|
[2764] | 120 | {
|
---|
[2808] | 121 | // temporal smoothing of depth values
|
---|
[2848] | 122 | const float x = .5f;
|
---|
[2808] | 123 | depth = d * x + depth * (1.0f - x);
|
---|
[2764] | 124 |
|
---|
[2808] | 125 | const float offs = mSceneBox.Size(2) * 5e-2f;
|
---|
| 126 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
|
---|
| 127 |
|
---|
[2764] | 128 | return true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[2778] | 134 |
|
---|
| 135 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
[2764] | 136 | {
|
---|
| 137 | cout << "Preparing scene queries" << endl;
|
---|
| 138 |
|
---|
[3101] | 139 | const float xlen = mSceneBox.Size().x * .5f;
|
---|
| 140 | const float ylen = mSceneBox.Size().y * .5f;
|
---|
[2796] | 141 |
|
---|
[3062] | 142 | PerspectiveCamera *orthoCam = new PerspectiveCamera(xlen / ylen);
|
---|
[3061] | 143 | //orthoCam->SetOrtho(true);
|
---|
[2764] | 144 |
|
---|
| 145 | orthoCam->SetNear(0.0f);
|
---|
[2897] | 146 | orthoCam->Yaw(.5 * M_PI);
|
---|
[2888] | 147 | orthoCam->SetDirection(Vector3(0, 0, -1));
|
---|
[2764] | 148 |
|
---|
[2796] | 149 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
[2764] | 150 | orthoCam->SetPosition(pos);
|
---|
| 151 |
|
---|
[2877] | 152 | FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
|
---|
[3001] | 153 | fbo->AddColorBuffer(ColorBufferObject::RGBA_UBYTE,
|
---|
[2965] | 154 | ColorBufferObject::WRAP_CLAMP_TO_EDGE,
|
---|
| 155 | ColorBufferObject::FILTER_NEAREST,
|
---|
| 156 | ColorBufferObject::FILTER_NEAREST);
|
---|
[2809] | 157 |
|
---|
| 158 |
|
---|
[2877] | 159 | fbo->Bind();
|
---|
[2809] | 160 |
|
---|
[2877] | 161 | glDrawBuffers(1, mrt);
|
---|
[2887] | 162 |
|
---|
| 163 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2877] | 164 | glViewport(0, 0, texWidth, texHeight);
|
---|
[2764] | 165 |
|
---|
[2877] | 166 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[2764] | 167 |
|
---|
[3101] | 168 | // hack: disable cull face because of alpha textured balconies
|
---|
[2897] | 169 | glDisable(GL_CULL_FACE);
|
---|
[3101] | 170 | mRenderState->LockCullFaceEnabled(true);
|
---|
[2808] | 171 |
|
---|
[3101] | 172 | //glDisable(GL_CULL_FACE);
|
---|
| 173 | //glEnable(GL_DEPTH_TEST);
|
---|
| 174 |
|
---|
[2897] | 175 | glMatrixMode(GL_MODELVIEW);
|
---|
| 176 | glPushMatrix();
|
---|
| 177 | glLoadIdentity();
|
---|
| 178 |
|
---|
[2877] | 179 | glMatrixMode(GL_PROJECTION);
|
---|
| 180 | glPushMatrix();
|
---|
[2897] | 181 | glLoadIdentity();
|
---|
[2808] | 182 |
|
---|
[3089] | 183 | glOrtho(xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
|
---|
[3101] | 184 | //glOrtho(xlen, .0f, ylen, .0f, .0f, mSceneBox.Size().z);
|
---|
[2764] | 185 |
|
---|
[2877] | 186 | glMatrixMode(GL_MODELVIEW);
|
---|
[2764] | 187 |
|
---|
[2877] | 188 | orthoCam->SetupCameraView();
|
---|
[2778] | 189 |
|
---|
[2877] | 190 | mDepth = new float[texHeight * texWidth];
|
---|
[2809] | 191 |
|
---|
[2897] | 192 | Camera *oldCam = renderer->GetCamera();
|
---|
| 193 | renderer->SetCamera(orthoCam);
|
---|
| 194 |
|
---|
[2953] | 195 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 196 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 197 |
|
---|
[3101] | 198 | // forward rendering
|
---|
| 199 | mRenderState->SetRenderTechnique(0);
|
---|
| 200 |
|
---|
[2877] | 201 | renderer->RenderScene();
|
---|
[2897] | 202 |
|
---|
[2953] | 203 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 204 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 205 |
|
---|
[2897] | 206 | renderer->SetCamera(oldCam);
|
---|
[2877] | 207 |
|
---|
[3101] | 208 | mRenderState->LockCullFaceEnabled(false);
|
---|
| 209 | //glEnable(GL_CULL_FACE);
|
---|
[2897] | 210 |
|
---|
| 211 | glMatrixMode(GL_MODELVIEW);
|
---|
[2877] | 212 | glPopMatrix();
|
---|
[2897] | 213 |
|
---|
[2877] | 214 | glMatrixMode(GL_PROJECTION);
|
---|
| 215 | glPopMatrix();
|
---|
[2834] | 216 |
|
---|
[2877] | 217 | glPopAttrib();
|
---|
| 218 |
|
---|
| 219 | FrameBufferObject::Release();
|
---|
[2834] | 220 |
|
---|
[2877] | 221 | GrabDepthBuffer(mDepth, fbo->GetDepthTex());
|
---|
[3101] | 222 | ExportDepthBuffer(mDepth); PrintGLerror("grab depth");
|
---|
[2796] | 223 |
|
---|
[2877] | 224 | DEL_PTR(fbo);
|
---|
[2796] | 225 | DEL_PTR(orthoCam);
|
---|
[2764] | 226 | }
|
---|
| 227 |
|
---|
[2808] | 228 |
|
---|
| 229 |
|
---|
| 230 |
|
---|
[2764] | 231 | } |
---|