Why cant I see my whole object?

I just started trying to learn OpenGL, and I cant get a simple program to work. I can open a window using GLUT, but when I tried to make a colored pyramid, i could only see the nearest corner of it. I have been reading for hours, and looking at other peoples code, but I cant figure out whats wrong with mine.

#include <GL/glut.h>
#include <GL/gl.h>
#include <unistd.h>

void myDisplayFunction(void);

int main(int argc, char **argv){

glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(700,500);
glutCreateWindow(“First OpenGL Program”);

glClearColor(0,0,0,0);

glutDisplayFunc(&myDisplayFunction);
glutIdleFunc(&myDisplayFunction);

glutMainLoop();

}

void myDisplayFunction(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0,-.5,.5);
glRotatef(-15,1,0,0);

glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(0,0,1);
glVertex3f(-1,0,2);

glColor3f(0,0,1);
glVertex3f(-1,0,2);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);

glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,0,1);
glVertex3f(-1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);

glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);

glutSwapBuffers();
}

With a small change (highlighted with >>> ) I managed to get the pyramid to appear in my scene. With a fixed value in Rotatef the pyramid will not move as each scene is restarted. What you need to do is replace it with a global variable which is incremented at the end of the scene.

EG. If you want it to spin slowly round increment rotation by a small amount with the code:
rotation+=0.05;
Then in rotatef do the following:
rotatef(rotation,1,0,0);
to get it to show the next rotation position.

Hopefully that will help you.

Tina

>>> glTranslatef(0,0,-5);
glRotatef(15,1,0,0);

glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(0,0,1);
glVertex3f(-1,0,2);

glColor3f(0,0,1);
glVertex3f(-1,0,2);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);

glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,0,1);
glVertex3f(-1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);

glColor3f(1,0,0);
glVertex3f(0,1,1);
glColor3f(0,1,0);
glVertex3f(1,0,2);
glColor3f(1,0,1);
glVertex3f(0,0,0);
>>> glEnd();

I added what you put to my code, but then it didnt show any of my pyramid. What it looks like to me is that its not displaying anything farther than 1 unit away. When I have no transaltion i can see the closest half of the pyramid, but no more. When i have it rotating as you suggested, it looks like there are even more issues. I cant say i know much about this stuff, but i would guess that my initialization code is wrong, and thats why its only displaying part of my pyramid.

Aha, definitely sounds like the far and near settings are not set up… I’ll take a gander at one of my GLUT programs to double check what I did there…

Aha, I get a similar effect with my code… I’ll try and sort it out and get back to you. Probably some extra settings needed.

EDIT: Well, here’s the smallest GLUT program with your code (and my changes in). I now get to see all of your pyramid as it rotates around. The secret is in the Resize routine where the Perspective View is set up. Dependant on what the last two values are (farpoint/nearestpoint) would make your pyramid appear slightly different. Note this is a win32 app but most if not all of the commands should be able to be copied into what seems to be a unix based program.

======================================
#include <windows.h> //header file for windows

#include <gl\gl.h> //header file for openGL
#include <gl\glut.h> //header file for GLUT

/// Windows Only to allow the program to start with the routine main like the old dos programs.
#pragma comment( linker, “/entry:“mainCRTStartup”” ) // set the entry point to be main()

static void glut_render(void);
void glut_resize(int w, int h);

float xrot;

int main(int argc, char **argv)
{

// Initialise
glutInit(&argc,argv);                                       //initializes the GLUT framework
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   //sets up the display mode
glutInitWindowSize(640,480);
glutCreateWindow("Glut Test");                  //creates a window

// Functions
glutDisplayFunc(glut_render);    //specifies our redraw function
glutReshapeFunc(glut_resize);
glutIdleFunc(glut_render);

glutMainLoop();               //the main loop of the GLUT framework
return 0;

}

