Revised Code. I have revised this code so many times that, I don't know what else to do. I posted this code a few days ago and a number of people gave me pointers which I included in my code. But it's still not working. Based on examples I saw on the Web, I tried the following:
1. I copied and put the whole texture mapping operations in the void init( ) function and that did not work.
2. I took out the code that SOIL uses to upload the image and that did not work (I did that because one person said it was unnecessary)
3. I changed the height and width of my image to 256
4. I made sure the image was in the same folder as my project file
5. I included glBindTexture(GL_TEXTURE_2D, texture); in each function that I wrote:
eg: void draw Triagle (), void drawCircle ()
6. Initially, static GLuint texture; was inside the GLint LoadGLTexture(const char *filename, int width, int height) function. I took it out.
I have run out of ideas. Maybe you might be able to see something I am missing. Please help. This project was due a while back. Thanks.
static GLuint texture;
GLint LoadGLTexture(const char *filename, int width, int height)
{
unsigned char *data;
FILE *file;
// open texture data
file = fopen(filename, "r");
if (file == NULL) return 0;
// allocate buffer
data = (unsigned char*) malloc(width * height * 4);
//read texture data
fread(data, width * height * 4, 1, file);
fclose(file);
texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
"face.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
// check for an error during the load process
if(texture == 0)
{
cout << "SOIL loading error: \n" << endl;
}
glGenTextures(1, &texture); //get a name for the texture
glBindTexture(GL_TEXTURE_2D, texture); // Bind the texture object to its name
//Specify the parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // texture should tile
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
// Specify the application mode
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// build our texture mipmaps
// gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
// GL_RGB, GL_UNSIGNED_BYTE, data );
glTexImage2D(GL_TEXTURE_2D,
0, // One resolution (i.e. level 0)
3, // 3 components (i.e., RGB)
256, // Width
256, // Height
0, // Border width
GL_RGB, // Format
GL_UNSIGNED_BYTE, // Data type of the texels
data);
// Enable textures
glEnable(GL_TEXTURE_2D);
// free buffer
free(data);
return texture;
}
void init(void)
{
glClearColor (0.0, 0.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, winWidth, 0.0, winHeight);
glShadeModel (GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearDepth(1.0f); // Depth Buffer Setup
//glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void display(void)
{
//clear the screen
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texture);
LoadGLTexture("face.png", 256, 256);
// LoadGLTexture("face.png", 594, 738);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Angle, 0.0, 0.0, 0.0);
//Nose
drawTriangle();
//Face
circle (250, 250, 400);
//Inner Eyes
glLoadIdentity();
circle(200, 280, 10);
drawCircle1(-30, 280, 10);
//Eye lids
glLoadIdentity();
glScalef(0.6,0.2,0);
circle(530, 2400, 60);
circle(-150, 2150, 60);
//Arc for eyebrows
drawArc(500, 500, -70, -140, 100);
drawArc(270, 500, -70, -140, 100);
//Mouth
drawArc(380, 250, 110, -180, 180);
// drawArc(380, 100, -100, -180, 180);
drawLine();
// Force the rendering (off-screen)
glFlush();
// Handle the double buffering
glutSwapBuffers();
}