Why is z coordinates used in a triangle strip open gl

191 views Asked by At

I have just started using openGL which i am teaching myself from a book. In my book i am told to build quads using triangle strips because it is more efficient. The book uses these vertices to create the quad.

    vertices.push_back(-0.5f);
    vertices.push_back(0.0f);
    vertices.push_back(0.0f);

    vertices.push_back(0.5f);
    vertices.push_back(0.0f);
    vertices.push_back(0.0f);

    vertices.push_back(-0.5f);
    vertices.push_back(0.0f);
    vertices.push_back(-1.0f);

    vertices.push_back(0.5f);
    vertices.push_back(0.0f);
    vertices.push_back(-1.0f);

I don't understand why the z axis is not constant, it seems like the z axis should remain constant and the y axis should change to make the square display in 2d. I dont see why the z axis is used.

Here is the rest of the code

To load vertices in buffer

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

To display square

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 5.0, 0.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);

Despite me not understanding this however, the square is still displayed properly, so it must work.

2

There are 2 answers

2
derhass On BEST ANSWER

For a quad to make sense, all four points should lie in a plane. It does not have to be a plane with z=const. If you look at those vertices, you might discover that y is constantly 0 for all 3 points, so the quad is defined in the xz plane. YOu can also see that the camera is placed at (0, 5, 0.1), looking down to the origin, so using xz plane does make sense.

0
Jasper On

You look along the y-axis, therefore y is constant to make a square perpendicular to the viewport. With your call to gluLookAt(), you define the camera position and viewing direction. You could modify the arguments of gluLookAt, such that the camera looks along the z direction. In that case, z should be constant in your definition of the square.

Have a look at http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml .