[2753] | 1 | #include <iostream>
|
---|
| 2 | #include "OcclusionQuery.h"
|
---|
| 3 | #include "glInterface.h"
|
---|
| 4 |
|
---|
| 5 | using namespace std;
|
---|
| 6 |
|
---|
| 7 |
|
---|
[2776] | 8 | namespace CHCDemoEngine
|
---|
[2753] | 9 | {
|
---|
| 10 |
|
---|
| 11 | OcclusionQuery::OcclusionQuery()
|
---|
| 12 | {
|
---|
| 13 | glGenQueriesARB(1, &mQueryId);
|
---|
| 14 |
|
---|
[2786] | 15 | // reverse for multiqueries with 20 nodes
|
---|
[2753] | 16 | mNodes.reserve(32);
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | OcclusionQuery::~OcclusionQuery()
|
---|
| 21 | {
|
---|
| 22 | glDeleteQueriesARB(1, &mQueryId);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 |
|
---|
[2773] | 26 | void OcclusionQuery::BeginQuery() const
|
---|
[2753] | 27 | {
|
---|
| 28 | glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mQueryId);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
[2773] | 32 | void OcclusionQuery::EndQuery() const
|
---|
[2753] | 33 | {
|
---|
| 34 | glEndQueryARB(GL_SAMPLES_PASSED_ARB);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | unsigned int OcclusionQuery::GetQueryId() const
|
---|
| 39 | {
|
---|
| 40 | return mQueryId;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | bool 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 |
|
---|
| 54 | unsigned int OcclusionQuery::GetQueryResult() const
|
---|
| 55 | {
|
---|
| 56 | GLuint sampleCount = 1;
|
---|
| 57 |
|
---|
| 58 | glGetQueryObjectuivARB(mQueryId, GL_QUERY_RESULT_ARB, &sampleCount);
|
---|
| 59 |
|
---|
| 60 | return sampleCount;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[2763] | 63 |
|
---|
| 64 |
|
---|
| 65 | QueryHandler::QueryHandler(): mCurrentQueryIdx(0)
|
---|
[2781] | 66 | {
|
---|
[2800] | 67 | Allocate(500);
|
---|
[2781] | 68 | }
|
---|
[2763] | 69 |
|
---|
| 70 |
|
---|
| 71 | OcclusionQuery *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
|
---|
[2764] | 81 | {
|
---|
[2763] | 82 | query = mOcclusionQueries[mCurrentQueryIdx];
|
---|
[2773] | 83 | query->Reset();
|
---|
[2764] | 84 | }
|
---|
[2763] | 85 |
|
---|
[2764] | 86 | ++ mCurrentQueryIdx;
|
---|
| 87 |
|
---|
[2763] | 88 | return query;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | void QueryHandler::ResetQueries()
|
---|
| 93 | {
|
---|
| 94 | mCurrentQueryIdx = 0;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | void QueryHandler::DestroyQueries()
|
---|
| 99 | {
|
---|
| 100 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
[2800] | 101 |
|
---|
[2763] | 102 | mCurrentQueryIdx = 0;
|
---|
| 103 | mOcclusionQueries.clear();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
[2781] | 107 | void QueryHandler::Allocate(int n)
|
---|
| 108 | {
|
---|
| 109 | unsigned int *ids = new unsigned int[n];
|
---|
| 110 | glGenQueriesARB(n, ids);
|
---|
[2763] | 111 |
|
---|
[2781] | 112 | for (int i = 0; i < n; ++ i)
|
---|
| 113 | {
|
---|
| 114 | OcclusionQuery *q = new OcclusionQuery(ids[i]);
|
---|
| 115 | mOcclusionQueries.push_back(q);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | mCurrentQueryIdx = n;
|
---|
| 119 | delete [] ids;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
[2753] | 123 | } // namespace
|
---|