Results 1 to 4 of 4

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

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    5

    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. #2
    Member Contributor
    Join Date
    Jan 2001
    Posts
    82

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

  3. #3
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    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[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.

  4. #4
    Member Contributor
    Join Date
    Jan 2001
    Posts
    82

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

    Quote 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.

Similar Threads

  1. Replies: 4
    Last Post: 05-31-2016, 05:21 PM
  2. Texturing disappearing after applying shader
    By eyundubu in forum OpenGL: GLSL
    Replies: 4
    Last Post: 11-04-2012, 08:23 PM
  3. Texturing a Circle
    By Arkeshen in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-19-2012, 09:18 AM
  4. Drawing 3D SQUARE box and Applying Texturing
    By BalaMurugan in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-21-2009, 09:44 AM
  5. Draw Circle, MidPoint Circle Algorithm
    By Trivialnight in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-25-2004, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean