• Advertisement
Sign in to follow this  

Most Optimized way to draw a circle DirectX

This topic is 2147 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, I've made a 2D scale model of the solar system, it shows the orbits, which is a circle i draw. There is a circle being drawn for each planet, as I'm adding more features the program is getting extremely laggy sometimes due to all of the circles. I'm looking for a more optimized way to draw circles.

The way I'm doing it now is HIGHLY unoptimized I'm pretty sure, I'm using D3DXLine the circle gets drawn with however many lines i tell it. For planets that are farther away i have to give it a whopping 100 sides (D3DXlines) to draw separately for the circle to not show straight edges.

I KNOW there's a better way to do it than the way I am


void DrawCircle( const Vector2 &Center,float Radius,Color color,unsigned short Sides /*= 30*/ )
{
float Angle = ToRadian(360.0f/Sides);

float Cos = cos(Angle);
float Sin = sin(Angle);

Vector2 vec(Radius,0);

for(unsigned short i = 0;i < Sides;++i)
{
Vector2 rot( Cos*vec.x - Sin*vec.y , Sin*vec.x + Cos*vec.y );
rot += Center;
vec += Center;
DrawLine(vec,rot,color);
vec = rot - Center;
}
}
void DrawLine( const Vector2 &Point1,const Vector2 &Point2,Color &color,unsigned short BorderWidth /*= 1*/ )
{
Vector2 points[2] = { Point1,Point2 };

d3dLine->Begin();

d3dLine->Draw(points,2,color);

d3dLine->End();
}

// Anyone have a better way to draw a circle in Direct X, a 2D simulation of the Solar System should'nt lag like mine does
// As soon as I stop drawing the circles, the program is runs smoothly no lag at all
Edited by Muzzy A

Share this post


Link to post
Share on other sites
Advertisement
I'm always astonished that the latest platforms on the latest computers still offer opportunities for 286-like performance. ;)

I'm not a D3D expert, but this thread might help:
http://www.gamedev.net/topic/259860-drawing-circle-in-directx/

I'm guessing the speed problem is because you're doing many drawcalls in separate batches, e.g. each line drawn starts with d3dLine->Begin() and ends with d3dLine->End(). The sample code in that forum post uses a D3DPT_LINESTRIP, which is probably a more optimal way to draw a series of lines.

Share this post


Link to post
Share on other sites
Thanks all look into that when i get home tomorrow, but for now bed. night, thanks!

Share this post


Link to post
Share on other sites
Yeah, 100 lines is nothing, batch them up!

Drawing a circle as a bunch of lines is actually nothing strange and standard practice for drawing curves on graphics hardware.

You probably will see a big improvement by just moving the "Begin" and "End" to outside of the loop.
You could even put the Begin and End outside of the loop drawing all your circles.

Next step is building the vertex buffer from all the points in the circle and, as jeffery suggests, send them all in with a single Draw-call.

Share this post


Link to post
Share on other sites

vertex arrays

ah you reminded me of something

ID3DLine::Draw(); takes a list of vectors, and the 2nd parameter is how many vectors there are in the list. I can make alot of use out of that. Thanks.


Yeah, 100 lines is nothing, batch them up!

Drawing a circle as a bunch of lines is actually nothing strange and standard practice for drawing curves on graphics hardware.

I actually assumed that the way I was doing it was not really a good way to go, Good to know I was in the right direction with it =D. I don't know why I couldn't notice that doing a 'begin()' and 'end()' over and over to draw a circle wasn't good. It's like having a word on the tip of your tongue and you just can't get it lol.

Thanks for the support guys - Muzzy A

Share this post


Link to post
Share on other sites
One thing you can do to greatly optimize this little piece of code is to create the vertices for a circle beforehand.

make the circle have a radius of 1 with the center at (0,0), then use matrix transformations to move and scale the circle.



#define CircleResolution 100

static Vector2 gCircleData[CircleResolution];

void InitCircle()
{
for (int i = 0; i < CircleResolution; ++i)
{
gCircleData.x = cos(2 * PI * i / CircleResolution);
gCircleData.y = sin(2 * PI * i / CircleResolution);
}
}

// then to draw your cirlce
void DrawCircle( const Vector2 &Center,float Radius,Color color)
{
// save word matrix transformation
// set worldtransform = TranslateMatrix(Center) * ScaleMatrix(Radius) * currentWordTransform

d3dLine->Begin();

d3dLine->Draw(gCircleData, CircleResolution,color);

d3dLine->End();

// restore previous world transform

}


By doing it this way you avoid calculating sin/cos all the time which can be expensive. If you want to use lower resolution circles for smaller circles I would have a few premade circles at different resolutions and you choose from those few. Anyway, those are my two cents.


By doing it this way you

Share this post


Link to post
Share on other sites
I haven't used D3D in many years, but last time I used it, I recall that the UP (user pointer to CPU memory... which would often mean a copy from CPU to GPU RAM every time you call DrawPrimitiveUP) functions were inherently not as fast as using a vertex buffer and non-UP functions. I assume that this ID3DXLine::Draw function that takes a user pointer to CPU memory would suffer from the same "problem".

You asked for the most optimal... like ____ hints at, if your circles are static from frame to frame, then stuff them all into a vertex buffer / index buffer pair and go to town (yes, an index buffer does make a difference in performance). This way you only transfer the data across the bus once (and well, once again whenever the device is lost and regained, but you get the idea). That would likely be the most optimal, if you want to get nitpicky. smile.png This is all assuming that the latest D3D still has vertex / index buffers. Edited by taby

Share this post


Link to post
Share on other sites
This is all assuming that the latest D3D still has vertex / index buffers.[/quote]
It does, and furthermore starting from D3D10, devices can no longer be lost (they can, however, be removed, such as when a laptop switches between two graphics cards or your graphics driver fails, but recovery is easier as you pretty much just go through the whole graphics initialization code again instead of picking specific objects to recreate).

Note that for D3D9 which is being used here, I'm pretty sure a vertex/index buffer solution would be good enough. But it depends on what you use the circles for - perhaps it is, in fact, more efficient to simply render a quad and draw a texture of a circle over it (especially if you are already drawing a texture on top of the circle). Edited by Bacterius

Share this post


Link to post
Share on other sites

Note that for D3D9 which is being used here, I'm pretty sure a vertex/index buffer solution would be good enough. But it depends on what you use the circles for - perhaps it is, in fact, more efficient to simply render a quad and draw a texture of a circle over it (especially if you are already drawing a texture on top of the circle).


Ya know, that's actually pretty brilliant!

Share this post


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

  • Advertisement