Jump to content

  • Log In with Google      Sign In   
  • Create Account

drawing circle in directx


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 lcy   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 July 2004 - 10:49 PM

how to draw circle in directx ?

Sponsor:

#2 Kambiz   Members   -  Reputation: 758

Like
0Likes
Like

Posted 26 July 2004 - 10:54 PM

Use an alpha texture!


#3 Pipo DeClown   Members   -  Reputation: 804

Like
0Likes
Like

Posted 26 July 2004 - 11:02 PM

A linestrip with pre-calculated points.
I'm not back. I'm just visiting.

#4 __Daedalus__   Members   -  Reputation: 480

Like
0Likes
Like

Posted 26 July 2004 - 11:17 PM

Solid or line circle? Either way just use a textured quad. Use the DirectX SDK Texture Tool to create a .dds file that contains a texture of your circle and an alpha map that shows what parts of the texture are transparent. Load this .dds file to a texture that has the format D3DFMT_A8R8G8B8, enable alpha blending, render and *bam* you have a circle.

#5 Teric   Members   -  Reputation: 204

Like
0Likes
Like

Posted 27 July 2004 - 03:59 AM

Here's what I've done:

//-----------------------------------------------------------------------------------
//Draw a simple circle centered around the given point, using the given
//radius and color. This is in Screen Space (2D).
//-----------------------------------------------------------------------------------
void Draw2DCircle(POINT pt, float radius, D3DCOLOR color, LPDIRECT3DDEVICE9 dev)
{
const int NUMPOINTS = 24;
const float PI = 3.14159;

D3DTLVERTEX Circle[NUMPOINTS + 1];
int i;
float X;
float Y;
float Theta;
float WedgeAngle; //Size of angle between two points on the circle (single wedge)

//Precompute WedgeAngle
WedgeAngle = (float)((2*PI) / NUMPOINTS);

//Set up vertices for a circle
//Used <= in the for statement to ensure last point meets first point (closed circle)
for(i=0; i<=NUMPOINTS; i++)
{
//Calculate theta for this vertex
Theta = i * WedgeAngle;

//Compute X and Y locations
X = (float)(pt.x + radius * cos(Theta));
Y = (float)(pt.y - radius * sin(Theta));

Circle[i] = CreateD3DTLVERTEX(X, Y, 0.0f, 1.0f, color, 0.0f, 0.0f);
}

//Now draw the circle
dev->SetFVF(D3DFVF_TL);
dev->SetTexture(0, NULL);
dev->DrawPrimitiveUP(D3DPT_LINESTRIP, NUMPOINTS, &Circle[0], sizeof(Circle[0]));

}//Draw2DCircle


Edit - Coder: Used source instead of code tags. The code block is large enough, and it's much more clear now [smile]

[Edited by - Coder on July 27, 2004 11:59:34 AM]

I am always open to feedback, positive or negative...

#6 lcy   Members   -  Reputation: 100

Like
0Likes
Like

Posted 27 July 2004 - 11:15 PM

thank for reply
Teric the program got error
i am try to draw circle with wireframe in directx 9
i want the calculation of the circle in directx


#7 Teric   Members   -  Reputation: 204

Like
0Likes
Like

Posted 28 July 2004 - 02:18 AM

What was the error? Yes, my above code draws a wireframe circle (just the circumference, not filled) in DirectX 9.

EDIT: I just looked again, and I saw that there are a couple of things in there that need explanation. First is the CreateD3DTLVERTEX function:

struct D3DTLVERTEX
{
float fX;
float fY;
float fZ;
float fRHW;
D3DCOLOR Color;
float fU;
float fV;
};

//-----------------------------------------------------------------------------------
//CreateD3DTLVERTEX--Populate a D3DTLVERTEX structure
//-----------------------------------------------------------------------------------
D3DTLVERTEX CreateD3DTLVERTEX (float X, float Y, float Z, float RHW,
D3DCOLOR color, float U, float V)
{
D3DTLVERTEX v;

v.fX = X;
v.fY = Y;
v.fZ = Z;
v.fRHW = RHW;
v.Color = color;
v.fU = U;
v.fV = V;

return v;
}//CreateD3DTLVERTEX



And the other thing that needs explanation: D3DFVF_TL. This is a #define that I put together:

#define D3DFVF_TL (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)



#8 AhmedSaleh   Members   -  Reputation: 155

Like
0Likes
Like

Posted 28 July 2004 - 04:48 AM

bool mrScreen::DrawCircle (float iCenterX, float iCenterY,
float iRadius, Unsigned int iRed,
Unsigned int iGreen, Unsigned intiBlue,
Unsigned int iAlpha, Unsigned intiVertices)
{

// the Algorithm is simple , a circle is a set of points and has a center and a radius ..etc

Unsigned int iColor;
iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);
Vertex * pkVertices;
/* Allocate needed vertices */
pkVertices = new Vertex [iVertices + 1];

float fAngle = 0;
float fComplete;
floatiVertex;

/* Calculate each vertex position */
for (iVertex = 0; iVertex < iVertices; iVertex ++)
{
/* Percentage of circle already drawn */
fComplete = (float)iVertex / (float)iVertices;
pkVertices [iVertex].m_fX = (float) ((float)iCenterX +
((float)iRadius * cos (6.2831f*fComplete)));
pkVertices [iVertex].m_fY = (float) ((float)iCenterY +
((float)iRadius * sin (6.2831f*fComplete))); // Rx=r*sin(theta)

pkVertices [iVertex].m_fZ = 0; // We Are in 2D Space !
pkVertices [iVertex].m_fRHW = 1.0f; // Transformed Vertices
pkVertices [iVertex].m_iColor = iColor; // Your Circle Colour/Color
pkVertices [iVertex].m_ftU = 0; // U's Texture Co-ordinate
pkVertices [iVertex].m_ftV = 0;// V's Texture Co-ordinate
}

