gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > libfat: Loading a *.bin graphic

#132197 - Rajveer - Sun Jun 24, 2007 3:37 pm

Hi guys. I'm currently rewriting a game of mine to use libfat, and I'm trying to load my original *.bin graphic files. What I'm doing so far:

Code:
   FILE *file_Pointer;

//Create Texture Space
   int main_Menu_Texture[1];
   int main_Menu_Texture_Palette[1];

//Load Textures
//Open first texture file, check if exists
   if(( file_Pointer = fopen("/Futuracer/Menus/Main Menu/Graphics/Graphic1.bin", "rb") ) == NULL)
   {printf("ERROR: Loading Graphic1.bin\n");}
   else{printf("Reading Graphic1.bin\n");}
   
//Load file into texture space
   glGenTextures(1, &main_Menu_Texture[0]);
   glBindTexture(0, main_Menu_Texture[0]);
   glTexImage2D(0, 0, GL_RGB256, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, (u8*)file_Pointer);
   glBindTexture(0, 0);
   
//Close filestream
   fclose(file_Pointer);
   
//Open first texture file palette, check if exists
   if(( file_Pointer = fopen("/Futuracer/Menus/Main Menu/Graphics/Graphic1_pal.bin", "rb") ) == NULL)
   {printf("ERROR: Loading Graphic1_pal.bin\n");}
   else{printf("Reading Graphic1_pal.bin\n");}
   
//Load palette file into palette space
   main_Menu_Texture_Palette[0] = gluTexLoadPal((u16*)file_Pointer, 32, GL_RGB32_A3 );
   
//Close filestream
   fclose(file_Pointer);


This results in the quad not being textured. I'm not sure if I'm making proper use of glTexImage2D's last argument (pointer to the graphic), and this is the first time I've ever used any file I/O so can anybody show me where I'm going wrong?

#132198 - chishm - Sun Jun 24, 2007 3:41 pm

You have to read a file into memory. Use fread to load it into malloc'd RAM, then you can copy that to VRAM. Don't try to fread into VRAM directly, as it may use byte writes, which VRAM won't handle correctly.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#132201 - Rajveer - Sun Jun 24, 2007 4:59 pm

Cheers for the reply chishm. I've changed my code, it now checks the file length, mallocs the size of the file and reads the file into RAM. I'm guessing now is the time to use glTexImage2D and use the pointer to the RAM location as the texture pointer, to put the texture into VRAM?

My code so far which doesn't work ;) (JUST for the texture not the palette, as its essentially the same thing):

Code:
FILE *file_Pointer; //Pointer to an opened file
long file_Length; //Variable to store length of opened file
int main_Menu_Texture[1]; //Create Texture Space

//Load Textures:
//FIRST TEXTURE/////////////////////////////////
//Open first texture file, check if exists
   if(( file_Pointer = fopen("/Futuracer/Menus/Main Menu/Graphics/Graphic1.bin", "rb") ) == NULL)
   {printf("ERROR: Loading Graphic1.bin\n");}
   else{printf("Reading Graphic1.bin\n");}
   
//Find length of file
   fseek(file_Pointer, 0L, SEEK_END);
   file_Length = ftell(file_Pointer);
   rewind(file_Pointer);
   iprintf("Length of Graphic1.bin = %d\n", file_Length);
   
//Create space for texture storage in RAM, read into RAM and close filestream
   char *texture_In_RAM = malloc(file_Length + 1);
   fread(texture_In_RAM, file_Length, 1, file_Pointer);
   fclose(file_Pointer);
         
//Load file into texture space
   glGenTextures(1, &main_Menu_Texture[0]);
   glBindTexture(0, main_Menu_Texture[0]);
   glTexImage2D(0, 0, GL_RGB256, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, (u8*)texture_In_RAM);
   glBindTexture(0, 0);

//FIRST TEXTURE PALETTE/////////////////////////

