drawing a 2D torus (basically a circle with a hole in it).

Started by
2 comments, last by Enerjak 11 years, 1 month ago

I've been trying to render a torus (in 2D) in OpenGL and Well, I think I'm on the right track but I thought I'd ask you how to go about getting it to work. here's the code:


// attempting to create a torus (in 2D)
void createRing(double radius,
				double radius2,		
				int numSegments, 
				double x, 
				double y)
{
	double mAngle = 0.0;
	double mAngle2 = 0.0;
	glBegin(GL_POLYGON);
	for(int i = 0; i < numSegments; i++)
	{
		mAngle = i * (2 * 3.14/numSegments);
		mAngle2 = i * (2 * 3.14/numSegments);
		glVertex2d(x + radius * cosf(mAngle), y + radius * sinf(mAngle));
		glVertex2d(x + (radius + radius2 * cosf(mAngle2)) * cosf(mAngle2),
				y + (radius + radius2 * sinf(mAngle2)) * sinf(mAngle2));
	}
		
	glEnd();
}

now here's the polygon this produces:

http://puu.sh/2j6uh

please let me know if there's anything I need to do to fix this.

Advertisement

Looks like you are on the right track for the ring. Does increasing your number of segments give you a smoother looking ring?

The shape you're trying to draw is not a convex polygon, so using GL_POLYGON is wrong. I think GL_TRIANGLE_STRIP is a better fit. In switching to a triangle strip you should change the 'for' condition to <=.

Also, your second glVertex2d call looks very wrong to me. It should be exactly the same as the first, but using radius2 instead of radius (or (radius + radius2) depending on what you want radius2 to mean).

thanks, I can always count on the fine people on this forum!

This topic is closed to new replies.

Advertisement