Archived

This topic is now archived and is closed to further replies.

Shader Integration

This topic is 5383 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 guys! I''ve got a small problem integrating shaders into my engine. When you code a shader, you have to take a vertex in world/object space and move it to clip space. Well, I''m having difficulty constructing this View-Proj matrix, as I can''t find anything that describes what it is! I''ve tried several combinations, from View x Proj, Proj x View, to Inverse View x Proj, and so on so forth. I''m actually using Cg shaders, and DirectX, so any specific answer would be appreciated. The bizarre thing is, the vertex shader correctly transforms the vertex to clip space, but if I try moving the camera, then it appears as though clip planes move through the scene and stuff just dissapears! If I try moving forward, for example, then EVERYTHING dissapears, all at once. If I move up/down, however, clip planes just move through my scene slowly, but they''re there! Chris Pergrossi < ctoan >

Share this post


Link to post
Share on other sites
Shaders! Great fun eh! ^_^

This is how I'm doing it with my engine (but I'm writing the shaders in the ASM style languages - I'm not sure how you pass the matrix to CG as I havnt tried it yet ^_^)

Anyway, during initialisation, I build the projection matrix with D3DX:

D3DXMatrixPerspectiveFovRH(&m_projection,D3DX_PI/4,
SCREEN_WIDTH/SCREEN_HEIGHT,1.0f,1000.0f);

g_pD3DDEVICE->SetTransform(D3DTS_PROJECTION,&m_projection);


Where m_projection is a global D3DX matrix, D3DXMATRIX m_projection;
and the 1.0f/1000.0f bit is the min/max Z-ranges of the scene, and g_pD3DDEVICE is the D3D device object.

Secondly, I build the view matrix (in my engine I only do this once - When I want to move the camera, I move all the objects in the world instead, so when I move forward 2 units (or whatever) I just move the world 2 units in the oposite direction - if you get what I mean. Not sure if I'll stick with this though.. Have to wait and see - works well with my Per pixel Specular term though - as I'm not moving the camera at all, but the world, so it saves a bit of code. I build the view matrix as so:

D3DXMatrixLookAtLH( &mview,
&D3DXVECTOR3( 0.0f, 0.0f, -1.0f ),
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ));


Where mview is a global D3DX matrix : D3DXMATRIX mview;

Thirdly, I do this whenever something is rotated/translated:

D3DXMatrixTranspose( &FinalMat , &(mworld * mview * m_projection) );

Ok. That should build your clip matrix. Now, I dont know how you're doing it, but in my engine, I pass the matrix (FinalMat) to the vertex shader via:

g_pD3DDEVICE->SetVertexShaderConstant(4, FinalMat, 4);
I.e. Pass the matrix to Vertex shader constant 4, and its 4 128bit chunks(4 floats) in size, so it occupies Vertex shader constants 4,5,6,7.

Then in my shader

And in my vertex shader, I transform the vertex into clip space like so:

"m4x4 r0, v0, c4\n"
"mov oPos, r0\n"


I'm just doing a 4*4 matrix multiplication using v0(incoming vertex), and c4 (our final matrix), then sending it to oPos (which is a vertex shader constant for the clip-space vertex output). And Tada! Problem solved!

Be careful though. I made the serious mistake when I was just starting out with shaders, or using the clip space vertex for other operations when I wanted the world space vertex - I was such an idiot! I couldnt see why after passing mworld (my world matrix) to the vertex program, it wasnt working! Thats beacuse I didnt transpose it first (to be honest - I dont see exactly what "transposing" it with :
D3DXMatrixTranspose( &mworld , &(mworld ) );
actually does! I mean, does it "change" the matrix somehow?? I dont know!

Anyway I hope I've been of some help!

Woah! Forgot to mention! I'm using right hand coordinates as in OpenGL (Z decreases into the screen) , if your'e using Left handed coordinates (Z increases into the screen), then change

D3DXMatrixPerspectiveFovRH

to

D3DXMatrixPerspectiveFovLH

[edited by - danielk on March 23, 2003 11:06:12 AM]

Share this post


Link to post
Share on other sites
Thanks dude! I read through your explanation and realized what I was doing wrong! I''ve been searching for an answer all day (and I''m sure they''re sick of me on #gamedev ), but I wasn''t transposing the view matrix! See, in my engine, instead of moving everything via world matrices (which would get really confusing when skinned models come into play, cause they''ve got very special ways of storing rotations and stuff), I simply update my view matrix with the D3DXMatrixLookAtRH(). Thanks dude, they work brilliantly now! :D

Share this post


Link to post
Share on other sites