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 > serious problem with md2 loader

#110429 - Xgame - Tue Nov 28, 2006 4:08 pm

hi to evrybody!
I've just ended to port http://tfc.duke.free.fr/coding/md2-specs-en.html md2 loader to nds.
I done that becouse I found this topic: http://forum.gbadev.org/viewtopic.php?t=7176&start=0&postdays=0&postorder=asc&highlight=collision
when i read this, i decided to port it on Ds:
Quote:

put md2 in data dir

#include <myCoolDude_md2.h>

void loadmd2(u8* file, myObjStruct obj)
{
..blah blah some init

//fseak
possition = triangle_offset;

//fread
memcpy(&file[possition], obj->triangles, sizeoftriangles);

//or just: obj->triangles = &file[possition];

file io is much more cumbersum than treating the md2 like an array
...
blah blah

}


.
ok, in less than an hour I converted the code for ds ( really easy). when I compile i don't get problem, but when I try to emulate it, it dosen't work. I haven't tryed it on hardware, yet, but I think is a problem of my code.
please found bug!
Code:
typedef float vec3_t[3];


/* md2 header */
typedef struct
{
  int ident;
  int version;

  int skinwidth;
  int skinheight;

  int framesize;

  int num_skins;
  int num_vertices;
  int num_st;
  int num_tris;
  int num_glcmds;
  int num_frames;

  int offset_skins;
  int offset_st;
  int offset_tris;
  int offset_frames;
  int offset_glcmds;
  int offset_end;

} md2_header_t;


/* texture name */
typedef struct
{
  char name[64];

} md2_skin_t;


/* texture coords */
typedef struct
{
  short s;
  short t;

} md2_texCoord_t;


/* triangle info */
typedef struct
{
  unsigned short vertex[3];
  unsigned short st[3];

} md2_triangle_t;


/* compressed vertex */
typedef struct
{
  unsigned char v[3];
  unsigned char normalIndex;

} md2_vertex_t;


/* model frame */
typedef struct
{
  vec3_t scale;
  vec3_t translate;
  char name[16];
  md2_vertex_t *verts;

} md2_frame_t;


/* gl command packet */
typedef struct
{
  float s;
  float t;
  int index;

} md2_glcmd_t;


/* md2 model structure */
typedef struct
{
  md2_header_t header;

  md2_skin_t *skins;
  md2_texCoord_t *texcoords;
  md2_triangle_t *triangles;
  md2_frame_t *frames;
  int *glcmds;

  GLuint tex_id;

} md2_model_t;


md2_model_t *md2file;

/* table of precalculated normals */
vec3_t anorms_table[162] = {
#include "anorms.h"
};

int LoadMd2(u8* filemdl, md2_model_t *mdl){
    int position = 0; //posizione da usare al posto di fseek
    //mdl.header = &filemdl[position];
   
    memcpy(&filemdl[position],&mdl->header,sizeof (md2_header_t));
    //position++;
   

   
     /*
      if ((mdl->header.ident != 844121161) ||
      (mdl->header.version != 8))
    {

     
      return 0;
    }*/
   
  mdl->skins = (md2_skin_t *)malloc (sizeof (md2_skin_t) * mdl->header.num_skins);
  mdl->texcoords = (md2_texCoord_t *)malloc (sizeof (md2_texCoord_t) * mdl->header.num_st);
  mdl->triangles = (md2_triangle_t *)malloc (sizeof (md2_triangle_t) * mdl->header.num_tris);
  mdl->frames = (md2_frame_t *)malloc (sizeof(md2_frame_t) * mdl->header.num_frames);
  mdl->glcmds = (int *)malloc (sizeof (int) * mdl->header.num_glcmds);

 
 
  position = mdl->header.offset_skins;
  memcpy(&filemdl[position],&mdl->skins, sizeof (md2_skin_t));
 
  position = mdl->header.offset_st;
  memcpy(&filemdl[position],&mdl->texcoords, sizeof (md2_texCoord_t));
 
  position = mdl->header.offset_tris;
  memcpy(&filemdl[position],&mdl->triangles, sizeof (md2_triangle_t));
 
  position = mdl->header.offset_glcmds;
  memcpy(&filemdl[position], &mdl->glcmds, sizeof (int));
   // read frames */
  position = mdl->header.offset_frames;
  int i;
  for (i = 0; i < mdl->header.num_frames; ++i)
    {
      // memory allocation for vertices of this frame */
      mdl->frames[i].verts = (md2_vertex_t *)
   malloc (sizeof (md2_vertex_t) * mdl->header.num_vertices);

      // read frame data */
      memcpy(&filemdl[position], &mdl->frames[i].scale, sizeof (vec3_t));
      //fread (mdl->frames[i].scale, sizeof (vec3_t), 1, fp);
      memcpy(&filemdl[position], &mdl->frames[i].translate, sizeof (vec3_t));
      //fread (mdl->frames[i].translate, sizeof (vec3_t), 1, fp);
      memcpy(&filemdl[position], &mdl->frames[i].name, sizeof (char));
      //fread (mdl->frames[i].name, sizeof (char), 16, fp);
      memcpy(&filemdl[position], &mdl->frames[i].verts, sizeof (md2_vertex_t));
      //fread (mdl->frames[i].verts, sizeof (md2_vertex_t), mdl->header.num_vertices, fp);
    }
 
  return 1;
}

