2 Replies - 1792 Views - Last Post: 21 May 2010 - 03:53 AM Rate Topic: -----

#1 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

OpenGL circle problem

Posted 20 May 2010 - 05:00 PM

Hi. I'm working on an exercise to create a polygon circle in openGL, the only problem is that I'm unsure how i go about doing a certain part of this function, the prototype is set as follows:
 void Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly ); 

Where the user tells the function how many segments they want in the circle, the problem I'm unsure on how to solve is how i make sure each vertic is placed correctly based on the center origin of the circle as a switch statement is not possible due to not having a constant for the segments, same goes for if statements, I've got the displacement change that need to be applied to each vertex stored in variables, but i need to update each as i go around the circle... say i had a radius of 10 with 8 segments, the angle from the center would be 45 degrees, meaning that if my first vertex after the origin was straight above so coordinates ( -10, 0 ) respectivly x and y axis, the next vertex placement would be of displacement from the previous by roughly 7 units on the x-axis and roughly 3 on the y but the next vertex would be only 3 on the x but 7 on the y. any ideas how i would go about this without knowing the amount of segments the user wants? Code so far is as follows:
void CGfxOpenGL::Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly ) 
{ 
        /* Init temp variables */ 
        double Change_angle = 360 / Segments; 
        double SeparationValue = sqrt( (radius*radius) + (radius*radius) - ( 2 * 10 * 10 * sin(Change_angle) ) );  
 
        /* displacement variables */ 
        double Tx, Ty; 
        float Nx, Ny; 
        Tx = radius * sin((double)Change_angle); 
        Ty = radius * cos((double)Change_angle); 
        Nx = (float)Tx; 
        Ny = radius - (float)Ty; 
 
        glBegin( GL_TRIANGLE_FAN ); 
        glVertex3f( x, y, z );          // Plot center point 
         
        for( int segs = 0; segs < Segments; segs++ ) 
        { 
                /* plot vertices based on current segment */ 
        } 
 
        glEnd(); 
 
}



Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL circle problem

#2 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

Re: OpenGL circle problem

Posted 21 May 2010 - 03:31 AM

anyone got any hints of how i should do this? I'm starting to think that with knowing the circle is semetrical to plot the vertices for 1 quarter of the circle and repeat the process for each section?
Was This Post Helpful? 0
  • +
  • -

#3 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

Re: OpenGL circle problem

Posted 21 May 2010 - 03:53 AM

I've sort of sorted it but there is just on question that I'm unsure of, when it draws the circle it doesn't fill the gap between the last vertex and the first, how does one go about getting with to close the circle to make it whole?

updated code is as follows:
void CGfxOpenGL::Draw2DCircle( float x, float y, float z, float radius, int Segments, bool edgeOnly )
{
	/* Init temp variables */
	#define DEG_TO_RAD 0.017453293f
	float Change_angle = 360 / (float)Segments;
	double SeparationValue = sqrt( (radius*radius) + (radius*radius) - ( 2 * 10 * 10 * sin(Change_angle) ) ); 

	if( edgeOnly )
	{}

	/* displacement variables */
	double Tx, Ty;
	float Nx, Ny;
	Tx = radius * sin((double)Change_angle);
	Ty = radius * cos((double)Change_angle);
	Nx = (float)Tx;
	Ny = radius - (float)Ty;

	glBegin( GL_TRIANGLE_FAN );
	glVertex3f( x, y, z );		// Plot center point
	
	for( int segs = 0; segs < Segments; segs++ )
	{
		glVertex3f( radius * (float)cos(segs * Change_angle * DEG_TO_RAD ), 0.0, radius * (float)sin(segs * Change_angle * DEG_TO_RAD ) );
	}
	glEnable( GL_LINE_LOOP );

	glEnd();

}


This post has been edited by Kain6622: 21 May 2010 - 05:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1