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 > Loading display lists from fat

#159340 - Meduusa - Sun Jun 29, 2008 3:29 pm

Im trying to load display lists from fat so that I dont have to link them to the binary.

First I took a Display_List_2 example from libnds examples and modified it so

Code:
#include <stdio.h>
#include <nds.h>
#include <fat.h>

int main()
{
   FILE *model;

   powerON(POWER_ALL);

   //irqs are nice
   irqInit();
   irqSet(IRQ_VBLANK, 0);

   //set mode 0, enable BG0 and set it to 3D
   videoSetMode(MODE_0_3D);

   // initialize gl
   glInit();
   
   // enable antialiasing
   glEnable(GL_ANTIALIAS);
   
   // setup the rear plane
   glClearColor(0,0,0,31); // BG must be opaque for AA to work
   glClearPolyID(63); // BG must have a unique polygon ID for AA to work
   glClearDepth(0x7FFF);

   //this should work the same as the normal gl call
   glViewport(0,0,255,191);
   
   //any floating point gl call is being converted to fixed prior to being implemented
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(70, 256.0 / 192.0, 0.1, 40);
   
   gluLookAt(   0.0, 0.0, 3.5,      //camera possition
            0.0, 0.0, 0.0,      //look at
            0.0, 1.0, 0.0);      //up
   
   glLight(0, RGB15(31,31,31) , 0,              floattov10(-1.0),       0);
   glLight(1, RGB15(31,0,31),   0,              floattov10(1) - 1,          0);
   glLight(2, RGB15(0,31,0) ,   floattov10(-1.0), 0,                0);
   glLight(3, RGB15(0,0,31) ,   floattov10(1.0) - 1,  0,                0);
   
   //not a real gl function and will likely change
   glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 | POLY_FORMAT_LIGHT1 |
           POLY_FORMAT_LIGHT2 | POLY_FORMAT_LIGHT3 );


   fatInitDefault();
   model = fopen("model.bin", "r");

   while(1)      
   {
      glPushMatrix();
      
      glCallList((u32*)model);

      glPopMatrix(1);   
      glFlush(0);
   }
   return 0;
}


everything goes fine untill it gets to
Code:
glCallList((u32*)model);


I think its correct but for some reason my model dosen show up

model.bin is a teapot.bin from a data folder that I renamed to model.bin and put into root of my flash card.

#159341 - tepples - Sun Jun 29, 2008 4:30 pm

In your code, I don't see anything that allocates RAM to hold the model and loads the model from the card into RAM. The fopen() function does not do this by itself; you have to use malloc(), fread(), and fclose().
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#159345 - DiscoStew - Sun Jun 29, 2008 4:51 pm

Basically what tepples said. The data needs to be loaded into memory prior to using it. Just allocate enough memory to hold the model, open and read the file once into the allocated memory, close that file, and use the new pointer from the allocation to use with glCallList.
_________________
DS - It's all about DiscoStew

#159350 - M3d10n - Sun Jun 29, 2008 6:10 pm

When you do this:

Code:
model = fopen("model.bin", "r");


Model gets a pointer to a struct that holds information about the file in question, not the file data itself. You'll need to read the file size, malloc enough memory to hold it then use fread to read the entire file into the memory area you alloced. A pointer to the malloc'ed memory is what goes into glCallList().

Here's a code snippet that does that:


Code:
char* readWholeFile(const char * filename)
{
   //Open file
   FILE* dl = NULL;
   dl = fopen(filename, "r");
   
   //Find file size
   fseek(dl , 0 , SEEK_END);
   int dlTotalBytes = ftell(dl);
   rewind(dl);

   //allocate memory to contain the whole file:
   char* fileBuffer = (char*)malloc(dlTotalBytes);      
   int result = fread(fileBuffer,1,dlTotalBytes,dl);

   //Something went wrong? Free the memory.
   if (result != dlTotalBytes)
   {
      free(fileBuffer);
      fileBuffer = NULL;
   }

   //Close and return
   fclose(dl);   
   return fileBuffer;
}


Of course you'll have to get rid of the resulting pointer yourself.

#159354 - Meduusa - Sun Jun 29, 2008 6:44 pm

BIG thanks to you guys, you rule!
I got it working now