Jump to content
  • Advertisement
Sign in to follow this  
Aaron Smith

DirectX 11 Volume Rendering Advice Needed

This topic is 1265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All


I have been following a few different tutorials on volume rendering using ray casting but i have come across a few issues which im hoping someone will be able to give me advice on


1. So I first render a cube front and back and then save these to textures which I use for the actual volume rendering after..... when I move the camera into the volume cube I notice the render of the front side no longer draws and only the back side draws, which this means I no longer can draw the actual volume render


What could I do to get around this problem as I really want to be able to move freely through the volume renders


2. this theory is based on the bounding volume being a area that goes from 0,0,0 to 1,1,1 and this works fine ....but if I want the volume area to only be scaled to 0.5,0.5,0.5 this now ruins the volume and its very confusing to why this would happen? I will try provide as much code for the area as possible but any advice would be great




Pixel Shader code for the volume render

float2 texC = input.pos.xy /= input.pos.w;
	texC.x = 0.5f*texC.x +0.5f;
	texC.y = 0.5f*texC.y +0.5f;

	float3 frontTex = txDiffuse2.Sample(samLinear, texC).xyz;
	float3 backTex =  txDiffuse.Sample(samLinear, texC).xyz;

	float3 direction = normalize(backTex - frontTex);// input.PosW);
	float4 pos = float4(frontTex, 0.0);
	float4 dst = float4(0, 0, 0, 0);
	float4 src = 0;

	float value = 0;

	float3 Step = normalize(direction) * 0.01;

	for (int i = 1; i < 50; i ++)
		pos.w = 0.0;
		value = volume.Sample(samLinear, pos).r;
		src = (float4)value;
		src.a *= 0.5f;

		dst.rgb = dst.rgb + (1 - dst.a) * src.a * src.rgb;
		dst.a = dst.a + (1 - dst.a) * src.a;
		//src.rgb *= src.a;
		//dst = (1.0f - dst.a) * src + dst;
		if (dst.a >= .95f)

		////advance the current position
		pos.xyz += Step;

		////break if the position is greater than <1, 1, 1>
		if (pos.x > 1.0f ||  pos.y > 1.0f || pos.z > 1.0f)

Vertex code for rendering the two textures

    VS_OUTPUT output = (VS_OUTPUT)0;

	input.PosL = input.PosL *float4(1, 1, 1, 1);
	float4 posW = mul(input.PosL, LocalWorld);
	//output.PosH = posW;
	output.PosW = posW.xyz;
	posW = mul(input.PosL, World);
	output.PosH = mul(posW, View);
	output.PosH = mul(output.PosH, Projection);
	output.Tex = input.Tex;

	float3 normalW = mul(float4(input.NormL, 0.0f), LocalWorld).xyz;
	output.NormW = normalize(normalW);

    return output;

Id be happy to help anyone who also is trying this but I fear I have either done something wrong or I am missing something


Thank You

Share this post

Link to post
Share on other sites

For #1, you are clipping the geometry that goes behind the near clipping plane of the view frustrum.  If you want to keep that from happening, you can modify your vertex shader to transform your vertices such that their Z component is set to 0 if it has a negative value after the transformation has been applied.  This will make the vertices get pushed back by your camera, and should keep your cube from being cut.


I don't really understand the issue in #2 - can you clarify that a bit more? 

Share this post

Link to post
Share on other sites
more efficient and simpler would be to do all in one pass.
render just the back faces, calculate with a simple ray intersection the near-distance of the cube (if t<0.0, then t=0.0); and trace through the volume.

Share this post

Link to post
Share on other sites

Thanks guys I have been looking through replies ...

Kyrpt0n - when you say to render the back faces, do you mean still render them to a texture and then draw the actual cube and pass that texture through to do the ray trace with?

Share this post

Link to post
Share on other sites

Hi all,


So I have come across a new problem, when the cube is rendered at 0,0,0 with a scale 1,1,1 and it seems to work well, but now i move the cube left and right hoping the volume would look the same but move and this happens




Can anyone help me understand why this now happens please?

I get that the vertex shader takes in position of the object  and then this is used within the pixel shader to reference from the backtexture but I cannot see how to get the volume to move while keeping the image consistent as i am hoping to use this for particle system


Thank You

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!