• Advertisement
Sign in to follow this  

HLSL Reflection

This topic is 3672 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

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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
ok, taken on board what you've said and come out with:


// Main vertex shader function
void 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


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

  • Advertisement