Drawing A 3D Scene Using OpenGL in C/C++

,
  
 
Introduction 
 
We have already applied textures to objects in the  following article.
Here, we will just draw some scene using QUADS. First, configure Visual Studio for glut. Read the following article to configure Visual Studio for glut and to get started with OpenGL.
For applying textures, we need textures image (256*256) 24 bit bitmap files.
 
Here's the scene graph image that I have drawn.
 
 
Coding 
 
I have created the following files in the project.
  • 3D_GameScene_OpenGL.cpp
  • DisplayScene.h
  • DisplayScene.cpp
  • LoadImageFile.h
  • LoadImageFile.cpp
  • DrawScene.h
  • DrawScene.cpp  
3D_GameScene_OpenGL.cpp

This is the main source file where I have defined a main() function.
 
DisplayScene.cpp 

In this file, I have used a switch case statement to draw scenes by providing different texture image files as arguments to it. I have declared class and data in DisplayScene.h header file. 
 
LoadImageFile.cpp

This file contains a function that applies the texture to the drawn objects.
 
DrawScene.cpp

In this file, I have drawn a scene or defined the function which are declared in DrawScene.h header file. 
  
See the code defined in the above files.
 
DisplayScene.h
  1. #ifndef _DISPLAYSCENE_H  
  2. #define _DISPLAYSCENE_H  
  3.   
  4. //define different textures values from 1 to 8  
  5. enum MyTextures  
  6. {  
  7.     DEFAULT = 0x01,  
  8.     ANIMATION=0x02,  
  9.     BUILDINGS=0x03,  
  10.     HORROR=0x04,  
  11.     GREENY=0x05,  
  12.     WOODS=0x06,  
  13.     ROCKS=0x07,  
  14.     CIRCUIT=0x08  
  15. };  
  16.   
  17.   
  18. //class to display whole scene  
  19. class DisplayGameScene  
  20. {  
  21. public:  
  22.     //define function to display a whole scene  
  23.     //first int is texture int  
  24.     //second int is used for where to show Roof or not  
  25.     //third BOOL is used to whether show outerwall or not  
  26.     static void DisplayTexturedGameScene(int,int,BOOL);  
  27. };  
  28.   
  29.   
  30.   
  31. #endif   
DisplayScene.cpp
  1. #include"LoadImageFile.h"  
  2.   
  3. #include"DrawScene.h"  
  4.   
  5. #include"DisplayScene.h"  
  6.   
  7.   
  8. //define function from class DisplayGameScene()  
  9. //call all function which wants to draw from class GameScene  
  10. void DisplayGameScene::DisplayTexturedGameScene(int type,int rooftype,BOOL outerwall)  
  11. {  
  12.     switch (type)  
  13.     {  
  14.         GameScene gs;  
  15.   
  16.     case DEFAULT :  
  17.   
  18.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), outerwall);  
  19.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));  
  20.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));  
  21.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));  
  22.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));  
  23.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));  
  24.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain.bmp"), rooftype,2.0);  
  25.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks3.bmp"), 6.0);  
  26.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/plain.bmp"));  
  27.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"));  
  28.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  29.   
  30.         break;  
  31.   
  32.     case ANIMATION :  
  33.   
  34.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/blocks4.bmp"), outerwall);  
  35.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"));  
  36.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon1.bmp"));  
  37.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon2.bmp"));  
  38.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon3.bmp"));  
  39.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon4.bmp"));  
  40.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), rooftype,0.5);  
  41.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), 0.5);  
  42.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"));  
  43.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"));  
  44.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  45.   
  46.         break;  
  47.   
  48.   
  49.     case BUILDINGS :  
  50.   
  51.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/metal.bmp"), outerwall);  
  52.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/building.bmp"));  
  53.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/building.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));  
  54.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/building.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));  
  55.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));  
  56.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));  
  57.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,2.0);  
  58.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), 1.0);  
  59.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));  
  60.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  61.   
  62.         break;  
  63.   
  64.   
  65.     case HORROR :  
  66.   
  67.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/horror2.bmp"), outerwall);  
  68.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/horror1.bmp"));  
  69.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/horror4.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));  
  70.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/horror1.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));  
  71.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));  
  72.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));  
  73.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), rooftype,1.0);  
  74.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/horror_floor.bmp"), 1.0);  
  75.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/horror3.bmp"));  
  76.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"));  
  77.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  78.   
  79.         break;  
  80.   
  81.     case GREENY :  
  82.   
  83.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/beans.bmp"), outerwall);  
  84.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"));  
  85.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));  
  86.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));  
  87.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));  
  88.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));  
  89.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/grass.bmp"), rooftype,1.0);  
  90.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/grass.bmp"), 1.0);  
  91.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"));  
  92.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/sky001.bmp"));  
  93.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  94.   
  95.         break;  
  96.   
  97.     case WOODS :  
  98.   
  99.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/plain.bmp"), outerwall);  
  100.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"));  
  101.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));  
  102.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));  
  103.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));  
  104.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));  
  105.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,1.0);  
  106.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), 1.0);  
  107.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));  
  108.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/wood2.bmp"));  
  109.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  110.   
  111.         break;  
  112.   
  113.     case ROCKS :  
  114.   
  115.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), outerwall);  
  116.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));  
  117.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));  
  118.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));  
  119.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"));  
  120.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"));  
  121.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,1.0);  
  122.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), 1.0);  
  123.         gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"));  
  124.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));  
  125.         gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));  
  126.   
  127.         break;  
  128.   
  129.     case CIRCUIT:  
  130.   
  131.         gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/circuitboard3.bmp"), outerwall);  
  132.         gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"));  
  133.         gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/circuitboard2.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));  
  134.         gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));  
  135.         gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));  
  136.         gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));  
  137.         gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"), rooftype, 1.0);  
  138.         gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/circuit.bmp"), 1.0);  
  139.         gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"));  
  140.   
  141.         break;  
  142.     }  
  143. }   
