feat: fonction load3ds terminée pour chargement d'un modèle 3D sans texture, à tester

This commit is contained in:
Nicolas Hordé 2018-11-01 01:12:30 +01:00
parent 5f526bf2b7
commit c773880c2e
3 changed files with 136 additions and 31 deletions

View File

@ -8,9 +8,9 @@
typedef struct model3d { typedef struct model3d {
u8 name[12]; u8 name[12];
matrix44 view; matrix44 view;
vector4 **vertexlist; vector4 *vertexlist;
u16 vertexnb; u16 vertexnb;
u16 **facelist; u16 *facelist;
u16 facenb; u16 facenb;
} model3d __attribute__ ((packed)); } model3d __attribute__ ((packed));
@ -25,13 +25,29 @@ typedef struct vertex3d {
}; };
} vertex3d __attribute__ ((packed)); } vertex3d __attribute__ ((packed));
void proj(vector4 list[], vertex2d plane[], vector4 origin[], u16 number, float factor); void proj(vector4 list[], vertex2d plane[], vector4 origin[], u16 number, float factor);
void cube(vector4 list[], vector4 *origin, u16 size); void cube(vector4 list[], vector4 *origin, u16 size);
int load3ds(u8 *pointer,u32 size, model3d *model);
/*******************************************************************************/ /*******************************************************************************/
/* Fichier 3DS */ /* Fichier 3DS */
typedef enum dsState
{
DS_READ_CHUNK_ID,
DS_READ_CHUNK_LENGTH,
DS_READ_OBJECT_NAME,
DS_SKIP_CHUNK,
DS_READ_POINT_COUNT,
DS_READ_POINTS,
DS_READ_FACE_COUNT,
DS_READ_FACES,
DS_READ_MATRIX,
DS_READ_DONE
} dsState __attribute__ ((packed));
#define MAIN3DS 0x4D4D #define MAIN3DS 0x4D4D
//>------ Main Chunks //>------ Main Chunks

113
lib/3d.c
View File

@ -4,6 +4,7 @@
#include "3d.h" #include "3d.h"
#include "types.h" #include "types.h"
#include "video.h" #include "video.h"
#include "string.h"
/*******************************************************************************/ /*******************************************************************************/
/* Crée une projection simple pour test */ /* Crée une projection simple pour test */
@ -58,35 +59,123 @@ void cube(vector4 list[], vector4 *origin, u16 size)
/*******************************************************************************/ /*******************************************************************************/
/* Charge un fichier 3DS */ /* Charge un fichier 3DS */
void load3ds(u8 *pointer, model3d *model) int load3ds(u8 *pointer,u32 size, model3d *model)
{ {
u16 chunk; u8 *ptr=pointer;
u32 size; u16 chunk_id;
bool formatok; u32 chunk_size;
while(true) { u16 i;
chunk=(u16) *(pointer); float *listfloat;
pointer+=2; u16 *listunsigned;
size=(u32) *(pointer); dsState state=DS_READ_CHUNK_ID;
pointer+=4; bool dsfile=false;
switch(chunk) while(ptr-pointer<size) {
switch(state)
{
case DS_READ_CHUNK_ID:
chunk_id=*((u16*) ptr);
ptr+=2;
state=DS_READ_CHUNK_LENGTH;
break;
case DS_READ_CHUNK_LENGTH:
chunk_size=*((u32*) ptr);
ptr+=4;
switch(chunk_id)
{ {
case MAIN3DS: case MAIN3DS:
dsfile=true;
ptr+=10;
state=DS_READ_CHUNK_ID;
break; break;
case EDIT3DS: case EDIT3DS:
break; state=DS_READ_CHUNK_ID;
case EDIT_OBJECT:
break; break;
case OBJ_TRIMESH: case OBJ_TRIMESH:
state=DS_READ_CHUNK_ID;
break;
case EDIT_OBJECT:
state=DS_READ_OBJECT_NAME;
break; break;
case TRI_VERTEXL: case TRI_VERTEXL:
state=DS_READ_POINT_COUNT;
break; break;
case TRI_FACEL1: case TRI_FACEL1:
state=DS_READ_FACE_COUNT;
break; break;
case TRI_LOCAL: case TRI_LOCAL:
state=DS_READ_MATRIX;
break; break;
default: default:
if (!dsfile) return 1;
ptr+=(chunk_size-6);
state=DS_READ_CHUNK_ID;
break; break;
} }
break;
case DS_READ_OBJECT_NAME:
strcpy(ptr, model->name);
ptr+=(strlen(ptr)+1);
state=DS_READ_CHUNK_ID;
break;
case DS_SKIP_CHUNK:
break;
case DS_READ_POINT_COUNT:
model->vertexnb=*((u16*) ptr);
state=DS_READ_POINTS;
ptr+=2;
break;
case DS_READ_POINTS:
i=0;
listfloat=ptr;
model->vertexlist=0x00300000;
while(i<model->vertexnb)
{
model->vertexlist[i].x=*(listfloat++);
model->vertexlist[i].y=*(listfloat++);
model->vertexlist[i].z=*(listfloat++);
model->vertexlist[i++].w=1.0;
}
ptr=listfloat;
state=DS_READ_CHUNK_ID;
break;
case DS_READ_FACE_COUNT:
model->facenb=*((u16*) ptr);
state=DS_READ_FACES;
ptr+=2;
break;
case DS_READ_FACES:
i=0;
listunsigned=ptr;
model->facelist=0x00400000;
while(i<model->facenb*3)
{
model->facelist[i++]=*(listunsigned++);
model->facelist[i++]=*(listunsigned++);
model->facelist[i++]=*(listunsigned++);
listunsigned++;
}
ptr=listunsigned;
state=DS_READ_CHUNK_ID;
break;
case DS_READ_MATRIX:
i=0;
listfloat=ptr;
while(i<4)
{
model->view.V[i].x=*(listfloat++);
model->view.V[i].y=*(listfloat++);
model->view.V[i].z=*(listfloat++);
model->view.V[i++].w=0.0f;
}
model->view.V[3].w=1.0f;
ptr=listfloat;
state=DS_READ_CHUNK_ID;
break;
case DS_READ_DONE:
break;
}
} }
} }

View File

@ -429,7 +429,7 @@ int test3d()
return 1; return 1;
} }
model3d model; model3d model;
load3ds(&sphere, &model); load3ds(&sphere, sizeof(sphere), &model);
vector4 list3d[8]; vector4 list3d[8];
vertex2d list2d[8]; vertex2d list2d[8];
matrix44 rotatex,rotatey,rotatez,mrotatex,mrotatey,mrotatez; matrix44 rotatex,rotatey,rotatez,mrotatex,mrotatey,mrotatez;