i have a simple plain (square) with lighting and material on it, i want to put an earth texture on it. but nothing appears. I've checked, and think it loads the RGB data correctly so my guess is that there is a problem with this line (data is *char and the image is BMP24)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);
here is a .bmp load function:
GLuint LoadTexture(const char* imagepath) {
printf("Reading image %s\n", imagepath);
unsigned int outWidth = -1;
unsigned int outHeight = -1;
unsigned char header[54];
unsigned int dataPos;
unsigned int imageSize;
FILE* file;
if (errno_t err = fopen_s(&file, imagepath, "rb") != 0) {
perror("Can't Open file");
return 0;
}
// If less than 54 byes are read, problem
if (fread(header, 1, 54, file) != 54) {
printf("Not a correct BMP file\n");
return NULL;
}
// A BMP files always begins with "BM"
if (header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
return NULL;
}
// Make sure this is a 24bpp file
if (*(int*)&(header[0x1E]) != 0) { printf("Not a correct BMP file\n"); return NULL; }
if (*(int*)&(header[0x1C]) != 24) { printf("Not a correct BMP file\n"); return NULL; }
// Read the information about the image
dataPos = *(int*)&(header[0x0A]);
outWidth = *(int*)&(header[0x12]);
outHeight = *(int*)&(header[0x16]);
imageSize = *(int*)&(header[0x22]);
// Some BMP files are misformatted, guess missing information
if (imageSize == 0) imageSize = outWidth * outHeight * 3; // 3 : one byte for each Red, Green and Blue component
if (dataPos == 0) dataPos = 54; // The BMP header is done that way
// Read the actual data from the file into the buffer
unsigned char* data = new unsigned char[imageSize];
fread(data, 3, imageSize, file);
// Everything is in memory now, the file wan be closed
fclose(file);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);
return tex;
}
here's the main and display function:
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float ratio = 0.75;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, earthTextureData);
glBegin(GL_QUADS);
glVertex3f(-0.5, -0.5, 0);
glVertex3f(-0.5, 0.5, 0);
glVertex3f(0.5, 0.5, 0);
glVertex3f(0.5, -0.5, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(1200, 800);
glutCreateWindow("app");
init(); // custome initilizing
initLighting();
earthTextureData = LoadTexture("Texture/globe.bmp");
glutDisplayFunc(render);
glutReshapeFunc(reshape);
glutMotionFunc(mouseMove);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutMainLoop();
}
I've played around with the code, and sometimes (when i use int instead of char*) i get a red material covering everything but don't know exactly what causes it
UPDATE: had to change a few lines to make it work (for bmp24 files):
1-edit this line: fread(data, 1, imageSize, file);
2- add this line: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3- edit this line (for colors to show properly): glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, (GLvoid*)data);
4-add UV coordinates (lookup glTexCoord)