LoadImageFile.h
  1. #ifndef _LOADIMAGEFILE_H  
  2. #define _LOADIMAGEFILE_H  
  3.   
  4. #include<Windows.h>  
  5. #include<stdio.h>  
  6. #include<gl/GL.h>  
  7. #include<GL/GLU.h>  
  8. #include<gl/glut.h>  
  9. #include<stdio.h>  
  10. #include<math.h>  
  11.   
  12.   
  13. class LoadTexture  
  14. {  
  15. public :  
  16.     static GLuint LoadTextureImageFile(const char *);  
  17.     static void FreeCreatedTexture(GLuint);  
  18. };  
  19.   
  20.   
  21.   
  22.   
  23. #endif   
LoadImageFile.cpp
  1. #include"LoadImageFile.h"  
  2.   
  3. //load image & set it as a texture with dimension as 256*256  
  4.   
  5. GLuint LoadTexture :: LoadTextureImageFile(const char * filename)  
  6. {  
  7.     GLuint texture = 0;  
  8.     int width, height;  
  9.     BYTE * data = NULL;  
  10.     FILE * file;  
  11.   
  12.     // open texture data  
  13.     fopen_s(&file, filename, "rb");  
  14.   
  15.     if (&file == NULL) return 0;  
  16.   
  17.     // allocate buffer  
  18.     width = 256;  
  19.     height = 256;  
  20.   
  21.     data = (BYTE*)malloc(width * height * 3);  
  22.   
  23.     // read texture data  
  24.     fread(data, width * height * 3, 1, file);  
  25.     fclose(file);  
  26.   
  27.     glGenTextures(1, &texture);  
  28.   
  29.     glBindTexture(GL_TEXTURE_2D, texture);  
  30.   
  31.     //  texture MIP maps  
  32.     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_BGRA_EXT, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);  
  33.   
  34.   
  35.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COLOR);  
  36.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
  37.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
  38.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
  39.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
  40.   
  41.     // free buffer  
  42.     free(data);  
  43.   
  44.     return texture;  
  45.   
  46. }  
  47.   
  48.   
  49.   
  50. void LoadTexture :: FreeCreatedTexture(GLuint texture)  
  51. {  
  52.     glDeleteTextures(1, &texture);  
  53. }   
DrawScene.h
  1. #ifndef _DRAWSCENE_H  
  2. #define _DRAWSCENE_H  
  3.   
  4. //declare GameScene class & functions which needs to draw  
  5. class GameScene  
  6. {  
  7. public:  
  8.     void Draw_OuterWall(GLuint,BOOL);  
  9.   
  10.     void Draw_InnerRoom_1(GLuint);  
  11.   
  12.     void Draw_InnerRoom_2(GLuint,GLuint);  
  13.   
  14.     void Draw_InnerRoom_3(GLuint,GLuint);  
  15.   
  16.     void Draw_InnerRoom_4(GLuint,GLuint);  
  17.   
  18.     void Draw_InnerRoom_5(GLuint,GLuint);  
  19.   
  20.   
  21.     void Draw_Roof(GLuint, int,float);  
  22.   
  23.     void Draw_Floor(GLuint, float);  
  24.   
  25.     void Draw_Pipes(GLuint);  
  26.   
  27.     void Draw_WoodBoxes(GLuint);  
  28.   
  29.     void Display_Text(GLuint);  
  30.   
  31. };  
  32.   
  33.   
  34. #endif   
