I'm creating a function that allows me to create rectangles with rounded edges in OpenGL Glut. The problem is that when the polygon is drawn it doesn't do it correctly, it seems to skip the point, even though the points are fine. This is the function:
void rect(GLfloat w, GLfloat h, GLfloat x, GLfloat y, GLfloat tl = 0, GLfloat tr = 0, GLfloat br=0, GLfloat bl=0) {
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_POLYGON);
// top left
glVertex2i(0, 0 + tl);
glVertex2i(0 + tl, 0 + tl);
glVertex2i(0 + tl, 0);
// top right
glVertex2i(w - tr, 0);
glVertex2i(w - tr, 0 + tr);
glVertex2i(w, 0 + tr);
// bottom right
glVertex2i(w, h - br);
glVertex2i(w - br, h - br);
glVertex2i(w - br, h);
// bottom left
glVertex2i(0 + bl, h);
glVertex2i(0 + bl, h - bl);
glVertex2i(0, h - bl);
glEnd();
glPopMatrix();
}
This code defines a function called "rect" that draws a rectangle in an OpenGL context using the following parameters:
- w: width of the rectangle.
- h: height of the rectangle.
- x: x coordinate of the point of origin of the rectangle.
- y: y coordinate of the point of origin of the rectangle.
- tl: radius of the upper left corner of the rectangle (default is zero).
- tr: radius of the upper right corner of the rectangle (default is zero).
- br: radius of the lower right corner of the rectangle (default is zero).
- bl: radius of the lower left corner of the rectangle (default is zero).
The rectangle is drawn clockwise, starting at the upper left corner, and the vertices of each corner are specified using the x and y coordinates and the radii tl, tr, br, and bl, as appropriate. The vertices are drawn using the glVertex2i() function. They do not implement the round edges yet, I am only creating the figure of the rectangle.
And the implementation in the display function is the following:
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(0, GLUT_WIN_HEIGHT, 0);
glRotatef(180, 1, 0, 0);
glColor3f(0, 0, 0);
rect(100, 100, 50, 50, 25, 25, 25, 25);
glPopMatrix();
glFlush();
}
When I run the program I get the following image:
.
Coordinates (0, 0) start at the top left of the window.
This only happens when I use the GL_POLYGON primitive but if I change it to GL_LINE_LOOP the lines do join correctly and form the shape I want.
This is my complete code:
#include <GL/glut.h>
#define GLUT_WIN_WIDTH 700
#define GLUT_WIN_HEIGHT 700
void display(void);
void init(void);
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(GLUT_WIN_WIDTH, GLUT_WIN_HEIGHT);
glutCreateWindow("Rectangle");
init();
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
void circle(GLfloat radius, GLfloat x, GLfloat y, GLfloat slice=2.0f, GLfloat dx=1.0f, GLfloat dy=1.0f) {
GLfloat vx, vy, angle;
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_POLYGON);
glVertex2i(0,0);
for (angle = 0.0f; angle <= (slice * GL_CTXT_PI); angle += 0.01f)
{
vx = radius * sin(angle);
vy = radius * cos(angle);
glVertex2i(dx*vx, dy*vy);
}
glEnd();
glPopMatrix();
}
void rect(GLfloat w, GLfloat h, GLfloat x, GLfloat y, GLfloat tl = 0, GLfloat tr = 0, GLfloat br=0, GLfloat bl=0) {
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_LINE_LOOP);
// top left
glVertex2i(0, 0 + tl);
glVertex2i(0 + tl, 0 + tl);
glVertex2i(0 + tl, 0);
// top right
glVertex2i(w - tr, 0);
glVertex2i(w - tr, 0 + tr);
glVertex2i(w, 0 + tr);
// bottom right
glVertex2i(w, h - br);
glVertex2i(w - br, h - br);
glVertex2i(w - br, h);
// bottom left
glVertex2i(0 + bl, h);
glVertex2i(0 + bl, h - bl);
glVertex2i(0, h - bl);
glEnd();
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(0, GLUT_WIN_HEIGHT, 0);
glRotatef(180, 1, 0, 0);
glColor3f(0, 0, 0);
rect(100, 100, 50, 50, 25, 25, 25, 25);
glPopMatrix();
glFlush();
}
void init(void) {
init_colors();
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0, GLUT_WIN_WIDTH, 0, GLUT_WIN_HEIGHT);
}