# Thread: To Draw a 3D Circle and Applying Texturing .

1. ## To Draw a 3D Circle and Applying Texturing .

Hi...

I want to draw a 3D Circle and applying texturing to it using OpenGL + VC++ [Esp in a MFC ActiveX Control]. Especially I want to texture the 3D Circle in a Square Shape Panel.So Please give me a fine solution to solve this problem.

Thanks,

Regards,
Balamurugan.

2. ## Re: To Draw a 3D Circle and Applying Texturing .

Here is some code that will draw you a circle (from this site):
Code :
```void DrawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float tangetial_factor = tanf(theta);//calculate the tangential factor

float x = r;//we start at angle = 0

float y = 0;

glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex

//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them

float tx = -y;
float ty = x;

x += tx * tangetial_factor;
y += ty * tangetial_factor;

}
glEnd();
}```

Yes, it is 2D. There is no such thing as a 3D circle. A circle is a 2D primitive only (can only exist with all points coplanar and still be called a circle).

Texturing the circle means computing texture coordinates for each vertex of the circle. That is where it gets tricky.

3. ## Re: To Draw a 3D Circle and Applying Texturing .

The Algorithm that MarkS give you draw only the outline of the circle. If you want a circle filed with a texture, you can use triangle fan. First, draw the vertex at the center of the circle. Then draw the vertex on the contour of the circle, use cos(angle)*radius for x and sin(angle)*radius for y. Since texture coordinates s and t are in the range [0 1] => s = (cos(angle)+1.0)*0.5 and t = (sin(angle)+1.0)*0.5 . The texture coordinate for the vertex at the center of the circle is (0.5,0.5).

Here is an unoptimized sample code that you can test:

Code :
```GLvoid draw_circle(const GLfloat radius,const GLuint num_vertex)
{
GLfloat vertex[4];
GLfloat texcoord[2];

const GLfloat delta_angle = 2.0*M_PI/num_vertex;

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBegin(GL_TRIANGLE_FAN);

//draw the vertex at the center of the circle
texcoord[0] = 0.5;
texcoord[1] = 0.5;
glTexCoord2fv(texcoord);

vertex[0] = vertex[1] = vertex[2] = 0.0;
vertex[3] = 1.0;
glVertex4fv(vertex);

for(int i = 0; i < num_vertex ; i++)
{
texcoord[0] = (std::cos(delta_angle*i) + 1.0)*0.5;
texcoord[1] = (std::sin(delta_angle*i) + 1.0)*0.5;
glTexCoord2fv(texcoord);

vertex[2] = 0.0;
vertex[3] = 1.0;
glVertex4fv(vertex);
}

texcoord[0] = (1.0 + 1.0)*0.5;
texcoord[1] = (0.0 + 1.0)*0.5;
glTexCoord2fv(texcoord);

vertex[2] = 0.0;
vertex[3] = 1.0;
glVertex4fv(vertex);
glEnd();

glDisable(GL_TEXTURE_2D);

}```

Notice that only the portion of a circle inside the square texture image will be displayed to avoid deformation.

4. ## Re: To Draw a 3D Circle and Applying Texturing .

Originally Posted by trinitrotoluene
The Algorithm that MarkS give you draw only the outline of the circle. If you want a circle filed with a texture, you can use triangle fan.
Correct. I didn't finish the post before hitting submit.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•