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 > MD2 as a bin

#151153 - iprice - Wed Feb 20, 2008 1:19 am

I have quite happily been using MD2 files in efs section and reading them in when I need them... but I need to include an MD2 as a bin so it is in main memory... are there any examples of MD2 as bins out there?

#151173 - nipil - Wed Feb 20, 2008 2:02 pm

i'd say, simply put it in a 'data' folder ?
_________________
NDS Lite Gold/Silver, MK5/R4DS, MSI PC54G2, D-Link DI-624

#151188 - iprice - Wed Feb 20, 2008 6:19 pm

What makefile commands are needed....

and how can I read chunks of the bin file similar to efs_read.....

#151191 - iprice - Wed Feb 20, 2008 6:48 pm

Yeah OK so the makefile is quite easy.... but how can I get chunks of the bin file such as the header, triangles etc from the bin? efs_read uses seek and read but I don't know how to do that with a bin file in memory, not in efs....

#151204 - yellowstar - Wed Feb 20, 2008 10:56 pm

Code:

u8 *stream = (u8*)md2_bin;//Change this to the name of your md2 file

//For reading data into structs, do this:
SSomeStruct *somestruct=NULL;
somestruct = (*SSomeStruct)pos;

//For buffers and similar things, do something like this:
u8 *somebuffer = (u8*)malloc(18);
memcpy(somebuffer,stream,18);
//For copying into single bytes, do this:
u8 my_byte = *stream;

//After every read like above, you must increase the value of stream
//by the number of bytes read.
//Structs
stream += sizeof(SSomeStruct);
//Buffers
stream += 18;//18 should be replaced by the actual number of the bytes read
//Single bytes
stream++;

//For seeking, simply do an addition or substraction operation on stream, or a = operation.

#151205 - iprice - Wed Feb 20, 2008 11:10 pm

This is the start of the MD2 code with efs

Code:
EFS_FILE* file;

file = EFS_fopen(Filename);

EFS_fread(&Models[num].header, 1, sizeof (md2_header_t), file);
   
Models[num].texcoords = (md2_texCoord_t *)malloc (sizeof (md2_texCoord_t) * Models[num].header.num_st);
Models[num].triangles = (md2_triangle_t *)malloc (sizeof (md2_triangle_t) * Models[num].header.num_tris);
Models[num].frames = (md2_frame_t *)malloc (sizeof(md2_frame_t) * Models[num].header.num_frames);
   
EFS_fseek(file, Models[num].header.offset_st, SEEK_SET);
EFS_fread(Models[num].texcoords, Models[num].header.num_st, sizeof (md2_texCoord_t), file);


I am trying to convert it to read an MD2 that has been compiled into the memory as a bin. Thanks for the help, I'll have a look at it again.

Anyone has any other help, please let me know.

Cheers

#151256 - iprice - Fri Feb 22, 2008 1:34 am



Last edited by iprice on Sun Feb 24, 2008 4:27 pm; edited 1 time in total

#151365 - iprice - Sun Feb 24, 2008 4:27 pm

Anyone got any spare time?

Cheers.

#151368 - DiscoStew - Sun Feb 24, 2008 6:49 pm

What was wrong with your most recent code? If I remembered how it looked, that was pretty similar to how I'd deal with loading a binary file into a section of memory, and parsing out bits of data for reading.
_________________
DS - It's all about DiscoStew

#151375 - iprice - Sun Feb 24, 2008 9:06 pm

This first bits of data are OK but very long winded.... but later I try to replace:

Code:
EFS_fread(Models[num].frames[i].verts, Models[num].header.num_vertices, sizeof(md2_vertex_t), file);


with:

Code:
for (uu = 0; uu < Models[num].header.num_vertices; uu++)
{
memcpy(somebuffer,stream,sizeof(unsigned char));
Models[num].frames[i].verts->v[0] = *somebuffer;
stream += sizeof(unsigned char);
memcpy(somebuffer,stream,sizeof(unsigned char));
Models[num].frames[i].verts->v[1] = *somebuffer;
stream += sizeof(unsigned char);
memcpy(somebuffer,stream,sizeof(unsigned char));
Models[num].frames[i].verts->v[2] = *somebuffer;
stream += sizeof(unsigned char);
memcpy(somebuffer,stream,sizeof(unsigned char));
Models[num].frames[i].verts->normalIndex = *somebuffer;
stream += sizeof(unsigned char);
}


but the verts are pointers in the structure and it's just not right.......

Code:
typedef struct
{
md2_header_t header;
md2_texCoord_t *texcoords;
md2_triangle_t *triangles;
md2_frame_t *frames;//this bit
nds_texCoord_t *uvs;
nds_triangle_t *tris;
nds_frame_t *frms;
}MD2Entity;

is the MD2

Code:
typedef struct
{
  vec3_t          scale;
  vec3_t          translate;
  char            name[16];
  md2_vertex_t    *verts;//verts are a pointer
} md2_frame_t;

//this is the frame

Code:
typedef struct
{
  unsigned char v[3];
  unsigned char normalIndex;
} md2_vertex_t;

//this is one the verts in the frame

my code just isn't right and I'm not sure how to fix it.... I assume I am re-writing the same vert over top of itself each time.....

any ideas? anyone want the full source to laugh at my terrible coding?

#151379 - yellowstar - Sun Feb 24, 2008 9:36 pm

iprice wrote:

but the verts are pointers in the structure and it's just not right.......

Code:

Models[num].frames[i].verts->v[0] = *somebuffer;



The answer is at the start of the loop...
Code:

for (uu = 0; uu < Models[num].header.num_vertices; uu++)


Code:

verts[uu].v[0]


I've wrote a MD2 loader before, just not on the DS.(PC)
(And it's been a long time since I even looked at that code, however...)

#151381 - iprice - Sun Feb 24, 2008 9:58 pm

Thanks.... before I wasn't getting anything displayed.... now I have garbled polys but I'm getting there.

Thanks... I'll keep going through it :)

#151492 - yellowstar - Wed Feb 27, 2008 9:41 pm

iprice wrote:

I'm trying to replace this:

Code:
EFS_fread(Models[num].frames[i].verts, Models[num].header.num_vertices, sizeof(md2_vertex_t), file);


with this:

Code:
for(...){...}



Try this:
Code:

memcpy(Models[num].frames[i].verts, stream, (int)(Models[num].header.num_vertices * sizeof(md2_vertex_t)));
stream += (int)(Models[num].header.num_vertices * sizeof(md2_vertex_t));

#151496 - iprice - Wed Feb 27, 2008 10:18 pm

Hmmm, when I output the verts to screen I am getting lots of -1 values which I assume are overflow values that are too big for the type..... but the types for the data are correct as it works with efs, so I must still be reading the wrong values from the bin file......

thanks for the idea, but still no joy :(


Hang on.....

I was reading the scale wrong, as an int, not a float , so it was overflowing...... works! yay.

Thanks for all your help yellowstar!