How to draw an oval?

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?

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

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

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)*yradius);
	} 
	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.

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() , it takes GLfloat’s as the two parameters.

  • Stack Overflow

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, :slight_smile:

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.

maybe you should try glVertex2f(cos(degInRad)xradius100.0f+200.0f,sin(degInRad)yradius100.0f+200.0f);
instead of
glVertex2f(cos(degInRad)*xradius,sin(degInRad)*yradius);

Thank you very much!