void
RenderFrame (int n, md2_model_t *mdl)
{
  int i, j;
  GLfloat s, t;
  vec3_t v;
  md2_frame_t *pframe;
  md2_vertex_t *pvert;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* draw the model */
  glBegin (GL_TRIANGLES);
    /* draw each triangle */
    for (i = 0; i < mdl->header.num_tris; ++i)
      {
   /* draw each vertex */
   for (j = 0; j < 3; ++j)
     {
       pframe = &mdl->frames[n];
       pvert = &pframe->verts[mdl->triangles[i].vertex[j]];

       /* compute texture coordinates */
       s = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].s / mdl->header.skinwidth;
       t = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].t / mdl->header.skinheight;

       /* pass texture coordinates to OpenGL */
       glTexCoord2f (s, t);

       /* normal vector */
       //glNormal3fv (anorms_table[pvert->normalIndex]);
       glNormal3f(anorms_table[pvert->normalIndex][0], anorms_table[pvert->normalIndex][1], anorms_table[pvert->normalIndex][2]);

      
      

       /* calculate vertex real position */
       v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
       v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
       v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];

       //glVertex3fv (v);
       glVertex3f(v[0], v[1], v[2]);
     }
      }
  glEnd ();
}


void
RenderFrameItp (int n, float interp, md2_model_t *mdl)
{
  int i, j;
  GLfloat s, t;
  vec3_t v_curr, v_next, v, norm;
  float *n_curr, *n_next;
  md2_frame_t *pframe1, *pframe2;
  md2_vertex_t *pvert1, *pvert2;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* draw the model */
  glBegin (GL_TRIANGLES);
    /* draw each triangle */
    for (i = 0; i < mdl->header.num_tris; ++i)
      {
   /* draw each vertex */
   for (j = 0; j < 3; ++j)
     {
       pframe1 = &mdl->frames[n];
       pframe2 = &mdl->frames[n + 1];
       pvert1 = &pframe1->verts[mdl->triangles[i].vertex[j]];
       pvert2 = &pframe2->verts[mdl->triangles[i].vertex[j]];

       /* compute texture coordinates */
       s = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].s / mdl->header.skinwidth;
       t = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].t / mdl->header.skinheight;

       /* pass texture coordinates to OpenGL */
       glTexCoord2f (s, t);

       /* interpolate normals */
       n_curr = anorms_table[pvert1->normalIndex];
       n_next = anorms_table[pvert2->normalIndex];

       norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
       norm[1] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
       norm[2] = n_curr[2] + interp * (n_next[2] - n_curr[2]);

       glNormal3f (norm[0], norm[1], norm[2]);

       /* interpolate vertices */
       v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
       v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
       v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];

       v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
       v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
       v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];

       v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
       v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
       v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);

       glVertex3f (v[0], v[1], v[2]);
     }
      }
  glEnd ();
}



