LWJGL Forum
Programming => General Java Game Development => Topic started by: TheBoneJarmer on December 03, 2014, 22:01:32

Hey guys
I got something that I really don't understand. Whenever I draw a cube with a width, height and depth of 1 it looks like this:
(http://s27.postimg.org/dbdibsizn/Screenshot_from_2014_12_03_22_53_23.png)
Obvious it's not as tall as it is wide. I did not do anything special. Any ideas?
Thanks in advance guys!
TBJ

Have a look at: http://www.songho.ca/opengl/gl_projectionmatrix.html (http://www.songho.ca/opengl/gl_projectionmatrix.html)
In OpenGL, the coordinates of our viewport/window always range from 1 to +1 in X, Y, and Z. This coordinate system is also called "normalized device coordinates" (NDC).
It is your responsibility to apply other transformations when you want other ranges, which will then get mapped onto this NDC coordinates system.
There is a neat function, gluPerspective, which does produce such a transformation and conveniently takes in the "aspect ratio" of your viewport.
Since it looks like you are doing an orthographic projection, you can of course also use glFrustum with some left and right values that fit the aspect ratio of your window.
EDIT: gluPerspective only exists in LWJGL 2, not LWJGL 3 (which as of yet is not release, so I was assuming you were using LWJG L2). That's why I mentioned it.

Hey Kai, thanks for the reply! I'm actually using glFrustum already. But before LWJGL3, I was acually using gluPerspective. I thought the GLUT library disappeared from LWJGL3, but since you are mentioning it, is it still available? Because I would love to use it.

I'm sorry, to disappoint you, but the GLU library, which gluPerspective is a part of, is not in LWJGL 3 for a simple reason: You don't need it :) or at least should not be using it anymore when doing "more modern" OpenGL.
Instead, build your matrices yourself. I know, I know, this sounds like a very bad thing to do, given that there were such nice utility functions earlier. But this is the way to go.
Actually, you can build your (rowmajor) matrix for gluPerspective yourself easily like this:
Pseudocode:
myOwnGluPerspective(fovY, aspect, near, far):
h = tan(toRadians(fovY) * 0.5) * near
w = h * aspect
fl = w
fr = w
fb = h
ft = h
fn = near
ff = far
set m (4x4 matrix) to identity matrix;
// set rowmajor matrix values:
m[0] = 2.0 * fn / (fr  fl)
m[2] = (fr + fl) / (fr  fl)
m[5] = 2.0 * fn / (ft  fb)
m[6] = (ft + fb) / (ft  fb)
m[10] = (ff + fn) / (ff  fn)
m[11] = 2.0 * ff * fn / (ff  fn)
m[14] = 1.0
m[15] = 0.0
Essentially, this is taken from the previously mentioned web article. Please take notice, that 'm' is assumed to be in rowmajor. OpenGL uses columnmajor matrices by default, so you must transpose the matrix (mirror by the diagonal) before uploading the matrix to OpenGL.
If you still must use legacy OpenGL, you can upload the matrix via glLoadMatrix.

Hey Kai
Sorry for the late reply. Got mixed up in more than I wanted. But anyway, thanks! It works now! For those who are interested, here is my code:
float h = (float) Math.tan((fov / 180 * Math.PI) * 0.5f) * near;
float w = h * aspect;
float fl = w;
float fr = w;
float fb = h;
float ft = h;
float fn = near;
float ff = far;
float[] pMatrix = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
pMatrix[0] = 2 * fn / (fr  fl);
pMatrix[8] = (fr + fl) / (fr  fl);
pMatrix[5] = 2 * fn / (ft  fb);
pMatrix[9] = (ft + fb) / (ft  fb);
pMatrix[10] = (ff + fn) / (ff  fn);
pMatrix[14] = 2 * ff * fn / (ff  fn);
pMatrix[11] = 1;
pMatrix[15] = 0;
FloatBuffer pMatrixBuffer = BufferUtils.createFloatBuffer(16);
pMatrixBuffer.put(pMatrix);
pMatrixBuffer.flip();
glLoadMatrix(pMatrixBuffer);