source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp @ 2887

Revision 2887, 3.9 KB checked in by mattausch, 16 years ago (diff)

made changes to view transformation but still not working

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