How do I stop the cube being clipped

I’m studying opengl 2.1 because that’s what my second-hand machines support.

I’ve managed to hack together some code to display a texture mapped cube over some alternating colours in the background. My only problem is the corners of the cube get clipped. How can I stop this happening?

//-lglut -lGLEW -lGL -lGLU -lm

#include <GL/glew.h> // Include the GLEW header file  
#include <GL/glut.h> // Include the GLUT header file  
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <iostream>

GLuint texture;
GLfloat angle = 0.0;

// Rotate X
double rX=0;
// Rotate Y
double rY=0;

bool running = true;
bool inc = false;
bool inc2 = false;
bool inc3 = false;
bool inc4 = false;
  
bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255)   

float yVal = 0.0f;
float yVal2 = 0.1f;
float yVal3 = 0.0f;
float yVal4 = 0.1f;

GLuint LoadBMP(const char *fileName)
{
FILE *file=fopen(fileName,"rb");
if (!file)
{
printf("Image could not be loaded\n");
return 0;
}

unsigned char header[54];
fread(header,1,54,file);

unsigned int dataPos=*(int*)&(header[0x0A]);
unsigned int imageSize=*(int*)&(header[0x22]);
unsigned int width=*(int*)&(header[0x12]);
unsigned int height=*(int*)&(header[0x16]);

if(imageSize == 0)
{
imageSize = width*height*3;
}
if(dataPos == 0)
{
dataPos = 54;
}

unsigned char *data = new unsigned char[imageSize];
fread(data,1,imageSize,file);
fclose(file);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_BGR,GL_UNSIGNED_BYTE,data);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete(data);

return texture;
}

void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}

void keyOperations(void)
{
if(keyStates['a'])
{
}
}

void drawCube(void)
{
glBindTexture(GL_TEXTURE_2D, texture);

glScalef(-0.5f, -0.5f, -0.5f);

glBegin(GL_QUADS);

//front
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(-1.0f,1.0f,1.0f);

//back
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(-1.0f,1.0f,-1.0f);

//top
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(-1.0f,1.0f,1.0f);

//bottom
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(1.0f,-1.0f,1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(-1.0f,-1.0f,1.0f);

//right
glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(1.0f,-1.0f,1.0f);

//left
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f)  ;glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(1.0f,1.0f)  ;glVertex3f(-1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f)  ;glVertex3f(-1.0f,-1.0f,1.0f);

glEnd();
}

void display(void)
{
keyOperations();

glClearColor(0.0f,0.0f,0.0f,1.0f);//set background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//set the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil. 
glLoadIdentity();

if(inc)
yVal -= 0.005f;
else
yVal += 0.005f;

if(yVal<-0.1f)
inc=false;
else if (yVal>1.0f)
inc=true;


if(inc2)
yVal2 -= 0.005f;
else
yVal2 += 0.005f;

if(yVal2<-0.1f)
inc2=false;
else if (yVal2>1.0f)
inc2=true;


if(inc3)
yVal3 -= 0.005f;
else
yVal3 += 0.005f;

if(yVal3<-1.0f)
inc3=false;
else if (yVal3>1.0f)
inc3=true;

if(inc4)
yVal4 -= 0.005f;
else
yVal4 += 0.005f;

if(yVal4<-0.5f)
inc4=false;
else if (yVal4>1.0f)
inc4=true;



// Draw the cube
glColor3f(yVal, 0.0, 0.0); // Red
glRectf(-1.0,1.0,1.0,0.5);

glColor3f(0.0, yVal2, 0.0); // Green
glRectf(-1.0,0.5,1.0,0.0);

glColor3f(yVal3, 0.25, 0.0); // Orange
glRectf(-1.0,0.0,1.0,-0.5);

glColor3f(0.0, yVal4, 0.25); // Orange
glRectf(-1.0,-0.5,1.0,-1.0);

glutPostRedisplay();

glRotatef( rX, 1.0, 0.0, 0.0 );

// angle Specifies the angle of rotation, in degrees.
//x, y, z Specify the x, y, and z coordinates of a vector, respectively.

glRotatef( rY, 0.0, 1.0, 0.0 );

rX+=0.1;
rY+=0.1;

drawCube();

glutSwapBuffers();

}


void reShape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)w/(GLfloat)h, 1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}



