I recently implemented Frustum culling in my game, and in an attempt to squeeze every last drop out of my render cycle I also decided to implement Occlusion Culling. It works brilliantly, however, I was distressed to discover that if I dont look at anything in my game(If I look out into the void and away from my game objects) my graphics card literally crashes. I am running a voxel-type game, which means it is a world filled with cubes. If there is no cubes present in my line of sight the crash occurs.
Here is my render loop which contains the Occlusion code:
protected override void OnRenderFrame( FrameEventArgs e ) {
base.OnRenderFrame( e );
GL.MatrixMode( MatrixMode.Modelview );
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
GL.EnableClientState( ArrayCap.VertexArray );
GL.EnableClientState( ArrayCap.TextureCoordArray );
GL.DisableClientState( ArrayCap.ColorArray );
/**
* Pass 1
* Do Occlusion Testing
*/
GameCamera.LookThrough( this , _mousePosition , e );
foreach( Voxel voxel in World.VisibleVoxels ) {
if( GameCamera.Frustum.SphereInFrustum( voxel.Location.X , voxel.Location.Y , voxel.Location.Z , 2.0f ) ) {
try {
GL.BeginQuery( QueryTarget.SamplesPassed , voxel.OcclusionID );
voxel.Render( GameCamera );
GL.EndQuery( QueryTarget.SamplesPassed );
} catch( Exception ex ) {
//
Console.WriteLine( "Setting It" );
Console.WriteLine( ex.StackTrace );
}
}
}
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
/**
* Pass 2
* Normal Rendering
*/
GameCamera.LookThrough( this , _mousePosition , e );
foreach( Voxel voxel in World.VisibleVoxels ) {
if( GameCamera.Frustum.SphereInFrustum( voxel.Location.X , voxel.Location.Y , voxel.Location.Z , 2.0f ) ) {
try {
GL.NV.BeginConditionalRender( voxel.OcclusionID , NvConditionalRender.QueryWaitNv );
voxel.Render( GameCamera );
GL.NV.EndConditionalRender();
} catch( Exception ex ) {
Console.WriteLine( "Testing It" );
Console.WriteLine( ex.StackTrace );
}
}
}
GL.DisableClientState( ArrayCap.VertexArray );
GL.DisableClientState( ArrayCap.TextureCoordArray );
GL.DisableClientState( ArrayCap.ColorArray );
RenderDeveloperHud();
SwapBuffers();
this.Title = GAME_NAME + " FPS: " + ( int )this.RenderFrequency;
}
I desperatly need to find a solution for this. It seems to me that OpenTK/OpenGL flips its shit when there is nothing visible in my viewport, but I dont know why. The loop itself should pass through if nothing is visible. Am I missing something here?
I can literally reproduce this crash every time I start the game and look away from the level. And by crash I mean my entire monitor goes black, hangs up, and then resumes with a message saying my display driver stopped working