1 | #include "SceneQuery.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "Vector3.h"
|
---|
4 | #include "Camera.h"
|
---|
5 | #include "SceneQuery.h"
|
---|
6 | #include "RenderTraverser.h"
|
---|
7 | #include <IL/il.h>
|
---|
8 | #include <assert.h>
|
---|
9 | #include "RenderTexture.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | int texWidth = 2048;
|
---|
15 | int texHeight = 2048;
|
---|
16 |
|
---|
17 | /*static void cgErrorCallback()
|
---|
18 | {
|
---|
19 | CGerror lastError = cgGetError();
|
---|
20 |
|
---|
21 | if(lastError)
|
---|
22 | {
|
---|
23 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
24 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
25 | printf("Cg error, exiting...\n");
|
---|
26 |
|
---|
27 | exit(0);
|
---|
28 | }
|
---|
29 | }*/
|
---|
30 |
|
---|
31 |
|
---|
32 | static void PrintGLerror(char *msg)
|
---|
33 | {
|
---|
34 | GLenum errCode;
|
---|
35 | const GLubyte *errStr;
|
---|
36 |
|
---|
37 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
38 | {
|
---|
39 | errStr = gluErrorString(errCode);
|
---|
40 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | void startil()
|
---|
46 | {
|
---|
47 | ilInit();
|
---|
48 | assert(ilGetError() == IL_NO_ERROR);
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | void stopil()
|
---|
53 | {
|
---|
54 | ilShutDown();
|
---|
55 | assert(ilGetError() == IL_NO_ERROR);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | namespace CHCDemoEngine
|
---|
60 | {
|
---|
61 |
|
---|
62 |
|
---|
63 | void GrabDepthBuffer(float *data, RenderTexture *rt)
|
---|
64 | {
|
---|
65 | rt->BindDepth();
|
---|
66 | rt->EnableTextureTarget();
|
---|
67 |
|
---|
68 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
69 | glGetTexImage(rt->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
70 |
|
---|
71 | rt->DisableTextureTarget();
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void ExportDepthBuffer(float *data)
|
---|
76 | {
|
---|
77 | startil();
|
---|
78 |
|
---|
79 | ILstring filename = ILstring("depth.tga");
|
---|
80 | ilRegisterType(IL_FLOAT);
|
---|
81 |
|
---|
82 | const int depth = 1;
|
---|
83 | const int bpp = 1;
|
---|
84 |
|
---|
85 | if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
|
---|
86 | {
|
---|
87 | cerr << "IL error " << ilGetError() << endl;
|
---|
88 | stopil();
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (!ilSaveImage(filename))
|
---|
93 | {
|
---|
94 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
95 | }
|
---|
96 |
|
---|
97 | stopil();
|
---|
98 |
|
---|
99 | cout << "exported depth buffer" << endl;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer):
|
---|
104 | mSceneBox(sceneBox), mDepth(NULL)
|
---|
105 | {
|
---|
106 | Prepare(renderer);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | bool SceneQuery::CalcIntersection(Vector3 &pt)
|
---|
111 | {
|
---|
112 | const int px = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0);
|
---|
113 | const int py = (pt.y - mSceneBox.Min(1)) * (texHeight - 1) / mSceneBox.Size(1);
|
---|
114 |
|
---|
115 | float d = mDepth[px + py * texHeight];
|
---|
116 |
|
---|
117 | static float depth = d;
|
---|
118 |
|
---|
119 | if (d > 0)
|
---|
120 | {
|
---|
121 | // temporal smoothing of depth values
|
---|
122 | const float x = 0.5f;
|
---|
123 | depth = d * x + depth * (1.0f - x);
|
---|
124 |
|
---|
125 | const float offs = mSceneBox.Size(2) * 5e-2f;
|
---|
126 | pt.z = mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
|
---|
127 |
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | void SceneQuery::Prepare(RenderTraverser *renderer)
|
---|
136 | {
|
---|
137 | cout << "Preparing scene queries" << endl;
|
---|
138 |
|
---|
139 | RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true);
|
---|
140 |
|
---|
141 | #ifdef ATI
|
---|
142 | depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
143 | #else
|
---|
144 | depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | PrintGLerror("Init");
|
---|
148 |
|
---|
149 |
|
---|
150 | const float xlen = mSceneBox.Size().x * 0.5f;
|
---|
151 | const float ylen = mSceneBox.Size().y * 0.5f;
|
---|
152 |
|
---|
153 | Camera *orthoCam = new Camera(xlen, ylen);
|
---|
154 | orthoCam->SetOrtho(true);
|
---|
155 |
|
---|
156 | orthoCam->SetNear(0.0f);
|
---|
157 | orthoCam->Yaw(M_PI * 0.5f);
|
---|
158 |
|
---|
159 | Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
|
---|
160 | orthoCam->SetPosition(pos);
|
---|
161 |
|
---|
162 | depthTexture->BeginCapture();
|
---|
163 |
|
---|
164 | glViewport(0, 0, texWidth, texHeight);
|
---|
165 |
|
---|
166 | glClearColor(1, 1, 1, 1);
|
---|
167 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
168 |
|
---|
169 | glFrontFace(GL_CCW);
|
---|
170 | glCullFace(GL_BACK);
|
---|
171 |
|
---|
172 | glDisable(GL_CULL_FACE);
|
---|
173 | //glEnable(GL_CULL_FACE);
|
---|
174 |
|
---|
175 | glShadeModel(GL_FLAT);
|
---|
176 | glEnable(GL_DEPTH_TEST);
|
---|
177 |
|
---|
178 | glMatrixMode(GL_PROJECTION);
|
---|
179 | glLoadIdentity();
|
---|
180 |
|
---|
181 | glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z);
|
---|
182 |
|
---|
183 | glMatrixMode(GL_MODELVIEW);
|
---|
184 |
|
---|
185 | orthoCam->SetupCameraView();
|
---|
186 |
|
---|
187 | mDepth = new float[texHeight * texWidth];
|
---|
188 |
|
---|
189 | //renderer->SetCamera(orthoCam);
|
---|
190 | renderer->RenderScene();
|
---|
191 |
|
---|
192 | depthTexture->EndCapture();
|
---|
193 |
|
---|
194 | GrabDepthBuffer(mDepth, depthTexture);
|
---|
195 | //ExportDepthBuffer(mDepth);
|
---|
196 | //PrintGLerror("grab");
|
---|
197 |
|
---|
198 | DEL_PTR(depthTexture);
|
---|
199 | DEL_PTR(orthoCam);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 | } |
---|