Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Problem of not displaying the desired cube

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    24

    Problem of not displaying the desired cube

    I am a newbie in using opengl, i do not know why my code do not show any of the drawings, I appreciate if anyone can help,thanks~

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    // Specification:
    //
    //
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <math.h>
    #include <GL/glut.h>

    //////////////////////////////////////////////////////////////////
    //
    // Include the header file of our rotation user-interface.
    //
    #include "gsrc.h"
    //
    //////////////////////////////////////////////////////////////////

    #define PI 3.141592654

    void cube(){
    GLint viewport[4];

    glGetIntegerv( GL_VIEWPORT, viewport );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( 45, double(viewport[2])/viewport[3], 0.1, 10 );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt( 0,0,3, 0,0,0, 0,1,0 );
    //glMultMatrixf( gsrc_getmo() ); // get the rotation matrix from the rotation user-interface
    //
    //////////////////////////////////////////////////////////////////

    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT); // Clear display window.

    glColor3f(1,0,0); // Set line segment color to red.

    glPushMatrix();
    glutSolidCube(2.0);
    glPopMatrix();

    }

    void main (int argc, char** argv)
    {
    //cube();
    glutInit(&amp;argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); // Set display mode.
    glutInitWindowPosition( 50, 100 ); // Set top-left display-window position.
    glutInitWindowSize( 400, 300 ); // Set display-window width and height.
    glutCreateWindow( "OpenGL Program for tutorial 2" ); // Create display window.

    glutDisplayFunc(cube);
    glutMainLoop(); // Display everything and wait.
    }

  2. #2
    Senior Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356

    Re: Problem of not displaying the desired cube

    You are using a doble buffer
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    but you are not swapping from a buffer to another, so you are always drawing in the back buffer.
    Add glutSwapBuffers(); at the end of your draw function.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Problem of not displaying the desired cube

    Hi,
    Main issue: You need to call glutSwapBuffers() at the end of your render routine in double buffered mode to present the back buffer on screen.
    Sec. issues:
    1) Typically we put the viewport setup in the resize handler.
    2) Your projection matrix is also setup in the resize function.
    Thats it here is the altered code. See if this helps.
    Code :
    #include <GL/glut.h>
    void init_gl() {
    	glClearColor(1,1,1,1);
    }
    void resize(int nw, int nh) {
       glViewport(0,0,nw,nh);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective( 45, double(nw)/nh, 0.1, 10 );
       glMatrixMode(GL_MODELVIEW);  
    }
     
    void display(){ 
    	glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
     
    	glLoadIdentity();
    	gluLookAt( 0,0,10, 0,0,0, 0,1,0 );
     
    	glColor3f(1,0,0); // Set line segment color to red.
    	glPushMatrix();
    		glutSolidCube(2.0);
    	glPopMatrix();
    	glFinish();
    	glutSwapBuffers();
    }
     
    void main (int argc, char** argv)
    { 
    	glutInit(&amp;argc, argv); 
    	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); // Set display mode.
    	glutInitWindowPosition( 50, 100 ); // Set top-left display-window position.
    	glutInitWindowSize( 400, 300 ); // Set display-window width and height.
    	glutCreateWindow( "OpenGL Program for tutorial 2" ); // Create display window.
    	init_gl();
    	glutDisplayFunc(display); 
    	glutReshapeFunc(resize);
    	glutMainLoop(); // Display everything and wait.
    }
    Regards,
    Mobeen

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    24

    Re: Problem of not displaying the desired cube

    Sorry for the late reply and million thanks for the help, the cube is now shown, but why the resize function does not have the parameter passed into it?

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Problem of not displaying the desired cube

    Quote Originally Posted by openglam
    why the resize function does not have the parameter passed into it?
    I did not understand this, the resize handler will be invoked by glut when the window is resized. You just pass the function pointer, the arguments will be given in by the glut framework.
    Regards,
    Mobeen

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    24

    Re: Problem of not displaying the desired cube

    Thanks for the answer, what I mean is don't we need to put any numbers into nw and nh in the resize function? Coz I can't see any actual parameters inserted into it.

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Problem of not displaying the desired cube

    Quote Originally Posted by openglam
    Thanks for the answer, what I mean is don't we need to put any numbers into nw and nh in the resize function? Coz I can't see any actual parameters inserted into it.
    The nw and nh will be passed in the new width and new height by the glut framwork automatically. This is pretty simple. What glut is doing is it has stored a pointer underneath that is assigned to the function you have passed when u call glutReshapeFunc. Now when u resize the window, the window resize event is raise with the new width and new height passed in. glut simply sends these new values to your function. So its something like this
    Code :
    WndProc(WPARAM wParam, LPARAM lParam, ...) {//the window proc. func
    switch(event) {
       case WM_SIZE:
         int nw = LOWORD(lParam); 
         int nh = HIWORD(lParam);
         resize(nw,nh);
       break;
    }
    }
    Regards,
    Mobeen

  8. #8
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    24

    Re: Problem of not displaying the desired cube

    Thanks~~mobeen,you are a good teacher~Clear explanation~~I think I understand what you mean~

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    24

    Re: Problem of not displaying the desired cube

    And I am wondering what the function of glPush/Pull Matrix is, when I comment out 2 of them, it seems that it does not have visible changes

    here is my code

    Code :
    ....
            glPushMatrix();
    	glTranslatef(0,1,0);
    	glutSolidCube(2.0);
    	glPopMatrix();
    ...

  10. #10
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Problem of not displaying the desired cube

    Quote Originally Posted by openglam
    And I am wondering what the function of glPush/Pull Matrix is, when I comment out 2 of them, it seems that it does not have visible changes

    here is my code

    Code :
    ....
            glPushMatrix();
    	glTranslatef(0,1,0);
    	glutSolidCube(2.0);
    	glPopMatrix();
    ...
    glPush/PopMatrix functions allow u to concatenate transformations. In this simple case they are not needed. I copied this snippet from a bigger code where i was building a hierarchy of boxes linked as a chain. IN posting here i forgot to remove the last glPush/PopMatrix call.
    Regards,
    Mobeen

Page 1 of 2 12 LastLast

Similar Threads

  1. 3D cube not displaying correctly
    By Syzygy187 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-12-2015, 06:36 AM
  2. Visible triangle edges (when none are desired)
    By nealj in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 01-19-2012, 03:56 PM
  3. KeyboardFunc not working as desired
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-14-2005, 05:04 AM
  4. Radeon 9700 color dept problem - desired: 32 bit, get: 16 bit
    By XFire in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 12-04-2002, 03:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean