• ### Announcements

#### Archived

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

# Direct3D newbie...

## Recommended Posts

IOFILE    122
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 on other sites
IOFILE    122
Please, anybody. I really need help

##### Share on other sites
Solarbeam    122
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 on other sites
IOFILE    122
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 on other sites
Solarbeam    122
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 on other sites
IOFILE    122
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 on other sites
furby100    102
Matrices are principally used to perform transformations. A matrix which translates (moves) something looks like this:
1  0  0  00  1  0  00  0  1  0tx 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  00  sy 0  00  0  sz 00  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     00     COSa  SINa  00     -SINa COSa  00     0     0     1Rotation around the Y axis:COSa  0     SINa  00     1     0     0-SINa 0     COSa  00     0     0     1Rotation around the Z axis:COSa  SINa  0     0-SINa COSa  0     00     0     1     00     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

##### Share on other sites
furby100    102
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

##### Share on other sites
Solarbeam    122
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 on other sites
furby100    102
It makes a difference when mixing translations and rotations, that''s to be sure. Always rotate first!

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

Solarbeam    122
true

##### Share on other sites
IOFILE    122
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 on other sites
BeanDog    1065
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 );