polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

test_dae.c (3272B)


      1 
      2 #include "animation.h"
      3 #include "util.h"
      4 #include "ply.h"
      5 #include "mdl.h"
      6 #include "vec3.h"
      7 #include "debug.h"
      8 #include <assert.h>
      9 #include <stdio.h>
     10 
     11 #include "dae.h"
     12 
     13 static void test_load_pose()
     14 {
     15     printf("test_load_pose\n");
     16 
     17     struct model model;
     18     struct make_geometry mkgeom;
     19     init_model(&model);
     20     init_make_geometry(&mkgeom);
     21 
     22     load_dae("data/models/pirate_officer.dae", &model, &mkgeom);
     23 
     24     assert(mkgeom.indices);
     25 
     26     assert(model.nposes == 1);
     27     struct pose *pose = &model.poses[0];
     28 
     29     debug("pose->nweights %d\n", pose->nweights);
     30     assert(pose->nweights == 389);
     31     assert(approxeq(pose->weights[0], 0.05213558));
     32     debug("pose last weight %f\n", pose->weights[388]);
     33     assert(approxeq(pose->weights[388], 0.01394611));
     34 
     35     assert(pose->njoints == 11);
     36 
     37     struct joint *joint = &pose->joints[0];
     38     struct node *node = get_node(&joint->node_id);
     39     assert(node);
     40     assert(approxeq(node->mat[0], 0.999897));
     41 
     42     joint = &pose->joints[1];
     43     node = get_node(&joint->node_id);
     44     assert(node);
     45     assert(approxeq(node->mat[1], -0.01434187));
     46 
     47     assert(pose->joints[0].n_children_ids == 3);
     48 
     49     free_make_geometry(&mkgeom);
     50 }
     51 
     52 static void test_save_mdl()
     53 {
     54     printf("test_save_mdl\n");
     55     struct model model, model2;
     56     struct mdl_geometry mg, mg2;
     57 
     58     init_model(&model);
     59     init_mdl_geometry(&mg);
     60     init_model(&model2);
     61     init_mdl_geometry(&mg2);
     62 
     63     struct make_geometry *mk1 = &mg.mkgeom;
     64     struct make_geometry *mk2 = &mg.mkgeom;
     65 
     66     // TODO: test more models
     67     load_dae("data/models/pirate_officer.dae", &model, mk1);
     68 
     69     const char *tmpfile = "/tmp/test.mdl";
     70     save_mdl(tmpfile, &mg);
     71     load_mdl(tmpfile, &model2, &mg2);
     72 
     73     debug("poses %d %d\n", model.nposes, model2.nposes);
     74 
     75     // TODO: mdl poses
     76     /* assert(model.nposes == model2.nposes); */
     77 
     78     assert(vec3_approxeq(mg.min, mg2.min));
     79     assert(vec3_approxeq(mg.max, mg2.max));
     80     assert(mk1->num_verts == mk2->num_verts);
     81     assert(mk1->num_indices == mk2->num_indices);
     82     assert(mk1->num_uv_components == mk2->num_uv_components);
     83     assert(mk1->num_joint_ids == mk2->num_joint_ids);
     84 
     85     assert(memeq(mk1->vertices, mk2->vertices,
     86                  mk1->num_verts * 3, mk2->num_verts * 3));
     87 
     88     assert(memeq(mk1->colors, mk2->colors,
     89                  mk1->num_verts * 3, mk2->num_verts * 3));
     90 
     91     assert(memeq(mk1->normals, mk2->normals,
     92                  mk1->num_verts * 3, mk2->num_verts * 3));
     93 
     94     assert(memeq(mk1->indices, mk2->indices,
     95                  mk1->num_indices, mk2->num_indices));
     96 
     97     assert(memeq(mk1->tex_coords, mk2->tex_coords,
     98                  mk1->num_verts * mk2->num_uv_components,
     99                  mk2->num_verts * mk2->num_uv_components));
    100 
    101     assert(memeq(mk1->joint_ids, mk2->joint_ids,
    102                  mk1->num_joint_ids, mk2->num_joint_ids));
    103 
    104     assert(memeq(mk1->joint_weights, mk2->joint_weights,
    105                  mk1->num_verts * 3, mk2->num_verts * 3));
    106 }
    107 
    108 int main(int argc, char *argv[])
    109 {
    110     struct pose *pose;
    111     struct joint *joint;
    112     struct node *node;
    113     struct model model;
    114     struct make_geometry geom;
    115     int nposes;
    116 
    117     init_node_manager();
    118     init_model(&model);
    119     init_make_geometry(&geom);
    120 
    121     test_load_pose();
    122     test_save_mdl();
    123 
    124     return 0;
    125 }