Circle turns out to be an oval

This topic is 2955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

glEnd();

I think the problem has to do with the window size being 800x600, 1024x768, etc., but im not sure.

Share on other sites
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.

Share on other sites
Quote:
 Original post by HodgmanThe 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?

Share on other sites
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.
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.

Share on other sites
Quote:
 Original post by Erik RufeltI 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.

Share on other sites
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).

Share on other sites

This topic is 2955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628634
• Total Posts
2983964

• 10
• 18
• 20
• 13
• 9