void init()
{
glEnable(GL_TEXTURE_2D);
texture=LoadBMP("Me8.bmp");
}



void reshape(int width, int height)
{

glViewport(0,0,(GLsizei)width, (GLsizei)height);
//Parameters
//x, y
//Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).
//width, height
//Specify the width and height of the viewport. When a GL context is first attached to a window, width and height are set to the
//dimensions of that window.

glMatrixMode(GL_PROJECTION); //glMatrixMode
glLoadIdentity();//replace the current matrix with the identity matrix

glMatrixMode(GL_MODELVIEW);
}

void keyPressed(unsigned char key, int x, int y)
{
keyStates[key]=true;
}

void keyUp(unsigned char key, int x, int y)
{
keyStates[key]=false;
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);//initialize the GLUT library.
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//sets the initial display mode.
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Photo Example With Coloured Background");

init();

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);//glutDisplayFunc

glutReshapeFunc(reshape);//sets the reshape callback for the current window.

glutKeyboardFunc(keyPressed);//sets the keyboard callback for the current window

glutKeyboardUpFunc(keyUp);//glutKeyboardUpFunc

glutMainLoop();//enters the GLUT event processing loop.

return 0;
}

Both the cube and the background are centred on Z=0. And you’re using depth testing, so parts of the cubes with Z>0 will be obscured by the background.

You should also use e.g. glOrtho to construct the projection matrix to increase the range of Z values (it’s currently -1 to 1, so the cube isn’t going to fit between the background and the near plane).

Whereabouts should I put it in the program and what values should I set it to to stop the clipping?

Where should I put the cube on the z plane to stop the clipping?

As he said, you are inside the cube (0,0,0). Your box goes from -1.0 z depth to 1.0 z depth.

glVertex3f() for front/back/top/bottom all need z-depths to be between +3 and +4. Yours are -1.0 to 1.0. Half the box is behind you. Your perspective matrix there says camera plane starts 1 unit away.

So at glLoadIdentity(), you are at 0,0,0 and your front plane clips 1 unit out. Surprised you see any cube at all. glTranslateF(0,0,-4); will also push the box 4 units into your computer screen.

When I Implement this I just get a black screen. Please tell me what I am doing wrong so |I can make this demo! Thanks.I have :

//-lglut -lGLEW -lGL -lGLU -lm

#include <GL/glew.h> // Include the GLEW header file  
#include <GL/glut.h> // Include the GLUT header file  
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <iostream>

GLuint texture;
GLfloat angle = 0.0;

const float zNNear=4.0;
const float zFar=-3.0;

// Rotate X
double rX=0;
// Rotate Y
double rY=0;

bool running = true;
bool inc = false;
bool inc2 = false;
bool inc3 = false;
bool inc4 = false;
  
bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255)   

float yVal = 0.0f;
float yVal2 = 0.1f;
float yVal3 = 0.0f;
float yVal4 = 0.1f;

GLuint LoadBMP(const char *fileName)
{
FILE *file=fopen(fileName,"rb");
if (!file)
{
printf("Image could not be loaded\n");
return 0;
}

unsigned char header[54];
fread(header,1,54,file);

unsigned int dataPos=*(int*)&(header[0x0A]);
unsigned int imageSize=*(int*)&(header[0x22]);
unsigned int width=*(int*)&(header[0x12]);
unsigned int height=*(int*)&(header[0x16]);

if(imageSize == 0)
{
imageSize = width*height*3;
}
if(dataPos == 0)
{
dataPos = 54;
}

unsigned char *data = new unsigned char[imageSize];
fread(data,1,imageSize,file);
fclose(file);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_BGR,GL_UNSIGNED_BYTE,data);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete(data);

return texture;
}

void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}

void keyOperations(void)
{
if(keyStates['a'])
{
}
}

void drawCube(void)
{
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);

//front
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zNNear);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zNNear);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);

//back
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zFar);

//top
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);

//bottom
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,-0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,-0.5f,zNNear);

//right
glTexCoord2f(0.0f,0.0f);glVertex3f(0.5f,-0.5f,-zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(0.5f,-0.5f,zNNear);

//left
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(-0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,-0.5f,zNNear);

glEnd();
}

void display(void)
{
keyOperations();

glClearColor(0.0f,0.0f,0.0f,1.0f);//set background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//set the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil. 
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);
glutPostRedisplay();

