Sign in to follow this  

shader bug & related math

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

Hello gents,
I've been having some issues getting my shaders to work correctly... I developed a few basic shaders (reflection, refraction, normal mapping etc...) in RenderMonkey 1.6, they run, and look wonderfully, but when I bring it into my game (XNA 4.0) the mesh gets stretched/warped across the entire bottom half of the screen... here's the code I'm running...

The Shader...
// Effect File exported by RenderMonkey 1.6

// Object

float4x4 view_proj_matrix : ViewProjection;
float4 view_position : ViewPosition;
Texture environment_texture;

struct VS_OUTPUT
float4 Pos: POSITION;
float2 TexCoord: TEXCOORD0;
float3 viewVec: TEXCOORD1;
float3 Normal: TEXCOORD2;

VS_OUTPUT vs_main(float4 inPos: POSITION,
float3 inNormal: NORMAL,
float2 inTxr: TEXCOORD0)

// Compute the projected position and send out the texture coordinates
Out.Pos = mul(view_proj_matrix, inPos);
Out.TexCoord = inTxr;

Out.viewVec = view_position - inPos;
Out.Normal = inNormal;

return Out;

sampler EnvMap = sampler_state
Texture = (environment_texture);

float4 ps_main(float2 inTxr: TEXCOORD0,
float3 viewVec: TEXCOORD1,
float3 inNormal: TEXCOORD2) : COLOR
viewVec = normalize(viewVec);

// Compute reflection
float3 inReflect = reflect(-viewVec,inNormal);

// Compute the reflection vector using Snell's law
// n_i * sin(theta_i) = n_r * sin(theta_r)

// sin(theta_i)
float cosine = dot(viewVec, inNormal);
float sine = sqrt(1 - cosine * cosine);

// sin(theta_r)
float sine2;
float cosine2;

// get refraction direction from normal and tangent vectors
float3 x = -inNormal;
float3 y = normalize(cross(cross(viewVec, inNormal), inNormal));

// Determine refraction for each color component
float4 refr = float4(0,0,0,1);

// --- RED ---
sine2 = saturate(1.14 * sine);
cosine2 = sqrt(1 - sine2 * sine2);
refr.r = texCUBE(EnvMap,x * cosine2 + y * sine2).r;

// --- GREEN ---
sine2 = saturate(1.18 * sine);
cosine2 = sqrt(1 - sine2 * sine2);
refr.g = texCUBE(EnvMap,x * cosine2 + y * sine2).g;

// --- BLUE ---
sine2 = saturate(1.20 * sine);
cosine2 = sqrt(1 - sine2 * sine2);
refr.b = texCUBE(EnvMap,x * cosine2 + y * sine2).b;

// Output texture color with reflection map
return sine * texCUBE(EnvMap,inReflect) + ((1 - sine2) * refr + 0.2);

// Technique Section for BasicEnvScene
technique Scene
pass Object

VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();

and this is how I draw the model...

[code]private void ShaderDrawCrystal(Model model)
foreach (EffectPass pass in m_testEffect.CurrentTechnique.Passes)
foreach (ModelMesh mesh in model.Meshes)
foreach (ModelMeshPart part in mesh.MeshParts)
part.Effect = m_testEffect;
Matrix vpm = m_camera.ViewMatrix * m_camera.ProjectionMatrix;
Vector4 pos = new Vector4(m_camera.Position.X, m_camera.Position.Y, m_camera.Position.Z, 1f);

I've tried 3 different shaders and all 3 give me the same result, the shader works, but the model mesh gets stretched across the bottom of the screen, I have a feeling it has something to do with my view_projection_matrix, but I'm not a math expert. Any help will be most grateful as usual. Thanks!

Share this post

Link to post
Share on other sites

This topic is 2321 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this