source: GTP/trunk/App/Demos/Vis/CHC_revisited/src/OcclusionQuery.cpp @ 2782

Revision 2782, 1.9 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include <iostream>
2#include "OcclusionQuery.h"
3#include "glInterface.h"
4
5using namespace std;
6
7
8namespace CHCDemoEngine
9{
10
11OcclusionQuery::OcclusionQuery()
12{
13        glGenQueriesARB(1, &mQueryId);
14
15        // reverse for multiequeries with 32 nodes
16        mNodes.reserve(32);
17}
18
19
20OcclusionQuery::~OcclusionQuery()
21{
22        glDeleteQueriesARB(1, &mQueryId);
23}
24
25
26void OcclusionQuery::BeginQuery() const
27{
28        glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mQueryId);
29}
30
31
32void OcclusionQuery::EndQuery() const
33{
34        glEndQueryARB(GL_SAMPLES_PASSED_ARB);
35}
36
37
38unsigned int OcclusionQuery::GetQueryId() const
39{
40        return mQueryId;
41}
42
43
44bool OcclusionQuery::ResultAvailable() const
45{
46        GLuint available = GL_FALSE;
47
48        glGetQueryObjectuivARB(mQueryId, GL_QUERY_RESULT_AVAILABLE_ARB, &available);
49
50        return available == GL_TRUE;
51}
52
53
54unsigned int OcclusionQuery::GetQueryResult() const
55{
56        GLuint sampleCount = 1;
57
58        glGetQueryObjectuivARB(mQueryId, GL_QUERY_RESULT_ARB, &sampleCount);
59       
60        return sampleCount;
61}
62
63
64
65QueryHandler::QueryHandler(): mCurrentQueryIdx(0)
66{
67        Allocate(1000);
68}
69
70
71OcclusionQuery *QueryHandler::RequestQuery()
72{
73        OcclusionQuery *query;
74
75        if (mCurrentQueryIdx == mOcclusionQueries.size())
76        {
77                query = new OcclusionQuery();
78                mOcclusionQueries.push_back(query);
79        }
80        else
81        {
82                query = mOcclusionQueries[mCurrentQueryIdx];
83                query->Reset();
84        }
85
86        ++ mCurrentQueryIdx;
87
88        return query;
89}
90
91
92void QueryHandler::ResetQueries()
93{
94        mCurrentQueryIdx = 0;
95}
96
97
98void QueryHandler::DestroyQueries()
99{
100        CLEAR_CONTAINER(mOcclusionQueries);
101        mCurrentQueryIdx = 0;
102        mOcclusionQueries.clear();
103}
104
105
106void QueryHandler::Allocate(int n)
107{
108        unsigned int *ids = new unsigned int[n];
109        glGenQueriesARB(n, ids);
110
111        for (int i = 0; i < n; ++ i)
112        {
113                OcclusionQuery *q = new OcclusionQuery(ids[i]);
114                mOcclusionQueries.push_back(q);
115        }
116
117        mCurrentQueryIdx = n;
118        delete [] ids;
119}
120
121
122} // namespace
Note: See TracBrowser for help on using the repository browser.