if(inc)
yVal -= 0.005f;
else
yVal += 0.005f;

if(yVal<-0.1f)
inc=false;
else if (yVal>1.0f)
inc=true;


if(inc2)
yVal2 -= 0.005f;
else
yVal2 += 0.005f;

if(yVal2<-0.1f)
inc2=false;
else if (yVal2>1.0f)
inc2=true;


if(inc3)
yVal3 -= 0.005f;
else
yVal3 += 0.005f;

if(yVal3<-1.0f)
inc3=false;
else if (yVal3>1.0f)
inc3=true;

if(inc4)
yVal4 -= 0.005f;
else
yVal4 += 0.005f;

if(yVal4<-0.5f)
inc4=false;
else if (yVal4>1.0f)
inc4=true;


// Draw the cube
glColor3f(yVal, 0.0, 0.0); // Red
//glRectf(-1.0,1.0,1.0,0.5);
glBegin(GL_QUADS);
glVertex3f(0.25,0.25,0.75);
glVertex3f(0.75,0.25,0.75);
glVertex3f(0.75,0.75,0.75);
glVertex3f(0.25,0.75,0.75);

glVertex3f(-1.0,1.0,2.0);
glVertex3f(1.0,0.5,2.0);

glEnd();

//glColor3f(0.0, yVal2, 0.0); // Green
//glRectf(-1.0,0.5,1.0,0.0);

//glColor3f(yVal3, 0.25, 0.0); // Orange
//glRectf(-1.0,0.0,1.0,-0.5);

//glColor3f(0.0, yVal4, 0.25); // Orange
//glRectf(-1.0,-0.5,1.0,-1.0);

glRotatef( rX, 1.0, 0.0, 0.0 );

// angle Specifies the angle of rotation, in degrees.
//x, y, z Specify the x, y, and z coordinates of a vector, respectively.

glRotatef( rY, 0.0, 1.0, 0.0 );

rX+=0.1;
rY+=0.1;

//drawCube();

glutSwapBuffers();

}


void reShape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,10.0,10.0);
//glOrtho(0.0,0.0,0.0,0.0,4.0,4.0);
gluPerspective(60,(GLfloat)w/(GLfloat)h, 1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}



void init()
{
glEnable(GL_TEXTURE_2D);
texture=LoadBMP("Me8.bmp");
}



void reshape(int width, int height)
{

glViewport(0,0,(GLsizei)width, (GLsizei)height);
//Parameters
//x, y
//Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).
//width, height
//Specify the width and height of the viewport. When a GL context is first attached to a window, width and height are set to the
//dimensions of that window.

glMatrixMode(GL_PROJECTION); //glMatrixMode
glLoadIdentity();//replace the current matrix with the identity matrix

glMatrixMode(GL_MODELVIEW);

}

void keyPressed(unsigned char key, int x, int y)
{
keyStates[key]=true;
}

void keyUp(unsigned char key, int x, int y)
{
keyStates[key]=false;
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);//initialize the GLUT library.
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//sets the initial display mode.
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Photo Example With Coloured Background");

init();

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);//glutDisplayFunc

glutReshapeFunc(reshape);//sets the reshape callback for the current window.

glutKeyboardFunc(keyPressed);//sets the keyboard callback for the current window

glutKeyboardUpFunc(keyUp);//glutKeyboardUpFunc

glutMainLoop();//enters the GLUT event processing loop.

return 0;
}

Please inspect.

Sorry, I meant both should be minus (-). To push objects into the screen, you are moving across negative z-axis, so all points on the cube need to be less than -1.0 since your perspective near parameter is 1.0 from camera center. Your camera is (0,0,0). The film projection plane would then be 1.0 unit away (-1.0). So your triangles need to be further than -1.0. Also glDisabl(GL_CULL_FACE) if you still cant render. It could be that you have the winding order wrong.
const float zNNear=-3.0;
const float zFar=-4.0;

Note that his code has two functions for setting up a projection matrix: reShape is confused about whether it’s setting an orthographic projection or a perspective projection, but that function isn’t actually used. The reshape function (which is used) sets an identity matrix.

OP needs to read a book or tutorial on OpenGL transformations. This is something you need to actually understand. Cargo cult-ism won’t work here.