Jump to content
  • Advertisement
Sign in to follow this  

OpenGL How to draw circle use openGL?

This topic is 4522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Look e.g. at this

The shape of especially a circle could be approximated by iterating an angle and computing the cartesian co-ordinates from the polar co-ordinates, e.g. so:

for(angle = 0; angle < 2*PI; angle += 2*PI/steps) {
x0 = radius * cos(angle);
y0 = 0;
z0 = radius * sin(angle);

Positioning of the circle as a whole appears like with any other drawing primitive by glTranslate.

Share this post

Link to post
Share on other sites
Theres no circle primitive, so you'll either have to make your own out of alot of triangles, or you'll have to use a library like GLUT.

Edit: beaten to it, and it was a much more complete answer.

Share this post

Link to post
Share on other sites
Here is a bit of code I used to draw stuff on one of my server programs, it only did collision, not drawing soIi only drew the radius of the object and a little line showing the direction of movement from the center of the circle

// we need cos(..) and sin(..)
#include <cmath>

const float DEG2RAD = 3.14159/180.0;

The vector can be just an x and y float co-ordinate struct

struct Vector
float x,y;

void drawCircle( const Vector& pos, float radius, float rotation, float r, float g, float b )
glTranslatef( pos.x, pos.y, 0 );
this makes a line pointing in the direction of rotation


for (int i=0; i < 360; i+=10)
float degInRad = i*DEG2RAD;


Share this post

Link to post
Share on other sites
like this example how to change square to circle using this coding? I don't know how to change it.

#include <windows.h>
#include <gl\gl.h>
#include <gl\glaux.h>

// intial square position and its size
GLfloat x = 250.0f;
GLfloat y = 0.0f;
GLsizei rsize = 20;

// step size in x and y directions
// number of pixel to move each time
GLfloat xstep = 0.5f;
GLfloat ystep = 0.5f;

// keep track of window's changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

//called by AUX library when the windows has changed size

void CALLBACK ChangeSize(GLsizei w, GLsizei h)
//prevent a divide by zero, when window is too short
//you cannot make a window zero width

if (h==0)
//set the viewport to be the entire window
glViewport(0, 0, w, h);

//reset the coordinate system before modifying

//keep the square this time, save calculated width and height for later use
windowHeight = 250.0f * h / w;
windowWidth = 250.0f;
windowWidth = 250.0f * w / h;
windowHeight = 250.0f;

//set the clipping volume
glOrtho(1.0f, windowWidth, 0.01, windowHeight, 1.0f, - 1.0f);

//called by AUX library to update window
void CALLBACK RenderScene(void)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//set drawing color to red and draw rectangle at current position
glColor3f(1.0f, 1.0f, 0.0f);
glRectf(x, y, x+rsize, y+rsize);

//called by AUX library when idle(window not being resized or moved

void CALLBACK IdleFunction(void)
//reverse direction when you reach let or right edge
if ( x > windowWidth-rsize || x < 0 )
xstep = -xstep;

//reverse direction when you reach top or bottom edge
if ( y > windowHeight - rsize || y < 0 )
ystep = -ystep;

// check bounds. This is in case the window is made samller
// and the rectangel is outside the new clipping volume
if( x > windowWidth - rsize)
x = windowWidth - rsize - 1;
if ( y > windowHeight - rsize)
y = windowHeight - rsize - 1;

//Actually move the square
x += xstep;
y += ystep;

//redraw the scene with new coordinates

//main body of the program
void main(void)
//aux window setup and initialization
auxInitDisplayMode(AUX_DOUBLE || AUX_RGBA);
auxInitPosition(100, 100, 250, 250);
auxInitWindow("Assignment 1");

//set function to call when window is resized

//start main loop

Share this post

Link to post
Share on other sites
Original post by bradleyong83
Can you give me example about circle movement from top to botton or right and left.....i dunno how to move for looping


Strongly suggested.... I have a feeling that you are just pulling out random code out of nowhere and squishing it into your program at a given location...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!