void
RenderFrameWithGLCmds (int n, md2_model_t *mdl)
{
  int i, *pglcmds;
  vec3_t v;
  md2_frame_t *pframe;
  md2_vertex_t *pvert;
  md2_glcmd_t *packet;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* pglcmds points at the start of the command list */
  pglcmds = mdl->glcmds;

  /* draw the model */
  while ((i = *(pglcmds++)) != 0)
    {
      if (i < 0)
   {
     glBegin (GL_TRIANGLE_STRIP);
     i = -i;
   }
      else
   {
     glBegin (GL_TRIANGLE_STRIP);
   }

      /* draw each vertex of this group */
      for (/* nothing */; i > 0; --i, pglcmds += 3)
   {
     packet = (md2_glcmd_t *)pglcmds;
     pframe = &mdl->frames[n];
     pvert = &pframe->verts[packet->index];

     /* pass texture coordinates to OpenGL */
     glTexCoord2f (packet->s, packet->t);

     /* normal vector */
      glNormal3f(anorms_table[pvert->normalIndex][0], anorms_table[pvert->normalIndex][1], anorms_table[pvert->normalIndex][2]);

     /* calculate vertex real position */
     v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
     v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
     v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];

     glVertex3f (v[0], v[1], v[2]);
   }

      glEnd ();
    }
}


void
RenderFrameItpWithGLCmds (int n, float interp, md2_model_t *mdl)
{
  int i, *pglcmds;
  vec3_t v_curr, v_next, v, norm;
  float *n_curr, *n_next;
  md2_frame_t *pframe1, *pframe2;
  md2_vertex_t *pvert1, *pvert2;
  md2_glcmd_t *packet;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* pglcmds points at the start of the command list */
  pglcmds = mdl->glcmds;

  /* draw the model */
  while ((i = *(pglcmds++)) != 0)
    {
      if (i < 0)
   {
     glBegin (GL_TRIANGLE_STRIP);
     i = -i;
   }
      else
   {
     glBegin (GL_TRIANGLE_STRIP);
   }

      /* draw each vertex of this group */
      for (/* nothing */; i > 0; --i, pglcmds += 3)
   {
     packet = (md2_glcmd_t *)pglcmds;
     pframe1 = &mdl->frames[n];
     pframe2 = &mdl->frames[n + 1];
     pvert1 = &pframe1->verts[packet->index];
     pvert2 = &pframe2->verts[packet->index];

     /* pass texture coordinates to OpenGL */
     glTexCoord2f (packet->s, packet->t);

     /* interpolate normals */
     n_curr = anorms_table[pvert1->normalIndex];
     n_next = anorms_table[pvert2->normalIndex];

     norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
     norm[1] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
     norm[2] = n_curr[2] + interp * (n_next[2] - n_curr[2]);

     glNormal3f (norm[0], norm[1], norm[2]);

     /* interpolate vertices */
     v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
     v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
     v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];

     v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
     v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
     v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];

     v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
     v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
     v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);

     glVertex3f (v[0], v[1], v[2]);
   }

      glEnd ();
    }
}

void
Animate (int start, int end, int *frame, float *interp)
{
  if ((*frame < start) || (*frame > end))
    *frame = start;

  if (*interp >= 1.0f)
    {
      /* move to next frame */
      *interp = 0.0f;
      (*frame)++;

      if (*frame >= end)
   *frame = start;
    }
}

this is the code of "Md2loader.h", a file I include in main code.
PS
for the 1st argoument of "LoadMd2" I use (u8*)tris.
I get "tris" including a md2 file, renamed in ".bin".
problem is Here? I don't think, but maybe...
[EDIT]
With a simple brakepoint system I understood that the bug is in "memcpy"

#110462 - Payk - Tue Nov 28, 2006 7:55 pm

Hey...
2 things
1: Use fixpoint to get a much faster result
2: Use hardware futures (translate/scale) to get a much faster result

Overgiving the translate/scale data to hardware even takes time too!
I use to precalc every frame and store in fixed point. So i jut give the matrix the verticepoints and texturecords (and a light color precalced by myy engine using simplified normals ;P )

If u do same u really can use 2048 polys @ 60FPS (also with regualr fixed point normals and 4 hardware light sources)
otherwhise that wont work.
If you need help, just ask ;)

#110577 - Xgame - Wed Nov 29, 2006 2:55 pm

fixpoint are the "3d math" inside Ds, right? so, using them, I could have mor poly displayed. are glScalev and glTranslatev hardware funcion? I never used these code optimizer... But for the main problem: why memcpy freeze evrythigs?

