source: GTP/trunk/Lib/Vis/Preprocessing/src/OcclusionQuery.cpp @ 2612

Revision 2612, 3.0 KB checked in by bittner, 16 years ago (diff)

occlusion query bugfix

Line 
1#include "OcclusionQuery.h"
2#include <iostream>
3
4
5#ifdef _WIN32
6#include <glInterface.h>
7#else
8#include <GL/gl.h>
9#include <GL/glu.h>
10//#include <GL/glxext.h>
11#include <SDL/SDL_opengl.h>
12#endif
13
14#define _ARBGL
15
16using namespace std;
17
18namespace GtpVisibilityPreprocessor {
19
20bool OcclusionQuery::sUseArbQueries = true;
21
22
23OcclusionQuery::OcclusionQuery()
24{
25        GLuint id;
26
27        if (sUseArbQueries)
28        {
29#ifdef _ARBGL
30          // VH
31          glGenQueriesARB(1, &id);
32#endif
33        }
34        else
35        {               
36#ifdef _ARBGL
37          // VH
38                glGenOcclusionQueriesNV(1, &id);
39#endif
40        }
41}
42
43OcclusionQuery::OcclusionQuery(const unsigned int idx):
44mId(idx)
45{}
46
47
48OcclusionQuery::~OcclusionQuery()
49{
50        if (sUseArbQueries)
51        {
52#ifdef _ARBGL
53          // VH
54                glDeleteQueriesARB(1, &mId);
55#endif
56        }
57        else
58        {
59#ifdef _ARBGL
60          // VH
61                glDeleteOcclusionQueriesNV(1, &mId);
62#endif
63        }
64       
65}
66//-----------------------------------------------------------------------
67void OcclusionQuery::BeginQuery()
68{
69        if (sUseArbQueries)
70        {
71#ifdef _ARBGL
72          // VH
73                glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mId);
74#endif
75        }
76        else
77        {
78#ifdef _ARBGL
79          // VH
80                glBeginOcclusionQueryNV(mId);
81#endif
82        }
83}
84//-----------------------------------------------------------------------
85void OcclusionQuery::EndQuery()
86{
87        if (sUseArbQueries)
88        {
89#ifdef _ARBGL
90          // VH
91                glEndQueryARB(GL_SAMPLES_PASSED_ARB);
92#endif
93        }
94        else
95        {
96#ifdef _ARBGL
97          // VH
98                glEndOcclusionQueryNV();       
99#endif
100        }
101}
102
103
104unsigned int OcclusionQuery::GetQueryId() const
105{
106        return mId;
107}
108
109bool OcclusionQuery::ResultAvailable() const
110{
111        GLuint available;
112
113        if (sUseArbQueries)
114        {
115#ifdef _ARBGL
116          // VH
117                //GLint available;
118                glGetQueryObjectuivARB(mId,
119                                                           GL_QUERY_RESULT_AVAILABLE_ARB,
120                                       &available);
121#endif         
122                return available == GL_TRUE;
123        }
124        else
125        {
126#ifdef _ARBGL
127          // VH
128          glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
129#endif
130                return available == GL_TRUE;
131        }
132
133}
134
135
136unsigned int OcclusionQuery::GetQueryResult() const
137{
138        GLuint sampleCount;
139
140        if (sUseArbQueries)
141        {
142#ifdef _ARBGL
143          // VH
144                glGetQueryObjectuivARB(mId, GL_QUERY_RESULT_ARB, &sampleCount);
145#endif
146                return sampleCount;
147        }
148        else
149        {       
150#ifdef _ARBGL
151          // VH
152                glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_NV, &sampleCount);
153#endif         
154                return sampleCount;
155        }       
156}
157
158
159void OcclusionQuery::GenQueries(std::vector<OcclusionQuery * > &queries, const int numQueries)
160{
161        if ((int)queries.size() < numQueries)
162        {
163                const int n = numQueries - (int)queries.size();
164                unsigned int *newQueries = new unsigned int[n];
165
166
167                if (sUseArbQueries)
168                {
169#ifdef _ARBGL
170          // VH
171                        glGenQueriesARB(n, (unsigned int *)newQueries);
172#endif
173                }
174                else
175                {
176#ifdef _ARBGL
177          // VH           
178                  glGenOcclusionQueriesNV(n, (unsigned int *)newQueries);
179#endif           
180                }
181
182                for (int i = 0; i < n; ++ i)
183                {
184                        queries.push_back(new OcclusionQuery(newQueries[i]));
185                        //cout << "q: " << i << " id: " << newQueries[i] << endl;
186                }
187
188                delete [] newQueries;
189        }
190}
191} // namespace
Note: See TracBrowser for help on using the repository browser.