Cross section fill using Stencil Buffer problem (moved post from 2.0+)

Problems building or running the engine, queries about how to use features etc.
Post Reply
gabbsson
Halfling
Posts: 65
Joined: Wed Aug 08, 2018 9:03 am
x 13

Cross section fill using Stencil Buffer problem (moved post from 2.0+)

Post by gabbsson »

Ogre Version: 2.1
Operating System: Linux
Render System: GL3Plus

I'm using a stencil buffer to create cross sections "fillings" for clipped objects, displaying them simply as if the clipped area was covered by a flat surface.
More or less by following this: http://glbook.gamedev.net/GLBOOK/glbook ... vclip.html
I loosely "translated" the GL code into a .compositor file (reusing the sample compositor file for StencilTest) best I could and I'm fairly close.
In short i decrement the stencil buffer on back faces and increment on front faces, so that only the visible back faces "remain".

EDIT: I have embedded the entire compositor in case it is needed:

Code: Select all

compositor_node StencilTestNode
{
	in 0 rt_renderwindow
	
	//We need to render to an RTT because otherwise we can't share the depth buffer between
	//rtt_depthbuffer & rt_renderwindow in OpenGL (basically, an issue with GL)
	//see RSC_RTT_MAIN_DEPTHBUFFER_ATTACHABLE
	texture rtt				target_width target_height PF_R8G8B8 depth_format PF_D32_FLOAT_X24_S8_UINT depth_texture depth_pool 1
	texture rtt_depthbuffer	target_width target_height PF_D32_FLOAT_X24_S8_UINT depth_pool 1

	target rtt
	{
		pass clear
		{
			colour_value 0.2 0.4 0.6 1
		}

        pass render_scene
        {
            overlays    off
            rq_first    0
            rq_last     1
        }

		//Render overlays last on the entire screen
		pass render_scene
		{
		
			overlays	on
			rq_first	1
			rq_last	2
			
		}

	}
	
	//Write to depth/stencil with no colour
	target rtt_depthbuffer
	{	
		//Enable stencil
		pass stencil
		{
			check true
			
			mask		0xff
			read_mask	0xff
			
			ref_value		1
			
            		back
			{
				pass_op		decrement
				depth_fail_op	keep
				fail_op		keep

				comp_func	always_pass
			}
            		front
			{
				pass_op		increment
				depth_fail_op	keep
				fail_op		keep

				comp_func	always_pass
			}
		}

		//Render cross section objects
		pass render_scene
		{
			overlays	off
			rq_first	2
			rq_last	3
		}
	}
	
	//Regular draw
	target rtt
	{
		//Set the stencil to only draw if it passes the stencil.
		pass stencil
		{
			check true
			
			mask		0xff
			read_mask	0x1
			
			ref_value	1
			
			//'both' is a shortcut for setting 'back' and 'front' at the same time
			//to the same values. Here we do it explicitly just to show how it works.
			both
			{
				//pass_op			keep
				//depth_fail_op		keep
				//fail_op			keep

				comp_func		equal
			}
			
		}

		//Render objects affected by sphere of truth.
		pass render_scene
		{
			overlays	off
			rq_first	3
			rq_last	253
		}

		//Disable stencil to render the overlays
		pass stencil
		{
			check false
		}

        pass render_scene
        {
            overlays    	off
            rq_first    	253
            rq_last     	254
        }
		
		//Render overlays last on the entire screen
		pass render_scene
		{
		
			rq_first	254
			rq_last	255
			
			overlays	on
		}
	}
	
	//Copy results to render window
	target rt_renderwindow
	{
		pass clear
		{
			colour_value 0.2 0.4 0.6 1
		}
		
		pass render_quad
		{
			material Ogre/Copy/4xFP32
			input 0 rtt
		}
	}
}

workspace StencilTestWorkspace
{
	connect_output StencilTestNode 0
}
I have it working the way I want it, except when an object is inside the clipped object:
Image
What you are seeing is a "cloud" of triangles a clipped sphere and a plane.
Technically a second sphere affects the the stencil buffer (invisible) and is exactly the same as the rendered sphere other than the datablock being cull_none. Both are clipped by a plane using a vertex shader. The plane is then masked using the stencil buffer to only show where one would otherwise see the inside of the sphere.

The triangles visible through the cross section of the sphere cancel out my stencil buffer, I'm quite sure its due to failing the depth check. I have it on always pass, which I would have assumed meant it should pass the depth check too, have I misunderstood?. It may look like the triangles in the sphere are in front of the cross section plane, but they are in fact behind it. They are visible because the stencil buffer is set to the wrong values at the triangles. Quick image for illustrating where objects are in depth, because it may be unclear in the screenshot:

Image

So I think what i need is to turn off the depth check but only for the back-pass of the stencil buffer? (Correct me if I'm wrong). I have tried to simply turn off depth check for the entire macroblock but that does not have the desired effect. I am far from proficient with these techniques, my knowledge comes from piecing together tutorials and looking at the original sample compositor.
In the StencilTest.compositor file the stencil pass is done on the rtt_depthbuffer, not sure if that is also an "issue"?

So my question is:
Is it possible to turn off depth check for back faces only in the compositor stencil pass?
Or perhaps even more general, does anyone have an example of a way to do cross section flling?

(I deleted my post in 2.0+ where I originally posted this, I believe it belongs here. Hope thats ok!)
Post Reply