HLSL Reflection

Started by
12 comments, last by Roadkill247 16 years ago
I am trying to add reflection as the first shader to my scene but seem to be having troubles with it, it doesn't reflect any of the landscape around it, or the sky, it does appear to be the colour beyond the skybox but I don't know how it is doing that. For info my source: Vertex Shader

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------

float4x4 WorldView;
float4x4 ViewProj;

//-----------------------------------------------------------------------------
// Input / output structures
//-----------------------------------------------------------------------------

struct VS_Input 
{
	float4 Position : POSITION;
	float3 Normal	: NORMAL;
};

struct VS_Output
{
	float4 Position : POSITION;
	float2 TexCoord	: TEXCOORD0;
};
 

//-----------------------------------------------------------------------------
// Main function
//-----------------------------------------------------------------------------

// Main vertex shader function
void main( in VS_Input i, out VS_Output o )
{
    o.Position = mul( i.Position, WorldView );

    // Compute normal in camera space
    float3 NormalVec = mul( i.Normal, WorldView );
    NormalVec = normalize( NormalVec );

    // Obtain the reverse eye vector
    float3 EyeReflectVec = -normalize( o.Position );

    // Compute the reflection vector
    float3 Reflect = 2 * dot( EyeReflectVec, NormalVec ) * NormalVec - EyeReflectVec;

    // Store the reflection vector in texcoord0
    o.TexCoord = Reflect;

    // Apply the projection
    o.Position = mul( o.Position, ViewProj );
}


Pixel Shader

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------

float4 reflectivity = 1.0f;

samplerCUBE enviroMap = 
sampler_state
{
    Texture = <CubeMap>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
};


//-----------------------------------------------------------------------------
// Input / output structures
//-----------------------------------------------------------------------------

struct PS_Input
{
	float3 TexCoord : TEXCOORD0;
};

struct PS_Output
{
	float4 Colour : COLOR;
};

//-----------------------------------------------------------------------------
// Main function
//-----------------------------------------------------------------------------

void main( in PS_Input i, out PS_Output o )
{
	o.Colour = reflectivity * texCUBE( enviroMap, i.TexCoord );
}


Can anyone see any stupid errors I'm making ? [Edited by - Roadkill247 on April 1, 2008 10:58:29 AM]
Advertisement
When you mirror vertex data, the order of points in triangles is reversed, so clockwise triangles become counter-clockwise and vice versa. This means that you also have to reverse the back-face culling function (otherwise, all the front-faces would become back-faces and be culled away).
2 things:

1. You can calculate a reflection vector just like this:

float3 vEyeToVertexVS = normalize( mul( i.Position, WorldView ) );float3 vNormalVS = normalize( mul( i.Normal, WorldView ) );o.TexCoord = reflect( vEyeToVertexVS, vNormalVS );


2. You're multiplying your position by your view matrix twice, since you're multiplying a view-space position by your ViewProj matrix. Multiply it by your projection matrix only, or multiply the original position by your WorldViewProj.

ok, taken on board what you've said and come out with:

// Main vertex shader functionvoid main( in VS_Input i, out VS_Output o ){		    // Compute normal in camera space    float3 NormalVec = normalize( mul( i.Normal, ViewProj) );    // Obtain the reverse eye vector    float3 EyeReflectVec = normalize( mul( i.Position, ViewProj) );    // Store the reflection vector in texcoord0    o.TexCoord = reflect( EyeReflectVec, NormalVec );    // Apply the projection    o.Position = mul( i.Position, mul( WorldView, ViewProj ) );}


but now, my water isn't rendering at all

[Edited by - Roadkill247 on April 1, 2008 11:42:19 AM]
Again, you're applying the view matrix twice:

o.Position = mul( i.Position, mul( WorldView, ViewProj ) );
That must of been my combination at the time, my mistake.

Ok, well water is rendering, but isn't reflecting.


I couldn't explain without a picture:



As you can see (highlighted by the circle) there is a line going through my water, plus also the fact that it has the water texture although incredibly zoomed in.
ok got my reflection working like a good'un BUT now i have some more issues, this is more to do with texturing as for some reason the square that im rendering to which reflects has a tear down the middle, making it obvious that it is 2 triangles, any idea how to get round it ?

This is the element declaration
const D3DVERTEXELEMENT9 PosNormUV[] ={	{ 0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },	{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0 },	{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },	D3DDECL_END()};


The effect uses this, so I'm not sure what I'm missing out on.

[Edited by - Roadkill247 on April 2, 2008 2:30:14 PM]
To me it looks like a simple matter of incorrect texture coordinates.
From my code logic, I am setting the VertexDeclaration the one that I explained before with the Position, Normal and TexCoord, so I'm not sure where it is going wrong, I will have a play but if anyone has any suggestions I would be greatful.
Ok, the output from PIX on my waters vertex decleration is:

Stream Offset Type Method Usage UsageIndex
0 0 FLOAT3 DEFAULT POSITION 0
0 12 FLOAT3 DEFAULT NORMAL 0
0 24 FLOAT2 DEFAULT TEXCOORD 0
D3DDECL_END


so the pixel has the right decleration just isn't displaying the right thing.

This topic is closed to new replies.

Advertisement