Confused about z-axis behaviour

I'm a newcomer to OpenGL and I was playing around with drawing triangles with different z-coordinates. From what I understand, the z axis point out of the screen, and the -z axis points into the screen.

When I draw a square with 3 corners at a 0.0 z-coordinate, and the last corner at, say, 3.0 z-coordinate, I get this:

I don't understand how it's making this shape... I thought it would be something like this, since the 4th vertex is just 'far away'.

Can someone explain?

Edit: This is my vertex data

// vertex array
float vertices[] = {
-0.5f,  0.5f,  0.0f, 1.0f, 0.0f, 0.0f, // top left, first 3 are location, last 3 are color
0.5f,  0.5f,  0.0f, 0.0f, 1.0f, 0.0f, // top right
-0.5f, -0.5f,  -2.0f, 0.0f, 0.0f, 1.0f, // bottom left
0.5f, -0.5f,  0.0f, 1.0f, 1.0f, 1.0f // bottom right
};

// element buffer array
GLuint elements[] = {
0, 1, 2,
2, 1, 3
};


And I am calling the draw like:

glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT,0);

• What transformations are you applying? Or are these coordinates directly in clip space with no transformations? And how are you triangulating the square? – Nathan Reed Feb 18 '16 at 4:21
• No transformations, I edited my post to show code – Chara Feb 18 '16 at 4:25

1 Answer

The post-projective Z range in OpenGL extends between −1 and 1. So, a Z-value of 2 is out of range and will be clipped by the far plane.

The reason the square appears with a quadrant missing is that it's split into triangles from bottom left to top right, and so each triangle has two vertices with Z = 0 and one vertex with Z = 2. So, interpolating along the triangle, it hits Z = 1 (the far plane) halfway across, and everything beyond that is clipped. This results in half of each triangle disappearing.

BTW, you'll need a perspective projection matrix in order for far-away objects to appear smaller on screen. With no projection matrix, you're effectively using a parallel projection.