[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"
|
---|
[2800] | 7 | #include <IL/il.h>
|
---|
| 8 | #include <assert.h>
|
---|
[2764] | 9 |
|
---|
| 10 |
|
---|
[2800] | 11 | void startil()
|
---|
| 12 | {
|
---|
| 13 | ilInit();
|
---|
| 14 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 15 | }
|
---|
[2764] | 16 |
|
---|
| 17 |
|
---|
[2800] | 18 | void stopil()
|
---|
| 19 | {
|
---|
| 20 | ilShutDown();
|
---|
| 21 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 |
|
---|
[2776] | 25 | namespace CHCDemoEngine
|
---|
[2764] | 26 | {
|
---|
| 27 |
|
---|
| 28 | using namespace std;
|
---|
| 29 |
|
---|
| 30 |
|
---|
[2800] | 31 | //const static int viewport[4] = {0, 0, 512, 512};
|
---|
| 32 | const static int viewport[4] = {0, 0, 1024, 768};
|
---|
| 33 | //const static int viewport[4] = {0, 0, 2048, 2048};
|
---|
[2764] | 34 |
|
---|
| 35 |
|
---|
| 36 |
|
---|
[2778] | 37 | SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer):
|
---|
[2796] | 38 | mSceneBox(sceneBox), mDepth(NULL)
|
---|
[2764] | 39 | {
|
---|
[2778] | 40 | Prepare(renderer);
|
---|
[2764] | 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
| 45 | {
|
---|
| 46 | int px = (pt.x - mSceneBox.Min(0)) * (viewport[2] - 1) / mSceneBox.Size(0);
|
---|
[2796] | 47 | int py = (pt.y - mSceneBox.Min(1)) * (viewport[3] - 1) / mSceneBox.Size(1);
|
---|
[2764] | 48 |
|
---|
[2800] | 49 | unsigned char d = mDepth[px + py * viewport[2]];
|
---|
[2764] | 50 |
|
---|
[2800] | 51 | const float offs = mSceneBox.Size(2) * 1e-1f;
|
---|
| 52 |
|
---|
| 53 | static float depth = (float)d;
|
---|
| 54 |
|
---|
| 55 | if (d > 0)
|
---|
[2764] | 56 | {
|
---|
[2800] | 57 | const float x = 0.1f;
|
---|
| 58 | depth = depth * x + d * (1.0f - x);
|
---|
| 59 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth / 255.0f + offs;
|
---|
| 60 | //cout << "new depth " << pt.z << " (" << d << ")" << endl;
|
---|
[2764] | 61 |
|
---|
| 62 | return true;
|
---|
| 63 | }
|
---|
[2800] | 64 | //cout << "invalid depth: " << d << endl;
|
---|
[2764] | 65 |
|
---|
| 66 | return false;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[2778] | 69 |
|
---|
| 70 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
[2764] | 71 | {
|
---|
| 72 | cout << "Preparing scene queries" << endl;
|
---|
| 73 |
|
---|
[2796] | 74 | const float xlen = mSceneBox.Size().x * 0.5f;
|
---|
| 75 | const float ylen = mSceneBox.Size().y * 0.5f;
|
---|
| 76 |
|
---|
| 77 | Camera *orthoCam = new Camera(xlen, ylen);
|
---|
| 78 | orthoCam->SetOrtho(true);
|
---|
[2764] | 79 |
|
---|
| 80 | orthoCam->SetNear(0.0f);
|
---|
[2796] | 81 | orthoCam->Yaw(M_PI * 0.5f);
|
---|
[2764] | 82 |
|
---|
[2796] | 83 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
[2764] | 84 | orthoCam->SetPosition(pos);
|
---|
| 85 |
|
---|
[2797] | 86 | //glPixelStorei(GL_PACK_ROW_LENGTH, viewport[2]);
|
---|
[2800] | 87 | glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
---|
| 88 | glReadBuffer(GL_BACK);
|
---|
[2764] | 89 |
|
---|
[2796] | 90 | // hack: should create offscreen buffer for this
|
---|
| 91 | glViewport(0, 0, viewport[2], viewport[3]);
|
---|
[2764] | 92 |
|
---|
[2796] | 93 | glMatrixMode(GL_PROJECTION);
|
---|
| 94 | glLoadIdentity();
|
---|
[2764] | 95 |
|
---|
[2796] | 96 | glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z);
|
---|
| 97 |
|
---|
| 98 | glMatrixMode(GL_MODELVIEW);
|
---|
| 99 |
|
---|
| 100 | orthoCam->SetupCameraView();
|
---|
[2764] | 101 |
|
---|
[2796] | 102 | glClear(GL_DEPTH_BUFFER_BIT);
|
---|
[2764] | 103 |
|
---|
[2800] | 104 | mDepth = new unsigned char[viewport[2] * viewport[3]];
|
---|
| 105 | //mDepth = new float[viewport[2] * viewport[3]];
|
---|
[2796] | 106 |
|
---|
| 107 | //renderer->SetCamera(orthoCam);
|
---|
| 108 |
|
---|
[2778] | 109 | renderer->RenderScene();
|
---|
| 110 |
|
---|
[2800] | 111 | glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, mDepth);
|
---|
| 112 | //glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, mDepth);
|
---|
| 113 | /*
|
---|
| 114 | startil();
|
---|
[2796] | 115 |
|
---|
[2800] | 116 | if (!ilTexImage(viewport[2], viewport[3], 1, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, mDepth))
|
---|
| 117 | {
|
---|
| 118 | cerr << "IL error " << ilGetError() << endl;
|
---|
| 119 | stopil();
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | ILstring writename = ILstring("out.tga");
|
---|
| 124 |
|
---|
| 125 | ilSetInteger(IL_TGA_RLE, 1);
|
---|
| 126 | if (!ilSaveImage(writename))
|
---|
| 127 | {
|
---|
| 128 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | stopil();
|
---|
| 132 | */
|
---|
[2796] | 133 | DEL_PTR(orthoCam);
|
---|
[2764] | 134 | }
|
---|
| 135 |
|
---|
| 136 | } |
---|