glVertex3i Z can only be -1, 0, or 1. Why?

Hey. I have this code


#include <iostream>
#include <GLUT/GLUT.h>

using namespace std;

void display();
void reshape(int, int);
void idle();
void drawTile(int, int);

int world[32][32];

int main(int argc, char ** argv){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowSize(640, 480);
	glutCreateWindow("GLUT program");
	
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(idle);
	
	glutMainLoop();
	return 0;
};

void display(){
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	for (int y = 0; y < 32; y++){
		for (int x = 0; x < 32; x++){
			if ((x+y)%2){
				drawTile(x*16, y*8);
			};
		};
	};

	glutSwapBuffers();
};

void reshape(int width, int height){
	glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0, width, height, 0);
    glMatrixMode(GL_MODELVIEW);
};

void idle(){
	/* code */
	glutPostRedisplay();
};

void drawTile(int x, int y){
	glColor3b(x, y, 127);
	glBegin(GL_QUADS);
	glVertex3i(x   , y   , (y   )*-1);
	glVertex3i(x-16, y+8 , (y+8 )*-1);
	glVertex3i(x   , y+16, (y+16)*-1);
	glVertex3i(x+16, y+8 , (y+8 )*-1);
	glEnd();
};

Said code doesn’t exactly work though. It doesn’t show the squares. After some analysis and variable changing, I concluded that Z can only be -1, 0, 1. I suspect this is due to clipping. Why is this, and how can I fix it? I would simply like to add some depth to my squares.

gluOrtho2D(0, width, height, 0);

This function sets the near and far clipping planes -1 and 1.

So your values of
(y+8 )*-1);
are draw outside the viewing frustum

[QUOTE=dukey;1245796]This function sets the near and far clipping planes -1 and 1.

So your values of
(y+8 )*-1);
are draw outside the viewing frustum[/QUOTE]

What can I use instead of it?

You can use a custom Orthogonal Projection matrix, explain here: OpenGL Projection Matrix (Orthographic Projection section))
So =>


f: Far
n: Near
t: Top
r: Right

Good luck :slight_smile:

ps: A simply solution, use this GL function glOrtho => glOrtho
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);

glOrtho

but Ortho will not supply depth, right? I would like depth (perspective, where as in farther away is smaller) in my game. Thanks!

It really depends on if you’re willing to rely on the GL utilities (glu.h) or use deprecated functions. I can’t be entirely sure about your case because I’ve never used GLUT, but read on, I guess.

Since you’re using immediate mode OpenGL, easiest way is to use glu.h, which I think is included in GLUT. If not, it’s #include <GL/GLU.h>.

You would make a function call to

gluPerspective(vertical field of view angle, aspect ratio (x over y), z near clipping distance, z far clipping distance);

The documentation for it is here: gluPerspective.

Another method that doesn’t require the usage of the GL utilities is to compute some of the numbers yourself and passing them to glFrustum instead. This is a deprecated function, but I’m guessing you aren’t too worried about that if you’re working with immediate mode.

You’d use something like this (C code with #include <math.h>, not C++, but you get the idea):

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLdouble pi = 3.141592;    // You get the idea
GLdouble fieldOfViewAngle = 45.0;

GLdouble zNear = 0.1;
GLdouble zFar = 255.0;
GLdouble aspectRatio = ((GLdouble) width) / ((GLdouble) height);    // Since width and height are usually integers, cast to avoid integer division problems
GLdouble clipHeight = tan(fieldOfViewAngle / 360.0 * pi) * zNear;
GLdouble clipWidth = fH * aspectRatio;

glFrustum(-clipWidth, clipWidth, -clipHeight, clipHeight, zNear, zFar);

The documentation for it is here: glFrustum.

If you want to do this in modern OpenGL (3.0+), you need to do the math (which can be found on that last documentation page) yourself and pass the matrices to the shaders.

EDIT: Mind that both of these methods set up a perspective view where positive x points to your right, positive y points up, and positive z points out of the screen. If you want to be able to see anything, you’ll need to either translate the modelview or change your coordinates so that they’ll render in or near the +x, +y, -z octant of space (after transformation).

If you want to do this in modern OpenGL (3.0+), you need to do the math (which can be found on that last documentation page) yourself and pass the matrices to the shaders.

Check out glm library if you go this way; it has a lot of replacement functions for the glu functions.