Original Post
how to draw circle in directx ?
//-----------------------------------------------------------------------------------//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 = 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]));}//Draw2DCirclestruct 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#define D3DFVF_TL (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)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/ColorpkVertices [iVertex].m_ftU = 0; // U's Texture Co-ordinatepkVertices [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 Bufferreturn true;}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
Quote:
Original post by OrcishCoder
Another question to all: How to draw Arc and Eplise in DX8 or above?
Thanks
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)));[SOURCE]//Bresenham's circle algorithmvoid drawCircle (){ int tx,ty; int xp = 0; int yp = rad; //swap(centerX,centerY); double d = 1-rad; while(xp <= yp) { tx = xp+centerX; ty = yp+centerY; //second octant drawPixle(tx,ty,1); //third octant drawPixle(xp-2*xp+centerX,ty,1); //7th octant drawPixle(tx,yp-2*(yp)+centerY,1); //first octant drawPixle(yp+centerX,xp+centerY,1); //8th octant drawPixle(yp+centerX,xp-2*xp+centerY,1); //sixth octant drawPixle(-1*xp+centerX,-yp+centerY,1); //4th octant drawPixle(-yp+centerX,(-xp+2*xp)+centerY,1); //5th octant drawPixle(-yp+centerX,-xp+centerY,1); //go E if(d < 0.0) { d = d + 2*xp + 3; xp = xp+1; } else //go SE { d = d + 2*xp - 2*yp + 5; xp = xp+1; yp = yp - 1; } }}[/SOURCE]This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more