Jump to content
  • Advertisement
Sign in to follow this  
LJ_1102

HLSL: Shader renders indices wrong[XNA]

This topic is 3791 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 again ^^ I'm still working on my terrain-engine. I want to get into HLSL and tried to render my terrain with my own shader. Its just a basic shader that calculates the position and texture but it messes up the index order... With the basic shader all looks great... Shader Source
float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldViewProjection;
float4 AmbientColor;
texture Texture;

struct VertexShaderInput
{
    float4 Position : POSITION0;
	float4 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float4 TexCoord : TEXCOORD0;
};

sampler TextureSampler = sampler_state
{
	texture = <Texture>;
	magfilter = Anisotropic;
	minfilter = Anisotropic;
	mipfilter = Anisotropic;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
	WorldViewProjection = mul(mul(World,View),Projection);
	output.Position = mul(input.Position,WorldViewProjection);
	output.TexCoord = input.TexCoord;
    return output;
}

struct PixelShaderInput
{
	float2 TexCoord : TEXCOORD0;
};

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
    return( tex2D(TextureSampler,input.TexCoord) * AmbientColor);
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_1_1 PixelShaderFunction();
    }
}
Screenshot of the messed up terrain http://www.gwebspace.de/lj_1102/WindowsGame22008-03-3100-42-51-25.png PS: culling is disabled on the screenshot to show the structure...

Share this post


Link to post
Share on other sites
Advertisement
I'm pretty sure you need to multiply the view and projection matrices first, and then multiply the world matrix by that product to compute the correct world-view-projection transform.

For example,


float4x4 viewProj = mul(View, Projection);
float4x4 worldViewProj = mul(World, viewProj);

output.Position = mul(input.Position, worldViewProj);

Share this post


Link to post
Share on other sites
Quote:
Original post by nilkn
I'm pretty sure you need to multiply the view and projection matrices first, and then multiply the world matrix by that product to compute the correct world-view-projection transform.

For example,


float4x4 viewProj = mul(View, Projection);
float4x4 worldViewProj = mul(World, viewProj);

output.Position = mul(input.Position, worldViewProj);

And you should probably do this outside the shader, since you're just using the WorldViewProj*position calculation as one operation; there's no reason to execute numerous matrix multiplications in the shader when you don't need the world/view/proj separately.

Share this post


Link to post
Share on other sites
there's no difference and the vertices are still wrong...
Quote:
float4x4 viewProj = mul(View, Projection);

float4x4 worldViewProj = mul(World, viewProj);



output.Position = mul(input.Position, worldViewProj);

Share this post


Link to post
Share on other sites
What is the purpose of PixelShaderInput? You output a VertexShaderOutput from your vertex shader, so why not use that as the input for your pixel shader?

Also, make use of DebugView and the Debug D3D Runtimes and see if it is spewing out any errors with regards to your app.

You could also try stepping through your vertex shader in PIX.

Regards,
ViLiO

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!