feat: ajout 3d fil de fer et fonction show3dmodel, cube utilise model3d

This commit is contained in:
Nicolas Hordé 2018-11-01 19:19:29 +01:00
parent c773880c2e
commit 35ae3559db
5 changed files with 15821 additions and 56 deletions

View File

@ -5,12 +5,18 @@
#include "matrix.h" #include "matrix.h"
#include "video.h" #include "video.h"
typedef struct triface {
u16 V1;
u16 V2;
u16 V3;
} triface __attribute__ ((packed));
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; triface *facelist;
u16 facenb; u16 facenb;
} model3d __attribute__ ((packed)); } model3d __attribute__ ((packed));
@ -25,10 +31,20 @@ typedef struct vertex3d {
}; };
} vertex3d __attribute__ ((packed)); } vertex3d __attribute__ ((packed));
typedef enum type3D
{
TYPE3D_POINTS,
TYPE3D_LINES,
TYPE3D_FACES,
TYPE3D_FLAT,
TYPE3D_TEXTURE,
} type3D __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(model3d *model, vector4 *origin, u16 size);
int load3ds(u8 *pointer,u32 size, model3d *model); int load3ds(u8 *pointer,u32 size, model3d *model);
void show3dmodel(model3d *model, matrix44 *transformation, vector4 origin[], float factor, type3D type);
/*******************************************************************************/ /*******************************************************************************/
/* Fichier 3DS */ /* Fichier 3DS */

15661
lib/3D/man.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1473,4 +1473,3 @@ static u8 sphere[] = {
0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80,
0x3f 0x3f
}; };
unsigned int sphere_3DS_len = 17665;

148
lib/3d.c
View File

