Problem about vertex shader in Skybox

Started by
3 comments, last by swdever 10 years, 10 months ago

Suppose my skybox is a sphere. I have learned that when draw a skybox, we can make the sphere's z value equal to its w value in the vertex shader, like this:


VertexOut VS(VertexIn vin)
{
	VertexOut vout;
	
        //PosH stand for position in Homogeneous coordinate 
  	vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj).xyww;
	
        //PosL stan for position in local coordinate,used as a lookup vector in cubemap
	vout.PosL = vin.PosL;
	
	return vout;
}

I can't totally undestand why we can do like that. If we do so, after perspective divide, wouldn't there be two part of the sphere becomes in the view frustum's far plane, one behind the camera originally and the other in front of the camera originally? If they already in the view frustum, then, how can the pipeline clip out the one which behind the camera originally? Hope someone help me figure it out?

Advertisement

Hey, culling will be performed based on the winding order of the vertices, so the back faces will still be culled correctly.

However, in the example you give, if you're using PosL as your cube-map lookup vector. You do not want to copy the w coordinate into that vectors z element.

You will want something more akin to:

    vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);
    vout.PosL = vin.PosL;
    vout.PosH.z = vout.PosH,w;

n!

Hey, culling will be performed based on the winding order of the vertices, so the back faces will still be culled correctly.

However, in the example you give, if you're using PosL as your cube-map lookup vector. You do not want to copy the w coordinate into that vectors z element.

You will want something more akin to:


    vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);
    vout.PosL = vin.PosL;
    vout.PosH.z = vout.PosH,w;

n!

Thank you for your answer. But the sample code,which I am studying, sets CullMode = None. But the result is still perfect. After read you answer, I try to modify the code like this: CullMode = Front. Still the demo works well. Then I try to modify the code like this: CullMode = Back. This time, the skybox disappear. But other objects in my scene are rendered still. So I think maybe the CullMode isn't the key point in my problem.

Interesting. I put that sample through PIX and yes, the behind-the-camera triangles don't get rasterized, even with cullmode = none. I think the reason is the clipping rules:

0 <= zc <= wc (clip space)

Behind the camera w (and so z) are negative, so get clipped.

Interesting. I put that sample through PIX and yes, the behind-the-camera triangles don't get rasterized, even with cullmode = none. I think the reason is the clipping rules:

0 <= zc <= wc (clip space)

Behind the camera w (and so z) are negative, so get clipped.

Good job! This should be the right answer.Thank you!

This topic is closed to new replies.

Advertisement