Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


danromeo

Member Since 18 May 2001
Offline Last Active Sep 20 2014 10:29 AM

Topics I've Started

XNA Stencil Buffer problem, with code

01 September 2014 - 01:47 PM

hi. 

 

I'm trying to create a simple stencil mask on a RenderTarget2D from a bunch of primitives, and then later draw pixels from that render target to another rendertarget in a shader based on the stencil test pass/fail.  Code is below.  The results that I'm getting seems to be that the stencil test either always passes every pixel or always fails every pixel, regardless of which settings I try for DepthStencilStates.  
 
The idea is to create an overhead view of a forested world and then lay that view over the terrain when viewed from overhead rather than redrawing the forests on every frame, BUT my question is about Stencil Buffers..  
 
I set up the following resources:
 
MyRenderTarget = new RenderTarget2D(graphicsDevice, mapSize, mapSize, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);


NewRenderTarget = new RenderTarget2D(graphicsDevice, mapSize, mapSize, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);

DepthStencilState writeStencil = new DepthStencilState()
                {
                    StencilEnable = true,
                    DepthBufferEnable = false,
                    ReferenceStencil = 1,
                    StencilFunction = CompareFunction.Always,
                    StencilPass = StencilOperation.Replace,
                };

            DepthStencilState stencilMask = new DepthStencilState()
            {
                StencilEnable = true,
                DepthBufferEnable = false,
                ReferenceStencil = 0,
                StencilFunction = CompareFunction.NotEqual,
                StencilPass = StencilOperation.Keep,
            };

During initialization to create my overhead render target with stencil I set the DepthStencilState to stencilMask and draw the forests to the rendertarget, which SHOULD give me a stencil buffer containing 0's where there are no trees and 1's where there are trees.  
 
graphicsDevice.SetRenderTarget(MyRenderTarget);
graphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Stencil | ClearOptions.Target, Microsoft.Xna.Framework.Color.Black, 1.0f, 0);


graphicsDevice.DepthStencilState = writeStencil;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphicsDevice.DrawUserIndexedPrimitives<Position4Texture>(PrimitiveType.TriangleList,
                   Vertices, 0, 4, Indices, 0, 2);

            }
graphicsDevice.DepthStencilState = DepthStencilState.Default;        

And then at render time I render my terrain, and then in a second pass I set the DepthStencilState to stencilMask and render a quad over the terrain pulling pixels from MyRenderTarget based on stencil test pass/fail:
 
graphicsDevice.SetRenderTarget(NewRenderTarget);
graphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Stencil | ClearOptions.Target, Microsoft.Xna.Framework.Color.Black, 1.0f, 0);

graphicsDevice.DepthStencilState = DepthStencilState.Default;
< DRAW TERRAIN TO NewRenderTarget >


graphicsDevice.DepthStencilState = stencilMask;
effect.Parameters["Texture"].SetValue(MyRenderTarget);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {

                pass.Apply();

                graphicsDevice.DrawUserIndexedPrimitives<Position4Texture>(PrimitiveType.TriangleList,
                   Vertices, 0, 4, Indices, 0, 2);

            }
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
And in the simple pixel shader I am returning:
 
return = tex2D(Texture,input.TexCoord);
I've tried various settings in the DepthStencilStates, and the end result is the stencil test either always passes all pixels, giving me overhead forests with black terrain, or always fails, giving me terrain with no forests.  I've never used stencil buffers before but would like to make extensive use of them.  Can somebody tell me what I'm doing wrong?  
 
THANKS
 
 
 
 
 

 

 

 
 

XNA 4 RenderTarget Stencil Buffer

26 August 2014 - 02:30 PM

Hi.  

 

Can somebody explain to me or point me to a resource on using a basic stencil buffer on a render target in XNA 4?  I'm finding a lot of XNA 3 resources and I know that it's changed in XNA 4, and a lot of articles on XNA4 that are overkill and confusing.  

 

I want a rendertarget with a stencil buffer, which is drawn to during initialization.  Then at draw time I want to pass the render target to the pixel shader, selecting pixels from the passed render target to render to the current render target, but discarding pixels based on the original render target's stencil buffer.  

 

I THINK, correct me if I'm wrong:

 

The graphics device has a stencil buffer which is zeroed out when you set the rendertarget.  

 

I have to enable the stencil buffer.  

 

I can set the game state to compare the graphics device stencil buffer and the render target stencil buffer at draw time.  

 

BUT, how do I set the values in the render target stencil buffer?  

 

Also I'm using Multiple Render Targets, if this makes any difference.  

 

I know this is probably simple stuff but I'm not finding any resource that gives a simple explanation of What It Is and How To Use It.

 

Thanks


How to monitor video card memory

30 June 2014 - 11:46 AM

How can I see the contents and allocations of my video card memory, either in real time or in Pix or any other (free) diag program?  How can I poll the available video memory from a program before making allocations?  What is the best way to fine tune allocations to video memory?  I just realized that allocating beyond the available memory seems to crash the computer with no warning.  How do I avoid this while still taking advantage of the full available memory?  I'm using XNA at the moment but am porting to Sharp DX.  

 

Thanks 


HLSL Camera view angle to object with object rotation

07 June 2014 - 06:06 PM

How do I determine camera view angle to an object with the object's rotation factored in, to display a 2d "imposter" based on the actual view angle of the object?

 

I can get the camera angle to the object with:



half3 center = mul(input.inPos, xWorld);
half3 EyeVector = normalize(center-CameraPosition); 
float lookYaw = atan2(EyeVector.x, EyeVector.z);



But I'm not having any luck determining the view angle with object rotation factored in either by adding or subtracting the EyeVector with the ObjectRotation vector or by atan2'ing the ObjectRotation vector and adding or subtracting the result with lookYaw.  All vectors in question are normalized.  

 

Hope this makes sense.  Thanks!

 

 


Vertex Shader unexpected results, bizarre

08 May 2014 - 09:09 PM

Hi.

 

I have a version 3.0 vertex shader for (what I call) Static Imposters that essentially decides which image in a texture atlas should be used and sends that info to the pixel shader.  

 

I am confident that the shader is written correctly but it always displays the wrong image!  If I run the shader through a debugger, the numbers are all correct, but the wrong image displays.  I have isolated this to a single variable, whose value is assigned from the program with Effect.Parameters["ImagesPerView"].SetValue(ImagesPerView);  

 

Running the shader through the Pix debugger, the value of ImagesPerView is always set correctly to 12, but the program displays the image as if the value of ImagesPerView is set to 11.  If I hardcode the value of ImagesPerView in the vertex shader to 12, overriding the program assignment, the program displays the correct image!  WHY am I getting different results even though the value of ImagesPerView is always 12?  

 

The program is assigning the value of 12 to ImagesPerView on every draw call.  I don't think this is due to any sort of  implicit conversions as I have checked the code thoroughly, and even did an (int)ImagesPerView conversion on every operation to make sure.  All of the other variable assignments in the shader appear to be correct and functioning correctly.  Stared at it until I was crosseyed.....What The Heck am I doing wrong?  

Any ideas welcome.

 

THANKS


PARTNERS