Sorry for the somewhat newb question

Started by
3 comments, last by sathenzar 19 years, 2 months ago
Hey everyone, sorry to bog this forum down with this newb posting, but I really don't understand these matrix functions. First off, I created a cube with the vertex buffer, no problems there. Then I moved the cube with the following code in the render function

D3DXMATRIX matTranslate, matFinal;
			D3DXMatrixIdentity(&matFinal);
			D3DXMatrixTranslation(&matTranslate, 0.0f, 0.0f, 0.0f);
			D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
			pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

How does the cube know that you're telling it to move? All I see is functions that calculate matrix values. I don't see any movment assigned to the actual cube. Here's the complete render code below.

// Clear the backbuffer to a black color
			pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );

			pd3dDevice->BeginScene();
			D3DXMATRIX matTranslate, matFinal;
			D3DXMatrixIdentity(&matFinal);
			D3DXMatrixTranslation(&matTranslate, 0.0f, 0.0f, 0.0f);
			D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
			pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);
			pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
			pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
			
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
			pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
			
			pd3dDevice->EndScene();

			// Present the backbuffer contents to the display
			pd3dDevice->Present( NULL, NULL, NULL, NULL );

Again, sorry and thanks for the help in advance.
Advertisement
Quote:I don't see any movment assigned to the actual cube.

Change the values in the D3DXMatrixTranslation function to move the cube.

Also:
Since the SetTransform method requires a initialized matrix, you can substitute:

D3DXMatrixIdentity(&matFinal);
D3DXMatrixTranslation(&matTranslate, tx , ty , tz );
D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

by:

D3DXMatrixTranslation(&matTranslate, tx , ty , tz );
pd3dDevice->SetTransform(D3DTS_WORLD, &matTranslate);

[Edited by - adriano_usp on February 9, 2005 8:26:29 PM]
hey man.

you are setting the world transform here:

pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

from this, DX's default vertex shader will deal with that matrix matFinal to transform the vertices ( you don't need to know more about shaders for now, just understand they magically do work for you ).

of course, you'll have to set the view and projection matrices too if yer gonna turn the camera around n stuff :)

also, the D3d matrix translation function will move your object around, but i believe it blows away the other elements of that matrix, so any rotations are lost ( i think i remember it being that way ).

we always just write functions to work with ._41, ._42, ._43
yes you are correct... u r not giving the cube any instruction. the thing is that you are telling DirectX to transform (translate and/or rotate) whatever it is about to draw through this line:

pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

YOU PASS THE MATRIX TO DIRECT3D

So here's what you do:

1. you first create a cube through the vertex buffer
2. Then you calculate some matrices. )Remember, that matrices are used to position and orient objects in 3D space)
3. Then you pass the matrix you have just calculated to Direct3D
4. Henceforth any thing that you draw will be transformed by the matrix that you passed to Direct3D

ok that makes sense now. Thanks for helping me on that, I appreciate it.

This topic is closed to new replies.

Advertisement