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(&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.
}

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.

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.

#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(&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.
}

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?

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.

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


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

Thanksmobeen,you are a good teacher~Clear explanationI think I understand what you mean~

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


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

Thanks mobeen~ Now I have modified my code in order to display a cylinder, but white screen appears again:(


////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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"
// 
//////////////////////////////////////////////////////////////////
GLUquadric* qobj;
void init_gl() {
	glClearColor(1,1,1,1);
	qobj = gluNewQuadric(); //to create the quadric object
}

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 cylinder(){ 
	glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
	glLoadIdentity();
	gluLookAt( 0,0,7, 0,0,0, 0,1,0 );
	glMultMatrixf( gsrc_getmo() ); 
	glColor3f(1,0,0); // Set line segment color to red.
	//glPushMatrix();
	gluCylinder(qobj, 30.0, 30.0, 40.0, 10.0, 10.0);
	//glPopMatrix();
	glFinish();
	glutSwapBuffers();
}

void main (int argc, char** argv)
{ 
	glutInit(&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 Cylinder Display" ); // Create display window.
	init_gl();
  //////////////////////////////////////////////////////////////////
  // 
  // Register mouse-click and mouse-move glut callback functions
  // for the rotation user-interface.
  // 
  glutMouseFunc( gsrc_mousebutton );
  glutMotionFunc( gsrc_mousemove );
  //
  //////////////////////////////////////////////////////////////////
	glutDisplayFunc(cylinder); 
	glutReshapeFunc(resize);
	glutMainLoop(); // Display everything and wait.
}

Could u comment out this line and see if u get the cylinder.


glMultMatrixf( gsrc_getmo() );

No, unfortunately, white screen again :frowning:

since the size of the cylinder is big change the projection matrix to


gluPerspective( 45, double(nw)/nh, 0.1, 100 );

Your cylinder has dimensions far too large to fit into your viewing volume. Try

gluCylinder (qobj, 1.0, 0.5, 0.5, 10.0, 10.0);

Thanks bros~ But I wanna know the unit of the parameters of glucylinder, because I wanna create a cylinder with 30x30x40 dimension, so I try to plug in 30,30 and 40 as the parameters

30 means the 30 world units. You camera is placed at world pos ( 0,0,7) i.e. 7 units on Z axis while the cylinder is 30 units in size clearly you are inside the cylinder and hence u cannot see it. Solution is to either reduce the cylinder size to a normalized size by dividing by the maximum dimension or place the camera a bit further away from the object.

Thanks maxH and mobeen, I’ve made the cylinder appears, but when I tried to drag the mouse to view in different angles, the appearance looks weird :frowning:

The following is my modified code


void resize(int nw, int nh) {
   glViewport(0,0,nw,nh);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective( 45, double(nw)/nh, 0.1, 100 );
  // gluPerspective( 45, 2.0, 0.1, 100 );
   glMatrixMode(GL_MODELVIEW);  
}

void cylinder(){ 
	glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
	glLoadIdentity();
	gluLookAt( 0,0,120, 0,0,0, 0,1,0 );
	glMultMatrixf( gsrc_getmo() ); 
	glColor3f(1,0,0); // Set line segment color to red.
	//glPushMatrix();
	gluCylinder(qobj, 30.0, 30.0, 40.0, 40.0, 40.0);
	//glPopMatrix();
	glFinish();
	glutSwapBuffers();
}

Problem solved when I increase the distance between the viewing angle and the far clipping plane :slight_smile:

I think your program is working. gluCylinders do not have top and bottom surfaces. If you look at them along their axis of symmetry (or close to it) you’ll see out the other end. Like looking down a fat tube. Perspective projections can also make things look distorted, particularly if no lighting is being used. Add some clipping and you get the image you posted. Play around with it some more. Rotate it. Scale it to different sizes. Maybe add a green cube inside the cylinder. Things will start to make sense. Do a Google image search on ‘gluCylinder’. You’ll see lots of pictures, some of which look similar to the one you posted.