#110579 - simonjhall - Wed Nov 29, 2006 3:30 pm

The fixed point stuff he's talking about is avoiding the use of float and doubles and replacing them with ints, shorts etc. I think there are fixed-point versions of some of the OpenGL functions. The DS doesn't have a floating-point unit, so whenever you use floats it has to emulate them in software. Which is really slow.

These GL functions there are most likely done in hardware.

And if your memcpy is locking up then it's 99% likely that one of the pointers used is invalid, or you're accessing beyond the end of your array or something...

#110582 - Xgame - Wed Nov 29, 2006 4:04 pm

simonjhall wrote:
The fixed point stuff he's talking about is avoiding the use of float and doubles and replacing them with ints, shorts etc. I think there are fixed-point versions of some of the OpenGL functions. The DS doesn't have a floating-point unit, so whenever you use floats it has to emulate them in software. Which is really slow.

These GL functions there are most likely done in hardware.

And if your memcpy is locking up then it's 99% likely that one of the pointers used is invalid, or you're accessing beyond the end of your array or something...

I don't got any warning when i compile it... maybe an md2 file don't begin from 0 but from 1 or 2.
PS
but if I use an int for 3d numbers wold be huge! but if it's better...

#111436 - Xgame - Wed Dec 06, 2006 4:35 pm

I moved the project to kosfs, but it isn't working...(ds give 2 black screen) here's the code of loader with KOS funcion. why it dosen't work?
Code:



typedef float vec3_t[3];


/* md2 header */
typedef struct
{
  int ident;
  int version;

  int skinwidth;
  int skinheight;

  int framesize;

  int num_skins;
  int num_vertices;
  int num_st;
  int num_tris;
  int num_glcmds;
  int num_frames;

  int offset_skins;
  int offset_st;
  int offset_tris;
  int offset_frames;
  int offset_glcmds;
  int offset_end;

} md2_header_t;


/* texture name */
typedef struct
{
  char name[64];

} md2_skin_t;


/* texture coords */
typedef struct
{
  short s;
  short t;

} md2_texCoord_t;


/* triangle info */
typedef struct
{
  unsigned short vertex[3];
  unsigned short st[3];

} md2_triangle_t;


/* compressed vertex */
typedef struct
{
  unsigned char v[3];
  unsigned char normalIndex;

} md2_vertex_t;


/* model frame */
typedef struct
{
  vec3_t scale;
  vec3_t translate;
  char name[16];
  md2_vertex_t *verts;

} md2_frame_t;


/* gl command packet */
typedef struct
{
  float s;
  float t;
  int index;

} md2_glcmd_t;


/* md2 model structure */
typedef struct
{
  md2_header_t header;

  md2_skin_t *skins;
  md2_texCoord_t *texcoords;
  md2_triangle_t *triangles;
  md2_frame_t *frames;
  int *glcmds;

  GLuint tex_id;

} md2_model_t;


md2_model_t *md2file;

/* table of precalculated normals */
vec3_t anorms_table[162] = {
#include "anorms.h"
};

int
ReadMD2Model (const char *filename, md2_model_t *mdl)
{
  KOS_FILE *fp;
  int i;

  fp = KOS_fopen (filename, "rb");
  if (!fp)
    {
      //fprintf (stderr, "error: couldn't open \"%s\"!", filename);
      return 0;
    }

  /* read header */
  KOS_fread (&mdl->header, 1, sizeof (md2_header_t), fp);

  if ((mdl->header.ident != 844121161) ||
      (mdl->header.version != 8))
    {
      /* error! */
      //fprintf (stderr, "error: bad version!");
      KOS_fclose (fp);
      return 0;
    }

  /* memory allocation */
  mdl->skins = (md2_skin_t *)malloc (sizeof (md2_skin_t) * mdl->header.num_skins);
  mdl->texcoords = (md2_texCoord_t *)malloc (sizeof (md2_texCoord_t) * mdl->header.num_st);
  mdl->triangles = (md2_triangle_t *)malloc (sizeof (md2_triangle_t) * mdl->header.num_tris);
  mdl->frames = (md2_frame_t *)malloc (sizeof(md2_frame_t) * mdl->header.num_frames);
  mdl->glcmds = (int *)malloc (sizeof (int) * mdl->header.num_glcmds);

  /* read model data */
  KOS_fseek (fp, mdl->header.offset_skins, SEEK_SET);
  KOS_fread (mdl->skins, sizeof (md2_skin_t), mdl->header.num_skins, fp);

  KOS_fseek (fp, mdl->header.offset_st, SEEK_SET);
  KOS_fread (mdl->texcoords, sizeof (md2_texCoord_t), mdl->header.num_st, fp);

  KOS_fseek (fp, mdl->header.offset_tris, SEEK_SET);
  KOS_fread (mdl->triangles, sizeof (md2_triangle_t), mdl->header.num_tris, fp);

  KOS_fseek (fp, mdl->header.offset_glcmds, SEEK_SET);
  KOS_fread (mdl->glcmds, sizeof (int), mdl->header.num_glcmds, fp);

  /* read frames */
  KOS_fseek (fp, mdl->header.offset_frames, SEEK_SET);
  //int i;
  for (i = 0; i < mdl->header.num_frames; ++i)
    {
      /* memory allocation for vertices of this frame */
      mdl->frames[i].verts = (md2_vertex_t *)
   malloc (sizeof (md2_vertex_t) * mdl->header.num_vertices);

      /* read frame data */
      KOS_fread (mdl->frames[i].scale, sizeof (vec3_t), 1, fp);
      KOS_fread (mdl->frames[i].translate, sizeof (vec3_t), 1, fp);
      KOS_fread (mdl->frames[i].name, sizeof (char), 16, fp);
      KOS_fread (mdl->frames[i].verts, sizeof (md2_vertex_t), mdl->header.num_vertices, fp);
    }

  KOS_fclose (fp);
  return 1;
}

void
RenderFrame (int n, md2_model_t *mdl)
{
  int i, j;
  GLfloat s, t;
  vec3_t v;
  md2_frame_t *pframe;
  md2_vertex_t *pvert;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* draw the model */
  glBegin (GL_TRIANGLES);
    /* draw each triangle */
    for (i = 0; i < mdl->header.num_tris; ++i)
      {
   /* draw each vertex */
   for (j = 0; j < 3; ++j)
     {
       pframe = &mdl->frames[n];
       pvert = &pframe->verts[mdl->triangles[i].vertex[j]];

       /* compute texture coordinates */
       s = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].s / mdl->header.skinwidth;
       t = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].t / mdl->header.skinheight;

       /* pass texture coordinates to OpenGL */
       glTexCoord2f (s, t);

       /* normal vector */
       //glNormal3fv (anorms_table[pvert->normalIndex]);
       glNormal3f(anorms_table[pvert->normalIndex][0], anorms_table[pvert->normalIndex][1], anorms_table[pvert->normalIndex][2]);

      
      

       /* calculate vertex real position */
       v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
       v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
       v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];

       //glVertex3fv (v);
       glVertex3f(v[0], v[1], v[2]);
     }
      }
  glEnd ();
}


void
RenderFrameItp (int n, float interp, md2_model_t *mdl)
{
  int i, j;
  GLfloat s, t;
  vec3_t v_curr, v_next, v, norm;
  float *n_curr, *n_next;
  md2_frame_t *pframe1, *pframe2;
  md2_vertex_t *pvert1, *pvert2;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* draw the model */
  glBegin (GL_TRIANGLES);
    /* draw each triangle */
    for (i = 0; i < mdl->header.num_tris; ++i)
      {
   /* draw each vertex */
   for (j = 0; j < 3; ++j)
     {
       pframe1 = &mdl->frames[n];
       pframe2 = &mdl->frames[n + 1];
       pvert1 = &pframe1->verts[mdl->triangles[i].vertex[j]];
       pvert2 = &pframe2->verts[mdl->triangles[i].vertex[j]];

       /* compute texture coordinates */
       s = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].s / mdl->header.skinwidth;
       t = (GLfloat)mdl->texcoords[mdl->triangles[i].st[j]].t / mdl->header.skinheight;

       /* pass texture coordinates to OpenGL */
       glTexCoord2f (s, t);

       /* interpolate normals */
       n_curr = anorms_table[pvert1->normalIndex];
       n_next = anorms_table[pvert2->normalIndex];

       norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
       norm[1] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
       norm[2] = n_curr[2] + interp * (n_next[2] - n_curr[2]);

       glNormal3f (norm[0], norm[1], norm[2]);

       /* interpolate vertices */
       v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
       v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
       v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];

       v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
       v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
       v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];

       v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
       v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
       v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);

       glVertex3f (v[0], v[1], v[2]);
     }
      }
  glEnd ();
}



