Archived

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

IOFILE

Direct3D newbie...

Recommended Posts

OK. I have a desperate need to learn D3D. I want to create all sorts of 3D programs, but really I need a deep understanding of the API in order to get anywhere. I''m fine with the architecture of DDraw, and I understand, vectors, viewports, blah, blah, blah. However, I really don''t understand the view matrix, the world matrix, or the projection matrix, and how they work and how the values stored in them mean anything, or how they are used to manipulate anything. I also don''t understand the slightest bit of how to create an object, and the tutorials that I have discovered don''t really seem to explain things well from the beginning. Please help out a D3D newbie, the carma will reward you . No but seriously, any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Um, alright, here goes...Matrices are used for doing transformations; if you dont know what this means, dont worry about it unless you''re writing your own engine (which is probably not the case). The view matrix is where you look from. The world matrix is used to move objects around and rotate them. The projection matrix helps the 3d renderer determine how it should draw stuff to your screen; only worry about this if you wanna change the FOV. Otherwise, leave it an identity matrix, which looks like this:
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]

About drawing objects: what do you mean by "object"? Most drawing (all?) in direct3d is done by calls to DrawPrimitive; look it up. You could say an object is an array of vertexes i suppose. Anyway, thats about it. If you want a more detailed explanation of matrices, just ask.

------------------------------
*Large explosion consumes building, hero walks away unharmed* (wtf? why?) CUT

Share this post


Link to post
Share on other sites
Well, that cleared up a lot. But still, it leaves a lot to be answered. To tell you the truth (as silly as it may sound) I am going to try to create a 3D engine. I do understand, from you and from the DX7 SDK, what the different matrices do, but what I don''t understand is how to use them. I don''t even know how to set up a scene of triangles. How do i use a renderer? How do I use the world matrix? And among all, how do I even start?

Share this post


Link to post
Share on other sites
Um, ok, here we go. Heres a little code to demonstrate how to use matrices...

// sets the projection matrix (dont worry about this too much)
D3DMATRIX matProj = mat;
matProj._11 = 2.0f;
matProj._22 = 2.0f;
matProj._34 = 1.0f;
matProj._43 = -1.0f;
matProj._44 = 0.0f;
pd3dDevice->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &matProj );

// set the view matrix
// dont worry about all the math down here
// actually, i suggest using the d3dx functions for building
// matrices; they make things MUCH easier
D3DMATRIX matView;
ZeroMemory( &matView, sizeof(D3DMATRIX) );
matView._11 = 1.0f;
matView._22 = (FLOAT)sin(-0.5f);
matView._23 = -(FLOAT)cos(-0.5f);
matView._32 = -(FLOAT)cos(-0.5f);
matView._33 = (FLOAT)sin(-0.5f);
matView._43 = 50.0f;
matView._44 = 1.0f;
pd3dDevice->SetTransform( D3DTRANSFORMSTATE_VIEW, &matView );

// What this does is move stuff by x, y, and z
// (this is in the SDK, btw)
D3DMATRIX ret;
ret._11 = 1.0f; ret._12 = 0.0f; ret._13 = 0.0f; ret._14 = 0.0f;
ret._21 = 0.0f; ret._22 = 1.0f; ret._23 = 0.0f; ret._24 = 0.0f;
ret._31 = 0.0f; ret._32 = 0.0f; ret._33 = 1.0f; ret._34 = 0.0f;
ret._41 = x; ret._42 = y; ret._43 = z; ret._44 = 1.0f;

pd3dDevice->SetTransform( D3DTRANSFORMSTATE_WORLD, &ret);

// Ok, now lets try to move two things in different ways...
// pretend that lpDots is an array of D3DVERTEXes and DOT_VERTS
// holds the number of vertexes

D3DMATRIX ret;
ret._11 = 1.0f; ret._12 = 0.0f; ret._13 = 0.0f; ret._14 = 0.0f;
ret._21 = 0.0f; ret._22 = 1.0f; ret._23 = 0.0f; ret._24 = 0.0f;
ret._31 = 0.0f; ret._32 = 0.0f; ret._33 = 1.0f; ret._34 = 0.0f;
ret._41 = 20; ret._42 = 20; ret._43 = 20; ret._44 = 1.0f;

D3DMATRIX ret2;
ret._11 = 1.0f; ret._12 = 0.0f; ret._13 = 0.0f; ret._14 = 0.0f;
ret._21 = 0.0f; ret._22 = 1.0f; ret._23 = 0.0f; ret._24 = 0.0f;
ret._31 = 0.0f; ret._32 = 0.0f; ret._33 = 1.0f; ret._34 = 0.0f;
ret._41 = 150; ret._42 = 20; ret._43 = 60; ret._44 = 1.0f;

// all rendering operations (DrawPrimitive, etc) must be
// held between BeginScene() and EndScene()
if( FAILED( pd3dDevice->BeginScene() ) )
return E_FAIL;

pd3dDevice->SetTransform( D3DTRANSFORMSTATE_WORLD, &ret);

pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, D3DFVF_NORMAL,lpDots, DOT_VERTS, D3DDP_WAIT);

pd3dDevice->SetTransform( D3DTRANSFORMSTATE_WORLD, &ret2);

pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, D3DFVF_NORMAL,lpDots, DOT_VERTS, D3DDP_WAIT);

Ok, what that does is draw two dot fields, one translated differently from the other. Hope it helps.




------------------------------
*Large explosion consumes building, hero walks away unharmed* (wtf? why?) CUT

Share this post


Link to post
Share on other sites
Thanks, I believe I have enough info to start working. Thank you for all the time i''m sure you spent explaining the stuff to me. Much obliged

Share this post


Link to post
Share on other sites
Matrices are principally used to perform transformations. A matrix which translates (moves) something looks like this:

1 0 0 0
0 1 0 0
0 0 1 0
tx ty tz 1

tx is the amount to translate in the x axis, ty in the y axis, tz in the z axis. A matrix which will scale an object is as follows:

sx 0 0 0
0 sy 0 0
0 0 sz 0
0 0 0 1

sx is the scaling in the x axis, and so on...
There are three rotation matrices:

Rotation around the X axis:
1 0 0 0
0 COSa SINa 0
0 -SINa COSa 0
0 0 0 1

Rotation around the Y axis:
COSa 0 SINa 0
0 1 0 0
-SINa 0 COSa 0
0 0 0 1

Rotation around the Z axis:
COSa SINa 0 0
-SINa COSa 0 0
0 0 1 0
0 0 0 1


Where a is the angle of rotation.

One of the good points about matrices is you can multiply them together, and then the resulting matrix will have both transformations in it, in the order you multiplied. This is basically the way the view matrix works, you take a translation matrix and put the negative of the camera coordinates in there, then you take the negative of all the angles of the camera and do a rotation matrix for each one. You then multiply them all together, translation first, to get the view matrix. YOu then take all the vertices which are visible and multiply them by this matrix. All the points are now transformed so that the camera is at 0, 0, 0 and looking down the Z axis.

------------------------------
#pragma twice


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
To multiply a vertex by a matrix, D3D will take all the vertices and put them in little matrices like this:

x y z 1

Where x is the x coordinate, y is the y coordinate, and z is the z coordinate. It then multiplies these small matrices by the larger 4x4 transformation matrices.

------------------------------
#pragma twice


sharewaregames.20m.com

Share this post


Link to post
Share on other sites
Note that matrix multiplication is NOT commutative, i.e. multiplying it in one order will produce wholly different results from multiplying it in another (a * b != b * c); although, in my experience, it rarely makes a difference.

Share this post


Link to post
Share on other sites
Yo, guys, thanks for all the info. I couldn''t find ANYTHING ANYWHERE else that had the stuff on matrices that I was looking for. Using the knowledge you provided and some brain work, I was able to manipulate my first triangle in a 3D coordinate system!!! YES! Well, hopefully one day I will understand it all (lighting, textures...) and maybe even create an engine of my own (without the D3D API) . What A task that would be (for someone with my amount of knowledge at least). Well, thanks again for all the help! Hope to get help from you guys again.

Share this post


Link to post
Share on other sites
As far as I can tell, all D3D really does for you is hold the world, camera, and projection matrices, and does the actual pixel drawing. It automates some matrix stuff, but that''s really all it does.

Actually, D3D does one more very important thing for you. Look up Vertex Buffers. They will make your life 100% easier. Guaranteed or your money back

If you''re going to be doing this, take the DirectX manual''s advice - set up a good database for your objects . Don''t just slop them all together in a big array. Keep each object''s vertices separated, and preferably in a placeholder class. I wrote an entire d3dApp and d3dObject class that will do hierarchies, automated lighting, texturing, etc. I just say d3dApp.Render() and it will render each of its d3dObject''s in turn.

Good luck! I can tell you from experience, you''re gonna need it



~BenDilts( void );

Share this post


Link to post
Share on other sites
Thanks for the luck, anybody needs luck when learning ANYTHING in DirectX. Anyway, I was definitely going to create a model class, and probably a d3d capsul to control everything. I did that with DirectDraw, but then I stopped for many reasons, but mainly because, it brought you away from the API I was working with, and it made my code harder to understand for other people because the DDSURFACE class I made was not standard. It was good for portability and personal simplicity, but the functions were sometimes too broad, and created slowdowns in my program. However, I am certainly going to create a model class, as D3D is much more complex and needs such orgization and arrays and big masses like that are not standard to leave lying around. I will probably encapsulate my D3D Object into some sort of handeling device because engines need extreme orginization and D3D is MUCH more complicated than DirectDraw. Besides, you don''t make 3D engines portable, you make them complicated, organized, and GOOD. At least from what seems logical. Any more suggestions and help would still be appreciated, and thank you BeanDog for the last bit of advice. If I ever become a famous programmer (yeah right!) I''ll owe it all to you guys

Share this post


Link to post
Share on other sites