Sign in to follow this  

Shaders and Sprite Interface

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

Hey everyone, i am trying to just render a fullscreen quad in which i texture it using a pixel shader. I would just use DrawPrimitive but I do not have a camera set up at the moment because my project doesnt actually need one. Therefore, the only way i could get my image to be drawn completely on the screen was with the sprite interface. Now I am running into trouble getting this quad to be shaded. After reading lots of posts i have made sure to use D3DXSPRITE_DONOTMODIFY_RENDERSTATE but now i just get nothing. My code is below and right now my shader code is just a pixel shader (since no vertex changes need to be made). If anyone has some advice on what might be wrong, i would greatly appreciate it. Thanks in advance
    // Clear the render target and the zbuffer 
    V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );

	g_pTextureEffect->SetTechnique("LinearLF");

    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
		g_pTextureEffect->Begin(&cPasses, 0);
	
		for (iPass = 0; iPass < cPasses; iPass++)
		{
			V( g_pTextureEffect->BeginPass(iPass) );

			g_pTextureSprite->Begin( D3DXSPRITE_DONOTMODIFY_RENDERSTATE );

			// Build our matrix to rotate, scale and position our sprite
			D3DXMATRIX mat;

			D3DXVECTOR2 spriteCentre = D3DXVECTOR2(g_WindowRect.right / 2.0f ,g_WindowRect.bottom / 2.0f);

			D3DXVECTOR2 scaling(1.0f,1.0f);

			// out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation
			D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,NULL,NULL);

			g_pTextureSprite->SetTransform(&mat);

			// Draw the sprite 
			g_pTextureSprite->Draw(g_pClearTexture, NULL, NULL, NULL, 0xFFFFFFFF);

			g_pTextureSprite->End();

			V( g_pTextureEffect->EndPass() );
		
		}// for

		g_pTextureEffect->End();
Now my shader code:

float4 LinearLF_PS() : COLOR0
{   
   return( float4( 1.0f, 0.0f, 0.0f, 1.0f ) );
   
}

technique LinearLF
{
    pass p0
    {
        PixelShader = compile ps_2_0 LinearLF_PS();
    }
}
You might wonder why this is in an effect file but it is because i will be doing multiple shading passes once i get this part to work.

Share this post


Link to post
Share on other sites
It's not necessary for you to use ID3DXSPRITE, even if you don't have a camera set up. What you can do is use the D3DFVF_XYZRHW FVF code for your vertex buffer, which specifies that the vertices are pre-transformed and shouldn't be transformed by the graphics pipeline. Here's some code for creating a vertex buffer for a squad covering the entire screen:



//set up vertices for full-screen quad
FLOAT fWidth5 = (FLOAT)backBufferW - 0.5f;
FLOAT fHeight5 = (FLOAT)backBufferH - 0.5f;

FLOAT fTexWidth1 = 1.0f;
FLOAT fTexHeight1 = 1.0f;

//create our vertex buffer
d3dDevice->CreateVertexBuffer(sizeof(SCREEN_VERTEX) * 4, 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1,
D3DPOOL_DEFAULT, &fullScreenQuadVB, NULL);

SCREEN_VERTEX* vertices = NULL;
fullScreenQuadVB->Lock(0, 0, (void**)&vertices, 0);

// Fill in the vertex values
vertices[0].pos = D3DXVECTOR4(fWidth5, -0.5f, 0.0f, 1.0f);
vertices[0].clr = D3DXCOLOR(0.5f, 0.5f, 0.5f, 0.66666f);
vertices[0].tex1 = D3DXVECTOR2(fTexWidth1, 0.0f);

vertices[1].pos = D3DXVECTOR4(fWidth5, fHeight5, 0.0f, 1.0f);
vertices[1].clr = D3DXCOLOR(0.5f, 0.5f, 0.5f, 0.66666f);
vertices[1].tex1 = D3DXVECTOR2(fTexWidth1, fTexHeight1);

vertices[2].pos = D3DXVECTOR4(-0.5f, -0.5f, 0.0f, 1.0f);
vertices[2].clr = D3DXCOLOR(0.5f, 0.5f, 0.5f, 0.66666f);
vertices[2].tex1 = D3DXVECTOR2(0.0f, 0.0f);

vertices[3].pos = D3DXVECTOR4(-0.5f, fHeight5, 0.0f, 1.0f);
vertices[3].clr = D3DXCOLOR(0.5f, 0.5f, 0.5f, 0.66666f);
vertices[3].tex1 = D3DXVECTOR2(0.0f, fTexHeight1);

fullScreenQuadVB->Unlock();






You could then use this vertex buffer with a DrawPrimitive command like this:


d3dDevice->SetStreamSource(0, fullScreenQuadVB, 0, sizeof (SCREEN_VERTEX));
d3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);



Share this post


Link to post
Share on other sites
Thanks for the help, i now have the quad drawn and can use the pixel shader to change the color. However, i can not get it to be textured using my texture and sampler. I know the texture is sent to the shader since i do setTexture and commit Values. However, i am unsure if the texture coordinates are actually being sent

here is my code


struct PS_INPUT
{
float2 texCoord : TEXCOORD1;
};

struct PS_OUTPUT
{
float4 FinalColor : COLOR0;
};

PS_OUTPUT LinearLF_PS()

{

PS_OUTPUT output;

output.FinalColor = tex2d(LFTexSampler, input.texCoord);

return output;



}



technique LinearLF

{

pass p0

{


PixelShader = compile ps_2_0 LinearLF_PS();

}

}



Can anyone see what is wrong? Also, if i wanted to use a vertex shader with the quad drawn using your method, how would i do it?

I really appreciate the help.

Share this post


Link to post
Share on other sites
When i do that, i get nothing at all. The reason i believe that i need texcoord1 is that if you look at the previous code MJP provided, he used TEX1 and not TEX0. Any other suggestions?

Share this post


Link to post
Share on other sites

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