Well you all know our planetary system, it has one sun and many planets revolving around the sun. To draw all these objects with textures first you need to have the textured images of that object. You can download all these images on Google Images and modify them.
The textured image must have the following properties:
Extension | 24 Color Bitmap(.bmp) |
Dimensions | 256*256 |
Bit Depth | 24 |
You can edit or change the dimension and extension of images by using simple Paint application in Windows. Download the source code for code & images.
Ok now you have all images for your planets. Now start Visual Studio and configure it for glut library. Don't know how to configure Visual Studio for glut?
All right, now you have configured the Visual Studio for glut, now let's move to the coding. First you need to create the function that sets texture to the object. I am using LoadTextureImageFile(const char * filename) function that takes a char pointer parameter.
Here's the code for adding texture to the object.
- GLuint 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 FreeCreatedTexture(GLuint texture)
- {
- glDeleteTextures(1, &texture);
- }
Description
The above function returns the build texture having data type GLuint. First read the image file & fread that data to the declared variable of BYTE. Then call glGenTextures() & glBindTexture() methods for binding read data from image. Then build the 2D Mipmaps that sets the color & texture to the object. We are using 256*256 image dimension so width & height will be 256 respectively. Remember our image is of bitmap(.bmp) type, so to use the color of image you must set GL_BGRA_EXT instead of GL_RGBA_EXT because .bmp images store the image data in the Blue-Green-Red(BGR) format not in RGB. Then you can set your own parameters for drawing textured image objects.
FreeCreatedTexture() function release all texture data from memory.
Example
Consider you want to draw a textured square. The following function draws a textured square.
First call above function & pass image filename as a parameter(e.g : rocks.bmp).
Then call glEnable(GL_TEXTURE_2D) for enabling the image texture.
Then call glBindTexture(GL_TEXTURE_2D,GLuint texturedata).
You can draw the texture of any object by using glTexCoord2f(x,y) method.
Remember x & y are not co-ordinates,they set the texture to object according to the x and y values.
- glPushMatrix();
-
- GLuint texture = LoadTextureImageFile("textures/rocks.bmp");
-
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0);
- glVertex3f(-2.0, 2.0, 0.0);
- glTexCoord2f(0.0, 1.0);
- glVertex3f(2.0, 2.0, 0.0);
- glTexCoord2f(1.0, 1.0);
- glVertex3f(2.0, -2.0, 0.0);
- glTexCoord2f(1.0, 0.0);
- glVertex3f(-2.0, -2.0, 0.0);
- glEnd();
-
- glPopMatrix();
Ok now you have set the function that added texture to the object, now let's move to draw the object.
Let's draw a sun,
To draw a textured sphere use gluSphere() function instead of glutSolidSphere(). To enable the texture for sphere, first you need to define GLUquadric pointer variable and assign memory to it by calling gluNewQuadric() function. Then call gluQuadricTexture() & pass the created GLUquadric pointer variable. Then call the gluSphere() method.
See the following code for better understanding. Code draws a textured sphere, which means our sun.
- GLuint texture = LoadTextureImageFile("textures/sun.bmp");
-
- GLUquadric *quad;
-
- glPushMatrix();
-
-
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
- quad = gluNewQuadric();
- gluQuadricTexture(quad, 40);
-
- glTranslatef(0.0, 0.0, 0.0);
-
- glRotatef((GLfloat)sun, 0.0, 1.0, 0.0);
- gluSphere(quad, 30.0, 100, 100);
-
- glPopMatrix();
To disable texture for next drawn object use glDisable(GL_TEXTURE_2D) method.
Using the same code that we used to draw the sun, you can draw planets. Download the source code.
Here's the code that draws the earth.
- glPushMatrix();
-
- glEnable(GL_TEXTURE_2D);
-
- texture = LoadTextureImageFile("textures/earth.bmp");
- glBindTexture(GL_TEXTURE_2D, texture);
-
- quad = gluNewQuadric();
- gluQuadricTexture(quad, 1);
- glTranslatef(0.0, 0.0, 120.0);
- glRotatef((GLfloat)planet, 0.0, 1.0, 0.0);
- gluSphere(quad, 5.0, 70, 70);
-
- FreeCreatedTexture(texture);
-
- glPopMatrix();
You can also rotate or orient the whole scene by rotating the camera. This can be done by using gluLookAt() function. First declare the variables for your default camera position.
- float _Angle = 0.0;
- float _moveLeftRight = 0.0;
- float X = -5.0, Z = 150.0;
- float X_2 = 0.0, Z_2 = -1.0;
- float Y = 3.0;
- int _moveForeBack = 0;
- int moveUp = 0;
Following function rotate you to Left or Right direction when Left or Right key is pressed respectively.
- void Moving_Left_Right_Direction(float angle)
- {
- X_2 = sin(angle);
- Z_2 = -cos(angle);
- glLoadIdentity();
- gluLookAt(X, Y, Z, X + X_2, Y, Z + Z_2, 0.0f, 1.0f, 0.0f);
- }
Following function moves you to Foreward or Backward when Up or Down key is pressed respectively.
- void Moving_Foreword_Backword_Direction(int loc)
- {
- X = X + loc*(X_2)*fb;
- Z = Z + loc*(Z_2)*fb;
- glLoadIdentity();
- gluLookAt(X, Y, Z, X + X_2, Y, Z + Z_2, 0.0f, 1.0f, 0.0f);
- }
Here's the code for key pressed & key released function.
-
- void Key_Pressed(int key, int x, int y)
- {
- if (key == GLUT_KEY_LEFT)
- {
- _moveLeftRight = -0.09;
- }
- else if (key == GLUT_KEY_RIGHT)
- {
- _moveLeftRight = 0.09;
- }
- else if (key == GLUT_KEY_UP)
- {
- _moveForeBack = 8;
- }
- else if (key == GLUT_KEY_DOWN)
- {
- _moveForeBack = -8;
- }
- else if (key == GLUT_KEY_UP && key == GLUT_KEY_LEFT)
- {
- _moveLeftRight = 0.07;
- }
- else if (key == GLUT_KEY_UP && key == GLUT_KEY_RIGHT)
- {
- _moveLeftRight = 0.07;
- }
- else if (key == GLUT_KEY_PAGE_UP)
- {
- Y += 2.0;
- glLoadIdentity();
- gluLookAt(X, Y, Z, X + X_2, Y, Z + Z_2, 0.0f, 1.0f, 0.0f);
- glutPostRedisplay();
- }
- else if (key == GLUT_KEY_PAGE_DOWN)
- {
- Y -= 2.0;
- glLoadIdentity();
- gluLookAt(X, Y, Z, X + X_2, Y, Z + Z_2, 0.0f, 1.0f, 0.0f);
- glutPostRedisplay();
- }
-
- }
-
-
-
- void Key_Released(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_LEFT:
- if (_moveLeftRight < 0.0f)
- _moveLeftRight = 0.0f;
- break;
-
- case GLUT_KEY_RIGHT:
- if (_moveLeftRight > 0.0f)
- _moveLeftRight = 0.0f;
- break;
-
- case GLUT_KEY_UP:
- if (_moveForeBack > 0)
- _moveForeBack = 0;
-
- break;
-
- case GLUT_KEY_DOWN:
- if (_moveForeBack < 0)
- _moveForeBack = 0;
- break;
- }
- }
To use above key functions use following methods in main method.
glutSpecialFunc(Key_Pressed);
glutSpecialUpFunc(Key_Released);
To go to the Full screen mode call glutFullScreen() method before glutMainLoop() method.