Jump to content
  • Advertisement
Sign in to follow this  

Rotating triangle

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello forum, sorry for a noob question, I know it's a basic stuff and all that. So I have read some tutorials about basics of DX and found some complete code to draw lines, rectangles, circles, text.., combined it in my own class and it works pretty well. But I can't get how to draw triangles using primitives. I have found some code, but one problem is that I can't rotate it. Here is the code I've found:


    FLOAT x, y, z, rhw;    // from the D3DFVF_XYZRHW flag
    DWORD tColor;    // from the D3DFVF_DIFFUSE flag

HRESULT DrawFilledTriangleEx( FLOAT PosX, FLOAT PosY, FLOAT Size, DWORD tColourStart, DWORD tColourMid, DWORD tColourEnd, IDirect3DDevice9* Dev )

pLine[0].x      = PosX;
pLine[0].y      = PosY;
pLine[0].z      = 1.0f;
pLine[0].rhw    = 1.0f;
pLine[0].tColor = tColourStart;

pLine[1].x      = PosX + Size;
pLine[1].y      = PosY;
pLine[1].z      = 0.0f;
pLine[1].rhw    = 1.0f;
pLine[1].tColor = tColourMid;

pLine[2].x      = PosX;
pLine[2].y      = PosY + Size;
pLine[2].z      = 0.0f;
pLine[2].rhw    = 1.0f;
pLine[2].tColor = tColourEnd;


Dev->GetFVF( &dwFvF );

hRet = Dev->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 3, pLine, sizeof(CUSTOMVERTEX) ); 

Dev->SetFVF( dwFvF );

return hRet;

Here is the result of a function : here


So on the screen red triangle is what I got after DrawFilledTriangleEx call, white is what I want to get. I've tried to play with function code, but triangle just disappears quickly. I know I should spend the time on reading or find something different to do,  but seriously everything I was able to find in examples it's just one colored triangle and no information on how to setup it's size or angle. Please sorry one more time.



Share this post

Link to post
Share on other sites

If I remember correctly from OpenGl you apply transformations before you draw the triangle.


I hope people will correct me if I am wrong, but it seems as if you modify a matrix with ...

D3DXMatrixRotationX(...), D3DXMatrixRotationY(...), D3DXMatrixRotationZ(...)


and then apply it with ...






or D3DXMatrixRotationYawPitchRoll(&matRotate, ry, rx, rz);!?

There is an article:





I'd expect the transformation outside the drawing function for the triangle ...

Edited by DareDeveloper

Share this post

Link to post
Share on other sites

It appears you're confusing terms. You use D3DPT_TRIANGLELIST in your draw call and you do, indeed, get a filled triangle. If you want to draw lines (which is what the white object in the image appears to be comprised of), you may want to read the docs and look at D3DPT_LINELIST. IIRC, you'll get lines 1 pixel in width.


As DareDeveloper mentions, you should create your vertex array just once somewhere and reuse it for rendering. As you've coded it, you create the array, fill it, render it and delete it (when the function returns), every time the draw function is called.

Edited by Buckeye

Share this post

Link to post
Share on other sites

One problem you'll have applying a rotation transform is that your triangle is defined with its world positions directly in the buffer. You need to have an object relative to its rotational centre for the rotation matrix to apply properly.


In other words, define the triangle in the vertex buffer relative to its own centre. Then create a rotation transform and also a translation transform (D3DXMatrixTranslation) that moves it to the right place. Then create your world transform by multiplying the rotation transform by the translation transform (order is important) to first rotate the triangle round its centre, then move it to its world position.


It is really, really worth spending some time studying matrix stuff at a high level i.e. not how they work but how you use them.

Edited by Aardvajk

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!