Circle turns out to be an oval

Started by
4 comments, last by Erik Rufelt 14 years, 5 months ago
Hi, I'm creating my first opengl game in C++ and its pong. I'm trying to create the ball but it turns out to be oval-shaped. How can I make the ball circular?

glBegin(GL_TRIANGLE_FAN);

		const float RADIUS = 0.02f;
		const float PI = 3.141529f;

		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex2f(0.0f, 0.0f);

		for (float i = 0; i < 360; ++i)
		{
			float fRadians = i * PI/180.0f;
			glVertex2f(cos(fRadians) * RADIUS, sin(fRadians) * RADIUS);
		}

glEnd();
I think the problem has to do with the window size being 800x600, 1024x768, etc., but im not sure.
Advertisement
The problem probably lies with your projection matrix.

When generating your matrix, you need to specify the aspect ratio of the display. For an 800x600 display, the aspect ratio is 800/600 = 1.3333...

If you're using GLU (etc) to generate this matrix for you, the function should have an aspect parameter for you to pass in this value.
Quote:Original post by Hodgman
The problem probably lies with your projection matrix.

When generating your matrix, you need to specify the aspect ratio of the display. For an 800x600 display, the aspect ratio is 800/600 = 1.3333...

If you're using GLU (etc) to generate this matrix for you, the function should have an aspect parameter for you to pass in this value.


I do not really understand how to do this. I'm using the default matrix (where the center is (0, 0)), so I haven't generated my own. How do I create my own matrix? Where do i specify the aspect ratio?
I see, you don't change the matrix at all?
That means that -1.0f is the left side, and 1.0f is the right side, on the X-Axis, and bottom and top on the Y-axis. This means that you must multiply your X-coordinates with (height/width), in order to get the correct perspective. So you have an Y_RADIUS and an X_RADIUS, and multiply X_RADIUS by (600/800) for example.

EDIT: An easier way might be to add the following code at the beginning of your render, which should automatically do this for all vertices.
glLoadIdentity();
glScalef(height/width, 1.0f, 1.0f);

There are other ways to handle things also, like using the glOrtho function. With that you can for example specify glOrtho(0,width,height,0,0,1), in order to draw your vertices with pixel-coordinates.
Quote:Original post by Erik Rufelt
I see, you don't change the matrix at all?
That means that -1.0f is the left side, and 1.0f is the right side, on the X-Axis, and bottom and top on the Y-axis. This means that you must multiply your X-coordinates with (height/width), in order to get the correct perspective. So you have an Y_RADIUS and an X_RADIUS, and multiply X_RADIUS by (600/800) for example.

EDIT: An easier way might be to add the following code at the beginning of your render, which should automatically do this for all vertices.
glLoadIdentity();
glScalef(height/width, 1.0f, 1.0f);

There are other ways to handle things also, like using the glOrtho function. With that you can for example specify glOrtho(0,width,height,0,0,1), in order to draw your vertices with pixel-coordinates.


Thank you, the first option worked very well for my situation.

I did try using the glOrtho function before I posted my question on these forums, but converting all the coordinates and sizes would be a real hassle for me so i decided not to.
If you want, you can use glOrtho(-width/height, width/height, -1, 1, 0, 1), which should work the same as without it, just that the left/right edges of the screen have coordinates that are larger, making a unit the same length in pixels in both the X and Y direction. It should be the same result as glScalef(height/width, 1, 1).

This topic is closed to new replies.

Advertisement