Code:
/*Applying Textures to Surfaces: This Program Works Great
Good example video OpenGL (SDL,C++) tutorial 7 - Textures (part 1) - YouTube
The linux computer at school contains SDL libraries, using -lSDL */
/* Rotating Cube with Textures and Mouse click interactions
Rendered images are stored in OpenGL pictures in the UAB School Folder
This program is a intro to texturing. It use a 3D cube and textures it's sureface with colors defined in the color array RGB.
This program also introcudes a simple way to interact with cube using the mouse buttons.*/
//****Important Note put SDL.dll file in operating directory and link libraries
#include <stdlib.h>
#include <GL/glut.h>
#include <SDL/SDL.h>
#include <GL/glext.h> //
//Prototypes
unsigned int loadTexture(const char* filename); //I had to put this in since the initRendering function was using loadTexture function before it was declared
//note: notice that the range is from -1 to 1, and colors are from 0 to 1, while textures are from 0 to 1
GLfloat vertices[][3] = {{-1.0,-1.0,1.0},{-1.0,1.0,1.0},
{1.0,1.0,1.0}, {1.0,-1.0,1.0},
{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},
{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
GLfloat colors[][3] = {{1.0,0.0,0.0},{0.0,1.0,1.0},
{1.0,1.0,0.0},{0.0,1.0,0.0},
{0.0,0.0,1.0},{1.0,0.0,1.0}};
unsigned int tex; //global variable to hold texture
//Added initialization function
//Initializes 3D rendering
void initRendering() { //in this function I may need to add in the
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//glEnable(GL_LIGHTING); //enable lighting
//glEnable(GL_LIGHT0); //enable light #0
//glEnable(GL_LIGHT1); //enable light #1
//glEnable(GL_NORMALIZE); //Automatically normalize normals
glShadeModel(GL_SMOOTH); //enable smooth shading
glEnable(GL_TEXTURE_2D); //Enables the use of 2D textures
tex = loadTexture("Bricks.bmp"); //load up file
//Adds ambient light
//GLfloat ambientColor[] = {0.2, 0.2, 0.2, 1.0};
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Adds positioned light
/*GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/
//Adds directed light
/*GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);*/
}
//***********************************Texture Function for Loading*************************************************************
/*Here I need to load a bitmap image, generate textures, bind texture, set glTextImage2D, glTextParameteri
Some ppl use programs such as SDL to read in bitmap images (just not to reinvent the wheel). Depending on the requirements of the Viscube
I can aslo us SDL. In this case I am trying to establish reading using SDL*/
//Reading the texture from an image file: OpenGL has no function to load an image
//Here I am going to use the SDL as my image uploader //SDL uploader test
unsigned int loadTexture(const char* filename)
{
SDL_Surface* img =SDL_LoadBMP(filename);
unsigned int id; //assigning an id for glGenTextures
glGenTextures(1, &id); //idea assigned
glBindTexture(GL_TEXTURE_2D, id); //binding texture to the id
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
img ->w,
img->h,
0,
GL_RGB,
GL_UNSIGNED_SHORT_5_6_5, //SDL is having trouble processing this in windows
img->pixels); //For some reason this is causing a problem
//Setting Parameters
//Warping Parameters (how texture will be applied to the object)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //Many different parameters can be set her to manipulate the textures
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Filtering Parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //2d texture with a minimum filter, and linear interpolation (averaging pixlels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(img); //deletes the texture image out of the texture memory
return id;
}
void polygon(int a, int b, int c, int d)
{
/*draw a polygon via list of vertices */
glBindTexture(GL_TEXTURE_2D, tex); //This binds the textures to the polygon
glBegin(GL_POLYGON); //applying the textures are based on a square that is lower left(0,0), Upper left (0,1), lower right (1,0), upper right (1,1)
// glColor3fv(colors[a]); //These texture points are placed witht he corresponding vertices, to make sure that the texture is applied correctly
glTexCoord2f(0.0,0.0);
glVertex3fv(vertices[a]);
//glColor3fv(colors[b]);
glTexCoord2f(0.0,1.0);
glVertex3fv(vertices[b]);
//glColor3fv(colors[c]);
glTexCoord2f(1.0,1.0);
glVertex3fv(vertices[c]);
//glColor3fv(colors[d]);
glTexCoord2f(1.0,0.0);
glVertex3fv(vertices[d]);
glEnd();
/*Here are the vertices that he used
glBegin(GL_QUADS);
glVertex3f(-2.0,2.0,0.0);
glVertex3f(-2.0,-2.0,0.0);
glVertex3f(2.0,-2.0,0.0);
glVertex3f(2.0,2.0,0.0);
glEnd();*/
}
void colorcube(void)
{ //function is used to state which sections of the array for colors and vertext to use
/*map vertices to faces */
polygon(0, 3, 2, 1);
polygon(2, 3, 7, 6);
polygon(3, 0, 4, 7);
polygon(1, 2, 6, 5);
polygon(4, 5, 6, 7);
polygon(5, 4, 0, 1);
}
static GLfloat theta[] = {0.0,0.0,0.0}; //declaring two static variables theta and axis
static GLint axis = 2;
//display function
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0,0.0,0.0);
glRotatef(theta[1], 0.0,1.0,0.0);
glRotatef(theta[2], 0.0,0.0,1.0);
colorcube(); //refers to the function that mapped the vertices to faces
glutSwapBuffers();
}
//The below section contains the interaction component of the program
void spinCube()
{
theta[axis] += 0.05;
if(theta[axis] > 360.0)
theta[axis] -= 360.0;
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
//Mouse button interaction
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void myReshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w <= h)
glOrtho(-2.0, 2.0, -2.0* (GLfloat) h/ (GLfloat) w, 2.0 *(GLfloat) h/ (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0* (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h/ (GLfloat) w, -2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char ** argv)
{
GLubyte image[64][64][3]; //seems like 64 by 64 pixels taking a vertice made up of 3 points
int i, j, r, c;
for(i =0; i <64; i++)
{
for(j=0; j<64; j++)
{
c = ((((i & 0x8)== 0)^((j & 0x8)) == 0))*255; //I think 0x8 has to always be written like this, not 0 x 8 or it will cause problems.
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("colorcube");
//Place initialization function here
initRendering();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST); //glEnables can be moved to the initialization function
glEnable(GL_TEXTURE_2D); //This is where texturing starts
//Texture section, All of this can be defined in a load texture function
// glTexImage2D(GL_TEXTURE, 0,3,64,64,0, GL_RGB, GL_UNSIGNED_BYTE, image);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //Many different parameters can be set her to manipulate the textures
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //part of the original program
glutMainLoop();
return 0;
}