Hi all,
I’m new to openGL, and I’m attempting to load a texture using a bitmap file. The display function and the like are all fine, the problem is a pointer conversion. Here is the code:
int main(int argc, char ** argv) {
...
...
...
// The part of main that uses the function
// Load bitmap as texture
short int i, j;
long int width, height;
unsigned char *** bmp_texture;
// Get dimensions
get_bitmap_dimensions("tex.bmp", &width, &height);
// Allocate space for data
bmp_texture = (unsigned char ***)malloc(width * sizeof(unsigned char **));
for(i = 0; i < width; ++i) {
bmp_texture[i] = (unsigned char **)malloc(height * sizeof(unsigned char *));
for(j = 0; j < height; ++j) {
bmp_texture[i][j] = (unsigned char *)malloc(3 * sizeof(unsigned char));
}
}
// Load bitmap
load_bitmap_texture("tex.bmp", bmp_texture, width, height);
// and the rest
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
// Loop til exit
glutMainLoop();
free(bmp_texture);
return 0;
}
// The functions mentioned before
int get_bitmap_dimensions(const char * file_name, long int * width, long int * height) {
/* open file */
FILE * bitmap_file = fopen(file_name, "rb");
if(!bitmap_file) {
printf("Could not find file.
");
fclose(bitmap_file);
return 0;
}
printf("File opened.
");
/* grab width */
if(fseek(bitmap_file, 18, SEEK_SET)) {
printf("Error finding data.
");
fclose(bitmap_file);
return 0;
}
fread(width, 4, 1, bitmap_file);
printf("Bitmap with: %d
", *width);
/* grab height */
if(fseek(bitmap_file, 22, SEEK_SET)) {
printf("Error finding data.
");
fclose(bitmap_file);
return 0;
}
fread(height, 4, 1, bitmap_file);
printf("Bitmap height: %d
", *height);
/* check it worked */
if((width == NULL) || (height == NULL)) {
printf("Error reading data.
");
fclose(bitmap_file);
return 0;
}
/* close file */
fclose(bitmap_file);
/* return success */
return 1;
}
int load_bitmap_texture(const char * file_name, unsigned char *** texture_buffer, long int bmp_width, long int bmp_height) {
/* open file */
FILE * bitmap_file = fopen(file_name, "rb");
if(!bitmap_file) {
printf("Could not find file.
");
fclose(bitmap_file);
return 0;
}
printf("File opened.
");
/* declare some locals */
short int i, j;
/* read back to front */
fseek(bitmap_file, 3, SEEK_END);
for(i = 0; i < bmp_width; ++i) for(j = 0; j < bmp_height; ++j) {
fread(&texture_buffer[i][j][2], 1, 1, bitmap_file);
fread(&texture_buffer[i][j][1], 1, 1, bitmap_file);
fread(&texture_buffer[i][j][0], 1, 1, bitmap_file);
fseek(bitmap_file, -6, SEEK_CUR);
}
/* close file */
fclose(bitmap_file);
/* return success */
return 1;
}
The problem is that the char *** that is being passed to glTexImage2D isnt being read properly. The texture is just a load of grey and weird colours, I think it’s displaying the pointers instead of the values they point to. Any ideas?
Cheers
-Spec