Why am i outside the cube? .....just glTranslate question

Hi

the code below draws a 5x5x5 cube in the center of the OGL space,

As you can see, I have positioned the “camera” at (0,0,-2.5).

My problem is that when i use glTranslate in this way:

glTranslated(0,0,-4.5);

the “camera” is outside the cube. But when i write
glTranslated(0,0,-4.0; im inside the cube.

If the cube is 5x5x5, why happens this?

 

#include <stdafx.h>
#include <glut.h>
#include <math.h>

#include <iostream>
using namespace std;

//angle of rotation
double m=5;
double n=6;
double o=10;
double b=m/n;
double c=2*b;
double f=3*b;
int i=0;
double p=2;
double q=12;
double d1=(m*sqrt(p))/q;


//double array[11]={0, 0, };

float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;



void init (void) {
//cubepositions();
}

void enable (void) {
	glEnable (GL_DEPTH_TEST); //enable the depth testing
	//dglEnable (GL_LIGHTING); //enable the lighting
	glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
	glShadeModel (GL_SMOOTH); //set the shader to smooth shader
}

void camera (void) {
	

	glTranslated(0,0,-4.5); 
}

void display (void) {
	glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
    glLoadIdentity();  
	gluLookAt (0.0, 0.0, -2.5, 0.0, 0.0, 5.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera
	camera();
	enable();
		
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    
	glBegin(GL_QUADS);           // Draw The Cube Using quads
		glColor3f(0.0f,1.0f,0.0f);     // Color Blue
		glVertex3f( 2.5f, 2.5f,-2.5f);    // Top Right Of The Quad (Top)
		glVertex3f(-2.5f, 2.5f,-2.5f);    // Top Left Of The Quad (Top)
		glVertex3f(-2.5f, 2.5f, 2.5f);    // Bottom Left Of The Quad (Top)
		glVertex3f( 2.5f, 2.5f, 2.5f);    // Bottom Right Of The Quad (Top)

		glColor3f(1.0f,0.5f,0.0f);     // Color Orange
		glVertex3f( 2.5f,-2.5f, 2.5f);    // Top Right Of The Quad (Bottom)
		glVertex3f(-2.5f,-2.5f, 2.5f);    // Top Left Of The Quad (Bottom)
		glVertex3f(-2.5f,-2.5f,-2.5f);    // Bottom Left Of The Quad (Bottom)
		glVertex3f( 2.5f,-2.5f,-2.5f);    // Bottom Right Of The Quad (Bottom)

		glColor3f(1.0f,0.0f,0.0f);     // Color Red      
		glVertex3f( 2.5f, 2.5f, 2.5f);    // Top Right Of The Quad (Front)
		glVertex3f(-2.5f, 2.5f, 2.5f);    // Top Left Of The Quad (Front)
		glVertex3f(-2.5f,-2.5f, 2.5f);    // Bottom Left Of The Quad (Front)
		glVertex3f( 2.5f,-2.5f, 2.5f);    // Bottom Right Of The Quad (Front)

		glColor3f(1.0f,1.0f,0.0f);     // Color Yellow
		glVertex3f( 2.5f,-2.5f,-2.5f);    // Top Right Of The Quad (Back)
		glVertex3f(-2.5f,-2.5f,-2.5f);    // Top Left Of The Quad (Back)
		glVertex3f(-2.5f, 2.5f,-2.5f);    // Bottom Left Of The Quad (Back)
		glVertex3f( 2.5f, 2.5f,-2.5f);    // Bottom Right Of The Quad (Back)

		glColor3f(0.0f,0.0f,1.0f);     // Color Blue
		glVertex3f(-2.5f, 2.5f, 2.5f);    // Top Right Of The Quad (Left)
		glVertex3f(-2.5f, 2.5f,-2.5f);    // Top Left Of The Quad (Left)
		glVertex3f(-2.5f,-2.5f,-2.5f);    // Bottom Left Of The Quad (Left)
		glVertex3f(-2.5f,-2.5f, 2.5f);    // Bottom Right Of The Quad (Left)

		glColor3f(1.0f,0.0f,1.0f);     // Color Violet
		glVertex3f( 2.5f, 2.5f,-2.5f);    // Top Right Of The Quad (Right)
		glVertex3f( 2.5f, 2.5f, 2.5f);    // Top Left Of The Quad (Right)
		glVertex3f( 2.5f,-2.5f, 2.5f);    // Bottom Left Of The Quad (Right)
		glVertex3f( 2.5f,-2.5f,-2.5f);    // Bottom Right Of The Quad (Right)

    glEnd();
	
	
	glutSwapBuffers(); //swap the buffers
	angle++; //increase the angle
}

void reshape (int w, int h) {
	glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
	glMatrixMode (GL_PROJECTION); //set the matrix to projection
	glLoadIdentity ();
	gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0); //set the perspective (angle of sight, width, height, , depth)
	glMatrixMode (GL_MODELVIEW); //set the matrix back to model
}

void keyboard (unsigned char key, int x, int y) {
	
	

	if (key=='w')
	{
	zpos += 0.5;
	cout << zpos << endl;
	}
	
	

	

}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
	glutInitWindowSize (500, 500); //set the window size
	glutInitWindowPosition (100, 100); //set the position of the window
    glutCreateWindow ("A basic OpenGL Window"); //the caption of the window
	init (); //call the init function
    glutDisplayFunc (display); //use the display function to draw everything
	glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
	glutReshapeFunc (reshape); //reshape the window accordingly
	glutKeyboardFunc (keyboard); //check the keyboard
    glutMainLoop (); //call the main loop
    return 0;
}



 

I just went through your code very quickly, so I might not be right, but anyway here is what I think:
This could happen because you have set the near plane to 1.0 (3rd parameter to gluPerspective) and the cube gets drawn too close to the camera.
If you do the math you get the following: 2.5 (camera pos.) - 4.5 + 2.5 (size of cube) = 0.5, which is less than the near plane distance, so the cube gets culled away.
Try using a lower value for the near plane, hopefully that fixes the problem.