Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rounded Corner Optimization

Started by mooreaa Feb 23, 2008 at 9:01 PM 0 replies 800+ views
Original Post
mooreaa
mooreaa
Hello, I wanted to share some code that I wrote that draws rectangles with rounded corners. It works pretty well but I though I'd put it up here and see if someone could give me some pointers on making it more efficient.

void draw()
{
	float size_x = 100;
	float size_y = 100;
	
	float radius = size_x/2.f * sliderObj2.percent;
	float factor = sqrt(5.f);

	GLfloat topleft[4][3] = {
		{0.f,		radius,		0.f},
		{0.f,		radius/factor,	0.f},
		{radius/factor,	0.f,		0.f},
		{radius,	0.f,		0.f}
	};

	GLfloat topright[4][3] = {
		{size_x - radius,	0.f,	0.f},
		{size_x - radius/factor,0.f,	0.f},
		{size_x,  radius/factor,	0.f},
		{size_x,  radius,		0.f}
	};

	GLfloat bottomright[4][3] = {
		{size_x,  size_y - radius,		0.f},
		{size_x,  size_y - radius/factor,	0.f},
		{size_x - radius/factor, size_y,	0.f},
		{size_x - radius,	 size_y,	0.f},
	};

	GLfloat bottomleft[4][3] = {
		{radius,		size_y,		0.f},
		{radius/factor,	size_y,		0.f},	

		{0.f,	size_y - radius/factor,	0.f},
		{0.f,	size_y - radius,		0.f}
	};

	GLfloat filler[8][3] = {
		{0.f,		radius,		0.f},
		{radius,	0.f,		0.f},

		{size_x - radius,	0.f,	0.f},
		{size_x,		radius,	0.f},

		{size_x, size_y - radius,	0.f},
		{size_x - radius, size_y,	0.f},

		{radius, size_y,		0.f},
		{0.f,	size_y - radius,	0.f}

	};

	glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,& topleft[0][0]);
	glEnable(GL_MAP1_VERTEX_3);

	glBegin(GL_POLYGON);
	for (int i=0;i<=30;i++)
		glEvalCoord1f((GLfloat) i/30.0);
	glEnd();

	glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,& topright[0][0]);
	glBegin(GL_POLYGON);
	for (int i=0;i<=30;i++)
		glEvalCoord1f((GLfloat) i/30.0);
	glEnd();

	glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,& bottomright[0][0]);
	glBegin(GL_POLYGON);
	for (int i=0;i<=30;i++)
		glEvalCoord1f((GLfloat) i/30.0);
	glEnd();

	glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,4,& bottomleft[0][0]);
	glBegin(GL_POLYGON);
	for (int i=0;i<=30;i++)
		glEvalCoord1f((GLfloat) i/30.0);
	glEnd();

	glBegin(GL_POLYGON);
	for (int i=0;i<8;i++)
		glVertex3f( filler[0], filler[1], filler[2] );
	glEnd();
}

Halma
Halma
Try this:
//draw round rectangle centered at (0,0), with width w, height h, and roundness radius rvoid roundrect(double w, double h, double r) {   r=min(r,w/2); r=min(r,h/2);   w-=2*r; h-=2*r;   const int n=40;  //MUST be a multiple of 4      glBegin(GL_POLYGON);   for (int i=0;i<n;i++) {      double t=2*M_PI*(double(i)+0.5)/double(n);      double x=r*cos(t), y=r*sin(t);      if (x<0) x-=w/2; else x+=w/2;      if (y<0) y-=h/2; else y+=h/2;      glVertex2f(x,y);   }   glEnd();   }


I'm just writing off the top of my head, without testing, but I think it should work. Basically, it just draws a circle, but shifts the 4 quadrants to different corners.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.