Original Post
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.