@ -20,40 +20,114 @@ void proj(vector4 list[], vertex2d plane[], vector4 origin[], u16 number, float
/*******************************************************************************/ /*******************************************************************************/
/* Crée une liste de vertex3D pour un cube */ /* Crée une liste de vertex3D pour un cube */
void cube(vector4 list[], vector4 *origin, u16 size) void cube(model3d *model, vector4 *origin, u16 size)
{ {
list[0].x=origin->x; strcpy("cube",model->name);
list[0].y=origin->y; model->vertexnb=8;
list[0].z=origin->z; model->vertexlist=0x00300000;
list[0].w=1.0f; model->vertexlist[0].x=origin->x;
list[1].x=origin->x+size; model->vertexlist[0].y=origin->y;
list[1].y=origin->y; model->vertexlist[0].z=origin->z;
list[1].z=origin->z; model->vertexlist[0].w=1.0f;
list[1].w=1.0f; model->vertexlist[1].x=origin->x+size;
list[2].x=origin->x; model->vertexlist[1].y=origin->y;
list[2].y=origin->y+size; model->vertexlist[1].z=origin->z;
list[2].z=origin->z; model->vertexlist[1].w=1.0f;
list[2].w=1.0f; model->vertexlist[2].x=origin->x;
list[3].x=origin->x+size; model->vertexlist[2].y=origin->y+size;
list[3].y=origin->y+size; model->vertexlist[2].z=origin->z;
list[3].z=origin->z; model->vertexlist[2].w=1.0f;
list[3].w=1.0f; model->vertexlist[3].x=origin->x+size;
list[4].x=origin->x; model->vertexlist[3].y=origin->y+size;
list[4].y=origin->y; model->vertexlist[3].z=origin->z;
list[4].z=origin->z+size; model->vertexlist[3].w=1.0f;
list[4].w=1.0f; model->vertexlist[4].x=origin->x;
list[5].x=origin->x+size; model->vertexlist[4].y=origin->y;
list[5].y=origin->y; model->vertexlist[4].z=origin->z+size;
list[5].z=origin->z+size; model->vertexlist[4].w=1.0f;
list[5].w=1.0f; model->vertexlist[5].x=origin->x+size;
list[6].x=origin->x; model->vertexlist[5].y=origin->y;
list[6].y=origin->y+size; model->vertexlist[5].z=origin->z+size;
list[6].z=origin->z+size; model->vertexlist[5].w=1.0f;
list[6].w=1.0f; model->vertexlist[6].x=origin->x;
list[7].x=origin->x+size; model->vertexlist[6].y=origin->y+size;
list[7].y=origin->y+size; model->vertexlist[6].z=origin->z+size;
list[7].z=origin->z+size; model->vertexlist[6].w=1.0f;
list[7].w=1.0f; model->vertexlist[7].x=origin->x+size;
model->vertexlist[7].y=origin->y+size;
model->vertexlist[7].z=origin->z+size;
model->vertexlist[7].w=1.0f;
model->facelist=0x00310000;
model->facelist[0].V1=0;
model->facelist[0].V2=1;
model->facelist[0].V3=3;
model->facelist[1].V1=0;
model->facelist[1].V2=2;
model->facelist[1].V3=3;
model->facelist[2].V1=4;
model->facelist[2].V2=5;
model->facelist[2].V3=7;
model->facelist[3].V1=4;
model->facelist[3].V2=6;
model->facelist[3].V3=7;
model->facelist[4].V1=0;
model->facelist[4].V2=1;
model->facelist[4].V3=5;
model->facelist[5].V1=0;
model->facelist[5].V2=4;
model->facelist[5].V3=5;
model->facelist[6].V1=0;
model->facelist[6].V2=0;
model->facelist[6].V3=0;
model->facelist[7].V1=0;
model->facelist[7].V2=0;
model->facelist[7].V3=0;
model->facelist[8].V1=0;
model->facelist[8].V2=0;
model->facelist[8].V3=0;
model->facelist[9].V1=0;
model->facelist[9].V2=0;
model->facelist[9].V3=0;
model->facelist[10].V1=0;
model->facelist[10].V2=0;
model->facelist[10].V3=0;
model->facelist[11].V1=0;
model->facelist[11].V2=0;
model->facelist[11].V3=0;
}
/*******************************************************************************/
/* Affiche un modèle 3D */
void show3dmodel(model3d *model, matrix44 *transformation, vector4 origin[], float factor, type3D type)
{
u16 i;
vertex2d *plane=0x00250000;
for (i = 0; i < model->vertexnb; i++)
{
matrix44_transform(transformation, &model->vertexlist[i]);
}
proj(model->vertexlist, plane, origin, model->vertexnb, factor);
switch (type) {
case TYPE3D_POINTS:
for(i=0;i<model->vertexnb;i++) {
v_writepxl(&plane[i], egatorgb(4));
}
break;
case TYPE3D_LINES:
for(i=0;i<model->facenb;i++) {
v_line(&plane[model->facelist[i].V1], &plane[model->facelist[i].V2], egatorgb(4));
v_line(&plane[model->facelist[i].V1], &plane[model->facelist[i].V3], egatorgb(4));
v_line(&plane[model->facelist[i].V2], &plane[model->facelist[i].V3], egatorgb(4));
}
break;
case TYPE3D_FACES:
break;
case TYPE3D_FLAT:
break;
case TYPE3D_TEXTURE:
break;
}
} }
/*******************************************************************************/ /*******************************************************************************/
@ -147,11 +221,11 @@ int load3ds(u8 *pointer,u32 size, model3d *model)
i=0; i=0;
listunsigned=ptr; listunsigned=ptr;
model->facelist=0x00400000; model->facelist=0x00400000;
while(i<model->facenb*3) while(i<model->facenb)
{ {
model->facelist[i++]=*(listunsigned++); model->facelist[i].V1=*(listunsigned++);
model->facelist[i++]=*(listunsigned++); model->facelist[i].V2=*(listunsigned++);
model->facelist[i++]=*(listunsigned++); model->facelist[i++].V3=*(listunsigned++);
listunsigned++; listunsigned++;
} }
ptr=listunsigned; ptr=listunsigned;

View File

@ -16,6 +16,7 @@
#include "debug.h" #include "debug.h"
#include "VGA/ansi.c" #include "VGA/ansi.c"
#include "3D/sphere.c" #include "3D/sphere.c"
#include "3D/man.c"
static command commands[] = { static command commands[] = {
{"reboot" , "", &rebootnow}, {"reboot" , "", &rebootnow},
@ -429,35 +430,37 @@ int test3d()
return 1; return 1;
} }
model3d model; model3d model;
load3ds(&sphere, sizeof(sphere), &model); float factor=100.0f;
vector4 list3d[8]; type3D type=TYPE3D_POINTS;
vertex2d list2d[8]; matrix44 rotatex,rotatey,rotatez,mrotatex,mrotatey,mrotatez,identity;
matrix44 rotatex,rotatey,rotatez,mrotatex,mrotatey,mrotatez; matrix44_homogen(&identity);
matrix44* transformation;
matrix44_rotation_x(0.1f, &rotatex); matrix44_rotation_x(0.1f, &rotatex);
matrix44_rotation_y(0.1f, &rotatey); matrix44_rotation_y(0.1f, &rotatey);
matrix44_rotation_z(0.1f, &rotatez); matrix44_rotation_z(0.1f, &rotatez);
matrix44_rotation_x(-0.1f, &mrotatex); matrix44_rotation_x(-0.1f, &mrotatex);
matrix44_rotation_y(-0.1f, &mrotatey); matrix44_rotation_y(-0.1f, &mrotatey);
matrix44_rotation_z(-0.1f, &mrotatez); matrix44_rotation_z(-0.1f, &mrotatez);
matrix44* transformation=&identity;
vector4 origin={0.0f,0.0f,0.0f,0.0f}; vector4 origin={0.0f,0.0f,0.0f,0.0f};
vector4 cubeorigin={0.0f,0.0f,0.0f,0.0f}; vector4 cubeorigin={0.0f,0.0f,0.0f,0.0f};
origin.x=vinfo->currentwidth/2.0f; origin.x=vinfo->currentwidth/2.0f;
origin.y=vinfo->currentheight/2.0f; origin.y=vinfo->currentheight/2.0f;
origin.z=70.0f; origin.z=70.0f;
cube(&list3d, &cubeorigin, 35.0f); cube(&model, &cubeorigin, 35.0f);
u8 achar=' '; u8 achar=' ';
u8 i; u8 i;
while(achar!='a') while(achar!='q' && achar!='Q')
{ {
clearscreen(); clearscreen();
proj(&list3d, &list2d, &origin, 8, 100.0f); show3dmodel(&model, transformation, &origin, factor, type);
for (i = 0; i < 8; i++)
{
v_writepxl(&list2d[i], egatorgb(4));
}
achar=waitascii(); achar=waitascii();
switch(achar) { switch(achar) {
case '1':
load3ds(&man, sizeof(man), &model);
break;
case '2':
load3ds(&sphere, sizeof(sphere), &model);
break;
case 17: case 17:
transformation=&rotatex; transformation=&rotatex;
break; break;
@ -476,10 +479,22 @@ int test3d()
case 3: case 3:
transformation=&mrotatez; transformation=&mrotatez;
break; break;
case '-':
factor-=10.0;
break;
case '+':
factor+=10.0;
break;
case '*':
switch(type) {
case TYPE3D_POINTS:
type=TYPE3D_LINES;
break;
case TYPE3D_LINES:
type=TYPE3D_POINTS;
break;
} }
for (i = 0; i < 8; i++) break;
{
matrix44_transform(transformation, &list3d[i]);
} }
} }