,


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.
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.
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.
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.
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
- //define different textures values from 1 to 8
- enum MyTextures
- {
- DEFAULT = 0x01,
- ANIMATION=0x02,
- BUILDINGS=0x03,
- HORROR=0x04,
- GREENY=0x05,
- WOODS=0x06,
- ROCKS=0x07,
- CIRCUIT=0x08
- };
- //class to display whole scene
- class DisplayGameScene
- {
- public:
- //define function to display a whole scene
- //first int is texture int
- //second int is used for where to show Roof or not
- //third BOOL is used to whether show outerwall or not
- static void DisplayTexturedGameScene(int,int,BOOL);
- };
- #endif
DisplayScene.cpp
- #include"LoadImageFile.h"
- #include"DrawScene.h"
- #include"DisplayScene.h"
- //define function from class DisplayGameScene()
- //call all function which wants to draw from class GameScene
- 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"
- //load image & set it as a texture with dimension as 256*256
- GLuint LoadTexture :: LoadTextureImageFile(const char * filename)
- {
- GLuint texture = 0;
- int width, height;
- BYTE * data = NULL;
- FILE * file;
- // open texture data
- fopen_s(&file, filename, "rb");
- if (&file == NULL) return 0;
- // allocate buffer
- width = 256;
- height = 256;
- data = (BYTE*)malloc(width * height * 3);
- // read texture data
- fread(data, width * height * 3, 1, file);
- fclose(file);
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- // texture MIP maps
- 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 buffer
- free(data);
- return texture;
- }
- void LoadTexture :: FreeCreatedTexture(GLuint texture)
- {
- glDeleteTextures(1, &texture);
- }
DrawScene.h


Former memberPosted Feb 4, 2018, 12:10 PM
Nice program bro.........
Former memberPosted Oct 6, 2017, 7:34 AM
Very nice ....keep up
Pritam ZopePosted Jan 31, 2017, 2:26 AM
Thanks Nigel Fernandes & Vishal Prajapati
Vishal PrajapatiPosted Jan 30, 2017, 11:58 PM
Interesting one.... Keep up the good work..
Nigel FernandesPosted Jan 30, 2017, 9:46 PM
Wow thats really cool ...