YourDevice->SetVertexShader (D3DFVF_MIRUSVERTEX);
YourDevice->DrawPrimitiveUP (D3DPT_LINESTRIP, iVertices,pkVertices, sizeof (Vertex)); // Draw The Circle!

delete [] pkVertices; // After Drawing,Free the Vertex Buffer
return true;
}


Ahmed Saleh.
Edited by Coder: code too wide, used source tags instead.

[Edited by - Coder on July 29, 2004 3:48:35 AM]
Game Programming is the process of converting dead pictures to live ones .

#9 OrcishCoder   Members   -  Reputation: 176

Like
0Likes
Like

Posted 29 July 2004 - 01:04 AM

Hi Teric,
dev->SetFVF(D3DFVF_TL); /<- Only found in DX9? How about DX8?

Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks


#10 Pipo DeClown   Members   -  Reputation: 804

Like
0Likes
Like

Posted 29 July 2004 - 01:17 AM

Quote:
Original post by OrcishCoder
Hi Teric,
dev->SetFVF(D3DFVF_TL); /<- Only found in DX9? How about DX8?

Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks


Dev8->SetVertexShader();
I'm not back. I'm just visiting.

#11 Mushu   Members   -  Reputation: 1396

Like
0Likes
Like

Posted 29 July 2004 - 10:40 AM

Quote:
Original post by OrcishCoder
Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks

An arc is a portion of a circle or elipse. An elipse is a circle with the radius biased in either the x- or y-axis.

To draw an elipse, bias like this:

pkVertices [iVertex].m_fX = (float) ((float)iCenterX +
((float)iRadius * xBias * cos (6.2831f*fComplete)));
pkVertices [iVertex].m_fY = (float) ((float)iCenterY +
((float)iRadius * yBias * sin (6.2831f*fComplete)));


To draw an arc, only calculate vertices in the area you need (ie, calculate positions in the loop for only a subset of those in the circle)

Or you could do everything my way and precalculate everything so you don't have to worry about the math .

#12 nhatkthanh   Members   -  Reputation: 334

Like
0Likes
Like

Posted 29 July 2004 - 08:18 PM

look up for Bresenham's circle algorithm. You basically draw 1/4 of the circle and flip it 3 more times to complete the circle. This is the code that I wrote a while back for a class, just replace the drawpixel routine to a specific API to plot a pixel for a given screen coordinate.

attempting to post code.
<span class="cpp-comment">//Bresenham's circle algorithm</span>
<span class="cpp-keyword">void</span> drawCircle ()
{
	<span class="cpp-keyword">int</span> tx,ty;
	<span class="cpp-keyword">int</span> xp = <span class="cpp-number">0</span>;
	<span class="cpp-keyword">int</span> yp = rad;
	<span class="cpp-comment">//swap(centerX,centerY);</span>
	<span class="cpp-keyword">double</span> d = <span class="cpp-number">1</span>-rad;
	<span class="cpp-keyword">while</span>(xp <= yp)
	{
		
		tx = xp+centerX;
		ty = yp+centerY;
        
		<span class="cpp-comment">//second octant</span>
		drawPixle(tx,ty,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//third octant</span>
		drawPixle(xp-<span class="cpp-number">2</span>*xp+centerX,ty,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//7th octant</span>
		drawPixle(tx,yp-<span class="cpp-number">2</span>*(yp)+centerY,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//first octant</span>
		drawPixle(yp+centerX,xp+centerY,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//8th octant</span>
		drawPixle(yp+centerX,xp-<span class="cpp-number">2</span>*xp+centerY,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//sixth octant</span>
		drawPixle(-<span class="cpp-number">1</span>*xp+centerX,-yp+centerY,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//4th octant</span>
		drawPixle(-yp+centerX,(-xp+<span class="cpp-number">2</span>*xp)+centerY,<span class="cpp-number">1</span>);
		<span class="cpp-comment">//5th octant</span>
		drawPixle(-yp+centerX,-xp+centerY,<span class="cpp-number">1</span>);

		<span class="cpp-comment">//go E</span>
		<span class="cpp-keyword">if</span>(d < <span class="cpp-number">0</span>.<span class="cpp-number">0</span>)
		{
			d = d + <span class="cpp-number">2</span>*xp + <span class="cpp-number">3</span>;
			xp = xp+<span class="cpp-number">1</span>;
		}
		<span class="cpp-keyword">else</span>	<span class="cpp-comment">//go SE</span>
		{
			d = d + <span class="cpp-number">2</span>*xp - <span class="cpp-number">2</span>*yp + <span class="cpp-number">5</span>;
			xp = xp+<span class="cpp-number">1</span>;
			yp = yp - <span class="cpp-number">1</span>;
		}
	}
}


#13 nhatkthanh   Members   -  Reputation: 334

Like
0Likes
Like

Posted 29 July 2004 - 08:19 PM

hmmm ok, can someone tell me how to put code in the code tag?

thanks.

#14 FoxHunter2   Members   -  Reputation: 157

Like
0Likes
Like

Posted 29 July 2004 - 08:25 PM

use the [*source*] tag ;) (without asterisks)

#15 nhatkthanh   Members   -  Reputation: 334

Like
0Likes
Like

Posted 29 July 2004 - 08:33 PM

thanks, I was close though :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS