My First 3D Application

Hi there, I have taken an existing 2D app which just draws a box, and I am trying to add depth to it. It draws a quad on the plane, then it trys ( and fails ) to draw a box of distance -5 units away. (So it will look smaller by my calculations).

The app isn’t drawing the second box… I cannot for the life of me figure out why…

Here is the code:

#include <iostream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

const int windowWidth=300;
const int windowHeight=400;
using namespace std;

void display(void) // draws top to bottom

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

    glColor3f (1.0, 0.0, 0.0);	// (red,green,blue) colour components

    glBegin(GL_QUADS); // draw quad on surface
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);

    glColor3f (0.0, 1.0, 0.0);
    glTranslatef(0, 0.6, -5); //has to be called outside of glBegin & end * 0.0f in third arg is default


    glBegin(GL_QUADS); // try and draw quad which is further away and thus will look smaller..
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);


    glutSwapBuffers(); // Need to use when glutdisplaymode(GLUT_DOUBLE) instead of glflush();

void reshape(int w, int h) {
    GLdouble halfWidth=(GLdouble) w/2.0;
	GLdouble halfHeight=(GLdouble) h/2.0;



	//instead of gluortho2D
    // Calculate The Aspect Ratio Of The Window
	gluPerspective(0.0f,(GLfloat)w/(GLfloat)h, 0.1f, 100.0f); //FOV Y, aspect, Znear, Zfar
	//1: 45.0 = 45degrees above all objects drawn.. so 90 would be looking down on the objects
	//2: always w/h radio
	//3: The cloest objects we will draw ( not clip )
	//4: the furtherest objects we will draw ( not clip )
	//NOTE : gluPerspective means sets the entire window size to 2.0f, so objects need to be draw in proportion to this.

	glMatrixMode(GL_MODELVIEW);      //It is used to move objects around scene


void init(void)

	glClearColor (1.0, 1.0, 1.0, 0.0);	// RGB-value for white
    glViewport(0,0,windowWidth, windowHeight);


	gluPerspective(0.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.0f,100.0f); //FOV Y, aspect, Znear, Zfar



int main(int argc, char** argv)
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(windowWidth, windowHeight);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("My first OpenGL program");
	init ();						// initialise view
	glutDisplayFunc(display);		// draw scene
	return 0;

try +5 in translate…

yeh tried that… doesn’t work

Why do you set a 0 to the fov parameter??

i don’t don’t know how gluPerspective handles that, but that implies a division by zero.

Ok well I have changed that 0.1f…does anyone actually have any idea what the problem is? The snippet will compile fine…

No I am talking about the 1st parameter 'fild of view", it should not be 0. Typically it is set to 45.0f

and talking about near plane, set a greater value that 0, 0.1 would be fine at first.


and look at this to konw what I am talking about…