# Small DX question

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

## Recommended Posts

Hi, I'm reading about DX and I have a small question... Look this sample in OGL: glTranslatef( 0, 0,-5 ) glTranslatef( 1, 0, 0 ) RENDER OBJECT IN POSTION (1, 0, -5) Now my question is how can I do it DX? I made this code: D3DXMatrixTranslation( &projection_matrix, 0, 0, 5 ); Direct3D_device->SetTransform( D3DTS_WORLD, &projection_matrix ); D3DXMatrixTranslation( &projection_matrix, 1, 0, 0 ); Direct3D_device->SetTransform( D3DTS_WORLD, &projection_matrix ); RENDER OBJECT IN POSITION (1, 0, 0), NOT IN (1, 0, 5) In this code I set the translation in 0,0,5, then I set the translation in 1,0,0. Now I render the object in position 1,0,0 not in 1,0,5 like OGL, how can I solve this problem? Thanks!

##### Share on other sites
You have to multiply the matrices together for them to have the desired effect. There's two options: either multiply the matrices first and call SetTransform once - or - use SetTransform for the first matrix, and MultiplyTransform for the second (and any subsequent) matrix.

OpenGL does the multiplication automatically when you call glTranslatef. Both DX and OGL documentation discuss matrix multiplication in great detail.

Generally, as SetTransform takes relatively long for the CPU to execute, the best way is to multiply the matrices in your own application and call SetTransform as seldom as possible.

##### Share on other sites
What I saw, the DirectX only works with matrices (everything return a matrix value).

I found this small code:

D3DXMatrixMultiply(&matFinal, &matFinal, &matValue);

D3DXMatrixMultiply(&matFinal, &matFinal, &matValue);

d3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

First I set the value in a matrix, multiply the value then I set the tranformation...

Now I understood...

Thanks for the help!

##### Share on other sites
Now I made my test work, I have another quesion, now abou polygons, I can't create a square with DX, squares only with two triangles?

Other question is, in OGL I have glPushMatrix and glPopMatrix, what I have in DX?

Thanks again!

##### Share on other sites
You CAN create squares, but two triangles are faster I think.. Just render using trianglestrips.. :p

And those pop and push thingys.. Could possibly be a matrixstack? D3DX has those.. :p

##### Share on other sites
Quote:
 Original post by alex_rNow I made my test work, I have another quesion, now abou polygons, I can't create a square with DX, squares only with two triangles?Other question is, in OGL I have glPushMatrix and glPopMatrix, what I have in DX?Thanks again!

1) Squares can easily be formed from two triangles. If you want sprite/blitter style functionality, ID3DXSprite may be a good solution for you.

2) DirectX has an ID3DXMatrixStack interface which you can use to implement your transform hierarchy. The math involved is highly optimized to work with the cutting-edge processor architectures.

##### Share on other sites
Hi again, first I need to thanks everyone for the help, I made many progress in DX.

Now I have a problem here with DrawPrimitiveUP function, in MSDN I got this parameters info:

HRESULT DrawPrimitiveUP( D3DPRIMITIVETYPE PrimitiveType,
UINT PrimitiveCount,
CONST void* pVertexStreamZeroData,
UINT VertexStreamZeroStride
);

PrimitiveType
[in] Member of the D3DPRIMITIVETYPE enumerated type, describing the type of primitive to render.
PrimitiveCount
[in] Number of primitives to render. The maximum number of primitives allowed is determined by checking the MaxPrimitiveCount member of the D3DCAPS9 structure.
pVertexStreamZeroData
[in] User memory pointer to the vertex data. The vertex data must be in stream 0.
VertexStreamZeroStride
[in] The number of bytes of data for each vertex. This value may not be 0.

Direct3D_device->DrawPrimitiveUP( D3DPT_TRIANGLELIST, X, cVectorBufferFinal, sizeof(D3DVERTEX) );

My test is, try to make a piramid like NeHe tutorials, with four triangles...
Like what I read in some samples, X is the number of faces not the number of points like OGL, for examples, in my pyramid test X = 4, but if I like to render point (D3DPT_POINTLIST) X = 12...

This is right???

I don't get how this part work :-/

Thanks...

##### Share on other sites
I have a question,

First I made a matrix with values to create a cube, with triangle strips, with 24 vertex... This part is OK...
CubeVertex g_cubeVertices[] =
{
// Side #1 - red
{-1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },
{ 1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },
{-1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },
{ 1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },

// Side #2 - green
{-1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
{-1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
{ 1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },

// Side #3 - blue
{-1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },
{-1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },
{ 1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },

// Side #4 - yellow (red + green)
{-1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ) },
{-1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ) },
{ 1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ) },
{ 1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ) },

// Side #5 - magenta (red + blue)
{ 1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 1.0, 1.0 ) },
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 1.0, 1.0 ) },
{ 1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 1.0, 1.0 ) },
{ 1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 1.0, 0.0, 1.0, 1.0 ) },

// Side #6 - cyan (blue + green)
{-1.0f, 1.0f,-1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ) },
{-1.0f,-1.0f,-1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ) },
{-1.0f, 1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ) },
{-1.0f,-1.0f, 1.0f, D3DCOLOR_COLORVALUE( 0.0, 1.0, 1.0, 1.0 ) }
};

Now I need to render... Look this lines:
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 2 );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 8, 2 );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );

First I set the render mode (Triangle Strip), start vertex (0, 4, 8, 12, 16 and 20) and the end face (2, 2, 2, 2, 2 and 2)...
BUT WHY IS 2?!?!?!?!

##### Share on other sites
2 is not the end face, it's the number of triangles to draw. Since you're drawing each side separately each side is a square, and thus two triangles.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634118
• Total Posts
3015605
×