Jump to content
  • Advertisement
Sign in to follow this  
fordfanboi

Vertex Shader problems

This topic is 3339 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'm attempting to move the upper vertices of my grass square to animate the grass. I started to have trouble, so I stripped down the shader and just tried to move the y position of the top vertices up just to see if it's working. Everything compiles and runs, just that the grass isn't rendered when the shader is enabled. Keep in mind, the square grass draws fine with the shader turned off... Also - why does my output from the shader have to be FLOAT4 for position? Shader won't compile if I try to output FLOAT3 like the INPUT. Thanks for any help! FFB SHADER ------------------------------------------------
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
float4x4 mWorldViewProj;  // World * View * Projection transformation
float fTime;			  // Time parameter. This keeps increasing


//-----------------------------------------------------------------------------
// Vertex shader input structure
//-----------------------------------------------------------------------------
struct VS_INPUT {
	float3 vPosition : POSITION;
	float2 TexCoords : TEXCOORD0;
};


//-----------------------------------------------------------------------------
// Vertex shader output structure
//-----------------------------------------------------------------------------
struct VS_OUTPUT {
	float4 vPosition : POSITION;
	float2 TexCoords : TEXCOORD0;
};


//-----------------------------------------------------------------------------
// Name: GrassWave
// Type: Vertex shader                                      
// Desc: This shader waves the vertices
//-----------------------------------------------------------------------------
VS_OUTPUT GrassWave(const VS_INPUT v)
{
	VS_OUTPUT Output = ( VS_OUTPUT )0;
	
	// Output stuff
	if( v.TexCoords.y >= 0.1f )
		Output.vPosition = mul( float4( v.vPosition.x, v.vPosition.y + 2.0f, v.vPosition.z, 1.0f ), mWorldViewProj );
	else
		Output.vPosition = mul( float4( v.vPosition, 1.0f ), mWorldViewProj );
	
	Output.TexCoords = v.TexCoords;
	
    return Output;
}




VERTEX DECL ------------------------------------------------
struct GRASSVERTEX
{
    D3DXVECTOR3 p;
    FLOAT tu1, tv1;

    static const DWORD FVF;
};




D3DVERTEXELEMENT9 decl[] =
{
   { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
   { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
   D3DDECL_END()
};

SHADER ALLOCATION ------------------------------------------------

hr = device3D->CreateVertexDeclaration( decl, &g_pVertexDeclaration );

LPCSTR vs = D3DXGetVertexShaderProfile( device3D );//"vs_2_0";
hr = D3DXCompileShaderFromFile( _T("GrassShader.vsh"), NULL, NULL, "GrassWave", vs, NULL, &pCode, &errors, &g_pConstantTable );
if( hr == S_OK )
{
	device3D->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &GrassShader );
	pCode->Release();
}




CONSTANT UPDATE EACH FRAME ------------------------------------------------
D3DXMATRIXA16 mWorldViewProjection;
// Get the projection & view matrix from the camera class
mWorld = *g_Camera.GetWorldMatrix();
mProj = *g_Camera.GetProjMatrix();
mView = *g_Camera.GetViewMatrix();
mWorldViewProjection = mWorld * mView * mProj;

hr = g_pConstantTable->SetMatrix( pd3dDevice, "mWorldViewProj", &mWorldViewProjection );
hr = g_pConstantTable->SetFloat( pd3dDevice, "fTime", ( float )fTime );




RENDER ------------------------------------------------
device3D->SetStreamSource( 0, grassVB, 0, sizeof( GRASSVERTEX ) );
device3D->SetFVF( GRASSVERTEX::FVF );
device3D->SetVertexDeclaration( g_pVertexDeclaration );
device3D->SetVertexShader( GrassShader );

device3D->SetTexture( 0, textureType0 );
quad->scaleRotMatrix._41 = quad->rndOffset2.x;
quad->scaleRotMatrix._42 = quad->rndOffset2.y;
quad->scaleRotMatrix._43 = quad->rndOffset2.z;
device3D->SetTransform( D3DTS_WORLD, &quad->scaleRotMatrix );
device3D->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );




[Edited by - fordfanboi on October 23, 2009 11:43:27 AM]

Share this post


Link to post
Share on other sites
Advertisement
What if you remove the if-statement and just transform the position by the WVP matrix?

Also, how do you compute mWorldViewProjection and fTime?

Finally, have a look here.

Share this post


Link to post
Share on other sites
I updated the code above to include the world view proj calc as requested.

I have tried it without the if and still had no luck. =(

Thanks...

Share this post


Link to post
Share on other sites
So with the vertex shader, you're using g_Camera.GetWorldMatrix() as the world matrix, but without the shader you're using quad->scaleRotMatrix. Maybe that has something to do with it?

Share this post


Link to post
Share on other sites
I attempted the following - still nothing appearing.


//-----------------------------------------------------------------------------
// Name: GrassWave
// Type: Vertex shader
// Desc: This shader waves the vertices
//-----------------------------------------------------------------------------
VS_OUTPUT GrassWave(const VS_INPUT v)
{
VS_OUTPUT Output = ( VS_OUTPUT )0;

// Output stuff
//if( v.TexCoords1.y >= 0.1f )
// Output.vPosition = mul( float4( v.vPosition.x, v.vPosition.y + 2.0f, v.vPosition.z, 1.0f ), mWorldViewProj );
//else
// Output.vPosition = mul( float4( v.vPosition, 1.0f ), mWorldViewProj );

Output.vPosition = float4( v.vPosition, 1.0f );
Output.TexCoords = v.TexCoords;

return Output;
}

Share this post


Link to post
Share on other sites
Tried this with the original .vsh and still had no luck seeing anything.


device3D->SetStreamSource( 0, grassVB, 0, sizeof( GRASSVERTEX ) );
device3D->SetFVF( GRASSVERTEX::FVF );
device3D->SetVertexDeclaration( g_pVertexDeclaration );
device3D->SetVertexShader( GrassShader );

D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
D3DXMATRIXA16 mWorldViewProjection;

// Get the projection & view matrix from the camera class
mProj = *g_Camera.GetProjMatrix();
mView = *g_Camera.GetViewMatrix();

device3D->SetTexture( 0, textureType0 );
quad->scaleRotMatrix._41 = quad->rndOffset2.x;
quad->scaleRotMatrix._42 = quad->rndOffset2.y;
quad->scaleRotMatrix._43 = quad->rndOffset2.z;
device3D->SetTransform( D3DTS_WORLD, &quad->scaleRotMatrix );
mWorldViewProjection = quad->scaleRotMatrix * mView * mProj;
g_pConstantTable->SetMatrix( device3D, "mWorldViewProj", &mWorldViewProjection );
device3D->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );



Share this post


Link to post
Share on other sites
Yes.

For some reason, it told me it couldn't set the fTime constant. So, for now I've removed it. That's all I gleaned from using the debug runtimes however.

Thanks for the tip too!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!