//Open first texture file palette, check if exists
   if(( file_Pointer = fopen("/Futuracer/Menus/Main Menu/Graphics/Graphic1_pal.bin", "rb") ) == NULL)
   {printf("ERROR: Loading Graphic1_pal.bin\n");}
   else{printf("Reading Graphic1_pal.bin\n");}
   
   //Find length of file
   fseek(file_Pointer, 0L, SEEK_END);
   file_Length = ftell(file_Pointer);
   rewind(file_Pointer);
   iprintf("Length of Graphic1_pal.bin = %d\n", file_Length);
   
   //Create space for texture storage in RAM, read into RAM and close filestream
   char *texture_In_RAM2 = malloc(file_Length + 1);
   fread(texture_In_RAM2, file_Length, 1, file_Pointer);
   fclose(file_Pointer);
      
   //Load palette file into palette space
   main_Menu_Texture_Palette[0] = gluTexLoadPal((u16*)texture_In_RAM2, 32, GL_RGB32_A3 );



Or am I supposed to use some sort of memcpy function to copy it into VRAM? If so, how would I bind the texture to my variable main_Menu_Texture[0]?


Last edited by Rajveer on Sun Jun 24, 2007 11:16 pm; edited 2 times in total

#132228 - simonjhall - Sun Jun 24, 2007 9:10 pm

Which part doesn't work? Does it load into memory correctly? If you're not sure you can stick your screen in framebuffer mode and splat the texture directly onto the screen (and do a per-pixel colour lookup) just to check you've got your texture in memory correctly.
I could suggest loads more things to do, but yeah...what isn't working? :-)
_________________
Big thanks to everyone who donated for Quake2

#132239 - Rajveer - Sun Jun 24, 2007 11:15 pm

simonjhall wrote:
Which part doesn't work?


No idea :-) It "calculates" the total length of the file correctly with fseek and ftell so if it can do that I'm assuming that it can read it properly with fread. I'm also assuming that it loads into memory properly as the application is a few hundred lines long and doesn't run out of RAM (no other graphics or anything). So I think it's not loading the texture properly. Maybe it's the type that I'm using for texture_In_RAM (the pointer to the loaded texture in RAM), I don't know. The quad appears completely untextured, I've edited my previous post to include the code for the palette but I'm stumped! Any suggestions?

#132346 - Rajveer - Tue Jun 26, 2007 3:15 am

Anybody got any suggestions on what I should try now? :-)

#132432 - dannyboy - Tue Jun 26, 2007 5:44 pm

I think if you pass in the address of the texture in ram it may work. Something like:

glTexImage2D(0, 0, GL_RGB256, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, &texture_In_RAM);

or maybe

glTexImage2D(0, 0, GL_RGB256, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, &texture_In_RAM[0]);

You shouldn't need to cast it since char* is equivalent to u8* or uint8*

#132459 - Rajveer - Tue Jun 26, 2007 10:38 pm

Hahaa! It wasn't the code! I tried your method dannyboy but still the same results, so I tried compiling one of my older copies of my project which worked fine and all the textures are white with the latest libnds. So what did I change to make it compatible with the latest videoGL:

I deleted glReset and now call glInit once at the beginning of the application.

I changed glIdentity to glLoadIdentity.

I've looked in videoGL but can't find anything, has texture handling changed or something? Shall I post the WHOLE of my code (initialising OpenGL, all OpenGL calls e.t.c.)?

#132462 - simonjhall - Tue Jun 26, 2007 11:04 pm

Yeah that'd probably be handy. A brief description of what you expect it to do and what it actually does would be good. Plus if you could slim it down to the bare minimum (eg initting the system, loading a texture, splatting it on a quad and drawing it on the screen) that'd be nice too!
I'm sure we'll be able to spot any problem pretty quick :-)
_________________
Big thanks to everyone who donated for Quake2

#132464 - Rajveer - Tue Jun 26, 2007 11:17 pm

At the moment I'd say it's already at bare minimum ;) The aim is to simply load a texture with libfat and draw it onto a quad.

[code]FIXEDcode]

EDIT: Fixed the problem, it was the way I was converting the texture not the code. Cheers!