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 10 of 10

Thread: help drawing cube :(

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    help drawing cube :(

    hi,
    i have a very basic problem . i'm trying to draw a simple 3d cube to the screen. however whenever i use the gluPerspective() there is no output; i have only drawn the one side at the moment but will add the others later
    i am using the LWJGL but the code is all opengl

    here is my init code
    Code :
              glEnable(GL_DEPTH_TEST);
     
    	  /* Setup the view of the cube. */
    	  glMatrixMode(GL_PROJECTION);
    	  gluPerspective(50, (float)WIDTH/(float)HEIGHT, 0, 100);
    	  glMatrixMode(GL_MODELVIEW);

    and here is my loop code
    Code :
                    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    		glClear(GL_COLOR_BUFFER_BIT);
    		glLoadIdentity();
     
    		glColor3f(0f, 1f, 1f);
    		glBegin(GL_QUADS);
    		{
    			glVertex3i(10, 10, z);
    			glVertex3i(10+10, 10, z);
    			glVertex3i(10+10, 10+10, z);
    			glVertex3i(10, 10+10, z);
    		}
    		glEnd();
     
    		glFlush();

    does anyone know where im going wrong?

    -James

  2. #2
    Intern Contributor
    Join Date
    Jan 2010
    Location
    Japan - Greece
    Posts
    99

    Re: help drawing cube :(

    I can definitely say that in gluPerspective() the 3rd parameter (zNear) should always be positive and never zero. For further reference seek here:
    http://www.opengl.org/sdk/docs/man/x...erspective.xml

    So why don't you put something like 0.1? Also how come you are using 50 degrees for your field of view? Most starting tutorials have this set at a standard of 45 degrees.

    Another mistake is that you are using glClearColor inside your main loop. That function sets the color you want your screen to be clear with every time glClear() is called. So it does not need to be in the main loop.
    My Blog

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    Re: help drawing cube :(

    thanks for the reply,
    i have changed the znear to 1 and moved the clearcolor declaration to the init but i still see nothing in the output

    -James

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: help drawing cube :(

    You need glLoadIdentity before gluPerspective

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    Re: help drawing cube :(

    i have now added the glLoadIdentity(); call before the perspective one and still get a blank window
    the init code now looks like this

    Code :
              glEnable(GL_DEPTH_TEST);
     
    	  glMatrixMode(GL_PROJECTION);
    	  glLoadIdentity();
    	  gluPerspective(45, (float)WIDTH/(float)HEIGHT, 1, 100);
    	  glMatrixMode(GL_MODELVIEW);

  6. #6
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: help drawing cube :(

    Code :
    glBegin(GL_QUADS);
    {
    glVertex3i(10, 10, z);
    glVertex3i(10+10, 10, z);
    glVertex3i(10+10, 10+10, z);
    glVertex3i(10, 10+10, z);
    }

    What are the value of z? The quad you are drawing can be behind the camera.

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    Re: help drawing cube :(

    erm, I think its 50. So it should be half way through the scene

    -James

  8. #8
    Intern Contributor
    Join Date
    Jan 2010
    Location
    Japan - Greece
    Posts
    99

    Re: help drawing cube :(

    There is your problem then, trinitrotoluene pointed it out.

    z is too big and positive. Positive means towards the camera, towards you and so it puts the square behind the camera. Try a negative value, say -50.
    My Blog

  9. #9
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    Re: help drawing cube :(

    still get the same problem even with -50;

    here is the current complete code : http://pastebin.com/bifVwTuG


    wow opengl is harder than i first thought, i'm only trying to draw a cube! (well one side) :P

    -James

  10. #10
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Cheshire
    Posts
    6

    Re: help drawing cube :(

    ok
    ive fixed the problem now i ended up using only 1 and 0's to draw the plane and then scaled before the calles to Vector3f

    -James

Posting Permissions

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