PDA

View Full Version : glVertex3i Z can only be -1, 0, or 1. Why?



Plazmotech
12-14-2012, 11:54 PM
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.

dukey
12-15-2012, 02:49 AM
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

Plazmotech
12-16-2012, 10:16 PM
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

What can I use instead of it?

yoyonel
12-17-2012, 02:26 AM
You can use a custom Orthogonal Projection matrix, explain here: http://www.songho.ca/opengl/gl_projectionmatrix.html (Orthographic Projection section))
So =>
http://www.songho.ca/opengl/files/gl_projectionmatrix_eq25.png
f: Far
n: Near
t: Top
r: Right

Good luck :-)

ps: A simply solution, use this GL function glOrtho => http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);

dukey
12-17-2012, 04:07 AM
What can I use instead of it?

glOrtho
http://msdn.microsoft.com/en-gb/library/windows/desktop/dd373965%28v=vs.85%29.aspx

Plazmotech
12-17-2012, 10:10 AM
but Ortho will not supply depth, right? I would like depth (perspective, where as in farther away is smaller) in my game. Thanks!

Aestivae
12-17-2012, 02:20 PM
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: http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml.

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: http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml.

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).

tonyo_au
12-17-2012, 04:53 PM
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.