00001 #include "OcclusionQuery.h"
00002 #include <iostream>
00003
00004 #include "../../core/src/flexilog.h"
00005
00006 #ifdef _WIN32
00007 #define GLEW_STATIC
00008 #include <GL/glew.h>
00009 #else
00010 #define GLEW_STATIC
00011 #include <GL/glew.h>
00012 #include <GL/gl.h>
00013
00014
00015 #endif
00016
00017
00018 using namespace std;
00019
00020 bool OcclusionQuery::sUseArbQueries = true;
00021
00022 OcclusionQuery::OcclusionQuery()
00023 {
00024 GLuint id;
00025
00026 if (sUseArbQueries) {
00027 glGenQueriesARB(1, &id);
00028 } else {
00029 glGenOcclusionQueriesNV(1, &id);
00030 }
00031 }
00032
00033 OcclusionQuery::OcclusionQuery(const unsigned int idx):
00034 mId(idx)
00035 {}
00036
00037
00038 OcclusionQuery::~OcclusionQuery()
00039 {
00040 if (sUseArbQueries) {
00041 glDeleteQueriesARB(1, &mId);
00042 } else {
00043 glDeleteOcclusionQueriesNV(1, &mId);
00044 }
00045 }
00046
00047
00048 void
00049 OcclusionQuery::BeginQuery()
00050 {
00051 if (sUseArbQueries) {
00052 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mId);
00053 } else {
00054 glBeginOcclusionQueryNV(mId);
00055 }
00056 }
00057
00058 void
00059 OcclusionQuery::EndQuery()
00060 {
00061 if (sUseArbQueries) {
00062 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
00063 } else {
00064 glEndOcclusionQueryNV();
00065 }
00066 }
00067
00068
00069 unsigned int
00070 OcclusionQuery::GetQueryId() const
00071 {
00072 return mId;
00073 }
00074
00075 bool
00076 OcclusionQuery::ResultAvailable() const
00077 {
00078 GLuint available;
00079
00080 if (sUseArbQueries) {
00081 glGetQueryObjectuivARB(mId,
00082 GL_QUERY_RESULT_AVAILABLE_ARB,
00083 &available);
00084 return available == GL_TRUE;
00085 } else {
00086 glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
00087 return available == GL_TRUE;
00088 }
00089 }
00090
00091
00092 unsigned int
00093 OcclusionQuery::GetQueryResult() const
00094 {
00095 GLuint sampleCount;
00096
00097 if (sUseArbQueries) {
00098 glGetQueryObjectuivARB(mId, GL_QUERY_RESULT_ARB, &sampleCount);
00099 return sampleCount;
00100 } else {
00101 glGetOcclusionQueryuivNV(mId, GL_PIXEL_COUNT_NV, &sampleCount);
00102 return sampleCount;
00103 }
00104 }
00105
00106
00107 void
00108 OcclusionQuery::GenQueries(std::vector<OcclusionQuery * > &queries, const int numQueries)
00109 {
00110
00111 if ((int)queries.size() < numQueries) {
00112 const int n = numQueries - (int)queries.size();
00113 GLuint *newQueries = new GLuint[n];
00114
00115 LOG(wxString::Format(wxT("Generating %i occlusion queries."), n));
00116
00117 if (sUseArbQueries) {
00118 glGenQueriesARB(n, newQueries);
00119 } else {
00120 glGenOcclusionQueriesNV(n, newQueries);
00121 }
00122
00123 for (int i = 0; i < n; ++ i) {
00124 queries.push_back(new OcclusionQuery(newQueries[i]));
00125 }
00126
00127 delete newQueries;
00128 }
00129 }