Hardware occlusion query type

BlitzMax Forums/OpenGL Module/Hardware occlusion query type

JoshK(Posted 2008) [#1]
This code will handle hardware occlusion queries.

Usage:

If TQuery.supported()
query:TQuery=New TQuery
query.beginquery()
draw stuff
query.endquery()

Wait a few frames, so a GPU sync isn't forced (use the time value to see how long ago the query was performed)...

if query.available()
result=query.getqueryresult()
endif

It's kind of useless, and I have not gained much performance from this approach, and in some cases it makes things slower, but it is good for very expensive objects, like animated meshes and lights.

Strict

Import pub.opengl
Import leadwerks.glewex

Type TQuery
	Field id
	Field _available=0
	Field result=-1
	Field pixels=10
	
	Method New()
		glGenQueriesARB 1,Varptr id
	EndMethod
	
	Method Delete()
		glDeleteQueriesARB 1,Varptr id
	EndMethod
	
	Method BeginQuery()
		glColorMask 0,0,0,0
		gldepthmask 0
		gldisable GL_CULL_FACE
		glBeginQueryARB GL_SAMPLES_PASSED_ARB,id
	EndMethod
	
	Method EndQuery()
		glEndQueryARB GL_SAMPLES_PASSED_ARB
		glenable GL_CULL_FACE
		glColorMask 1,1,1,1
		gldepthmask 1
	EndMethod
	
	Method Available:Int()
		If _available Return 1
		glGetQueryObjectivARB(id,GL_QUERY_RESULT_AVAILABLE_ARB,Varptr _available)
		Return _available
	EndMethod
	
	Method GetQueryResult()
		If Not Available() Return 0
		Select result
			Case 1 Return 1
			Case 0 Return 0
			Case -1
				Local bits
				glGetQueryObjectuivARB id,GL_QUERY_RESULT_ARB,Varptr bits
				If bits<pixels result=0 Else result=1
				Return result
		EndSelect
	EndMethod
	
	Function Supported:Int()
		Global support
		Select support
			Case -1 Return 0
			Case 1 Return 1
			Case 0
				Local extensionstring$=String.FromCString( glGetString( GL_EXTENSIONS ) )
				If extensionstring.find("GL_ARB_occlusion_query")>-1 support=1 Else support=-1
				Return support
		EndSelect
	EndFunction
	
EndType