Using the following function that draws a filled circle in opengl, how do I make it show at different coordinates of the window?
The obvious way would be to call glTranslate
first. Note, however, that you can already accomplish the same a bit more easily with a combination of glPointSize
and glPoint
:
glPointSize(CIRCLE_RADIUS/2.0f);
glPoint(center_x, center_y, center_z);
Before you start drawing the circles, you'll want something like:
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
Otherwise, your "circles" could end up as squares.
Edit: Without knowing how you've set up your coordinates, it's impossible to know what the "top-left" position is, but you could do something like this:
void draw_circle(float x, float y, float radius) {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(x, y, 0.0f);
static const int circle_points = 100;
static const float angle = 2.0f * 3.1416f / circle_points;
// this code (mostly) copied from question:
glBegin(GL_POLYGON);
double angle1=0.0;
glVertex2d(radius * cos(0.0) , radius * sin(0.0));
int i;
for (i=0; i<circle_points; i++)
{
glVertex2d(radius * cos(angle1), radius *sin(angle1));
angle1 += angle;
}
glEnd();
glPopMatrix();
}
You could then call (for example):
draw_circle(0.0f, 0.0f, 0.2f); // centered
draw_circle(1.0f, 0.0f, 0.2f); // right of center
draw_circle(1.0f, 1.0f, 0.2f); // right and up from center
Of course, the directions I've given assume haven't (for example) rotated your view, so x
increases to the right and y
increases upward.
dasen
Updated on June 05, 2022Comments
-
dasen almost 2 years
I have got the following code to draw a filled circle in opengl. The problem is that it draws at the center of the screen. How do I make it draw in another position of it?
Here is the code:
#define CIRCLE_RADIUS = 0.15f int circle_points = 100; void draw() { glClear(GL_COLOR_BUFFER_BIT); double angle = 2* PI/circle_points ; glPolygonMode( GL_FRONT, GL_FILL ); glColor3f(0.2, 0.5, 0.5 ); glBegin(GL_POLYGON); double angle1 = 0.0; glVertex2d( CIRCLE_RADIUS * cos(0.0) , CIRCLE_RADIUS * sin(0.0)); int i; for (i = 0; i < circle_points; i++) { glVertex2d(CIRCLE_RADIUS * cos(angle1), CIRCLE_RADIUS *sin(angle1)); angle1 += angle ; } glEnd(); glFlush(); }
-
dasen over 13 yearsI'm not going to use the idea of the points, because I also have to draw squares, so for consistency sake, I'll draw both of them using polygons. But tell me, could you give me an example code to put the circle at the top-left position, for example?thanx