Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi this is my first question here so if I fail to include some necessary piece of information please dont rip my head off. ^^

I can't seem to get my volume raycaster to workt properly. I've been poking around changing things for days trying to get it to display my volume correctly but I seem only to get distorted mutations of it.

What I tried to do was a 1-pass renderer because the 2-pass one I made wasn't good enough for my purposes. Im using C# XNA and the renderer wont have to handle transparency so it only has to get in and fetch the first color it bumps into.

How it should look:

How it looks:

float4 OnePassPS(OnePassVSOutput input, float2 pixelCoord : VPOS) : COLOR0 {

float3 rayDir = normalize(mul(float3(((pixelCoord / WindowSize) * 2) - 1.0, -FocalLength), ModelView));

float4 pos = float4(RayOrigin + (rayDir * (input.pos.z / input.pos.w)), 0);
float3 Step = rayDir * StepSize;

float4 color = float4(0, 0, 0, 0);
for(int i = 0; i < Iterations; i++)
    color = tex3Dlod(VolumeS, pos); 

    if(color.a > 0)
        break; += Step;
return color; }

Help me stack exchange you're my only hope.

share|improve this question
up vote 0 down vote accepted
float3 rayDir = normalize(mul(float3(((pixelCoord / WindowSize) * 2) - 1.0, -FocalLength), ModelView));

this code is defintly wrong, i see the following problem here:

  • (a) You use a usual matrix operation to transform a direction, which is plainly wrong (you need different matrices for transforming positions and directions)

Your Matrix looks like this (from your comment)

Matrix.CreateTranslation(-Vector3.One / 2) * Matrix.CreateScale(10) * Matrix.CreateLookAt(camera.Position, Vector3.Zero, Vector3.Up);

to illustrate the problem here we take as an example a (0.7, 0.7, 0.0) unity normalized vector, when i multiply it with Matrix.CreateTranslation(5.0, 0.0, 0.0) i get as direction (5.7, 0.0, 0.0) which is no more normalized and wrong.

After showing why it is wrong i give you the advice, let (most of) the matrix stuff out of it and do the following (a version using matrices exists too for sure but i do it with vectors):

  • calculate the (normalized) direction of the look direction lookDir with rotating a unity vector
  • calculate the HorizontalVector using the cross product HorizontalVector = lookDir.cross(UpVector).normalized()
  • calculate the vertical vector VerticalVector = HorizontalVector.cross(lookDir).normalized()

now you can calculate the direction for each pixel with the following formula

Dir = (lookDir + VerticalVector.scale(pixelCoord.X / WindowSize.X - 0.5f) + HorizontalVector.scale(pixelCoord.Y / WindowSize.Y - 0.5f)).normalized()

Note that you have to check that the Vertical and Horizontal Vectors point in the right direction, if not switch the cross product.

share|improve this answer
Thanks for the answer! But what exactly is wrong with my matrix and what would I have to do differently to fix it? And for (b) yes that is what it does and yes it's a float vector. And the second bit I dont think is wrong because it moves the position forwards so it wont have to traverse the long distance to get to the volume. – Lorto6 Jul 11 '13 at 13:34
This is the matrix i give the shader: Matrix.CreateTranslation(-Vector3.One / 2) * Matrix.CreateScale(10) * Matrix.CreateLookAt(camera.Position, Vector3.Zero, Vector3.Up); – Lorto6 Jul 11 '13 at 13:35
Thanks for the explanation ^^ I implemented the vector method as you suggested and it looks exactly the same with all the same problems. Really confusing. – Lorto6 Jul 20 '13 at 12:49
Wasnt able to get your method working... But i came up with a better method that was simpler after tearing my hair out and resorting to paper and pen XD – Lorto6 Jul 26 '13 at 15:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.