DrawScene.cpp

Download the source code to view the complete code of 3D scene. Here, I am just displaying functions to draw Draw_Floor(), Draw_OuterWall(), and Draw_InnerRoom_1() because of large code. 
  1. #include"LoadImageFile.h"  
  2. #include"DrawScene.h"  
  3.   
  4.   
  5. //define each function from file DrawScene.h from class GameScene  
  6.   
  7.   
  8. //draw floor  
  9. void GameScene::Draw_Floor(GLuint texture,float f)  
  10. {  
  11.     glEnable(GL_TEXTURE_2D);  
  12.     glBindTexture(GL_TEXTURE_2D, texture);  
  13.   
  14.   
  15.     glPushMatrix();  
  16.   
  17.     for (float a = -10.0; a <= 170.0; a = a + 10.0)  
  18.     {  
  19.         glBegin(GL_QUADS);  
  20.         glTexCoord2f(0.0, 0.0);  
  21.         glVertex3f(0.0 + a, 0.0, 10.0);  
  22.         glTexCoord2f(0.0, f);  
  23.         glVertex3f(0.0 + a, 0.0, 0.0);  
  24.         glTexCoord2f(f, f);  
  25.         glVertex3f(10.0 + a, 0.0, 0.0);  
  26.         glTexCoord2f(f, 0.0);  
  27.         glVertex3f(10.0 + a, 0.0, 10.0);  
  28.         glEnd();  
  29.     }  
  30.   
  31.     for (float b = -10.0; b <= 320.0; b += 10.0)  
  32.     {  
  33.         for (float a = -10.0; a <= 170.0; a = a + 10.0)  
  34.         {  
  35.             glBegin(GL_QUADS);  
  36.             glTexCoord2f(0.0, 0.0);  
  37.             glVertex3f(0.0 + a, 0.0, -(b - 10.0));  
  38.             glTexCoord2f(0.0, f);  
  39.             glVertex3f(0.0 + a, 0.0, -b);  
  40.             glTexCoord2f(f ,f );  
  41.             glVertex3f(10.0 + a, 0.0, -b);  
  42.             glTexCoord2f(f, 0.0);  
  43.             glVertex3f(10.0 + a, 0.0, -(b - 10.0));  
  44.             glEnd();  
  45.   
  46.         }  
  47.     }  
  48.   
  49.     glPopMatrix();  
  50.   
  51.     glDisable(GL_TEXTURE_2D);  
  52.   
  53.     LoadTexture::FreeCreatedTexture(texture);  
  54. }  
  55.   
  56.   
  57. //draw outer wall  
  58. void GameScene::Draw_OuterWall(GLuint texture,BOOL isdisplay)  
  59. {  
  60.     if (isdisplay)  
  61.     {  
  62.         glPushMatrix();  
  63.   
  64.   
  65.         glEnable(GL_TEXTURE_2D);  
  66.         glBindTexture(GL_TEXTURE_2D, texture);  
  67.   
  68.         //wall 1  
  69.         for (float a = 0.0; a <= 180.0; a = a + 10.0)  
  70.         {  
  71.             glColor3f(0.7, 0.7, 0.5);  
  72.             glBegin(GL_QUADS);  
  73.             glTexCoord2f(0.0, 0.0);  
  74.             glVertex3f(-10.0 + a, 0.0, 20.0);  
  75.             glTexCoord2f(0.0, 1.0);  
  76.             glVertex3f(-10.0 + a, 50.0, 20.0);  
  77.             glTexCoord2f(1.0, 1.0);  
  78.             glVertex3f(0.0 + a, 50.0, 20.0);  
  79.             glTexCoord2f(1.0, 0.0);  
  80.             glVertex3f(0.0 + a, 0.0, 20.0);  
  81.             glEnd();  
  82.         }  
  83.   
  84.         //wall 2  
  85.         for (float a = 0.0; a <= 320.0; a = a + 20.0)  
  86.         {  
  87.             glColor3f(0.7, 0.7, 0.5);  
  88.             glBegin(GL_QUADS);  
  89.             glTexCoord2f(0.0, 0.0);  
  90.             glVertex3f(180.0, 0.0, 0.0 - a);  
  91.             glTexCoord2f(0.0, 1.0);  
  92.             glVertex3f(180.0, 50.0, 0.0 - a);  
  93.             glTexCoord2f(1.0, 1.0);  
  94.             glVertex3f(180.0, 50.0, 20.0 - a);  
  95.             glTexCoord2f(1.0, 0.0);  
  96.             glVertex3f(180.0, 0.0, 20.0 - a);  
  97.             glEnd();  
  98.         }  
  99.   
  100.         //wall 3  
  101.         for (float a = 0.0; a <= 180.0; a = a + 10.0)  
  102.         {  
  103.             glColor3f(0.7, 0.7, 0.5);  
  104.             glBegin(GL_QUADS);  
  105.             glTexCoord2f(0.0, 0.0);  
  106.             glVertex3f(180.0 - a, 0.0, -320.0);  
  107.             glTexCoord2f(0.0, 1.0);  
  108.             glVertex3f(180.0 - a, 50.0, -320.0);  
  109.             glTexCoord2f(1.0, 1.0);  
  110.             glVertex3f(170.0 - a, 50.0, -320.0);  
  111.             glTexCoord2f(1.0, 0.0);  
  112.             glVertex3f(170.0 - a, 0.0, -320.0);  
  113.             glEnd();  
  114.         }  
  115.   
  116.   
  117.         //wall 4  
  118.         for (float a = -10.0; a <= 320.0; a = a + 10.0)  
  119.         {  
  120.             glColor3f(0.7, 0.7, 0.5);  
  121.             glBegin(GL_QUADS);  
  122.             glTexCoord2f(0.0, 0.0);  
  123.             glVertex3f(-10.0, 0.0, 0.0 - a);  
  124.             glTexCoord2f(0.0, 1.0);  
  125.             glVertex3f(-10.0, 50.0, 0.0 - a);  
  126.             glTexCoord2f(1.0, 1.0);  
  127.             glVertex3f(-10.0, 50.0, 10.0 - a);  
  128.             glTexCoord2f(1.0, 0.0);  
  129.             glVertex3f(-10.0, 0.0, 10.0 - a);  
  130.             glEnd();  
  131.         }  
  132.   
  133.   
  134.         glDisable(GL_TEXTURE_2D);  
  135.   
  136.         LoadTexture::FreeCreatedTexture(texture);  
  137.   
  138.         glPopMatrix();  
  139.     }  
  140. }  
  141.   
  142.   
  143. //***********************************************************************  
  144. //draw inner room 1  
  145. void GameScene::Draw_InnerRoom_1(GLuint texture)  
  146. {  
  147.     glPushMatrix();  
  148.   
  149.   
  150.     glEnable(GL_TEXTURE_2D);  
  151.     glBindTexture(GL_TEXTURE_2D, texture);  
  152.   
  153.     //wall 1  
  154.     glColor3f(1.0, 1.0, 1.0);  
  155.     glBegin(GL_QUADS);  
  156.     glTexCoord2f(0.0, 0.0);  
  157.     glVertex3f(10.0, 0.0, -5.0);  
  158.     glTexCoord2f(0.0, 2.0);  
  159.     glVertex3f(10.0, 50.0, -5.0);  
  160.     glTexCoord2f(2.0, 2.0);  
  161.     glVertex3f(12.0, 50.0, -5.0);  
  162.     glTexCoord2f(2.0, 0.0);  
  163.     glVertex3f(12.0, 0.0, -5.0);  
  164.     glEnd();  
  165.   
  166.   
  167.     //wall 2  
  168.     glColor3f(0.6, 0.6, 0.6);  
  169.     glBegin(GL_QUADS);  
  170.     glTexCoord2f(0.0, 0.0);  
  171.     glVertex3f(12.0, 0.0, -5.0);  
  172.     glTexCoord2f(0.0, 1.0);  
  173.     glVertex3f(12.0, 50.0, -5.0);  
  174.     glTexCoord2f(1.0, 1.0);  
  175.     glVertex3f(12.0, 50.0, -12.0);  
  176.     glTexCoord2f(1.0, 0.0);  
  177.     glVertex3f(12.0, 0.0, -12.0);  
  178.     glEnd();  
  179.   
  180.     //wall 3  
  181.     for (float a = 0.0; a <= 100.0; a = a + 18.0)  
  182.     {  
  183.         glColor3f(1.0, 1.0, 1.0);  
  184.         glBegin(GL_QUADS);  
  185.         glTexCoord2f(0.0, 0.0);  
  186.         glVertex3f(12.0 + a, 0.0, -12.0);  
  187.         glTexCoord2f(0.0, 1.0);  
  188.         glVertex3f(12.0 + a, 50.0, -12.0);  
  189.         glTexCoord2f(1.0, 1.0);  
  190.         glVertex3f(30.0 + a, 50.0, -12.0);  
  191.         glTexCoord2f(1.0, 0.0);  
  192.         glVertex3f(30.0 + a, 0.0, -12.0);  
  193.         glEnd();  
  194.     }  
  195.   
  196.   
  197.     //wall 4  
  198.     glColor3f(0.7, 0.7, 0.7);  
  199.     glBegin(GL_QUADS);  
  200.     glTexCoord2f(0.0, 0.0);  
  201.     glVertex3f(120.0, 0.0, -12.0);  
  202.     glTexCoord2f(0.0, 1.0);  
  203.     glVertex3f(120.0, 50.0, -12.0);  
  204.     glTexCoord2f(1.0, 1.0);  
  205.     glVertex3f(120.0, 50.0, -5.0);  
  206.     glTexCoord2f(1.0, 0.0);  
  207.     glVertex3f(120.0, 0.0, -5.0);  
  208.     glEnd();  
  209.   
  210.     //wall 5  
  211.     glColor3f(1.0, 1.0, 1.0);  
  212.     glBegin(GL_QUADS);  
  213.     glTexCoord2f(0.0, 0.0);  
  214.     glVertex3f(120.0, 0.0, -5.0);  
  215.     glTexCoord2f(0.0, 1.0);  
  216.     glVertex3f(120.0, 50.0, -5.0);  
  217.     glTexCoord2f(1.0, 1.0);  
  218.     glVertex3f(140.0, 50.0, -5.0);  
  219.     glTexCoord2f(1.0, 0.0);  
  220.     glVertex3f(140.0, 0.0, -5.0);  
  221.     glEnd();  
  222.   
  223.     //wall 6  
  224.     glColor3f(1.0, 1.0, 1.0);  
  225.     glBegin(GL_QUADS);  
  226.     glTexCoord2f(0.0, 0.0);  
  227.     glVertex3f(140.0, 0.0, -5.0);  
  228.     glTexCoord2f(0.0, 1.0);  
  229.     glVertex3f(140.0, 50.0, -5.0);  
  230.     glTexCoord2f(1.0, 1.0);  
  231.     glVertex3f(140.0, 50.0, -15.0);  
  232.     glTexCoord2f(1.0, 0.0);  
  233.     glVertex3f(140.0, 0.0, -15.0);  
  234.     glEnd();  
  235.   
  236.     glColor3f(1.0, 1.0, 1.0);  
  237.     glBegin(GL_QUADS);  
  238.     glTexCoord2f(0.0, 0.0);  
  239.     glVertex3f(140.0, 0.0, -15.0);  
  240.     glTexCoord2f(0.0, 1.0);  
  241.     glVertex3f(140.0, 50.0, -15.0);  
  242.     glTexCoord2f(1.0, 1.0);  
  243.     glVertex3f(140.0, 50.0, -22.0);  
  244.     glTexCoord2f(1.0, 0.0);  
  245.     glVertex3f(140.0, 0.0, -22.0);  
  246.     glEnd();  
  247.   
  248.   
  249.     //wall 7  
  250.     glColor3f(0.8, 0.5, 0.4);  
  251.     glBegin(GL_QUADS);  
  252.     glTexCoord2f(0.0, 0.0);  
  253.     glVertex3f(140.0, 0.0, -22.0);  
  254.     glTexCoord2f(0.0, 1.0);  
  255.     glVertex3f(140.0, 50.0, -22.0);  
  256.     glTexCoord2f(1.0, 1.0);  
  257.     glVertex3f(120.0, 50.0, -22.0);  
  258.     glTexCoord2f(1.0, 0.0);  
  259.     glVertex3f(120.0, 0.0, -22.0);  
  260.     glEnd();  
  261.   
  262.     //wall 8  
  263.     glColor3f(1.0, 0.7, 0.6);  
  264.     glBegin(GL_QUADS);  
  265.     glTexCoord2f(0.0, 0.0);  
  266.     glVertex3f(120.0, 0.0, -22.0);  
  267.     glTexCoord2f(0.0, 1.0);  
  268.     glVertex3f(120.0, 50.0, -22.0);  
  269.     glTexCoord2f(1.0, 1.0);  
  270.     glVertex3f(120.0, 50.0, -35.0);  
  271.     glTexCoord2f(1.0, 0.0);  
  272.     glVertex3f(120.0, 0.0, -35.0);  
  273.     glEnd();  
  274.   
  275.     glColor3f(1.0, 0.7, 0.6);  
  276.     glBegin(GL_QUADS);  
  277.     glTexCoord2f(0.0, 0.0);  
  278.     glVertex3f(120.0, 0.0, -35.0);  
  279.     glTexCoord2f(0.0, 1.0);  
  280.     glVertex3f(120.0, 50.0, -35.0);  
  281.     glTexCoord2f(1.0, 1.0);  
  282.     glVertex3f(120.0, 50.0, -55.0);  
  283.     glTexCoord2f(1.0, 0.0);  
  284.     glVertex3f(120.0, 0.0, -55.0);  
  285.     glEnd();  
  286.   
  287.   
  288.     //wall 9  
  289.     glColor3f(0.7, 0.4, 0.3);  
  290.     glBegin(GL_QUADS);  
  291.     glTexCoord2f(0.0, 0.0);  
  292.     glVertex3f(120.0, 0.0, -55.0);  
  293.     glTexCoord2f(0.0, 1.0);  
  294.     glVertex3f(120.0, 50.0, -55.0);  
  295.     glTexCoord2f(1.0, 1.0);  
  296.     glVertex3f(100.0, 50.0, -55.0);  
  297.     glTexCoord2f(1.0, 0.0);  
  298.     glVertex3f(100.0, 0.0, -55.0);  
  299.     glEnd();  
  300.   
  301.     glColor3f(0.7, 0.4, 0.3);  
  302.     glBegin(GL_QUADS);  
  303.     glTexCoord2f(0.0, 0.0);  
  304.     glVertex3f(100.0, 0.0, -55.0);  
  305.     glTexCoord2f(0.0, 1.0);  
  306.     glVertex3f(100.0, 50.0, -55.0);  
  307.     glTexCoord2f(1.0, 1.0);  
  308.     glVertex3f(70.0, 50.0, -55.0);  
  309.     glTexCoord2f(1.0, 0.0);  
  310.     glVertex3f(70.0, 0.0, -55.0);  
  311.     glEnd();  
  312.   
  313.     //wall 10  
  314.     glColor3f(0.8, 0.5, 0.4);  
  315.     glBegin(GL_QUADS);  
  316.     glTexCoord2f(0.0, 0.0);  
  317.     glVertex3f(70.0, 0.0, -55.0);  
  318.     glTexCoord2f(0.0, 1.0);  
  319.     glVertex3f(70.0, 50.0, -55.0);  
  320.     glTexCoord2f(1.0, 1.0);  
  321.     glVertex3f(70.0, 50.0, -75.0);  
  322.     glTexCoord2f(1.0, 0.0);  
  323.     glVertex3f(70.0, 0.0, -75.0);  
  324.     glEnd();  
  325.   
  326.     glColor3f(0.8, 0.5, 0.4);  
  327.     glBegin(GL_QUADS);  
  328.     glTexCoord2f(0.0, 0.0);  
  329.     glVertex3f(70.0, 0.0, -75.0);  
  330.     glTexCoord2f(0.0, 1.0);  
  331.     glVertex3f(70.0, 50.0, -75.0);  
  332.     glTexCoord2f(1.0, 1.0);  
  333.     glVertex3f(70.0, 50.0, -95.0);  
  334.     glTexCoord2f(1.0, 0.0);  
  335.     glVertex3f(70.0, 0.0, -95.0);  
  336.     glEnd();  
  337.   
  338.     //wall 11  
  339.     glColor3f(1.0, 0.7, 0.6);  
  340.     glBegin(GL_QUADS);  
  341.     glTexCoord2f(0.0, 0.0);  
  342.     glVertex3f(70.0, 0.0, -95.0);  
  343.     glTexCoord2f(0.0, 1.0);  
  344.     glVertex3f(70.0, 50.0, -95.0);  
  345.     glTexCoord2f(1.0, 1.0);  
  346.     glVertex3f(40.0, 50.0, -95.0);  
  347.     glTexCoord2f(1.0, 0.0);  
  348.     glVertex3f(40.0, 0.0, -95.0);  
  349.     glEnd();  
  350.   
  351.     glColor3f(1.0, 0.7, 0.6);  
  352.     glBegin(GL_QUADS);  
  353.     glTexCoord2f(0.0, 0.0);  
  354.     glVertex3f(40.0, 0.0, -95.0);  
  355.     glTexCoord2f(0.0, 1.0);  
  356.     glVertex3f(40.0, 50.0, -95.0);  
  357.     glTexCoord2f(1.0, 1.0);  
  358.     glVertex3f(10.0, 50.0, -95.0);  
  359.     glTexCoord2f(1.0, 0.0);  
  360.     glVertex3f(10.0, 0.0, -95.0);  
  361.     glEnd();  
  362.   
  363.   
  364.   
  365.     //wall 12  
  366.     glColor3f(1.0, 1.0, 1.0);  
  367.     glBegin(GL_QUADS);  
  368.     glTexCoord2f(0.0, 0.0);  
  369.     glVertex3f(10.0, 0.0, -5.0);  
  370.     glTexCoord2f(0.0, 2.0);  
  371.     glVertex3f(10.0, 50.0, -5.0);  
  372.     glTexCoord2f(2.0, 2.0);  
  373.     glVertex3f(10.0, 50.0, -25.0);  
  374.     glTexCoord2f(2.0, 0.0);  
  375.     glVertex3f(10.0, 0.0, -25.0);  
  376.     glEnd();  
  377.   
  378.     glColor3f(1.0, 1.0, 1.0);  
  379.     glBegin(GL_QUADS);  
  380.     glTexCoord2f(0.0, 0.0);  
  381.     glVertex3f(10.0, 0.0, -25.0);  
  382.     glTexCoord2f(0.0, 2.0);  
  383.     glVertex3f(10.0, 50.0, -25.0);  
  384.     glTexCoord2f(2.0, 2.0);  
  385.     glVertex3f(10.0, 50.0, -45.0);  
  386.     glTexCoord2f(2.0, 0.0);  
  387.     glVertex3f(10.0, 0.0, -45.0);  
  388.     glEnd();  
  389.   
  390.     glColor3f(1.0, 1.0, 1.0);  
  391.     glBegin(GL_QUADS);  
  392.     glTexCoord2f(0.0, 0.0);  
  393.     glVertex3f(10.0, 0.0, -55.0);  
  394.     glTexCoord2f(0.0, 2.0);  
  395.     glVertex3f(10.0, 50.0, -55.0);  
  396.     glTexCoord2f(2.0, 2.0);  
  397.     glVertex3f(10.0, 50.0, -95.0);  
  398.     glTexCoord2f(2.0, 0.0);  
  399.     glVertex3f(10.0, 0.0, -95.0);  
  400.     glEnd();  
  401.   
  402.   
  403.   
  404.     glDisable(GL_TEXTURE_2D);  
  405.   
  406.     LoadTexture::FreeCreatedTexture(texture);  
  407.   
  408.     glPopMatrix();  
  409.   
  410.  
Now, let's move to the main() source file (3D_GameScene_OpenGL.cpp). Initialize OpenGL function or use all the functions defined in the following article instead of just Display_Scene().
To display our scene, we need to call DisplayTexturedGameScene() function defined in the class DisplayGameScene. 
  1. void Display_Scene()  
  2. {  
  3.     if (_moveForeBack)  
  4.     {  
  5.         Moving_Foreword_Backword_Direction(_moveForeBack);  
  6.     }  
  7.   
  8.     if (_moveLeftRight)  
  9.     {  
  10.         _Angle += _moveLeftRight;  
  11.         Moving_Left_Right_Direction(_Angle);  
  12.     }  
  13.   
  14.     //clears the buffer & depth   
  15.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  16.   
  17.     //call function from DisplayGameScene class which will be displayed  
  18.     DisplayGameScene::DisplayTexturedGameScene(texture_type, showRoof, showOuterWall);  
  19.   
  20.     glutSwapBuffers();  
  21. }   
 
We also need menu from where we can change our texture. You can create right click menu using glut.
 
First, create a function that takes an integer as argument. In this function, you must declare your actions to be performed when clicked on that menu item.
 
Following is the code when a specific menu item is clicked, then perform that action. 
  1. //menu function  
  2. void Menu_Process(int mode)  
  3. {  
  4.     switch (mode)  
  5.     {  
  6.     case 1 :  
  7.         texture_type = MyTextures::DEFAULT;  
  8.         glutPostRedisplay();  
  9.         break;  
  10.   
  11.     case 2:  
  12.         texture_type = MyTextures::ANIMATION;  
  13.         glutPostRedisplay();  
  14.         break;  
  15.   
  16.     case 3:  
  17.         texture_type = MyTextures::BUILDINGS;  
  18.         glutPostRedisplay();  
  19.         break;  
  20.   
  21.     case 4:  
  22.         texture_type = MyTextures::HORROR;  
  23.         glutPostRedisplay();  
  24.         break;  
  25.   
  26.     case 5:  
  27.         texture_type = MyTextures::GREENY;  
  28.         glutPostRedisplay();  
  29.         break;  
  30.   
  31.     case 6:  
  32.         texture_type = MyTextures::WOODS;  
  33.         glutPostRedisplay();  
  34.         break;  
  35.   
  36.     case 7:  
  37.         texture_type = MyTextures::ROCKS;  
  38.         glutPostRedisplay();  
  39.         break;  
  40.   
  41.     case 8:  
  42.         texture_type = MyTextures::CIRCUIT;  
  43.         glutPostRedisplay();  
  44.         break;  
  45.   
  46.     case 9:  
  47.         if (showOuterWall)  
  48.             showOuterWall = FALSE;  
  49.         else  
  50.             showOuterWall = TRUE;  
  51.         glutPostRedisplay();  
  52.         break;  
  53.   
  54.     case 10:  
  55.         if (showRoof == 1)  
  56.             showRoof = 0;  
  57.         else  
  58.             showRoof = 1;  
  59.         glutPostRedisplay();  
  60.         break;  
  61.   
  62.     case 11:  
  63.         MessageBox(NULL, TEXT("UP\t\t:\tGo Forward\nDOWN\t\t:\tGo Backward\nLEFT\t\t:\tMove Left\nRIGHT\t\t:\tMove Right\nPAGE UP\t\t:\tGo Upward\nPAGE DOWN\t:\tGo Downward\n\nMouse Right Click\t:\tTo Change Textures"), TEXT("How do i ......"), MB_OK);  
  64.         break;  
  65.   
  66.     case 12:  
  67.         exit(EXIT_SUCCESS);  
  68.         break;  
  69.   
  70.     }  
  71. }   
Then, create another function where we can declare our menu items.
 
First, call the glutCreateMenu() function and pass your created action function as an argument.

e.g glutCreateMenu(Menu_Process);
 
Then, call glutAddMenuEntry() function by passing a string to display an action integer.
 
e.g glutAddMenuEntry(" Animation", 2);
 
Finally, call the glutAttachMenu() function and pass argument about when to display this created menu.
 
e.g glutAttachMenu(GLUT_RIGHT_BUTTON); 
 
Here, I have attached a menu for the mouse right click. 
  1. //create menu function for glut  
  2. void Create_Menu()  
  3. {  
  4.   
  5.     glutCreateMenu(Menu_Process);  
  6.     glutAddMenuEntry(" Default             ", 1);  
  7.     glutAddMenuEntry(" Animation", 2);  
  8.     glutAddMenuEntry(" Buildings", 3);  
  9.     glutAddMenuEntry(" Horror", 4);  
  10.     glutAddMenuEntry(" Greeny", 5);  
  11.     glutAddMenuEntry(" Woods", 6);  
  12.     glutAddMenuEntry(" Rocks", 7);  
  13.     glutAddMenuEntry(" Circuits", 8);  
  14.     glutAddMenuEntry(" Outer Wall ", 9);  
  15.     glutAddMenuEntry(" Roof", 10);  
  16.     glutAddMenuEntry(" How do i........!", 11);  
  17.     glutAddMenuEntry(" Quit", 12);  
  18.     glutAttachMenu(GLUT_RIGHT_BUTTON);  
  19. }   
Call the Create_Menu() function in main() function before glutMainLoop() function. You can download the source code to view complete code of 3D scene with texture images. 
 
Copy "textures" folder from "3D_GameScene_OpenGL\3D_GameScene_OpenGL\"  to  "3D_GameScene_OpenGL\Debug" folder for eliminating the unnecessary errors. Otherwise, it will show "Debug Assertion Failed!" error dialog because of not finding resources that our program needs.

Up Next
    Ebook Download
    View all
    Learn
    View all