Programming - Games - OpenGL and C++

This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
Copyright (c) 2004 is owned by Martin Baker

Entering the World of 3D:

In this Tutorial we will made 3D objects rather then 2D. This tutorial is in continuation with the previous one. In this section we will change the same method ' DrawGLScene ' to draw 3D objects.

We will create cube by adding left, right, back, top and bottom to the squar and make pyramid by adding left, back, and right side to the triangle.Here is the code for pyramid.

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);

/* Front side with Red Color */

glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);

/* Right side with Blue Color */

glColor3f(0.0f,0.0f,1.0f);

glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);

/* Back side with Red Color */

glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);

/* Left Side with Green Color */

glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();

// Continue .....

First 6 lines are same.We have already draw the front side of the triangle in our previous tutorial. As u can see in the remaining part of the code we did nothing , just made 3 more sides(right, back , left ) with different colors. You can also use shades of different colors as we defined in our previous tutorial.

Here is the code for Cube.

glLoadIdentity();
glTranslatef(1.5f,0.0f,-7.0f);
glBegin(GL_QUADS);

/* Top Side of the Cube with different shades of colors */

glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left
glColor3f(0.0f,1.0f,1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right

/* Bottom Side of the Cube with different shades of colors */

glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right

/* Front Side of the Cube with different shades of colors */

glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right

/* BackSide of the Cube with different shades of colors */

glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left
glColor3f(0.0f,1.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right

/* LeftSide of the Cube with different shades of colors */

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right
glColor3f(0.0f,0.5f,1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left
glColor3f(1.0f,0.5f,0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right

/* Right Side of the Cube with different shades of colors */

glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left
glColor3f(0.0f,1.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left
glColor3f(1.0f,0.5f,1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right

glEnd();
return TRUE;
}

Same as previous we made other more 5 sides(back,botton,top,left,right) to draw a Cube.

Your output will be like .

Download the Source Code.

 

 

next: rotation


metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover 3D Game Creation - Includes 2 ready to use game engines, not much theory.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual C++ .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2015 Martin John Baker - All rights reserved - privacy policy.