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