#### Archived

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

# Direct3D newbie...

This topic is 6490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

##### 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 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 on other sites
Um, ok, here we go. Heres a little code to demonstrate how to use matrices...

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
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
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
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
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
It makes a difference when mixing translations and rotations, that''s to be sure. Always rotate first!

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

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002055
×