Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

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

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    3

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

    Hey. I have this code

    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.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    249
    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

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by dukey View Post
    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?

  4. #4
    Junior Member Newbie yoyonel's Avatar
    Join Date
    Sep 2010
    Location
    Paris
    Posts
    23
    You can use a custom Orthogonal Projection matrix, explain here: http://www.songho.ca/opengl/gl_projectionmatrix.html (Orthographic Projection section))
    So =>

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

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    3
    but Ortho will not supply depth, right? I would like depth (perspective, where as in farther away is smaller) in my game. Thanks!

  6. #6
    Intern Newbie
    Join Date
    Nov 2012
    Posts
    33
    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
    Code :
    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/...erspective.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):
    Code :
    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).
    Last edited by Aestivae; 12-17-2012 at 02:30 PM.

  7. #7
    Junior Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    249
    Quote Originally Posted by Plazmotech View Post
    What can I use instead of it?
    glOrtho
    http://msdn.microsoft.com/en-gb/libr...=vs.85%29.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •