PDA

View Full Version : How to draw an oval?



12-26-2004, 07:13 AM
I have problem in drawing oval, I browse through quite number of sites, but I can't get the solution.

Any one can help me in this?

protonovus
12-26-2004, 12:02 PM
What you want to draw is called an "ellipse". You probably wouldn't get too far searching for "oval".


#define DEG2RAD 3.14159/180.0

void DrawEllipse(float radiusX, float radiusY)
{
int i;

glBegin(GL_LINE_LOOP);

for(i=0;i<360;i++)
{
float rad = i*DEG2RAD;
glVertex2f(cos(rad)*radiusX,
sin(rad)*radiusY);
}

glEnd();
}

12-27-2004, 03:35 PM
Sorry for late reply. I have tried the code, but it does not work. There are errors when I try to compile it.

Is ok to declare variables using only 'int' or 'float'? Because in openGL book, the book is using syntax such as this 'GLfloat' 'GLint'.

12-27-2004, 04:00 PM
Below is my coding:


const float DEG2RAD = 3.14159/180.0;

void drawEllipse(float xradius, float yradius);

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

drawEllipse(5.0, 5.0);

glFlush();
}

void drawEllipse(float xradius, float yradius)
{
glBegin(GL_LINE_LOOP);

for(int i=0; i < 360; i++)
{
//convert degrees into radians
float degInRad = i*DEG2RAD;
glVertex2f(cos(degInRad)*xradius,sin(degInRad)*yra dius);
}
glEnd();
}

void setupEnv(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 400.0, 0.0, 400.0, 1.0, -1.0);

glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
}

void main(void)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("GL01");
glutInitWindowSize(400, 400);
glutDisplayFunc(display);

setupEnv();

glutMainLoop();
}Can you tell me what went wrong?

Thanks.

Stack Overflow
12-27-2004, 04:35 PM
Hello,

I compiled this code in MSVC++ 6 without warnings or errors. Please make sure you include the following header files:


#include <GL/glut.h>
#include <math.h>To answer your other question, GLfloat is typedef'd as a float. Same as GLint with int. For example:


typedef float GLfloat;
typedef int GLint;According to glVertex2f() (http://www.mevis.de/~uwe/opengl/glVertex.html) , it takes GLfloat's as the two parameters.

- Stack Overflow

12-29-2004, 06:51 AM
Thanks for your reply. And I really sorry for late reply because I am really busy with all my tests and assignments this recently. And this opengl is the worst for me.

I will try to compile the code again, if I face any problems, I will refer back here. thanks to everybody who help, :)

12-29-2004, 07:07 AM
I have tried it. I did a mistake where I save my file as .c rather than .cpp

But I can't see the ellipse.

splat
12-30-2004, 01:33 AM
maybe you should try glVertex2f(cos(degInRad)*xradius*100.0f+200.0f,sin (degInRad)*yradius*100.0f+200.0f);
instead of
glVertex2f(cos(degInRad)*xradius,sin(degInRad)*yra dius);

12-30-2004, 04:33 AM
Thank you very much!