[MDX] Create and draw a circle or ellipse

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

Recommended Posts

So, basicly my problem is that I don't have a clue how to make round objects with DirectX. I have used vertexBuffer so far for drawing stuff on screen (triangles, squares...) but it would require huge amounts of vertixes with this technique to make something round. There is a example code I used to make a triangle. Anyone got some tutorials or tips how to make these circles or ellipses?
vertexBuffer = Direct3D.VertexBuffer.CreateGeneric( _
Of CustomVertex.TransformedColored) _
(device, _
CustomVertex.TransformedColored.StrideSize * 3, _
Usage.WriteOnly Or Usage.Points Or Usage.Dynamic _
Or Usage.DoNotClip Or Usage.SoftwareProcessing, _
CustomVertex.TransformedColored.Format, _
Pool.Default, Nothing)

Dim verts As Generic.GraphicsBuffer( _
Of CustomVertex.TransformedColored) = _
vertexBuffer.Lock( _
Of CustomVertex.TransformedColored) _
(0, 3, LockFlags.None)

' Triangle
verts.Write(New CustomVertex.TransformedColored(500, 500, 0, 1.0F, Color.Red))
verts.Write(New CustomVertex.TransformedColored(200, 500, 0, 1.0F, Color.Yellow))
verts.Write(New CustomVertex.TransformedColored(250, 250, 0, 1.0F, Color.Blue))



Share on other sites
Hello there, this is the code I use to draw circles. Ignore the "ShapeDescriptor" object. I'm using these "shapes" as the apperance of MDX UI controls. This code creates both the vertexbuffer and the related indexbuffer.

Circles
public static ShapeDescriptor DrawCircle(Vector2 center, float radius, int slices, Color color)		{            CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[slices + 2];			int[] indices = new int[slices * 3];			int col1;			float x, y;			x = center.X;			y = center.Y;			col1 = color.ToArgb();			float deltaRad = Geometry.DegreeToRadian(360) / slices;			float delta = 0;            vertices[0] = new CustomVertex.TransformedColored(x, y, 0, 1, col1);			for (int i = 1; i &lt; slices+2; i++)			{				vertices = new CustomVertex.TransformedColored(					(float)Math.Cos(delta) * radius + x,					(float)Math.Sin(delta) * radius + y,					0, 1, col1);				delta += deltaRad;			}			indices[0] = 0;			indices[1] = 1;			for (int i = 0; i &lt; slices; i++ )			{				indices[3 * i] = 0;				indices[(3 * i) + 1] = i + 1;				indices[(3 * i) + 2] = i + 2;			}			return new ShapeDescriptor(slices, vertices, indices);

Ellipses
public static ShapeDescriptor DrawEllipse(Vector2 center, int radius1, int radius2, float radFrom, float radTo, int slices, Color color)		{			CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[slices + 2];			int[] indices = new int[slices * 3];			int col1;			float x, y;			x = center.X;			y = center.Y;			col1 = color.ToArgb();			float deltaRad = radTo / slices;			float delta = radFrom;			vertices[0] = new CustomVertex.TransformedColored(x, y, 0, 1, col1);			for (int i = 1; i &lt; slices + 2; i++)			{				vertices = new CustomVertex.TransformedColored(										(float)Math.Cos(delta) * radius1 + x,					(float)Math.Sin(delta) * radius2 + y,					0, 1, col1);				delta -= deltaRad;			}			indices[0] = 0;			indices[1] = 1;			for (int i = 0; i &lt; slices; i++)			{				indices[3 * i] = 0;				indices[(3 * i) + 1] = i + 2;				indices[(3 * i) + 2] = i + 1;			}			return new ShapeDescriptor(slices, vertices, indices);        }

If the code is unclear, let me know.

Share on other sites
You're right, you have to make curved surfaces out of (a lot of) triangles. You can see this in many games where circular surfaces have noticeable vertices sticking out slightly.

If you want to generate them at run time, here's some psuedo code (assuming Radians for cos and sin):

i = 0
while i < numTriangles
makeVertex(cos(i/numTriangles*2*PI),sin(i/numTriangles*2*PI))
i = i + 1

There. numTriangles is how many triangles you want to have in the circle. In this, makeVertex() should just add a vertex to a list of the vertices for the circle. The part inside the cos() and sin() is just finding out what angle the vertex should be at. Cos() gets the x coordinates for that angle, and sin() gets the y coordinate. Note that this will get a radius of 1, you'll have to scale it for other sizes. And to make an elipse, just scale it along an axis.

HTH

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• Forum Statistics

• Total Topics
631746
• Total Posts
3002022
×