void
RenderFrameWithGLCmds (int n, md2_model_t *mdl)
{
  int i, *pglcmds;
  vec3_t v;
  md2_frame_t *pframe;
  md2_vertex_t *pvert;
  md2_glcmd_t *packet;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* pglcmds points at the start of the command list */
  pglcmds = mdl->glcmds;

  /* draw the model */
  while ((i = *(pglcmds++)) != 0)
    {
      if (i < 0)
   {
     glBegin (GL_TRIANGLE_STRIP);
     i = -i;
   }
      else
   {
     glBegin (GL_TRIANGLE_STRIP);
   }

      /* draw each vertex of this group */
      for (/* nothing */; i > 0; --i, pglcmds += 3)
   {
     packet = (md2_glcmd_t *)pglcmds;
     pframe = &mdl->frames[n];
     pvert = &pframe->verts[packet->index];

     /* pass texture coordinates to OpenGL */
     glTexCoord2f (packet->s, packet->t);

     /* normal vector */
      glNormal3f(anorms_table[pvert->normalIndex][0], anorms_table[pvert->normalIndex][1], anorms_table[pvert->normalIndex][2]);

     /* calculate vertex real position */
     v[0] = (pframe->scale[0] * pvert->v[0]) + pframe->translate[0];
     v[1] = (pframe->scale[1] * pvert->v[1]) + pframe->translate[1];
     v[2] = (pframe->scale[2] * pvert->v[2]) + pframe->translate[2];

     glVertex3f (v[0], v[1], v[2]);
   }

      glEnd ();
    }
}


void
RenderFrameItpWithGLCmds (int n, float interp, md2_model_t *mdl)
{
  int i, *pglcmds;
  vec3_t v_curr, v_next, v, norm;
  float *n_curr, *n_next;
  md2_frame_t *pframe1, *pframe2;
  md2_vertex_t *pvert1, *pvert2;
  md2_glcmd_t *packet;

  /* check if n is in a valid range */
  if ((n < 0) || (n > mdl->header.num_frames - 1))
    return;

  /* enable model's texture */
  glBindTexture (GL_TEXTURE_2D, mdl->tex_id);

  /* pglcmds points at the start of the command list */
  pglcmds = mdl->glcmds;

  /* draw the model */
  while ((i = *(pglcmds++)) != 0)
    {
      if (i < 0)
   {
     glBegin (GL_TRIANGLE_STRIP);
     i = -i;
   }
      else
   {
     glBegin (GL_TRIANGLE_STRIP);
   }

      /* draw each vertex of this group */
      for (/* nothing */; i > 0; --i, pglcmds += 3)
   {
     packet = (md2_glcmd_t *)pglcmds;
     pframe1 = &mdl->frames[n];
     pframe2 = &mdl->frames[n + 1];
     pvert1 = &pframe1->verts[packet->index];
     pvert2 = &pframe2->verts[packet->index];

     /* pass texture coordinates to OpenGL */
     glTexCoord2f (packet->s, packet->t);

     /* interpolate normals */
     n_curr = anorms_table[pvert1->normalIndex];
     n_next = anorms_table[pvert2->normalIndex];

     norm[0] = n_curr[0] + interp * (n_next[0] - n_curr[0]);
     norm[1] = n_curr[1] + interp * (n_next[1] - n_curr[1]);
     norm[2] = n_curr[2] + interp * (n_next[2] - n_curr[2]);

     glNormal3f (norm[0], norm[1], norm[2]);

     /* interpolate vertices */
     v_curr[0] = pframe1->scale[0] * pvert1->v[0] + pframe1->translate[0];
     v_curr[1] = pframe1->scale[1] * pvert1->v[1] + pframe1->translate[1];
     v_curr[2] = pframe1->scale[2] * pvert1->v[2] + pframe1->translate[2];

     v_next[0] = pframe2->scale[0] * pvert2->v[0] + pframe2->translate[0];
     v_next[1] = pframe2->scale[1] * pvert2->v[1] + pframe2->translate[1];
     v_next[2] = pframe2->scale[2] * pvert2->v[2] + pframe2->translate[2];

     v[0] = v_curr[0] + interp * (v_next[0] - v_curr[0]);
     v[1] = v_curr[1] + interp * (v_next[1] - v_curr[1]);
     v[2] = v_curr[2] + interp * (v_next[2] - v_curr[2]);

     glVertex3f (v[0], v[1], v[2]);
   }

      glEnd ();
    }
}

