Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Matrices help

Started by n000b Oct 16, 2005 at 3:55 AM 3 replies 900+ views
Original Post
n000b
n000b
Hi guys, I've just started looking in to DirectX programming. I was going through the DirectX SDK help (I've got the "Summer 2004" SDK), and I got up to "Tutorial 3: Using Matrices". This tutorial made no sense, so I decided to go looking around for a better tutorial on how to manipulate vertices. I looked around on here but couldn't find anything useful, so I turned to Google. Here I found two tutorials, both using the same (well, similar methods). I understand neither, so I stopped Googling for more because I figure if those two use the same method, the rest probably will too. So, onto my problem. The tutorial I found draws 3 triangles on to the screen - the left one is transformed, the middle one is rotated and the right is scaled. You can see the final result at http://members.iinet.net.au/~kilhams/d3dtut.jpg. The vertices are getting defined like so:
struct tVertex				// Our Vertex Struct
{
    float x, y, z;			// Vertex Position
    DWORD color;			// Color Value In HEX Format
};

tVertex triangle[3] = {		// Vertex Array That Holds Our Triangle

   {  0.0f,  1.0f, 0.0f, 0xFFF0000F },
   {  1.0f, -1.0f, 0.0f, 0xF00FF00F },
   { -1.0f, -1.0f, 0.0f, 0xF0000FFF },
};
First thing I don't understand is, the values that are in this array. Doesn't an x value of "0.0f" mean that it is located at pixel number 0 on the X-axis, ie top left of the screen? Isn't "1.0f" pixel number 1 on the X-axis, etc? For a simple program that I wrote that just draws a triangle on to the screen, I passed in values of like 200, 300, etc and it put it 200px along the X-axis etc, if you get what I mean. I don't understand why it's different here? Ok, next thing. This code is from the Render(); function:
//NEW//////////////////NEW//////////////////NEW//////////////////NEW////////////////
	D3DXMATRIXA16 matWorld;			// World Matrix
	D3DXMATRIXA16 matRotate;		// Rotation Matrix
	D3DXMATRIXA16 matTranslation;	// Translation Matrix
	D3DXMATRIXA16 matScale;			// Rotation Matrix




	pD3DDevice->BeginScene();									// Begin Scene
				
	D3DXMatrixTranslation(&matWorld, -2.2f,fTranslate,5.0f);    // Translate The Left Triangle
	pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld );			// Set Transformation
	Draw_Triangle();											// Draw triangle

	D3DXMatrixRotationY(&matRotate, D3DXToRadian(fRotate));		// Rotate The Center Triangle
	D3DXMatrixTranslation(&matTranslation, 0.0f,0.0f,5.0f);		
	matWorld = matRotate * matTranslation;						// Multiply Matrices
	pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld );			// Set Transformation

	Draw_Triangle();											// Draw triangle

	D3DXMatrixScaling(&matScale, fScale, fScale, fScale);		// Scale with the same value in x,y,z direction
	D3DXMatrixTranslation(&matTranslation,  2.2f,0.0f,5.0f);	// Right Triangle
	matWorld = matScale * matTranslation;						// Multiply Matrices
	pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld );			// Set Transformation
	Draw_Triangle();											// Draw triangle

	pD3DDevice->EndScene();										// End Scene



	fTranslate += 0.005f;
	fRotate    -= 0.5f;
	fScale     -= 0.005f;

	if(fTranslate > 0.5f) fTranslate = 0.0f;
	if(fScale < 0.5f)     fScale     = 1.0f;
The function that is referenced to a few times above, Draw_Triangle();, is this:
void Draw_Triangle() // This function draws a triangle with RGB colors
{
	pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
	pD3DDevice->SetStreamSource(0,pVertexBuffer, 0, sizeof(tVertex));
	pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
}
Now, I can see in the above code that it is essentially broken up in to three bits; one for each of the triangles. But I cannot for the life of me see how each of those bits of codes actually references to each triangle. What I mean is, I can't see how the first bit of code makes the first triangle transform, rather than modifying them all (well, I can't even see why it makes any of the triangles change). eg, where in this following code does it actually say to transform any vertices?
	D3DXMatrixTranslation(&matWorld, -2.2f,fTranslate,5.0f);    // Translate The Left Triangle
	pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld );			// Set Transformation
	Draw_Triangle();											// Draw triangle
