You are on page 1of 3

Alrighty so we will look at draw box extended to give you the idea of how to do

this
First lets take a look at the function we will plug into the main .cpp file
drawBoxEx(x1, y1, z1, x2, y2, z2, transx, transy, transz, angle, texture);
okay so x, y, z and the rest are size co-ords... easy enough.
These all have to be floats so the numbers are written 0.0f, 0.5f, 1.0f etc...
You don't need to worry about that.
trans x, y, z is its position in 3d space.
All float values as well.
angle is the rotation of the object.
also a float value
and texture is the texture that will be applied to all faces of the object.
the texture is provided buy a pointer to the import script which is a GLuint.
which is just a special OpenGL integer. Nothing extreme.
Anyway You will need to mold your script around the command and the command arou
nd the script.

You will have to create a header file (.h) to accompany the funtion.
All you need to put in this file is what we wrote above with the variable defini
tions (int, float, double)
For example:

void drawBoxEx(float x1, float y1, float z1, float x2, float y2, float z2, float
tranx, float trany, float tranz, float angle, GLuint tex);

always put void at the start with this as we dont want it to return anything oth
er then true or false... we won't be checking.

The last thing you need to do is create the actual script. here is the one for t
he box.

#include <GL/glut.h> //include the things we need


#include <stdlib.h>
#include "BoxEx.h" //include the header file (this will also be included in the
main doc)

void drawBoxEx(float x1, float y1, float z1, float x2, float y2, float z2, float
tranx, float trany, float tranz, float angle, GLuint tex) //this brings in the
arguments from the program
{
glEnable(GL_TEXTURE_2D); //enables textures
glBindTexture(GL_TEXTURE_2D, tex); // loads the texture
glPushMatrix(); //something to do with transformations don't worry just
put it in
glTranslatef(tranx, trany, tranz); //moves what we are making to the rig
ht place
glRotatef(angle, 0.0f, 1.0f, 0.0f); // rotates it on the y (around in ci
rcles) axis by the angle specified
glBegin(GL_QUADS); // tells opengl we are going to start drawing using q
uads which take for co-ords to a face. google the other ones you can do.
//front
glNormal3f(0.0f, 0.0f, 1.0f); // Set Normals this so the light works p
roperly and it knows which side is facing out. you need to get this right. use t
rial and error. 1 or 0 or -1 needed
glTexCoord2f(0.0f, 0.0f); // this sets the texture cornors. imagine a sh
eet being stretched across something each with two x, y boolean (1 or 0) numbers
(possible combos (0, 0) (1, 1) (1, 0) (0, 1) ) just mimic me with this one
glVertex3f(x1, y2, z2); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x2, y2, z2); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x2, y1, z2); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x1, y1, z2); // Bottom Left
//back
glNormal3f(0.0f, 0.0f, -1.0f); // Set Normals
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x1, y2, z1); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x2, y2, z1); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x2, y1, z1); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x1, y1, z1); // Bottom Left

//right
glNormal3f(1.0f, 0.0f, 0.0f); // Set Normals
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x2, y1, z2); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x2, y2, z2); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x2, y2, z1); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x2, y1, z1); // Bottom Left

//left
glNormal3f(-1.0f, 0.0f, 0.0f); // Set Normals
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x1, y1, z2); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x1, y2, z2); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x1, y2, z1); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x1, y1, z1);// Bottom Left

//Top
glNormal3f(0.0f, 1.0f, 0.0f); // Set Normals
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x1, y2, z2); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x2, y2, z2); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x2, y2, z1); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x1, y2, z1); // Bottom Left

//Bottom
glNormal3f(0.0f, -1.0f, 0.0f); // Set Normals
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x1, y1, z2); // Top Left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x2, y1, z2); // Top Right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x2, y1, z1); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x1, y1, z1); // Bottom Left

glEnd(); //tells it we have finished with our shape


glPopMatrix(); //ends the stuff we did before
glDisable(GL_TEXTURE_2D); // stops using the texture
}

You might also like