void
Animate (int start, int end, int *frame, float *interp)
{
  if ((*frame < start) || (*frame > end))
    *frame = start;

  if (*interp >= 1.0f)
    {
      /* move to next frame */
      *interp = 0.0f;
      (*frame)++;

      if (*frame >= end)
   *frame = start;
    }
}

i'll put ints after get this working.

#111447 - Payk - Wed Dec 06, 2006 5:40 pm

u noticed that the directory starts with rd?
i mean "/rd/" is your root folder...
how did u init it?

Code:
      REG_IME=0;   
      sysSetCartOwner( BUS_OWNER_ARM9 );
      fs_init();
      fs_romdisk_mount("/rd", (uint8*)find_first_romfs_file((uint8*)0x08000000), 0);


next thing could be that datas are attached wrong.
So i moved all tools which are neececary in a folder called
"romtoolz" in the root folder of my development folder.
another folder which is called "bundle" is nececary.
then run a batch file like this:

Code:
@echo off

echo -- Packing data into a single KOSFS data file
romtoolz\genromfs -d rd2 -f extern.rom

echo -- Append KSOFS data file onto end of New.ds.gba
copy Combined.ds.gba bundle\New.ds.gba.bin
romtoolz\padbin 256 bundle\New.ds.gba.bin
copy /B bundle\New.ds.gba.bin+extern.rom bundle\Wolves.sc.nds
copy /B bundle\New.ds.gba.bin+extern.rom bundle\Wolves.ds.gba

echo -- Append KOSFS data file onto end of New.nds
copy Combined.nds bundle\New.nds.bin
romtoolz\padbin 256 bundle\New.nds.bin
copy /B bundle\New.nds.bin+extern.rom bundle\Wolves.emu.nds

pause

del extern.rom
del bundle\New.nds.bin
del bundle\New.ds.gba.bin

This way of doing it is from dslua ;P
I liked that ..
it makes a rom from the folder "rd" (i got more of them...stupid name)
then it takes the roms combined.ds.gba and nds, and attaches the rom to it. The result should be in bundle folder.

whats in the folder romtoolz?
Padbin.exe, genromfs.exe , cygwin1.dll and msys-1.0.dll.

I hope that helped a bit

#111448 - Payk - Wed Dec 06, 2006 5:44 pm

I highly recommmand some printouts for better troubleshoting.
After init u could print "init succesfully"

and print out if it founds the md2 file. maybe print out the md2 header...how many polys does it uses etc. That always helps to locate the problem ;)

#111926 - Xgame - Mon Dec 11, 2006 8:01 pm

I finally opened the file; so I read it, but it result wrong(the read data). I'am working over, if you know something about KOS_fread plese let me know.
PS
maybe is better to use fat?

#111928 - Payk - Mon Dec 11, 2006 8:22 pm

ahh
try fread with "rb" instead of "r"
Code:
KOS_FILE *fp = KOS_fopen(Filename,"rb") ;

Maybe that makes a difference. It shouldnt normaly, but i found out that it sometimes makes a difference even if u dont use getc command.
best is just try that out ;)

#112022 - Xgame - Tue Dec 12, 2006 3:13 pm

it still dosen't work! i print the ident number and it is "0194". is it the real model id or it's the result of a bug( an unright variable type, a wrong size of reading...)?
EDIT
i'am jumping the version test... soon i'll can say if it's work
EDIT2
it's seems working, now i'll try in 3d...
EDIT 3
I loaded an Md2 model! I am sure i loaded it because i create a funcion wich print the pointlist. but I can't draw it, I don't know why. I also draw the nehe 2 scene, but I can't see it. have you got any ideas? I'll do some test later.