void glut_resize(int w, int h)
{
if(h == 0) h = 1;
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1.0,1.0,-200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void glut_render(void)
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0,0,-5);
glRotatef(xrot,1,0,0);

glBegin(GL_TRIANGLES);
	glColor3f(1,0,0);		glVertex3f(0,1,1);
	glColor3f(0,1,0);		glVertex3f(1,0,2);
	glColor3f(0,0,1);		glVertex3f(-1,0,2);

	glColor3f(0,0,1);		glVertex3f(-1,0,2);
	glColor3f(0,1,0);		glVertex3f(1,0,2);
	glColor3f(1,0,1);		glVertex3f(0,0,0);

	glColor3f(1,0,0);		glVertex3f(0,1,1);
	glColor3f(0,0,1);		glVertex3f(-1,0,2);
	glColor3f(1,0,1);		glVertex3f(0,0,0);

	glColor3f(1,0,0);		glVertex3f(0,1,1);
	glColor3f(0,1,0);		glVertex3f(1,0,2);
	glColor3f(1,0,1);		glVertex3f(0,0,0);
glEnd();
xrot++;
glFlush();
glutSwapBuffers();

}

[This message has been edited by tinak (edited 08-31-2002).]

The code you posted works fine, until i try to resize the window, then everything goes blank again. I can put the object far in the distance, which was my original problem, but resizing the window always makes it go blank.

Thanks a lot for your help.

Hmm, will have to try the resizing. Maybe in glut there is something else that needs to be done similar to the initWindowSize command to reinitialise the window with the new size.

The glutreshape is called when the window is reshaped.
I have no problem with the screen being reshaped.

I could be after a reshape call you have put the view inside the triangle.

try translating the triangle about 5 units on the z-axis.

Originally posted by tinak:
Hmm, will have to try the resizing. Maybe in glut there is something else that needs to be done similar to the initWindowSize command to reinitialise the window with the new size.

[This message has been edited by nexusone (edited 09-01-2002).]

I looked at the program and made the following changes. Now resizes correctly.

#include <gl\glut.h> //header file for GLUT

/// Windows Only to allow the program to start with the routine main like the old dos programs.
//#pragma comment( linker, “/entry:"mainCRTStartup"” ) // set the entry point to be main()

static void glut_render(void);
void glut_resize(int w, int h);

float xrot;

int main(int argc, char **argv)
{

// Initialise
glutInit(&argc,argv); //initializes the GLUT framework
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); //sets up the display mode
glutInitWindowSize(640,480);
glutCreateWindow(“Glut Test”); //creates a window

// Functions
glutDisplayFunc(glut_render); //specifies our redraw function
glutReshapeFunc(glut_resize);
glutIdleFunc(glut_render);

glutMainLoop(); //the main loop of the GLUT framework
return 0;
}

void glut_resize(int w, int h)
{

glViewport(0,0, (GLsizei) w, (GLsizei) h); // added… new window size
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1.0,1.0,20.0); // You had -200, resize change direction of view.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void glut_render(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0,0,-5);
glRotatef(xrot,1,0,0);

glBegin(GL_TRIANGLES);
glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(0,0,1); glVertex3f(-1,0,2);

glColor3f(0,0,1); glVertex3f(-1,0,2);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);

glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,0,1); glVertex3f(-1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);

glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);
glEnd();
xrot++;
glFlush();
glutSwapBuffers();
}

Aha, great… also the extra LoadIdentity() there seemed to allow it to appear the -200 to 20 and the viewport addition didn’t seem to make a difference until I added that extra LoadIdentity

Tina

Yes, everything I added needed to be used.

What the loadidentity does is put the matrix a reset starting point.

Else you get a wrong view from any new changes.

Originally posted by tinak:
[b]Aha, great… also the extra LoadIdentity() there seemed to allow it to appear the -200 to 20 and the viewport addition didn’t seem to make a difference until I added that extra LoadIdentity

Tina[/b]