Featured Posts

8. Bump Mapping in GLSL8. Bump Mapping in GLSL Introduction Bump mapping is essential in todays computer games, and computer graphics in general. Would you like to know the best thing about it? It is extremely simple to implement. Bump mapping works...

Read more

Swiftless GLSL Shader DeveloperSwiftless GLSL Shader Developer Swiftless GLSL Shader Developer   Version 0.1a Currently Swiftless GLSL Shader Developer is in it's first public release, and is currently in alpha status, meaning it is not complete and may contain...

Read more

Wordpress Optimization Wordpress Website Optimizations Introduction Wordpress itself is a fairly wonderful tool. Since switching to it, I find it is a lot quicker to make changes to my website and it is also quicker to get...

Read more

36. OpenGL Framebuffers36. OpenGL Framebuffers Introduction Frame buffers are one of those mythical things that we have all heard of, but many beginner OpenGL developers avoid because there is not much information about them, and they can be confusing...

Read more

1. Terrain Class1. Terrain Class Terrain is one of those things that so far, hasn't been perfectly recreated in computer graphics. But it is almost there! Looking over a beautiful landscape can be one of the most amazing feelings in the...

Read more

  • Prev
  • Next

30. OpenGL Circle Drawing

Posted on : 25-03-2010 | By : Swiftless | In : OpenGL

Tags: , , ,

2

Here we are going to create a circle. This is something I don’t believe
you can do unless you create it on your own. In other words, it is not
preset.

Now because a circle is just a bunch of lines. That is what we are going
to make. A bunch of lines, close together that go around in a circle.

Now first of all I am going to create a typedef to hold our circle properties
as we call them

typedef struct
{

This will hold the x values as we need them
float x;
This will hold the y values as we need them
float y;
Call it CIRCLE
}CIRCLE;

Then I am setting the property circle to act like the typedef CIRCLE
CIRCLE circle;

Now for the meat of this tutorial, the circle creation function
The properties this takes are:
k – the translation on the y axis
r – the radius of the circle
h – the translation on the x axis

void createcircle (int k, int r, int h) {
Now we begin drawing our lines
glBegin(GL_LINES);
Here we are setting up the vertices all in one line function, this
sets every 2 vertices to a line.

First we begin our loop, this loop will cycle through, constantly
changing our x and y values for our lines. It cycles through until
it reaches 180, increasing in intervals of 1
for (int i = 0;i < 180;i++)
{

Now to set up the current x value that we need for our vertex
I am setting it to the radius of the circle, times by the cosine of
the current value of i, then I am taking h to translate it
circle.x = r * cos(i) – h;
Then to set up the current y value that we need for our vertex
I am setting it to the radius of the circle, times by the sine of
the current value of i, then I am adding k to translate it
circle.y = r * sin(i) + k;
Then I am drawing the vertex
glVertex3f(circle.x + k,circle.y – h,0);

Now for the second part of the line, I am doing the same as above, only
I am moving it 0.1 units so that I am not down to points. This also
Lowers the chance of any holes occuring in the circle
circle.x = r * cos(i + 0.1) – h;
circle.y = r * sin(i + 0.1) + k;

Then I am drawing the vertex
glVertex3f(circle.x + k,circle.y – h,0);
}
glEnd();
}

Then to call it, use something like this:
createcircle(0,10,0);

That would create a circle that is originated around (0,0) and with
a radius of 10 units.

Changing GL_LINES to another shape can come up with some other little
effects if you would like to give it a go.

But other than that, this is all you need for a circle.

If you have any queries, please email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
  #include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

typedef struct
{
float x;
float y;
}CIRCLE;

CIRCLE circle;

float rot = 0;

void createcircle (int k, int r, int h) {
    glBegin(GL_LINES);
    for (int i = 0; i < 180; i++)
    {
    circle.x = r * cos(i) - h;
    circle.y = r * sin(i) + k;
    glVertex3f(circle.x + k,circle.y - h,0);
    
    circle.x = r * cos(i + 0.1) - h;
    circle.y = r * sin(i + 0.1) + k;
    glVertex3f(circle.x + k,circle.y - h,0);
    }
    glEnd();
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0,0,-20);
    glRotatef(rot,0,1,0);
    glRotatef(rot,1,0,0);
    glRotatef(rot,0,0,1);
    glColor3f(1,1,1);
    createcircle(0,10,0);
    glRotatef(rot,0,1,0);
    glRotatef(rot,1,0,0);
    glRotatef(rot,0,0,1);
    glColor3f(1,0,0);
    createcircle(-2,8,-2);
    glRotatef(rot,0,1,0);
    glRotatef(rot,1,0,0);
    glRotatef(rot,0,0,1);
    glColor3f(0,1,0);
    createcircle(-1,6,-1);
    glRotatef(rot,0,1,0);
    glRotatef(rot,1,0,0);
    glRotatef(rot,0,0,1);
    glColor3f(0,0,1);
    createcircle(2,4,2);
    glRotatef(rot,0,1,0);
    glRotatef(rot,1,0,0);
    glRotatef(rot,0,0,1);
    glColor3f(0,1,1);
    createcircle(1,2,1);
    glutSwapBuffers();
    rot++;
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (“A basic OpenGL Window);

    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 7.0/10 (10 votes cast)
VN:F [1.9.3_1094]
Rating: -2 (from 2 votes)
30. OpenGL Circle Drawing, 7.0 out of 10 based on 10 ratings

Related posts:

  1. 29. OpenGL Bounding Sphere Collision
  2. 25. OpenGL Vertex Coloring
  3. 31. OpenGL Sphere Creation
  4. 5. OpenGL Color (Version 2.0)
  5. 24. OpenGL Camera Part 3

Comments (2)

Hi Cyp,

I see what you mean, I have combined both the creation and the rendering of the sphere into one method, which does make the struct pointless.

The efficient way to do this, would be to store all the vertices into the struct and then create another method with a loop for rendering the circle.

However when it comes to k and h, you can remove them if you go with the above implementation and just use translate, and you can even do this in the second example I just gave, but they are there because of the mathematics behind creating a circle involves k and h for setting the location.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I think that use of struct circle is pointless its working same way with local varibles.

Adding k, h varibles seems strange u can acomplish same thing with use of translate functions. less simple math ?
I’d rather add segmentation varible than this two.

Anyways i’m not pro opengl coder and i can be wrong but all in all this one rly helped me thx ^_^

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Write a comment

Improve the web with Nofollow Reciprocity.