SilverTiger / lwjgl3-tutorial

Tutorial for the Lightweight Java Game Library (LWJGL) 3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A problem about z value in vertices data

wangjiangchuan opened this issue · comments

In your tutorial Rendering, I found a problem. Your origin code is
vertices.put(-0.6f).put(-0.4f).put(0f).put(1f).put(0f).put(0f);
vertices.put(0.6f).put(-0.4f).put(0f).put(0f).put(1f).put(0f);
vertices.put(0f).put(0.6f).put(0f).put(0f).put(0f).put(1f);
vertices.flip();

then I changed the z value:
vertices.put(-0.6f).put(-0.4f).put(2.0f).put(1f).put(0f).put(0f);
vertices.put(0.6f).put(-0.4f).put(2.0f).put(0f).put(1f).put(0f);
vertices.put(0f).put(0.6f).put(2.0f).put(0f).put(0f).put(1f);
vertices.flip();

this is a simple triangle with color.But when i changed the vertices data's z value to 2.0f, then there was nothing. With a lot of tests, I found the z only chooses value that varies from -1.0f to 1.0f. Why this happened? Even I used a modelMatrix to translate it's z axis value, it still happened. If you know the reason, please tell me.

Then I use the perspective function to create a perspective matrix, but still there is nothing.
Matrix4f projection = Matrix4f.perspective(45f, 800f/600f, 0.1f, 100.0f);

In OpenGL you have a right-handed coordinate system. Basically if you want to see the object, you have to translate the z-Position in the reverse direction.

Does it work, if you use negative z-Coordinates like this?

vertices.put(-0.6f).put(-0.4f).put(-2.0f).put(1f).put(0f).put(0f);
vertices.put(0.6f).put(-0.4f).put(-2.0f).put(0f).put(1f).put(0f);
vertices.put(0f).put(0.6f).put(-2.0f).put(0f).put(0f).put(1f);

no it doesn't works. Finally, I find the reason. For I didn't use any projection matrix in it. It may acquiesce in using orthogonal projection not the perspective projection. So the z axis only contains value from -1 to 1. When you use the perspective projection, you will find you can change the z to -2.

With an orthogonal projection you can have any desired z values. When creating the projection matrix you have the Function Matrix4f.orthographic(left, right, bottom, top, near, far).
In the tutorial I'm using -1.0f and 1.0f for the values near and far but you can also change them to -2.0f and 2.0f or any other value.

Yes, this is right. When using the orthogonal projection, we can change the variation of z axis to any values.