,
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
- #ifndef _DISPLAYSCENE_H
- #define _DISPLAYSCENE_H
-
-
- enum MyTextures
- {
- DEFAULT = 0x01,
- ANIMATION=0x02,
- BUILDINGS=0x03,
- HORROR=0x04,
- GREENY=0x05,
- WOODS=0x06,
- ROCKS=0x07,
- CIRCUIT=0x08
- };
-
-
-
- class DisplayGameScene
- {
- public:
-
-
-
-
- static void DisplayTexturedGameScene(int,int,BOOL);
- };
-
-
-
- #endif
DisplayScene.cpp
- #include"LoadImageFile.h"
-
- #include"DrawScene.h"
-
- #include"DisplayScene.h"
-
-
-
-
- void DisplayGameScene::DisplayTexturedGameScene(int type,int rooftype,BOOL outerwall)
- {
- switch (type)
- {
- GameScene gs;
-
- case DEFAULT :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"), LoadTexture::LoadTextureImageFile("textures/blocks2.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain.bmp"), rooftype,2.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks3.bmp"), 6.0);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/plain.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
- case ANIMATION :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/blocks4.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon1.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon2.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon3.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), LoadTexture::LoadTextureImageFile("textures/cartoon4.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), rooftype,0.5);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks5.bmp"), 0.5);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks7.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
-
- case BUILDINGS :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/metal.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/building.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/building.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/building.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), LoadTexture::LoadTextureImageFile("textures/building.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,2.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), 1.0);
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
-
- case HORROR :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/horror2.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/horror1.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/horror4.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/horror1.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"), LoadTexture::LoadTextureImageFile("textures/horrorbabies.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), rooftype,1.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/horror_floor.bmp"), 1.0);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/horror3.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/skull2.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
- case GREENY :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/beans.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/grass2.bmp"), LoadTexture::LoadTextureImageFile("textures/green5.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/grass.bmp"), rooftype,1.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/grass.bmp"), 1.0);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/leaves.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/sky001.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
- case WOODS :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/plain.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/roughwood.bmp"), LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,1.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), 1.0);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/plainwood.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/wood2.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
- case ROCKS :
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/blocks8.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/rocks.bmp"), LoadTexture::LoadTextureImageFile("textures/rocks2.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/plain2.bmp"), rooftype,1.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"), 1.0);
- gs.Draw_Pipes(LoadTexture::LoadTextureImageFile("textures/blocks9.bmp"));
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/blocks.bmp"));
- gs.Display_Text(LoadTexture::LoadTextureImageFile("textures/text.bmp"));
-
- break;
-
- case CIRCUIT:
-
- gs.Draw_OuterWall(LoadTexture::LoadTextureImageFile("textures/circuitboard3.bmp"), outerwall);
- gs.Draw_InnerRoom_1(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"));
- gs.Draw_InnerRoom_2(LoadTexture::LoadTextureImageFile("textures/circuitboard2.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));
- gs.Draw_InnerRoom_3(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));
- gs.Draw_InnerRoom_4(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));
- gs.Draw_InnerRoom_5(LoadTexture::LoadTextureImageFile("textures/circuitboard4.bmp"), LoadTexture::LoadTextureImageFile("textures/circuitboard6.bmp"));
- gs.Draw_Roof(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"), rooftype, 1.0);
- gs.Draw_Floor(LoadTexture::LoadTextureImageFile("textures/circuit.bmp"), 1.0);
- gs.Draw_WoodBoxes(LoadTexture::LoadTextureImageFile("textures/circuitboard5.bmp"));
-
- break;
- }
- }
LoadImageFile.h
- #ifndef _LOADIMAGEFILE_H
- #define _LOADIMAGEFILE_H
-
- #include<Windows.h>
- #include<stdio.h>
- #include<gl/GL.h>
- #include<GL/GLU.h>
- #include<gl/glut.h>
- #include<stdio.h>
- #include<math.h>
-
-
- class LoadTexture
- {
- public :
- static GLuint LoadTextureImageFile(const char *);
- static void FreeCreatedTexture(GLuint);
- };
-
-
-
-
- #endif
LoadImageFile.cpp
- #include"LoadImageFile.h"
-
-
-
- GLuint LoadTexture :: LoadTextureImageFile(const char * filename)
- {
- GLuint texture = 0;
- int width, height;
- BYTE * data = NULL;
- FILE * file;
-
-
- fopen_s(&file, filename, "rb");
-
- if (&file == NULL) return 0;
-
-
- width = 256;
- height = 256;
-
- data = (BYTE*)malloc(width * height * 3);
-
-
- fread(data, width * height * 3, 1, file);
- fclose(file);
-
- glGenTextures(1, &texture);
-
- glBindTexture(GL_TEXTURE_2D, texture);
-
-
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_BGRA_EXT, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
-
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COLOR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-
-
- free(data);
-
- return texture;
-
- }
-
-
-
- void LoadTexture :: FreeCreatedTexture(GLuint texture)
- {
- glDeleteTextures(1, &texture);
- }
DrawScene.h
- #ifndef _DRAWSCENE_H
- #define _DRAWSCENE_H
-
-
- class GameScene
- {
- public:
- void Draw_OuterWall(GLuint,BOOL);
-
- void Draw_InnerRoom_1(GLuint);
-
- void Draw_InnerRoom_2(GLuint,GLuint);
-
- void Draw_InnerRoom_3(GLuint,GLuint);
-
- void Draw_InnerRoom_4(GLuint,GLuint);
-
- void Draw_InnerRoom_5(GLuint,GLuint);
-
-
- void Draw_Roof(GLuint, int,float);
-
- void Draw_Floor(GLuint, float);
-
- void Draw_Pipes(GLuint);
-
- void Draw_WoodBoxes(GLuint);
-
- void Display_Text(GLuint);
-
- };
-
-
- #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.
- #include"LoadImageFile.h"
- #include"DrawScene.h"
-
-
-
-
-
-
- void GameScene::Draw_Floor(GLuint texture,float f)
- {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
-
- glPushMatrix();
-
- for (float a = -10.0; a <= 170.0; a = a + 10.0)
- {
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(0.0 + a, 0.0, 10.0);
- glTexCoord2f(0.0, f);
- glVertex3f(0.0 + a, 0.0, 0.0);
- glTexCoord2f(f, f);
- glVertex3f(10.0 + a, 0.0, 0.0);
- glTexCoord2f(f, 0.0);
- glVertex3f(10.0 + a, 0.0, 10.0);
- glEnd();
- }
-
- for (float b = -10.0; b <= 320.0; b += 10.0)
- {
- for (float a = -10.0; a <= 170.0; a = a + 10.0)
- {
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(0.0 + a, 0.0, -(b - 10.0));
- glTexCoord2f(0.0, f);
- glVertex3f(0.0 + a, 0.0, -b);
- glTexCoord2f(f ,f );
- glVertex3f(10.0 + a, 0.0, -b);
- glTexCoord2f(f, 0.0);
- glVertex3f(10.0 + a, 0.0, -(b - 10.0));
- glEnd();
-
- }
- }
-
- glPopMatrix();
-
- glDisable(GL_TEXTURE_2D);
-
- LoadTexture::FreeCreatedTexture(texture);
- }
-
-
-
- void GameScene::Draw_OuterWall(GLuint texture,BOOL isdisplay)
- {
- if (isdisplay)
- {
- glPushMatrix();
-
-
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
-
- for (float a = 0.0; a <= 180.0; a = a + 10.0)
- {
- glColor3f(0.7, 0.7, 0.5);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(-10.0 + a, 0.0, 20.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(-10.0 + a, 50.0, 20.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(0.0 + a, 50.0, 20.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(0.0 + a, 0.0, 20.0);
- glEnd();
- }
-
-
- for (float a = 0.0; a <= 320.0; a = a + 20.0)
- {
- glColor3f(0.7, 0.7, 0.5);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(180.0, 0.0, 0.0 - a);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(180.0, 50.0, 0.0 - a);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(180.0, 50.0, 20.0 - a);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(180.0, 0.0, 20.0 - a);
- glEnd();
- }
-
-
- for (float a = 0.0; a <= 180.0; a = a + 10.0)
- {
- glColor3f(0.7, 0.7, 0.5);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(180.0 - a, 0.0, -320.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(180.0 - a, 50.0, -320.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(170.0 - a, 50.0, -320.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(170.0 - a, 0.0, -320.0);
- glEnd();
- }
-
-
-
- for (float a = -10.0; a <= 320.0; a = a + 10.0)
- {
- glColor3f(0.7, 0.7, 0.5);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(-10.0, 0.0, 0.0 - a);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(-10.0, 50.0, 0.0 - a);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(-10.0, 50.0, 10.0 - a);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(-10.0, 0.0, 10.0 - a);
- glEnd();
- }
-
-
- glDisable(GL_TEXTURE_2D);
-
- LoadTexture::FreeCreatedTexture(texture);
-
- glPopMatrix();
- }
- }
-
-
-
-
- void GameScene::Draw_InnerRoom_1(GLuint texture)
- {
- glPushMatrix();
-
-
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(10.0, 0.0, -5.0);
- glTexCoord2f(0.0, 2.0);
- glVertex3f(10.0, 50.0, -5.0);
- glTexCoord2f(2.0, 2.0);
- glVertex3f(12.0, 50.0, -5.0);
- glTexCoord2f(2.0, 0.0);
- glVertex3f(12.0, 0.0, -5.0);
- glEnd();
-
-
-
- glColor3f(0.6, 0.6, 0.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(12.0, 0.0, -5.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(12.0, 50.0, -5.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(12.0, 50.0, -12.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(12.0, 0.0, -12.0);
- glEnd();
-
-
- for (float a = 0.0; a <= 100.0; a = a + 18.0)
- {
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(12.0 + a, 0.0, -12.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(12.0 + a, 50.0, -12.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(30.0 + a, 50.0, -12.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(30.0 + a, 0.0, -12.0);
- glEnd();
- }
-
-
-
- glColor3f(0.7, 0.7, 0.7);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(120.0, 0.0, -12.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(120.0, 50.0, -12.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(120.0, 50.0, -5.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(120.0, 0.0, -5.0);
- glEnd();
-
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(120.0, 0.0, -5.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(120.0, 50.0, -5.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(140.0, 50.0, -5.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(140.0, 0.0, -5.0);
- glEnd();
-
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(140.0, 0.0, -5.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(140.0, 50.0, -5.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(140.0, 50.0, -15.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(140.0, 0.0, -15.0);
- glEnd();
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(140.0, 0.0, -15.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(140.0, 50.0, -15.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(140.0, 50.0, -22.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(140.0, 0.0, -22.0);
- glEnd();
-
-
-
- glColor3f(0.8, 0.5, 0.4);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(140.0, 0.0, -22.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(140.0, 50.0, -22.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(120.0, 50.0, -22.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(120.0, 0.0, -22.0);
- glEnd();
-
-
- glColor3f(1.0, 0.7, 0.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(120.0, 0.0, -22.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(120.0, 50.0, -22.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(120.0, 50.0, -35.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(120.0, 0.0, -35.0);
- glEnd();
-
- glColor3f(1.0, 0.7, 0.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(120.0, 0.0, -35.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(120.0, 50.0, -35.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(120.0, 50.0, -55.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(120.0, 0.0, -55.0);
- glEnd();
-
-
-
- glColor3f(0.7, 0.4, 0.3);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(120.0, 0.0, -55.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(120.0, 50.0, -55.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(100.0, 50.0, -55.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(100.0, 0.0, -55.0);
- glEnd();
-
- glColor3f(0.7, 0.4, 0.3);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(100.0, 0.0, -55.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(100.0, 50.0, -55.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(70.0, 50.0, -55.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(70.0, 0.0, -55.0);
- glEnd();
-
-
- glColor3f(0.8, 0.5, 0.4);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(70.0, 0.0, -55.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(70.0, 50.0, -55.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(70.0, 50.0, -75.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(70.0, 0.0, -75.0);
- glEnd();
-
- glColor3f(0.8, 0.5, 0.4);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(70.0, 0.0, -75.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(70.0, 50.0, -75.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(70.0, 50.0, -95.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(70.0, 0.0, -95.0);
- glEnd();
-
-
- glColor3f(1.0, 0.7, 0.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(70.0, 0.0, -95.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(70.0, 50.0, -95.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(40.0, 50.0, -95.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(40.0, 0.0, -95.0);
- glEnd();
-
- glColor3f(1.0, 0.7, 0.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(40.0, 0.0, -95.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(40.0, 50.0, -95.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(10.0, 50.0, -95.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(10.0, 0.0, -95.0);
- glEnd();
-
-
-
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(10.0, 0.0, -5.0);
- glTexCoord2f(0.0, 2.0);
- glVertex3f(10.0, 50.0, -5.0);
- glTexCoord2f(2.0, 2.0);
- glVertex3f(10.0, 50.0, -25.0);
- glTexCoord2f(2.0, 0.0);
- glVertex3f(10.0, 0.0, -25.0);
- glEnd();
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(10.0, 0.0, -25.0);
- glTexCoord2f(0.0, 2.0);
- glVertex3f(10.0, 50.0, -25.0);
- glTexCoord2f(2.0, 2.0);
- glVertex3f(10.0, 50.0, -45.0);
- glTexCoord2f(2.0, 0.0);
- glVertex3f(10.0, 0.0, -45.0);
- glEnd();
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(10.0, 0.0, -55.0);
- glTexCoord2f(0.0, 2.0);
- glVertex3f(10.0, 50.0, -55.0);
- glTexCoord2f(2.0, 2.0);
- glVertex3f(10.0, 50.0, -95.0);
- glTexCoord2f(2.0, 0.0);
- glVertex3f(10.0, 0.0, -95.0);
- glEnd();
-
-
-
- glDisable(GL_TEXTURE_2D);
-
- LoadTexture::FreeCreatedTexture(texture);
-
- glPopMatrix();
-
- }
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.
- void Display_Scene()
- {
- if (_moveForeBack)
- {
- Moving_Foreword_Backword_Direction(_moveForeBack);
- }
-
- if (_moveLeftRight)
- {
- _Angle += _moveLeftRight;
- Moving_Left_Right_Direction(_Angle);
- }
-
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-
- DisplayGameScene::DisplayTexturedGameScene(texture_type, showRoof, showOuterWall);
-
- glutSwapBuffers();
- }
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.
-
- void Menu_Process(int mode)
- {
- switch (mode)
- {
- case 1 :
- texture_type = MyTextures::DEFAULT;
- glutPostRedisplay();
- break;
-
- case 2:
- texture_type = MyTextures::ANIMATION;
- glutPostRedisplay();
- break;
-
- case 3:
- texture_type = MyTextures::BUILDINGS;
- glutPostRedisplay();
- break;
-
- case 4:
- texture_type = MyTextures::HORROR;
- glutPostRedisplay();
- break;
-
- case 5:
- texture_type = MyTextures::GREENY;
- glutPostRedisplay();
- break;
-
- case 6:
- texture_type = MyTextures::WOODS;
- glutPostRedisplay();
- break;
-
- case 7:
- texture_type = MyTextures::ROCKS;
- glutPostRedisplay();
- break;
-
- case 8:
- texture_type = MyTextures::CIRCUIT;
- glutPostRedisplay();
- break;
-
- case 9:
- if (showOuterWall)
- showOuterWall = FALSE;
- else
- showOuterWall = TRUE;
- glutPostRedisplay();
- break;
-
- case 10:
- if (showRoof == 1)
- showRoof = 0;
- else
- showRoof = 1;
- glutPostRedisplay();
- break;
-
- case 11:
- 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);
- break;
-
- case 12:
- exit(EXIT_SUCCESS);
- break;
-
- }
- }
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.
-
- void Create_Menu()
- {
-
- glutCreateMenu(Menu_Process);
- glutAddMenuEntry(" Default ", 1);
- glutAddMenuEntry(" Animation", 2);
- glutAddMenuEntry(" Buildings", 3);
- glutAddMenuEntry(" Horror", 4);
- glutAddMenuEntry(" Greeny", 5);
- glutAddMenuEntry(" Woods", 6);
- glutAddMenuEntry(" Rocks", 7);
- glutAddMenuEntry(" Circuits", 8);
- glutAddMenuEntry(" Outer Wall ", 9);
- glutAddMenuEntry(" Roof", 10);
- glutAddMenuEntry(" How do i........!", 11);
- glutAddMenuEntry(" Quit", 12);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- }
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.