Rotating triangle

Started by
2 comments, last by Aardvajk 10 years ago

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:


struct CUSTOMVERTEX
{
    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 )
{
CUSTOMVERTEX pLine[3];

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;

HRESULT hRet = D3D_OK;

DWORD dwFvF;
Dev->GetFVF( &dwFvF );
Dev->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );

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.

Advertisement

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

Dev->SetTransform(...)

EDIT

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

There is an article:

http://stackoverflow.com/questions/8788925/how-to-rotate-an-object-in-xyz-axes-on-directx

/EDIT

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

Given enough eyeballs, all mysteries are shallow.

MeAndVR

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

This topic is closed to new replies.

Advertisement