Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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.
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.

Share this post


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

Share this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this