All I can see is that it is modifying a matrice, then drawing the triangle using the vertices defined at the top. And finally, it is using the one array of three vertices to draw all three triangles - why aren't these three triangles on top of eachother if they are all using the same co-ordiates for the vertices? Hopefully you guys all understand what I'm talking about :) I've uploaded the full project code to http://members.iinet.net.au/~kilhams/d3dtut.txt if you need to check anything else out.
haegarr
haegarr
First, there are tons of tutorials on matrix (and other) 3D transformations available. Also here in GDnet in section Articles / Math and Physics / Matrices ... e.g. use http://www.gamedev.net/reference/articles/article695.asp
Please look there first.

Next, you have to distinguish several co-ordinate systems. The triangles are given in one (usually a "model local" system), the models are given in another (e.g. the "world"), the camera (describing what you see) defines the viewing CSys, and the window/screen defines a pixel co-ordinate system. (Others there are as well but need not mentioned here.) So co-ordinates of triangles need not (and usually do not) have anything to do with pixel co-ordinates directly.

Assume you describe the location of a chair relative to a special location of the room the chair is in, say the lower-left-behind corner of the room. The location of the room itself could be described relative to the house it is part of. The house of the city ... an so on. These are all co-ordinate systems, in this case one inside the other. A table besides the chair could also be given in the same co-ordinate system of the room.

If you shot a photo of the chair, you will probably inside the same room, so it would no problem to describe the camera's location relative to the room, and hence can the chair's location be described relative to the camera (this is called the model-view-transformation).

Pixel co-ordinates are like the color particles on the shot photo. They show the triangles of the chair, in correct arrangement, also the chair is given originally in co-ordinates of the room.

So, the co-ordinates of your tris will _not_ describe pixel positions. You should think of these theoretical things first.
LosBrutalos
LosBrutalos
a x-value 1.0f does not mean the first pixel on the left side, cause you do a 3D transformation, where any point depends on
1. the objects world matrix
2. the camera position and therefor the resulting
perspective and view matrix...

Have a look in the first dx sdk samples...

cu

Los Brutalos(Sorry for my brutal english ;)
n000b
n000b
Quote:
Original post by haegarr
First, there are tons of tutorials on matrix (and other) 3D transformations available. Also here in GDnet in section Articles / Math and Physics / Matrices ... e.g. use http://www.gamedev.net/reference/articles/article695.asp
Please look there first.


Yeah I read that article already. I understand what matrices are and stuff, I just don't understand how the code is using them to manipulate the triangles.



Ok, so if there are differen't co-ordinate systems, how do I define which one is in use? From what I can see, the bits that draw the triangles in this tutorial are basically the same as what I've done in my little program, except they are obviously using two different co-ordinate systems - where is this defined?

And I still don't understand where the specific code is actually saying to modify the triangle.
LosBrutalos
LosBrutalos
Hy again:

Principle:

First you have to define a (virtual) camera.
It has its position, view direction and view angle (most times called as field of view (FOV)), then you´ve got a near and a far plane, where you define the scene clipping in front and in the far.
With this camera you can calculate the cameras view matrix and perspective matrix.
Then you´ve got a object wich you have to place into the scene.
Its a good idea (for the first tests) to place your object at the
world coordinates (0,0,0) first.
Then, depends on your needs, you have to place a light into the scene.
This light has (depends of the type of light) a position, a color, a range ,
attenuation values and in case of a spot light a direction.

Then you have to calculate the following matrices:

1. The world matrix (this should be a identity-matrix for first tests)
2. The view - and 3. the perspective matrix are calculated from the position,
the FOW and the view direction of the camera.

Then you have to feed the d3ddevice with theese 3 matrices.
You have to set up the light into the device, if you need.
Also you have to define a material, wich should be setted into
the device too...
If you experiments with simple triangles its sometimes a good idea
to set renderstate.culling to None if youre not sure with the
view direction and so on.

Have a look in the INet, there are tons of beginner samples and
code snippets...





Los Brutalos(Sorry for my brutal english ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.