Advertisement
Kitomas

kttm.h in a wip refactoring state

Apr 28th, 2023
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. #ifndef KIT_KTTM_H
  2. #define KIT_KTTM_H
  3.  
  4. #define KIT_TTM_NEAR 1.0f
  5. #define KIT_TTM_FAR 20.0f
  6.  
  7. #include <stdint.h>
  8.  
  9. #include <SDL2/SDL.h>
  10.  
  11. #include <KIT/kmath.h>
  12.  
  13.  
  14. /* TTL */
  15.  
  16. typedef struct {
  17.   char* name;       //name of the material
  18.   KIT_vec3 Ka;      //ambient color
  19.   KIT_vec3 Kd;      //diffuse color
  20.   KIT_vec3 Ks;      //specular color
  21.   float Ns;         //specular exponent
  22.   float d;          //alpha (opaqueness)
  23.   KIT_vec3 Tf;      //transmission filter color (RGB)
  24.    //CIEXYZ transmission filter color not supported
  25.    //transmission filter color from spectral curve file not supported
  26.   float Ni;         //optical density (index of refraction)
  27.   uint32_t ilm;     //illumination model
  28.   SDL_Texture* mKa; //ambient texture map
  29.   SDL_Texture* mKd; //diffuse texture map
  30.   SDL_Texture* mKs; //specular color texture map
  31.   SDL_Texture* mNs; //specular highlight component
  32.   SDL_Texture* md;  //alpha texture map
  33.   SDL_Texture* mbm; //bump map
  34.   SDL_Texture* dsp; //displacement map
  35.   SDL_Texture* dcl; //stencil decal texture
  36.    //texture map option parameters are not supported
  37.   KIT_vec3 Ke;      //emissive color (.mtl extension)
  38. } KIT_TTL_Material;
  39. typedef struct {
  40.   KIT_BTS* bts;
  41.   KIT_TTL_Material* mats;
  42.   uint32_t mats_len;
  43. } KIT_TTL;
  44.  
  45.  
  46. /* TTM */
  47.  
  48. #define KIT_TTM_PRINT_TRIANGLE(t) \
  49.   printf("[[(%.1f,%.1f,%.1f), (%.1f,%.1f,%.1f), (%.1f,%.1f), 0x%X],\n", \
  50.           t->a.pos.x, t->a.pos.y, t->a.pos.z, \
  51.           t->a.norm.x,t->a.norm.y,t->a.norm.z, \
  52.           t->a.uv.x,  t->a.uv.y,  t->a.clri); \
  53.   printf(" [(%.1f,%.1f,%.1f), (%.1f,%.1f,%.1f), (%.1f,%.1f), 0x%X],\n", \
  54.           t->b.pos.x, t->b.pos.y, t->b.pos.z, \
  55.           t->b.norm.x,t->b.norm.y,t->b.norm.z, \
  56.           t->b.uv.x,  t->b.uv.y,  t->b.clri); \
  57.   printf(" [(%.1f,%.1f,%.1f), (%.1f,%.1f,%.1f), (%.1f,%.1f), 0x%X]]\n", \
  58.           t->c.pos.x, t->c.pos.y, t->c.pos.z, \
  59.           t->c.norm.x,t->c.norm.y,t->c.norm.z, \
  60.           t->c.uv.x,  t->c.uv.y,  t->c.clri);
  61. typedef struct {
  62.   KIT_vec3 pos,norm; //xyz, normal vector
  63.   SDL_FPoint     uv; //uv texture coordinates
  64.   union {
  65.     SDL_Color   clr; //rgba value (Uint8 * 4)
  66.     uint32_t   clri;
  67.   };
  68. } KIT_TTM_Vertex;
  69. typedef struct {
  70.   KIT_TTM_Vertex a,b,c;
  71.   KIT_vec3 mid;    //triangle midpoint = (a+b+c)/3
  72.   KIT_vec3 norm;   //triangle normal vector
  73. } KIT_TTM_Triangle;
  74.  
  75. typedef struct {
  76.   char* name; //points to bts data; don't free() this
  77.   KIT_TTL_Material* umt;      //material used, also don't free() this
  78.   KIT_TTM_Triangle* original; //ttm's original state; copy to work for transform
  79.   KIT_TTM_Triangle* work;     //work buffer for faces; copy to render for rendering
  80.   KIT_TTM_Triangle* render;   //buffer for rendering
  81.   uint32_t numTriangles;
  82.   uint32_t numWorkTris; //<= numTriangles due to clipping/culling
  83.   int s; //smooth shading; boolean
  84. } KIT_TTM_Object;
  85. typedef struct {
  86.   KIT_BTS* bts; //contains the model's raw bts data
  87.   KIT_TTL**          ttls; uint32_t ttls_len; //material libraries
  88.   KIT_TTL_Material** mats; uint32_t mats_len; //points to materials actually used
  89.   KIT_TTM_Object* objects; uint32_t objects_len;
  90. } KIT_TTM;
  91.  
  92. typedef struct {
  93.   SDL_Texture* texture;
  94.   SDL_Renderer* renderer;
  95.   float* zBuffer;
  96.   float v_w,v_h; //viewport dimensions (not in pixels)
  97.   int w,h;
  98.   //uint32_t pixelFormat; (assumed ABGR32)
  99. } KIT_TTM_Canvas3D;
  100.  
  101.  
  102. /* TTL */
  103. extern KIT_TTL* KIT_TTL_LoadFile(const char* filePath, SDL_Renderer* r);
  104. extern void KIT_TTL_Destroy(KIT_TTL** ttl_p);
  105.  
  106. /* TTM */
  107. extern KIT_TTM* KIT_TTM_LoadFile(const char* filePath, SDL_Renderer* r);
  108. extern void KIT_TTM_Destroy(KIT_TTM** ttm_p);
  109. extern int KIT_TTM_Transform(KIT_TTM* ttm,  int* opt, KIT_vec3* ops, int opCount);
  110.  
  111. extern int KIT_TTM_ZCompareVert(const void* p1, const void* p2);
  112. extern int KIT_TTM_ZCompareTri(const void* p1, const void* p2);
  113.  
  114. extern KIT_TTM_Canvas3D* KIT_TTM_CreateCanvas3D(SDL_Renderer* renderer,
  115.                                                 int w,int h, float v_w,float v_h);
  116. extern void KIT_TTM_DestroyCanvas3D(KIT_TTM_Canvas3D** c_p);
  117.  
  118. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement