5
$\begingroup$

I'm beginner in OpenGL and I stumbled into a problem. I'm in OpenGL 2.1 for the time being, if that's of any help.

I set my Frustum as such:

glFrustum(-1366, 1366, -768, 768, 1, 20000);

Now, when I create a cube from quads like this:

0.0, 500.0, 0.0
500.0, 500.0, 0.0
500.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 500.0, 500.0
500.0, 500.0, 500.0
500.0, 0.0, 500.0
etc.

It is not a cube, as I expected. It's extremely stretched along the z axis.

Here's a screenshot of how it looks now: https://www.dropbox.com/s/diznfumtev7g259/Screenshot%20from%202016-02-26%2017%3A01%3A58.png?dl=0 (I removed one of the faces temporarily)

The shape starts to resemble a cube a bit when I change the vertices to:

0.0, 500.0, 0.0
500.0, 500.0, 0.0
500.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 500.0, 0.3
500.0, 500.0, 0.3
500.0, 0.0, 0.3
etc.

Code related to this (I am translating this from assembly which I am actually using).

Matrices setup on initialization:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1366.0, 1366.0, -768.0, 768.0, 1.0, 20000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Transformations of the cube inside the "render()" function:

glLoadIdentity();
glTranslated(1000.0, 500.0, 0.0);
glRotated(degrees, 0.0, 0.0, 1.0); // degrees is +=2 every frame
glTranslated(-250.0, -250.0, distance); // distance is -=0.001 every frame

There are no other matrix operations in the code.

The code uses GLUT to initialize a window with double buffer and depth buffer. Depth testing is enabled.

It all seems to work as expected, the rotation is ok, movement also. I am only troubled by the fact that when I define a 500.0x500.0x500.0 cube, the actual output is not a cube.

What am I doing wrong? How to fix this?

$\endgroup$
4
  • $\begingroup$ Its a bit hard to say what something looks like depends on where your camera is and whet Leese you have going on in the scene. $\endgroup$
    – joojaa
    Commented Feb 26, 2016 at 14:20
  • $\begingroup$ There is only this one cube in the scene. It is slowly rotating around its z axis and slowly going into the distance (this seems to work fine). $\endgroup$ Commented Feb 26, 2016 at 14:39
  • 1
    $\begingroup$ Yes, but that does not help us if we cant see your matrix, etc. You dont have any code that i can help you with. Your supposed to give minimal code to reproduce the problem otherwise theres no debugging to be had. Yes cubes can look very elongated if they are very close to the camera plane. $\endgroup$
    – joojaa
    Commented Feb 26, 2016 at 14:58
  • $\begingroup$ @joojaa sorry. I was a bit reluctant to provide any additional code because I am writing all of this in assembly. I supposed that would not help if I posted it here :) $\endgroup$ Commented Feb 26, 2016 at 15:52

1 Answer 1

2
$\begingroup$

Okay. So after 6 hours of tinkering with my code, trying different approaches and googling for solutions, I found this page:

http://www.glprogramming.com/red/chapter03.html

There is a short C listing which put me on the right track to solve this.

What I did wrong was that I should have set the frustum to something like this:

GLdouble x0 = -(1366/768);
GLdouble x1 = 1366/768;
glFrustum(x0, x1, -1, 1, 1, 100);
// the last two values aren't critical, as long as they are positive

But before that, I should have set the glViewport properly, before setting up the frustum:

glViewport(0, 0, 1366, 768);

I didn't do it and that's why my code produced such weird results.

Now I can create a unit cube with following vertices:

0.0, 1.0, 0.0
1.0, 1.0, 0.0
1.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 1.0, 1.0
1.0, 1.0, 1.0
1.0, 0.0, 1.0
0.0, 0.0, 1.0

And it will display properly as a cube of size 1 in every direction.

$\endgroup$
1
  • 2
    $\begingroup$ To expand a little on the solution you found: with values like 1366 and 768 in glFrustum, you were effectively setting an extremely wide field of view (nearly 180 degrees), which caused extreme perspective distortion; that's why the cube looked weirdly stretched out. You might prefer to use gluPerspective instead of glFrustum, as the parameters there are more intuitive. $\endgroup$ Commented Feb 27, 2016 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.