Hello all.
I’m teaching myself openGL, or Glut to be more precise, over these last few weeks. I have been at it all day trying to learn about texturing but for the life of me cannot get a texture to show on a simple 4 vertices plane. I’m using a 24bit 200X200 BMP image. I have a custom texture loader that I’m using. The texture is being loaded OK. The plane however remains a darkish black color. I have tried changing image size, dimensions, aspect etc but nothing. Using Eclipse, Windows 8.1 Glut and minGW. Could somebody please have a look at my code and tell me what I’m doing wrong.
LOADER
GLuint loadTexture(const char * filename) {
FILE * file;
GLuint texture;
int width, height;
unsigned char * data;
file = fopen(filename, "rb");
if (file == NULL){
cout << "No file found!!!" << endl;
return 0;
}
width = 100;
height = 300;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file );
fclose(file);
for(int i = 0; i < width * height; ++i) {
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
free(data);
return texture;
}
PLANE OBJECT
void pictureFrame(void) {
GLuint tex1 = loadTexture(".\\Textures\\one.bmp");
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, tex1);
glNormal3f(0, 0, 1);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}