Sign in to follow this  
ProgrammerDX

Vertex Shader and D3DTLVERTEX

Recommended Posts

Hello,

I'm trying to implement a vertex shader in my program.
I want to input a Transformed, Lit vertex via DrawPrimitiveUP

However, my vertex shader seems not to take effect.

What's the problem?

Vertex shader code (it compiles without error):
[code]
struct VertexIn
{
float4 Pos : POSITION;
float4 Diffuse : COLOR0;
float4 Specular : COLOR1;
float2 tex : TEXCOORD0;
};

struct VertexOut
{
float4 Pos : POSITION;
float4 Color : COLOR;
};

VertexOut VShader( VertexIn In )
{
VertexOut Out;

Out.Pos = In.Pos;
Out.Color = float4(1,1,1,1);

return Out;
}

technique FirstTechnique
{
pass FirstPass
{
Lighting = FALSE;
ZEnable = TRUE;

VertexShader = compile vs_2_0 VShader();
}
}
[/code]

FVF vertex structure D3DTLVERTEX thats input via DrawPrimitiveUP:
[code]
struct D3DTLVERTEX
{
float x, y, z, rhw;
D3DCOLOR diffuse;
D3DCOLOR specular;
float tu, tv;
}
[/code]

Any idea?

Share this post


Link to post
Share on other sites
Hi,

Typically the vertex shader is disabled when the vertex structure contains transformed vertices.

However, as far as I know, you should be able to pass 4 component vector (not divided by the w component yet) as position to the vertex shader and pass it through without transformations. You'll just need to lie to Direct3D that the position isn't pre-transformed, so that the vertex shader doesn't get disabled.

Cheers!

Share this post


Link to post
Share on other sites
Maybe [color=#2A2A2A]D3DFVF_XYZW will do the trick?[/color]
[color=#2A2A2A]Have you considered using vertex declaration? The FVF (flexible vertex format) stuff is rather unflexible.[/color]

[color=#2A2A2A]Cheers![/color]

Share this post


Link to post
Share on other sites
I've tried tricking D3D by using D3DFVF_XYZW yes, but then the screen goes black.

So I'm assuming I have to do some additional vertex processing in the vertex shader, but I don't know what. Very little information about that on the internet.

Share this post


Link to post
Share on other sites
The vertex shader needs to output vertices in clip space (Which is (-1, -1) to (+1, +1) in X and Y I think?). If you're passing in screen coordinates, then you'll need to scale the coordinates appropriately - e.g. for a 1280x720 screen resolution, you'd want:
[code]
Out.pos.x = In.pos.x / 640.0 - 1.0;
Out.pos.y = In.pos.y / 360.0 - 1.0;
Out.pos.z = 0;
[/code]

Share this post


Link to post
Share on other sites
But in a normal scenario, you pass IN a vertex in the vertex shader thats in object space. And then inside the vertex shader you have to mul() it with the WorldViewProj Matrix.

And so the out vertex is normally in screen space (projection).

Why in this case it has to be in clip space?

Or I'm thinking wrong?

EDIT: I tried what you said Evil Steve and that worked! :) (at least partially):
Had to switch the y one. And z can't be null because of z buffer.
w has to be 1.

[code]

Out.pos.x = In.pos.x / 640.0 - 1.0;
Out.pos.y = 1.0 - In.pos.y / 360.0;
Out.pos.z = In.pos.z;
Out.pos.w = 1;
[/code]

Share this post


Link to post
Share on other sites
[quote name='ProgrammerDX' timestamp='1333386135' post='4927552']
But in a normal scenario, you pass IN a vertex in the vertex shader thats in object space. And then inside the vertex shader you have to mul() it with the WorldViewProj Matrix.

And so the out vertex is normally in screen space (projection).

Why in this case it has to be in clip space?

Or I'm thinking wrong?[/quote]The projection matrix puts coordinates into clip space, not screen space. It's the job of the rasterizer (I think, not 100% sure) to convert the vertex coordinates into viewport (screen) space. You can multiply a vector by the world*view*projection matrix and see the sort of coodinate ranges you get out.

[quote name='ProgrammerDX' timestamp='1333386135' post='4927552']
EDIT: I tried what you said Evil Steve and that worked! :) (at least partially):
Had to switch the y one. And z can't be null because of z buffer.
w has to be 1.[/quote]Ah, I forgot about w :)

Share this post


Link to post
Share on other sites

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