Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 19 Apr 2009
Offline Last Active Sep 06 2012 04:33 AM

Topics I've Started

[XNA] Order independently drawn transparent model is not reading depth

21 July 2012 - 03:08 PM

I'm using a method for drawing transparent objects without ordering using the weighted average method found in the following paper: http://Weighted Average Paper: http://developer.download.nvidia.com/SDK/10/opengl/src/dual_depth_peeling/doc/DualDepthPeeling.pdf

The problem with my setup is that the transparent model always draws on top of the opaque model even when I believe I have set up my render states correctly and it should be obscured.

The draw method can be broken down as follows:

- SetRenderTarget
- DrawOpaque
- UnsetRenderTarget
- SetTransparentRenderTargets
- DrawTransparent
- UnsetTransparentRenderTargets
- CombineTransparent result with OpaqueRenderTarget result

I have attached a simple source code and .exe demonstration of the problem. WASD keys and mouse to control the camera.

The opaque object is drawn in front of the camera and the transparent object is drawn behind the opaque object. It is not obscured by the opaque object and I am uncertain why this is the case?

What is the C# equivalent of the following C++ code?

02 February 2012 - 06:17 PM

char* trailFlags;		   // array of flag bits for trail points

bool someBool = CalculateBool();

trailFlags[index] = someBool ? 2 : 0;

I'm unsure as to how the integer value of 2 or 0 is stored as a flag bit char in C#. 0 appears to be stored as '0x002f6020' in C++.

How would I do the same in C#?

[XNA] Aligning texels to pixels doesn't work for small textures

28 January 2012 - 07:15 AM

I'm attempting to draw a texture that is 3 pixels by 3 pixels in size.

If I draw it using SpriteBatch then the texels are correctly aligned to the pixels

If I draw it using Texture Atlas look up then the texels are not correctly aligned and the x coordinate doesn't line up with the SpriteBatch result.

If I draw it straight from the original texture without the Atlas look up then the texels are not correctly aligned either.

Here is a picture demonstrating the results (All 3 quads have an x position of 100 yet they do not line up):

Posted Image

I'm attempting to line up the pixels to texels as follows:


            // [DirectX 9 - Align pixels to texels]
            // • Find size of half a pixel for render targets based on their size
            //   In this case render target size is backBufferWidth * backBufferHeight
            // • The reason that it is 1 / width instead of 0.5 / width is because, in clip space, 
            //   the coordinates range from -1 to 1 (width 2), and not from 0 to 1 (width 1) 
            //   like they do in textures, so you need to double the movement to account for that
            Vector4 halfPixelTexel = new Vector4();

            // Half Pixel
            //halfPixelTexel.X = 1f / textureAtlas.Texture.Width;
            //halfPixelTexel.Y = -1f / textureAtlas.Texture.Height;
            halfPixelTexel.X = 1f / GraphicsDevice.Viewport.Width;
            halfPixelTexel.Y = -1f / GraphicsDevice.Viewport.Height;
            //halfPixelTexel.X = 1f / GraphicsDevice.PresentationParameters.BackBufferWidth;
            //halfPixelTexel.Y = -1f / GraphicsDevice.PresentationParameters.BackBufferHeight;

            // Half Texel
            halfPixelTexel.Z = 0.5f / textureAtlas.Texture.Width;
            halfPixelTexel.W = 0.5f / textureAtlas.Texture.Height;
            //halfPixelTexel.Z = 0.5f / backBufferDim.X;
            //halfPixelTexel.W = 0.5f / backBufferDim.Y;



VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
    VertexShaderOutput Output = (VertexShaderOutput)0;

// Set world transform here (don't use a static identity matrix as it is slower)
float4x4 World =
// [Rotation, Scale]
input.Column1.x * input.TexCoordDimension.z, // * Scale X
input.Column1.y * input.TexCoordDimension.z,
input.Column1.z * input.TexCoordDimension.z,
input.Column2.x * input.TexCoordDimension.w, // * Scale Y
input.Column2.y * input.TexCoordDimension.w,
input.Column2.z * input.TexCoordDimension.w,

// [Translation]

// 'input.Position.w' should always equal 1
input.Position.w = 1;

// [Transformation]
// • Multiplying input.Position by World, then the result by ViewProjection is fast
// • Concatenating World and ViewProjection matrices then multiplying input.Position by the result is slower
input.Position = mul(input.Position, World); 
Output.Position = mul(input.Position, ViewProjection);

// [Texel To Pixel Align] 
// • Half pixel offset for correct texel centering)
// • Should be done AFTER transformation?
Output.Position.xy -= HalfPixelTexel.xy;

// [UV Coordinates]
Output.TextureCoordinates.xy = input.TexCoordDimension.xy;

// [Alpha]
Output.TextureCoordinates.w = input.Normal.w;

    return Output;

When using textures larger than 8 pixels this problem does not occur.

Can anyone please explain why the texels are not lining up correctly with the pixels with smaller textures?

Automatic GUI control generation based on object properties?

20 January 2012 - 05:42 PM

I have a simple class:

    public class MyObject
        public float A;
        public bool B;
        public int C;
        public Vector2 D;

I would like to automatically generate GUI objects based on each of the class' properties instead of creating them manually.

I thought about this approach, but I am uncertain how to fully implement it:

- Iterate through each property of the MyObject class (using reflection?), creating a relevant control based on the property type.
- Hook the control data change event up so that each change made will change the original class' property values.

Would there be a better approach to this or have I got the basic idea?

Any help or advice would be welcome.

[C#] Can anyone point me in the direction of an ASCII table formatter?

09 January 2012 - 02:31 PM

I'm after an ASCII table formatter in a similar vein to this one: http://cxc.harvard.e...dule-asciitable

I'm looking to automatically generate tables using ASCII characters to produce something similar to the following:

	| Column 1 |	Column 2	 |  Column 3			 |
    |FM-8		 | 1981-05		  | M68A09 @ 1MHz  | 
    |FM-7		 | 1982-11		  | M68B09 @ 2MHz  | 

The '|' should line up and auto size depending on the number of characters in each cell.

Any ideas?

Thank you.