Skip to content

Instantly share code, notes, and snippets.

@linusthe3rd
Created January 30, 2011 18:54
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save linusthe3rd/803118 to your computer and use it in GitHub Desktop.
Save linusthe3rd/803118 to your computer and use it in GitHub Desktop.
The two methods used to create circles in opengl
/*
* Function that handles the drawing of a circle using the triangle fan
* method. This will create a filled circle.
*
* Params:
* x (GLFloat) - the x position of the center point of the circle
* y (GLFloat) - the y position of the center point of the circle
* radius (GLFloat) - the radius that the painted circle will have
*/
void drawFilledCircle(GLfloat x, GLfloat y, GLfloat radius){
int i;
int triangleAmount = 20; //# of triangles used to draw circle
//GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); // center of circle
for(i = 0; i <= triangleAmount;i++) {
glVertex2f(
x + (radius * cos(i * twicePi / triangleAmount)),
y + (radius * sin(i * twicePi / triangleAmount))
);
}
glEnd();
}
/*
* Function that handles the drawing of a circle using the line loop
* method. This will create a hollow circle.
*
* Params:
* x (GLFloat) - the x position of the center point of the circle
* y (GLFloat) - the y position of the center point of the circle
* radius (GLFloat) - the radius that the painted circle will have
*/
void drawHollowCircle(GLfloat x, GLfloat y, GLfloat radius){
int i;
int lineAmount = 100; //# of triangles used to draw circle
//GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2.0f * PI;
glBegin(GL_LINE_LOOP);
for(i = 0; i <= lineAmount;i++) {
glVertex2f(
x + (radius * cos(i * twicePi / lineAmount)),
y + (radius* sin(i * twicePi / lineAmount))
);
}
glEnd();
}
@mantasink
Copy link

drawHollowCircle() draws a 'radius line' too for me. Any ideas how to avoid that?

@elvirabaenap
Copy link

I have copied the function but it does not draw anything, someone can help me please?

@elvirabaenap
Copy link

<main.c>

include <GL/GL.h>

include <GL/GLUT.h>

include <time.h>

define WINDOW_WIDTH 640

define WINDOW_HEIGHT 480

define MARGIN_WIDTH 40

GLint WindowWidth = WINDOW_WIDTH;
GLint WindowHeight = WINDOW_HEIGHT;

void init_gl(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,50);//100);
glutInitWindowSize(WindowWidth, WindowHeight);//(400,300);
glutCreateWindow(argv[0]);//"Ejemplo básico";
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-WindowWidth/2, WindowWidth/2, -WindowHeight/2, WindowHeight/2);
//gluOrtho2D(0,200,0,150);
}

void reshape_func(GLint width, GLint height) {
glViewport(0, 0, WindowWidth=width, WindowHeight=height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-WindowWidth/2, WindowWidth/2, -WindowHeight/2, WindowHeight/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display_func() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
// Compute data
GLuint radius = (WindowWidth<WindowHeight)?WindowWidth:WindowHeight;
radius = (radius/2) - MARGIN_WIDTH;
time_t rawtime;
struct tm *timeinfo;
time ( &rawtime );
timeinfo = localtime (&rawtime);
// Draw the clock
draw_hoop(0, 0, radius);
//draw_marks(0, 0, radius);
//draw_hands(0, 0, radius, timeinfo->tm_hour, timeinfo->tm_min);
glFlush();
}

int main(int argc, char * argv[]) {
init_gl(argc, argv);
glutReshapeFunc(reshape_func);
glutDisplayFunc(display_func);
glutMainLoop();
return 0;
}

<solution.c>

include <GL/GL.h>

//#include <GLUT/GLUT.h>

include <math.h>

include "solution.h"

void lineSegment(int x, int y,int x2,int y2){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.8,0.8);
glBegin(GL_LINES);
glVertex2i(x,y);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void draw_hoop(GLint center_x, GLuint center_y, GLuint radius) {

int i;
int lineAmount = 100; //# of triangles used to draw circle

//GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2 * 3.141592; 

glBegin(GL_LINE_LOOP);
    for(i = 0; i <= lineAmount;i++) { 
        glVertex2f(
            center_x + (radius * cos(i *  twicePi / lineAmount)), 
            center_y + (radius* sin(i * twicePi / lineAmount))
        );
    }
glEnd();

}

@aberlanas
Copy link

Remains the color!

@Hi-Angel
Copy link

Isn't usage of glBegin()/glEnd() frowned upon as legacy?

@htmlboss
Copy link

yup! Just add the vertices to a vertex buffer and draw using shaders.

@superblaubeere27
Copy link

Nice

@aliasdz
Copy link

aliasdz commented Jul 9, 2020

Thank you for this share.
my question, how I can draw a number in centre of the circle ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment