#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:
.
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!
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"
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"