View Full Version : To Draw a 3D Circle and Applying Texturing .

BalaMurugan

12-11-2008, 03:35 AM

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.

MarkS

12-11-2008, 07:40 AM

Here is some code that will draw you a circle (from this (http://slabode.exofire.net/circle_draw.shtml) site):

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 radial_factor = cosf(theta);//calculate the radial 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;

//add the tangential vector

x += tx * tangetial_factor;

y += ty * tangetial_factor;

//correct using the radial factor

x *= radial_factor;

y *= radial_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.

trinitrotoluene

12-11-2008, 11:42 AM

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:

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_RE PLACE);

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[0] = std::cos(delta_angle*i) * radius;

vertex[1] = std::sin(delta_angle*i) * radius;

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[0] = 1.0 * radius;

vertex[1] = 0.0 * radius;

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.

MarkS

12-11-2008, 12:01 PM

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. :eek:

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.