How to draw circle use openGL?

Started by
8 comments, last by nullsquared 18 years, 1 month ago
How to draw circle use openGL and then move circle from middle top and down looping
Advertisement
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.
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.
You can just use a texture with a circle drown on it.
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);}
Here's another cool code snippet, which looking at other posts, looks like what Rip-Off did. Replace the 'no5' in the url with other 'no#'s for other cool OpenGL code snippets.
If you want a simple performance boost, store the values of all the different sines and cosines in an array ahead of time.
Can you give me example about circle movement from top to botton or right and left.....i dunno how to move for looping
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);
}



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...

This topic is closed to new replies.

Advertisement