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