Subversion Repositories Games.Carmageddon

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "modsupt.h"
  2. #include "CORE/FW/resource.h"
  3. #include "CORE/MATH/matrix34.h"
  4. #include "CORE/V1DB/dbsetup.h"
  5. #include "harness/trace.h"
  6. #include <math.h>
  7.  
  8. // FIXME: move this definition (+BR_MAC3+BR_MAC4) to br_defs.h (and remove from matrix34.c matrix4.c ...)
  9. #define BR_MAC2(A, B, C, D) ((A) * (B) + (C) * (D))
  10.  
  11. // IDA: void __cdecl BrModelApplyMap(br_model *model, int map_type, br_matrix34 *xform)
  12. void BrModelApplyMap(br_model* model, int map_type, br_matrix34* xform) {
  13.     int v;
  14.     br_vertex* vp;
  15.     br_vector3 mv;
  16.     br_matrix34 default_xform;
  17.     br_scalar d;
  18.     LOG_TRACE("(%p, %d, %p)", model, map_type, xform);
  19.  
  20.     if (xform == NULL) {
  21.         BrMatrix34Identity(&default_xform);
  22.         xform = &default_xform;
  23.     }
  24.     for (v = 0; v < model->nvertices; v++, vp++) {
  25.         vp = &model->vertices[v];
  26.         BrMatrix34ApplyP(&mv, &vp->p, xform);
  27.         switch (map_type) {
  28.         case BR_APPLYMAP_PLANE:
  29.             vp->map.v[0] = (mv.v[0] + 1.f) * 0.5f;
  30.             vp->map.v[1] = (mv.v[1] + 1.f) * 0.5f;
  31.             break;
  32.         case BR_APPLYMAP_SPHERE:
  33.             vp->map.v[0] = BrAngleToDegrees(BR_ATAN2(-mv.v[2], mv.v[0])) / 360.f;
  34.             d = BR_MAC2(mv.v[0], mv.v[0], mv.v[2], mv.v[2]);
  35.             vp->map.v[1] = 1.f - BrAngleToDegrees(BR_ATAN2(d, mv.v[1])) / 180.f;
  36.             break;
  37.         case BR_APPLYMAP_CYLINDER:
  38.             vp->map.v[0] = BrAngleToDegrees(BR_ATAN2(-mv.v[2], mv.v[0])) / 360.f;
  39.             vp->map.v[1] = (mv.v[1] + 1.f) * 0.5f;
  40.             break;
  41.         case BR_APPLYMAP_DISC:
  42.             vp->map.v[0] = BrAngleToDegrees(BR_ATAN2(-mv.v[1], mv.v[0])) / 360.f;
  43.             vp->map.v[1] = sqrtf(BR_MAC2(mv.v[0], mv.v[0], mv.v[1], mv.v[1]));
  44.             break;
  45.         case BR_APPLYMAP_NONE:
  46.             vp->map.v[0] = 0.f;
  47.             vp->map.v[1] = 0.f;
  48.             break;
  49.         }
  50.     }
  51. }
  52.  
  53. // IDA: br_matrix34* __cdecl BrModelFitMap(br_model *model, int axis_0, int axis_1, br_matrix34 *transform)
  54. br_matrix34* BrModelFitMap(br_model* model, int axis_0, int axis_1, br_matrix34* transform) {
  55.     br_vector3 axis_3;
  56.     br_vector3 tr;
  57.     br_vector3 sc;
  58.     int i;
  59.     static br_vector3 axis_vectors[6] = {
  60.         { {  1.f,  0.f,  0.f } },
  61.         { {  0.f,  1.f,  0.f } },
  62.         { {  0.f,  0.f,  1.f } },
  63.         { { -1.f,  0.f,  0.f } },
  64.         { {  0.f, -1.f,  0.f } },
  65.         { {  0.f,  0.f, -1.f } },
  66.     };
  67.     LOG_TRACE("(%p, %d, %d, %p)", model, axis_0, axis_1, transform);
  68.  
  69.     BrMatrix34Identity(transform);
  70.     axis_3.v[0] = axis_vectors[axis_1].v[2] * axis_vectors[axis_0].v[1] - axis_vectors[axis_1].v[1] * axis_vectors[axis_0].v[2];
  71.     axis_3.v[1] = axis_vectors[axis_1].v[0] * axis_vectors[axis_0].v[2] - axis_vectors[axis_1].v[2] * axis_vectors[axis_0].v[0];
  72.     axis_3.v[2] = axis_vectors[axis_1].v[1] * axis_vectors[axis_0].v[0] - axis_vectors[axis_1].v[0] * axis_vectors[axis_0].v[1];
  73.     for (i = 0; i < 3; i++) {
  74.         transform->m[i][0] = axis_vectors[axis_0].v[i];
  75.         transform->m[i][1] = axis_vectors[axis_1].v[i];
  76.         transform->m[i][2] = axis_3.v[i];
  77.     }
  78.     for (i = 0; i < 3; i++) {
  79.         tr.v[i] = -(model->bounds.max.v[i] * .5f + model->bounds.min.v[i] * .5f);
  80.     }
  81.     for (i = 0; i < 3; i++) {
  82.         if (model->bounds.max.v[i] == model->bounds.min.v[i]) {
  83.             sc.v[i] = 1.f;
  84.         } else {
  85.             sc.v[i] = 1.f / (model->bounds.max.v[i] * .5f - model->bounds.min.v[i] * .5f);
  86.         }
  87.     }
  88.  
  89.     BrMatrix34PreScale(transform, sc.v[0], sc.v[1], sc.v[2]);
  90.     BrMatrix34PreTranslate(transform, tr.v[0], tr.v[1], tr.v[2]);
  91.     return transform;
  92. }
  93.  
  94. // IDA: void __cdecl BrModelFree(br_model *m)
  95. void BrModelFree(br_model* m) {
  96.     BrResFree(m);
  97. }
  98.  
  99. // IDA: br_model* __cdecl BrModelAllocate(char *name, int nvertices, int nfaces)
  100. br_model* BrModelAllocate(char* name, int nvertices, int nfaces) {
  101.     br_model* m;
  102.  
  103.     m = BrResAllocate(v1db.res, sizeof(br_model), BR_MEMORY_MODEL);
  104.     m->nfaces = nfaces;
  105.     m->nvertices = nvertices;
  106.     if (name != NULL) {
  107.         m->identifier = BrResStrDup(m, name);
  108.     }
  109.     if (nvertices != 0) {
  110.         m->vertices = BrResAllocate(m, sizeof(br_vertex) * nvertices, BR_MEMORY_VERTICES);
  111.     }
  112.     if (nfaces != 0) {
  113.         m->faces = BrResAllocate(m, sizeof(br_face) * nfaces, BR_MEMORY_FACES);
  114.     }
  115.     return m;
  116. }
  117.  
  118. // IDA: br_primitive_list* __cdecl BrPrimitiveListAllocate(br_uint_32 prim_type, br_uint_16 num_prims)
  119. br_primitive_list* BrPrimitiveListAllocate(br_uint_32 prim_type, br_uint_16 num_prims) {
  120.     LOG_TRACE("(%d, %d)", prim_type, num_prims);
  121.  
  122.     return NULL;
  123. }
  124.  
  125. // IDA: br_uint_32 __cdecl BrModelAddPrimitiveList(br_model *model, br_primitive_list *primitive_list)
  126. br_uint_32 BrModelAddPrimitiveList(br_model* model, br_primitive_list* primitive_list) {
  127.     LOG_TRACE("(%p, %p)", model, primitive_list);
  128.  
  129.     return 0; // Pierre-Marie Baty -- fixed return value
  130. }
  131.