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

Revision 2809, 3.8 KB checked in by mattausch, 16 years ago (diff)

working on ssao deferred shading approach (debug version!!)

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"
[2800]7#include <IL/il.h>
8#include <assert.h>
[2808]9#include "RenderTexture.h"
[2764]10
11
[2808]12using namespace std;
13
[2809]14static int texWidth = 2048;
15static int texHeight = 2048;
[2808]16
17
18
19static 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
[2800]32void startil()
33{
34        ilInit();
35        assert(ilGetError() == IL_NO_ERROR);
36}
[2764]37
38
[2800]39void stopil()
40{
41        ilShutDown();
42        assert(ilGetError() == IL_NO_ERROR);
43}
44
45
[2776]46namespace CHCDemoEngine
[2764]47{
48
49
[2808]50void GrabDepthBuffer(float *data, RenderTexture *rt)
51{
52        rt->BindDepth();
53        rt->EnableTextureTarget();
[2764]54
[2808]55        const int texFormat = GL_DEPTH_COMPONENT;
56        glGetTexImage(rt->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
[2764]57
[2808]58        rt->DisableTextureTarget();
59}
[2764]60
61
[2808]62void ExportDepthBuffer(float *data)
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
[2778]90SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer):
[2796]91mSceneBox(sceneBox), mDepth(NULL)
[2764]92{
[2778]93        Prepare(renderer);
[2764]94}
95
96
97bool SceneQuery::CalcIntersection(Vector3 &pt)
98{
[2808]99        const int px = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0);
100        const int py = (pt.y - mSceneBox.Min(1)) * (texHeight - 1) / mSceneBox.Size(1);
[2764]101
[2808]102        float d = mDepth[px + py * texHeight];
[2764]103
[2808]104        static float depth = d;
[2800]105
106        if (d > 0)
[2764]107        {
[2808]108                // temporal smoothing of depth values
109                const float x = 0.5f;
110                depth = d * x + depth * (1.0f - x);
[2764]111
[2808]112                const float offs = mSceneBox.Size(2) * 5e-2f;
113                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
114
[2764]115                return true;
116        }
117
118        return false;
119}
120
[2778]121
122void SceneQuery::Prepare(RenderTraverser *renderer)
[2764]123{
124        cout << "Preparing scene queries" << endl;
125
[2796]126        const float xlen = mSceneBox.Size().x * 0.5f;
127        const float ylen = mSceneBox.Size().y * 0.5f;
128       
129        Camera *orthoCam = new Camera(xlen, ylen);
130        orthoCam->SetOrtho(true);
[2764]131
132        orthoCam->SetNear(0.0f);
[2796]133        orthoCam->Yaw(M_PI * 0.5f);
[2764]134
[2796]135        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
[2764]136        orthoCam->SetPosition(pos);
137
[2809]138        RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true);
139
140#ifdef ATI
141        depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
142#else
143        depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
144#endif
145
146        PrintGLerror("Init");
147
[2808]148        depthTexture->BeginCapture();
[2809]149        {
150                glViewport(0, 0, texWidth, texHeight);
[2764]151
[2809]152                glClearColor(1, 1, 1, 1);
153                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[2764]154
[2809]155                glFrontFace(GL_CCW);
156                glCullFace(GL_BACK);
[2808]157
[2809]158                glEnable(GL_CULL_FACE);
[2808]159
[2809]160                glShadeModel(GL_FLAT);
161                glEnable(GL_DEPTH_TEST);
[2808]162
[2809]163                glMatrixMode(GL_PROJECTION);
164                glLoadIdentity();
[2808]165
[2809]166                glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z);
[2764]167
[2809]168                glMatrixMode(GL_MODELVIEW);
[2764]169
[2809]170                orthoCam->SetupCameraView();
[2778]171
[2809]172                mDepth = new float[texHeight * texWidth];
173
174                //renderer->SetCamera(orthoCam);
175                renderer->RenderScene();
176        }
[2808]177        depthTexture->EndCapture();
[2796]178
[2808]179        GrabDepthBuffer(mDepth, depthTexture);
180        //ExportDepthBuffer(mDepth);
181        //PrintGLerror("grab");
[2800]182
[2808]183        DEL_PTR(depthTexture);
[2796]184        DEL_PTR(orthoCam);
[2764]185}
186
[2808]187
188
189
[2764]190}
Note: See TracBrowser for help on using the repository browser.