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