Sign in to follow this  
bradleyong83

OpenGL How to draw circle use openGL?

Recommended Posts

Look e.g. at this
http://www.gamedev.net/community/forums/topic.asp?topic_id=378739
thread.

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 )
{
glPushMatrix();
glTranslatef( pos.x, pos.y, 0 );
glColor4f(r,g,b,1.0);
/*
this makes a line pointing in the direction of rotation
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(cosine(rotation)*radius,sine(rotation)*radius);
glEnd();*/


glBegin(GL_LINE_LOOP);

for (int i=0; i < 360; i+=10)
{
float degInRad = i*DEG2RAD;
glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
}

glEnd();
glPopMatrix();
glColor4f(1.0,1.0,1.0,1.0);
}




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)
h=1;
//set the viewport to be the entire window
glViewport(0, 0, w, h);

//reset the coordinate system before modifying
glLoadIdentity();

//keep the square this time, save calculated width and height for later use
if(w<=h)
{
windowHeight = 250.0f * h / w;
windowWidth = 250.0f;
}
else
{
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);
glClear(GL_COLOR_BUFFER_BIT);
//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);
glFlush();
}

//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
RenderScene();
}

//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
auxReshapeFunc(ChangeSize);
auxIdleFunc(IdleFunction);

//start main loop
auxMainLoop(RenderScene);
}



Share this post


Link to post
Share on other sites
Quote:
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

NeHe

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this