Advertisement
Caiwan

sm3ds.cpp

Sep 21st, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.76 KB | None | 0 0
  1. /*  ///////////////////////////////////
  2. SAD MAN'S 3DS LOADER             v0.07
  3. LGPL v2    by Caiwan, KitKett and Geri
  4. //////////////////////////////////////
  5.  
  6. // This loader IS TESTED and IT IS WORKING.
  7. // Supports up to 256 mesh in a model
  8. // Supports texture file names (if a mesh has no textures name, previous will be cloned to it)
  9. // Normals are currently ignored and calculated automatically by the loader
  10. // SAD MAN'S 3DS LOADER is written in C++, but interfaces are in C language
  11.  
  12. // THE PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT
  13. // ANY WARRANTY. IT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  14. // EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
  16. // TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM
  17. // PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR
  18. // OR CORRECTION.
  19.  
  20. // LICENSE: LGPL v2
  21.  
  22. // Tested compilers: GCC and MSVC
  23.  
  24. ///////////
  25. usage:
  26. ///////////
  27.    
  28. if(!sm3DSLoad(modellfile)){ // loads FILE * modellfile to the memory. WARNING: use "rb" in fopen instead of "r"!!!
  29.     fclose(modellfile);  // we faild for some reason that sould not happend.
  30.     return;
  31. }
  32. int meshek=sm3DSget_number_of_mesh(); // tells the number of the meshes
  33. if(meshek>254) meshek=254;
  34. for(int i=0;i<meshek;i++){
  35.     char texturaneve[14];
  36.     int vtex=sm3DSget_texture_name(i, texturaneve); // store textures name to texturaneve, and returns 1 if texture name is exist. if no texture, returns 0
  37.     float * v=sm3DSget_vtx(i); // returns pointer for geometry
  38.     float * t=sm3DSget_tex(i); // returns pointer for uv
  39.     float * n=sm3DSget_nrm(i); // returns pointer for normals
  40.     int f=sm3DSget_nmbr_faces(i); // returns the number of triangles
  41.     if(vtex){
  42.         YOUR_GAME_ENGINE_INPUT(modellszam, i, v, t, n, f, texturaneve);
  43.     }else{
  44.         YOUR_GAME_ENGINE_INPUT(modellszam, i, v, t, n, f, NULL);
  45.     }
  46. }
  47. sm3DS_die(); // freeing up everything
  48. fclose(modellfile);
  49. */
  50.  
  51.  
  52. // #define DEBUG_3DS_LOADER
  53.  
  54.  
  55. int weld3dsfiled=0;
  56. int m3dsfile_maxdata=0;
  57. int number_of_mesh_3ds=0;
  58. int osszes_polygon_3ds[512];
  59. float* polygon_3ds[512];
  60. float* uv_3ds[512];
  61. float* normal_3ds[512];
  62. char texneve[512][14];
  63. int ukchkFAIL=0;
  64. #include <stdio.h>
  65. #include <string.h>
  66. #include <stdlib.h>
  67. #include <vector>
  68. #include <cstring>
  69. #include <cstdio>
  70. #include <iostream>
  71. #include <fstream>
  72. #include <cmath>
  73. #ifndef __CAI_VECTORS_
  74. #define __CAI_VECTORS_
  75. #define VECTOR_ERR_DIV_BY_ZERO 200;
  76.  
  77. #ifdef DEBUG_3DS_LOADER
  78.     #define _MK_DUMP
  79. #endif
  80.  
  81. template <class T>
  82. class vector3d3dsloaderbb{
  83.     private:
  84.         // ..
  85.     public:
  86.         T x,y,z;
  87.         explicit vector3d3dsloaderbb();             // set zero constructor
  88.         vector3d3dsloaderbb(T, T, T);                   // set default value constructor
  89.         vector3d3dsloaderbb(const vector3d3dsloaderbb&);
  90.         vector3d3dsloaderbb &operator = (vector3d3dsloaderbb);  // overload operators
  91.         vector3d3dsloaderbb operator+ (const vector3d3dsloaderbb &);
  92.         vector3d3dsloaderbb operator- (const vector3d3dsloaderbb &);
  93.         vector3d3dsloaderbb operator* (T);          // :?
  94.         vector3d3dsloaderbb operator/ (T);
  95.         vector3d3dsloaderbb operator+= (T);
  96.         vector3d3dsloaderbb operator-= (T);
  97.         vector3d3dsloaderbb operator*= (T);
  98.         vector3d3dsloaderbb operator/= (T);
  99.         // extra vector fn.s
  100.         T vlen ();                          // vector length using pythagorean theorem
  101.         void setZero();                     // set
  102. };
  103.  
  104. // ------------------------------------------------
  105. template <class T> vector3d3dsloaderbb<T>::vector3d3dsloaderbb(void){
  106.     x = 0;
  107.     y = 0;
  108.     z = 0;
  109. }
  110. template <class T> vector3d3dsloaderbb<T>::vector3d3dsloaderbb(T a, T b, T c){
  111.     x = a;
  112.     y = b;
  113.     z = c;
  114. }
  115. template <class T> vector3d3dsloaderbb<T>::vector3d3dsloaderbb(const vector3d3dsloaderbb<T> &v){
  116.     x = v.x;
  117.     y = v.y;
  118.     z = v.z;   
  119. }
  120. template <class T> vector3d3dsloaderbb<T>& vector3d3dsloaderbb<T>::operator = (vector3d3dsloaderbb<T> v){
  121.     x = v.x;
  122.     y = v.y;
  123.     z = v.z;
  124.     return *this;
  125. }
  126. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator + (const vector3d3dsloaderbb & v){
  127.     vector3d3dsloaderbb<T> tmp (*this);
  128.     tmp.x += v.x;
  129.     tmp.y += v.y;
  130.     tmp.z += v.z;
  131.     return tmp;
  132. }
  133. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator - (const vector3d3dsloaderbb & v){
  134.     vector3d3dsloaderbb<T> tmp (*this);
  135.     tmp.x -= v.x;
  136.     tmp.y -= v.y;
  137.     tmp.z -= v.z;
  138.     return tmp;
  139. }
  140. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator+= (T a){
  141.     x += a;
  142.     y += a;
  143.     z += a;
  144.     return *this;
  145. }
  146. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator-= (T a){
  147.     x -= a;
  148.     y -= a;
  149.     z -= a;
  150.     return *this;
  151. }
  152. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator* (T a){
  153.     x *= a;
  154.     y *= a;
  155.     z *= a;
  156.     return *this;
  157. }
  158. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator/ (T a){
  159.     if (a == 0) a=1;
  160.     x /= a;
  161.     y /= a;
  162.     z /= a;
  163.     return *this;
  164. }
  165. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator*= (T a){
  166.     x *= a;
  167.     y *= a;
  168.     z *= a;
  169.     return *this;
  170. }
  171. template <class T> vector3d3dsloaderbb<T> vector3d3dsloaderbb<T>::operator/= (T a){
  172.     if (a == 0) a=1;
  173.     x /= a;
  174.     y /= a;
  175.     z /= a;
  176.     return *this;
  177. }
  178. template <class T> T vector3d3dsloaderbb<T>::vlen (){
  179.     return sqrt((x * x) + (y * y) + (z * z));   // pythagorean theorem
  180. }
  181. template <class T> void vector3d3dsloaderbb<T>::setZero (){
  182.     x = y = z = 0;
  183. }
  184. #endif
  185. #ifndef __CAI_MODELMGR_
  186. #define __CAI_MODELMGR_
  187. #define FW_CALCEDNORMALS 0x01
  188. namespace FWtexgen{         // this stucture is mainly defined in FWtexgen namespace in textureGenerator.h
  189.     typedef struct {        // i just copied here
  190.         float r,g,b,a;
  191.     } rgbaCol_t;       
  192. }
  193. namespace FWmodels {                            // everything is joined in one namespace
  194.     typedef vector3d3dsloaderbb<float> verticies_t;
  195.     typedef vector3d3dsloaderbb<float> normals_t;
  196.     typedef vector3d3dsloaderbb<unsigned short> meshes_t;
  197.     typedef struct {
  198.         float u,v;
  199.     } texUV_t;
  200.     typedef struct{
  201.         vector3d3dsloaderbb<float> x1, x2, x3, o;
  202.     } localCoord_t;
  203.     typedef struct{
  204.         std::string name;
  205.         std::vector<unsigned short> face;
  206.     } materialNfo_t;
  207.     // class to sore modles (can store only one)
  208.     class model{
  209.         private:   
  210.             int err;
  211.             short isNormalsCalced;  // bitfields?
  212.  
  213.         public:
  214.             std::vector<verticies_t>            vertList;   // mesh data
  215.             std::vector<meshes_t>               meshList;
  216.             std::vector<normals_t>              normList;
  217.             std::vector<FWmodels::texUV_t>      uvList;
  218.            
  219.             localCoord_t localCoord;                        // local coords ?
  220.  
  221.             // material data?
  222.             std::vector<materialNfo_t>          matList;
  223.  
  224.             model();
  225.             ~model();
  226.             //virtual void loadRawFile(char*);
  227.             void calcNormals();
  228.             void drawPlainModel();      // draw the raw model
  229.             int getError() const {return this->err; };  // is error fv.
  230.     };
  231.     class material{ // maybe a struct? :/
  232.         protected:
  233.             //float * ambP, diffP, specP;   //pointers for the float arrays
  234.         public:
  235.             std::string name;
  236.             char nametex[16];
  237.             FWtexgen::rgbaCol_t ambient, diffusal, specular;
  238.            
  239.             material();
  240.  
  241.             void bindMaterial(bool);
  242.  
  243.             /*float* getAmbientP() const;
  244.             float* getSpecularP() const;
  245.             float* getDiffusalP() const;*/
  246.     };
  247.     extern material* prev_material;     //previously (maybe the actual) binded class pointer
  248. };
  249. #endif
  250. #ifndef _C_3DSMAX_FORMAT_LOADER_
  251. #define _C_3DSMAX_FORMAT_LOADER_
  252.  
  253. #ifdef _DEBUG                   // allow dump
  254. //#define _MK_DUMP
  255. #endif
  256. #ifdef _MK_DUMP
  257. #define ASSERT(x) x;    // automatic asseriton
  258. #else
  259. #define ASSERT(x)
  260. #endif
  261. /*
  262.         some usage nfo will written here.
  263. */
  264.  
  265. // The chunk's id numbers
  266. #define M3DS_MAIN3DS                    0x4D4D
  267. #define M3DS_MAIN_VERS                  0x0002
  268. #define M3DS_EDIT3DS                    0x3D3D
  269. #define M3DS_MESH_VERS                  0x3D3E
  270. #define M3DS_OBJECT                             0x4000
  271. #define M3DS_TRIG_MESH                  0x4100
  272. #define M3DS_VERT_LIST                  0x4110
  273. #define M3DS_FACE_DESC                  0x4120
  274. #define M3DS_FACE_MAT                   0x4130
  275. #define M3DS_TEX_VERTS                  0x4140
  276. #define M3DS_SMOOTH_GROUP               0x4150
  277. #define M3DS_LOCAL_COORDS               0x4160
  278. #define M3DS_MATERIAL                   0xAFFF
  279. #define M3DS_MAT_NAME                   0xA000
  280. #define M3DS_MAT_AMBIENT                0xA010
  281. #define M3DS_MAT_DIFFUSE                0xA020
  282. #define M3DS_MAT_SPECULAR               0xA030
  283. #define M3DS_SHINY_PERC                 0xA040
  284. #define M3DS_SHINY_STR_PERC             0xA041
  285. #define M3DS_TRANS_PERC                 0xA050
  286. #define M3DS_TRANS_FOFF_PERC    0xA052
  287. #define M3DS_REF_BLUR_PERC              0xA053
  288. #define M3DS_RENDER_TYPE                0xA100
  289. #define M3DS_SELF_ILLUM                 0xA084
  290. #define M3DS_MAT_SELF_ILPCT             0xA08A
  291. #define M3DS_WIRE_THICKNESS             0xA087
  292. #define M3DS_MAT_TEXMAP                 0xA200
  293. #define M3DS_MAT_MAPNAME                0xA300
  294. #define M3DS_ONE_UNIT                   0x0100
  295.  
  296. // we don't need:
  297. #define M3DS_KEYF3DS                    0xB000
  298. #define M3DS_FRAMES                             0xB008
  299. #define M3DS_MESH_INFO                  0xB002
  300. #define M3DS_HIER_POS                   0xB030
  301. #define M3DS_HIER_FATHER                0xB010
  302. #define M3DS_PIVOT_PT                   0xB013
  303. #define M3DS_TRACK00                    0xB020
  304. #define M3DS_TRACK01                    0xB021
  305. #define M3DS_TRACK02                    0xB022
  306. #define M3DS_COLOR_RGB                  0x0010
  307. #define M3DS_COLOR_TRU                  0x0011
  308. #define M3DS_COLOR_TRUG                 0x0012
  309. #define M3DS_COLOR_RGBG                 0x0013
  310. #define M3DS_PERC_INT                   0x0030
  311. #define M3DS_PERC_FLOAT                 0x0031
  312.  
  313. namespace FWmodels {
  314.         class model3ds{
  315.                 private:
  316.  
  317. #ifdef _MK_DUMP
  318.                         std::fstream log;
  319. #endif
  320.                         typedef struct  {
  321.                                 unsigned short id;
  322.                                 unsigned long  len;
  323.                         } chunkInf;
  324.  
  325.                         unsigned char *data;                                                    // file data
  326.                         unsigned long fSize;
  327.                         unsigned short lVertNum;                                                // local sor for the number vert. and mesh;
  328.                         unsigned short lMeshNum;                                                // the vector template also can store everything
  329.                        
  330.                         int error;                                                                              // errcode; value -> pos. of the error; 0 is OK
  331.                         bool isResource;
  332.                         // chunk processors
  333.                         void procObject(unsigned int, model &);
  334.                         void procMaterial(unsigned int, material &);
  335.                        
  336.                         // chink info grabbers
  337.                         chunkInf getChunkInfo(const unsigned int);
  338.                         void get3fVector(const unsigned int, float*, float*, float*);
  339.                         void get2sVector(const unsigned int, short*, short*);
  340.  
  341.                         void pharseData();
  342.                
  343.                 public:
  344.                         model3ds(const char*);
  345.                         model3ds(const unsigned char*, unsigned long);
  346.                         ~model3ds();
  347.                        
  348.                         void drawModels();
  349.                        
  350.                         std::vector<FWmodels::model> objects;
  351.                         std::vector<FWmodels::material> materials;
  352.  
  353.                         int getError() const {return this-> error;};
  354.         };
  355. };
  356. #endif
  357. #ifdef _DEBUG           // allow dump
  358. #endif
  359.  
  360. using namespace std;
  361. using namespace FWmodels; // framework models namespace
  362.  
  363.  
  364. model3ds::model3ds(const char *input){
  365. /*  this->error =0; this->isResource = false;
  366.  
  367.     //if (!fIsExist(input)) {this->error = 1; return; }
  368.    
  369.     FILE * fp = fopen (input, "rb");
  370.    
  371.     if (fp == NULL) {this->error = 1; return; }
  372.    
  373.     fseek(fp, 0, SEEK_END);
  374.     this->fSize = ftell(fp);
  375.     fseek(fp, 0, SEEK_SET);
  376.  
  377.     data = new unsigned char [fSize];
  378.     fread(data, fSize, 1, fp);
  379.  
  380.     fclose(fp);
  381.  
  382.     this->pharseData();*/
  383. }
  384.  
  385. model3ds::model3ds(const unsigned char *input, unsigned long len){
  386.     this->isResource = true;
  387.     data=const_cast<unsigned char*>(input);
  388.     this->fSize = len;
  389.     this->pharseData();
  390. }
  391.  
  392. model3ds::~model3ds(){
  393. #ifdef _MK_DUMP
  394.     log << "end debug file.";
  395.     log.close();
  396. #endif
  397. if ((data) && (!this->isResource)) delete [] data;
  398. }
  399. // TODO: make allow to read it form PK file
  400.  
  401. void model3ds::pharseData(){
  402.  
  403. #ifdef _MK_DUMP
  404.     log.open("log.txt", std::ios::out);
  405.     log << "3DS loader runtime dump file." << endl;
  406.     log << ">USE IN DEBUG MODE ONLY !!! <" << endl;
  407.     log << "-----------------------------" << endl;
  408. #endif
  409.  
  410.     chunkInf mainCh = this->getChunkInfo(0);
  411.     if ((this->fSize != mainCh.len) & (mainCh.id != M3DS_MAIN3DS)) {this->error = 2; return;}
  412.  
  413.     unsigned int reloff=0 , offset = 6;
  414.     //this->objects = new vector<model>;
  415.     //this->materials = new vector<material>;
  416.  
  417.     #ifdef DEBUG_3DS_LOADER
  418.         ASSERT(char asda [1024]);
  419.     #endif
  420.  
  421.     while (offset < this->fSize) {
  422.         chunkInf chunk = this->getChunkInfo(offset);
  423.        
  424.         #ifdef DEBUG_3DS_LOADER
  425.             ASSERT(sprintf (asda, "@%06x +%06x $%04x ", offset, chunk.len, chunk.id));
  426.         #endif
  427.         switch(chunk.id){
  428.             case M3DS_EDIT3DS:
  429.                 offset += 6;
  430.                 #ifdef DEBUG_3DS_LOADER
  431.                     ASSERT(log << asda <<" M3DS_EDIT3DS chunk" << endl);
  432.                 #endif
  433.                 break;
  434.  
  435.             case M3DS_OBJECT:
  436.                 {
  437.                     #ifdef DEBUG_3DS_LOADER
  438.                         ASSERT(log << asda << " M3DS_OBJECT Triangle object chunk " << endl);
  439.                     #endif
  440.                     reloff = 6;
  441.                         // get pass the header
  442.                     while (data[offset+reloff] != '\0')
  443.                         reloff++;                                   // here is the name of the mesh, which we skip
  444.                     reloff++;
  445.                    
  446.                     // create a new model object
  447.                     model currentMod;
  448.                    
  449.                     // load the data into the object
  450.                     procObject(offset+reloff, currentMod);          // offset @ current chunk
  451.                     currentMod.calcNormals();                       // TODO: add normal loader/ skip this if there's no normals
  452.                     // add this object into the model vector//
  453.                     this->objects.push_back(currentMod);
  454.  
  455.                     //printf("object %d\n", this->objects->size());
  456.                     offset += chunk.len;
  457.                 }
  458.                 break;
  459.  
  460.             case M3DS_MATERIAL:
  461.                 {  
  462.                     #ifdef DEBUG_3DS_LOADER
  463.                         ASSERT(log << asda << " M3DS_MATERIAL Material chunk" << endl);
  464.                     #endif
  465.                    
  466.                     material currentMat;
  467.                     this -> procMaterial(offset, currentMat);
  468.                     this->materials.push_back(currentMat);
  469.                     offset += chunk.len;
  470.                 }
  471.                 break;
  472.  
  473.             default:
  474.                 #ifdef DEBUG_3DS_LOADER
  475.                     ASSERT(log << asda << " UNKNOWN chunk " << endl);
  476.                 #endif
  477.                 offset += chunk.len;
  478.                 ukchkFAIL++;
  479.                 if(ukchkFAIL>1024) return; // fail
  480.                 break;
  481.         }
  482.     }
  483.     #ifdef DEBUG_3DS_LOADER
  484.         ASSERT(printf("3DS Objects loaded: %d\n", this->objects.size()));
  485.     #endif
  486. }
  487.  
  488. char materialok_nevei[256][16];
  489. char materialok_nevei_tex[256][16];
  490. int ujmat=0;
  491. void model3ds::procMaterial(unsigned int offs, material &curr){
  492.     ujmat++;
  493.     unsigned int offset = offs, reloff = 6;
  494.     chunkInf matchunk = getChunkInfo(offset);
  495.  
  496.  
  497.     while (data[offset+reloff] != '\0'){
  498.         curr.name += (data[offset+reloff]);
  499.         reloff++;                                           // here is the name of the mesh, what we skip
  500.     }
  501.  
  502.  
  503.     for(int i=offset+reloff;i<m3dsfile_maxdata-3;i++){
  504.         if(data[i]=='.'){
  505.             if( (tolower(data[i+1])=='j' && tolower(data[i+2])=='p' && tolower(data[i+3])=='g') || (tolower(data[i+1])=='b' && tolower(data[i+2])=='m' && tolower(data[i+3])=='p') || (tolower(data[i+1])=='t' && tolower(data[i+2])=='g' && tolower(data[i+3])=='a')  || (tolower(data[i+1])=='p' && tolower(data[i+2])=='n' && tolower(data[i+3])=='g') ){
  506.                 // ok megvan a neve a kis gecinek, akkor most az elejére mászunk
  507.                 for(int j=i;j>0;j--){
  508.                     if(data[j]=='\0'){
  509.                         j++;
  510.                         snprintf(curr.nametex, 13, "%s", &data[j]);
  511.                         snprintf(materialok_nevei_tex[ujmat-1], 13, "%s", &data[j]);
  512.                         i=99999999999ll;
  513.                         break;
  514.                     }
  515.                 }
  516.             }
  517.         }
  518.     }
  519.  
  520.    
  521.     unsigned short val; //bit int
  522.     while(reloff<=matchunk.len){
  523.         chunkInf chunk = getChunkInfo(offs+reloff);
  524.         //material_t mater; memset(&mater, NULL, sizeof(material_t));
  525.         switch(chunk.id){
  526.         case M3DS_MAT_NAME:
  527.             {
  528.                 reloff +=6;
  529.                 int kezd=offset+reloff;
  530.                 while (data[offset+reloff] != '\0'){
  531.                     curr.name += (data[offset+reloff]);
  532.                     reloff++;                                           // here is the name of the mesh, what we skip
  533.                 }
  534.                 snprintf(materialok_nevei[ujmat-1], 13, "%s", &data[kezd]);
  535.                 reloff++;
  536.             }
  537.             break;
  538.  
  539.             case M3DS_MAT_AMBIENT:
  540.             case M3DS_MAT_DIFFUSE:
  541.             case M3DS_MAT_SPECULAR:
  542.                 {
  543.                     FWtexgen::rgbaCol_t temp_col;
  544.                     reloff += chunk.len-3;
  545.                     temp_col.r = (float)((int)data[offset+reloff  ])/255;
  546.                     temp_col.g = (float)((int)data[offset+reloff+1])/255;
  547.                     temp_col.b = (float)((int)data[offset+reloff+2])/255;
  548.                     temp_col.a=1;
  549.                     reloff+=3;
  550.  
  551.                     switch(chunk.id){
  552.                         case M3DS_MAT_AMBIENT:  memcpy(&curr.ambient,  &temp_col, sizeof(temp_col)); break;
  553.                         case M3DS_MAT_DIFFUSE:  memcpy(&curr.diffusal, &temp_col, sizeof(temp_col)); break;
  554.                         case M3DS_MAT_SPECULAR: memcpy(&curr.specular, &temp_col, sizeof(temp_col)); break; //lalala :)
  555.                     }
  556.                 }
  557.                 break;
  558.             default: reloff+=chunk.len; break;
  559.         }
  560.     }
  561. }
  562.  
  563. void model3ds::procObject (const unsigned int offs, model & mobj){
  564.     unsigned int offset = offs+6;               // skip the curren ch header
  565.     chunkInf chunk, objCh = getChunkInfo(offs); // get info and retun when fail (TODO)
  566.  
  567.     #ifdef DEBUG_3DS_LOADER
  568.         ASSERT(log << "  PROC TRIANGLE MESH:");
  569.     #endif
  570.     char asda[1024];
  571.  
  572.     this->error = 0;
  573.     if (objCh.id != M3DS_TRIG_MESH){    // wrong offset position, do nothing
  574.         this->error = 1;
  575.         return;
  576.     }
  577.                                    
  578.     #ifdef DEBUG_3DS_LOADER
  579.         ASSERT(log <<" ok"<< endl);
  580.     #endif
  581.  
  582.     short sVal;                             // allocate a sotage for various values
  583.     unsigned int uiVal, reloff = 0;         // and relative offset
  584.  
  585.     while (reloff < objCh.len){
  586.         chunk = getChunkInfo(offset+reloff);
  587.         #ifdef DEBUG_3DS_LOADER
  588.             ASSERT(sprintf (asda, "@%06x +%06x $%04x ", offset+reloff, chunk.len, chunk.id));
  589.         #endif
  590.        
  591.         switch (chunk.id){
  592.             case M3DS_VERT_LIST:{                                           // ------ PROCESS VERT LIST ------
  593.                 #ifdef DEBUG_3DS_LOADER
  594.                     ASSERT(log << asda << " Triangle mesh data");
  595.                 #endif             
  596.                 verticies_t tmpVert;
  597.  
  598.                 memcpy(&sVal, &data[offset+reloff+6], 2);                   // get mesh no.
  599.                 reloff += 8;                                                // move past reloff 6+2 bytes
  600.                
  601.                 #ifdef DEBUG_3DS_LOADER
  602.                     ASSERT(log << " - Coords no: " << sVal << endl);
  603.                 #endif
  604.                
  605.                 this->lVertNum = sVal;
  606.                 for (int i=0; i<this->lVertNum; i++){                       // sVal is the current num of meshes
  607.                     //memcpy(&this->tmpVert->x, &data[offset+reloff],     4);   // memory -> vector(xyz)
  608.                     //memcpy(&this->tmpVert->y, &data[offset+reloff+4], 4);
  609.                     //memcpy(&this->tmpVert->z, &data[offset+reloff+8], 4);
  610.                     get3fVector(offset+reloff, &tmpVert.x, &tmpVert.y, &tmpVert.z);
  611.                     reloff += 12;                                           // 3*(float) = 12 bytes
  612.  
  613.                     mobj.vertList.push_back(tmpVert);
  614.  
  615.                     //ASSERT(log << "x="<< tmpVert.x << "; y=" << tmpVert.y << "; z=" << tmpVert.z << ";" << endl);
  616.                 }
  617.             } break;
  618.  
  619.             case M3DS_FACE_DESC:{                                           // ------ PROCESS FACE LIST ------
  620.                 meshes_t tmpMesh;
  621.                
  622.                 #ifdef DEBUG_3DS_LOADER
  623.                     ASSERT(log << asda << "face description list");
  624.                 #endif
  625.                
  626.                 memcpy(&sVal, &data[offset+reloff+6], 2);                   // get mesh no.
  627.                 reloff += 8;                                                // move past reloff 6+2 bytes
  628.                
  629.                 #ifdef DEBUG_3DS_LOADER
  630.                     ASSERT(log << " - Coords no: " << sVal << endl);
  631.                 #endif
  632.                
  633.                 this->lMeshNum = sVal;
  634.                 for (int i=0; i<this->lMeshNum; i++){                       // sVal is the current num of meshes
  635.                     memcpy(&tmpMesh.x, &data[offset+reloff],    2); // memory -> vector(xyz)
  636.                     memcpy(&tmpMesh.y, &data[offset+reloff+2]2);
  637.                     memcpy(&tmpMesh.z, &data[offset+reloff+4]2);
  638.                     memcpy(&sVal,       &data[offset+reloff+6], 2); // face props (drop)
  639.                     reloff += 8;                                    // 3*(float) = 12 bytes
  640.                     // ...
  641.  
  642.                     mobj.meshList.push_back(tmpMesh);
  643.  
  644.                     //ASSERT(log << "x="<< tmpMesh.x << "; y=" << tmpMesh.y << "; z=" << tmpMesh.z << "; @" << reloff <<endl);
  645.                 }
  646.             } break;
  647.            
  648.             case M3DS_TEX_VERTS:
  649.                 {                                           // ------ PROCESS TEXTURE VERTICIES ------
  650.                     short len;
  651.                     texUV_t uv_tmp;
  652.                     #ifdef DEBUG_3DS_LOADER
  653.                         ASSERT(log << asda << "texture uv nfo" << endl);
  654.                     #endif
  655.                     memcpy (&len, &data[offset+reloff+6], 2);                   // face no
  656.                     reloff += 8;                                                // keep walking
  657.                    
  658.                     for (int i=0; i<len; i++){                                  // read data
  659.                         memcpy(&uv_tmp.u, &data[offset+reloff],   4);
  660.                         memcpy(&uv_tmp.v, &data[offset+reloff+4], 4);
  661.                         reloff += 8;
  662.                         mobj.uvList.push_back(uv_tmp);                          // push into vector
  663.                     }  
  664.                 }
  665.                 break;
  666.  
  667.             case M3DS_FACE_MAT:                                             // ------ PROCESS FACE MATERIALS ------
  668.                 {
  669.                    
  670.                     vector<unsigned short> meshNo;
  671.                     FWmodels::materialNfo_t matNfo_tmp;
  672.                     reloff +=6;                                             // keep going, skip chunk
  673.                     #ifdef DEBUG_3DS_LOADER
  674.                         ASSERT(log << asda << "material nfo" << endl);
  675.                     #endif
  676.                     while (data[offset+reloff] != '\0'){
  677.                         matNfo_tmp.name += (data[offset+reloff]);
  678.                         reloff++;                                           // here is the name of the mesh, what we skip
  679.                     }
  680.                     reloff++;
  681.  
  682.                     unsigned short var, var2;
  683.                     memcpy(&var, &data[offset+reloff], 2); reloff +=2;
  684.                     for (int i=0; i<var; i++){
  685.                         memcpy(&var2, &data[offset+reloff], 2);
  686.                         matNfo_tmp.face.push_back(var2);
  687.                         reloff +=2;
  688.                     }
  689.                     mobj.matList.push_back(matNfo_tmp);
  690.                 }
  691.                 break;
  692.  
  693.             case M3DS_SMOOTH_GROUP:                                         // ------ PROCESS FACE MATERIALS ------
  694.                 #ifdef DEBUG_3DS_LOADER
  695.                     ASSERT(log << asda << "smoothing groups" << endl);
  696.                 #endif
  697.                 reloff += chunk.len;   
  698.                 break;
  699.  
  700.             // ------------------------------------------------------------ //         
  701.             case M3DS_LOCAL_COORDS:                                         // ------ LOCAL COORDS ------
  702.                 #ifdef DEBUG_3DS_LOADER
  703.                     ASSERT(log << asda << "local coord nfo");
  704.                 #endif
  705.                 //memcpy(&mobj.localCoord->x1.x, &data[offset+reloff+6],    4); // x1 ( header: 6 bytes)
  706.                 //memcpy(&mobj.localCoord->x1.y, &data[offset+reloff+10],   4); //    ( ..+ 4 bytes float = 10 ... etc)
  707.                 //memcpy(&mobj.localCoord->x1.z, &data[offset+reloff+14],   4);
  708.  
  709.                 //memcpy(&mobj.localCoord->x2.x, &data[offset+reloff+18], 4);   // x2
  710.                 //memcpy(&mobj.localCoord->x2.y, &data[offset+reloff+22], 4);
  711.                 //memcpy(&mobj.localCoord->x2.z, &data[offset+reloff+26], 4);
  712.  
  713.                 //memcpy(&mobj.localCoord->x3.x, &data[offset+reloff+30], 4);   // x3
  714.                 //memcpy(&mobj.localCoord->x3.y, &data[offset+reloff+34], 4);
  715.                 //memcpy(&mobj.localCoord->x3.z, &data[offset+reloff+38], 4);
  716.  
  717.                 //memcpy(&mobj.localCoord->o.x,  &data[offset+reloff+42], 4);   // o
  718.                 //memcpy(&mobj.localCoord->o.y,  &data[offset+reloff+46], 4);
  719.                 //memcpy(&mobj.localCoord->o.z,  &data[offset+reloff+50], 4);   // ( 6 + 4*3*4 float = 6 + 48 = 50 + 4 = 54 bytes long)
  720.                 //
  721.                 /*
  722.                 ASSERT(sprintf(asda, "%f; %f; %f;",&mobj.localCoord->x1.x, &mobj.localCoord->x1.y, &mobj.localCoord->x1.z); log << " X1("<<asda<<")");
  723.                 ASSERT(sprintf(asda, "%f; %f; %f;",&mobj.localCoord->x2.x, &mobj.localCoord->x2.y, &mobj.localCoord->x2.z); log << " X2("<<asda<<")");
  724.                 ASSERT(sprintf(asda, "%f; %f; %f;",&mobj.localCoord->x3.x, &mobj.localCoord->x3.y, &mobj.localCoord->x3.z); log << " X3("<<asda<<")");
  725.                 ASSERT(sprintf(asda, "%f; %f; %f;",&mobj.localCoord->o.x,  &mobj.localCoord->o.y,  &mobj.localCoord->o.z);   log << " O("<<asda<<")");
  726.                 */
  727.                 #ifdef DEBUG_3DS_LOADER
  728.                     ASSERT(log<< endl);
  729.                 #endif
  730.  
  731.                 // TODO: fuck it doesn't work. offset shift is incorrect
  732.                 // make an inline vector reader function: 3f and 2s (float n short)
  733.  
  734.                 reloff += chunk.len;   
  735.                 break;
  736.  
  737.             // ------------------------------------------------------------ //
  738.             default:
  739.                 #ifdef DEBUG_3DS_LOADER
  740.                     ASSERT(log <<asda<<" unknown chunk" << endl);
  741.                 #endif
  742.                 reloff += chunk.len;
  743.                 ukchkFAIL++;
  744.                 if(ukchkFAIL>1024) return; // fail
  745.                 break;
  746.         }
  747.  
  748.         // cp (loaded data -> model data);
  749.     }
  750.     #ifdef DEBUG_3DS_LOADER
  751.         ASSERT(log << "  :RET" <<endl);
  752.     #endif
  753. }
  754.  
  755. // -----------------------------------------------------------------------------------------
  756.  
  757. void model3ds::drawModels(){
  758.     this->error =0 ;
  759.     if (this->objects.empty()) {this->error=1; return;}
  760.     for (int i=0; i<this->objects.size(); i++)
  761.         this->objects[i].drawPlainModel();
  762. }
  763.  
  764. // -----------------------------------------------------------------------------------------
  765. void model3ds::get3fVector(const unsigned int offs, float *x, float *y, float *z){
  766.     memcpy(x, &data[offs],   4);
  767.     memcpy(y, &data[offs+4], 4);
  768.     memcpy(z, &data[offs+8], 4);
  769. }
  770.  
  771. model3ds::chunkInf model3ds::getChunkInfo(const unsigned int offset){
  772.     chunkInf nfo;
  773.     memcpy(&nfo.id,  &this->data[offset]    ,2);
  774.     memcpy(&nfo.len, &this->data[offset+2]  ,4);
  775.     return nfo;
  776. }
  777.  
  778.  
  779.  
  780. //#include <vector>
  781.  
  782. //#define _LOG_DUMP
  783. #define _AUTOTEXTURE
  784.  
  785. using namespace std;
  786. using namespace FWmodels;
  787.  
  788. model::model(){
  789.     this->err = 0;              // cle
  790.     this->isNormalsCalced = 0;  // clc
  791. }
  792.  
  793. model::~model(){
  794.  
  795. }
  796.  
  797. // =============================================================================================================== //
  798. void model::calcNormals(){
  799.     //printf("calc normals\n");
  800.     for(int i=0; i<this->meshList.size(); i++) {
  801.         meshes_t tmpMesh(0, 0, 0);
  802.         tmpMesh = meshList[i];
  803.        
  804.         vector3d3dsloaderbb<float> a, b, c, v1, v2, normal;
  805.        
  806.         a = this->vertList[tmpMesh.x];
  807.         b = this->vertList[tmpMesh.y];
  808.         c = this->vertList[tmpMesh.z];
  809.  
  810.         v1 = a - b;  //AB
  811.         v2 = c - a;  //AC
  812.         normal.x =   (v1.y * v2.z) - (v1.z * v2.y) ;// determine the normal vector
  813.         normal.y = -((v2.z * v1.x) - (v2.x * v1.z));
  814.         normal.z =  (v1.x * v2.y) - (v1.y * v2.x);
  815.  
  816.         normal /= normal.vlen();
  817.        
  818.         this->normList.push_back(normal);
  819.     }
  820. }
  821.  
  822. void model::drawPlainModel(){
  823.     // custom drawing porcedure inside the class
  824. }
  825.  
  826. // =============================================================================================================== //
  827.  
  828. material* FWmodels::prev_material;  // define here the extern
  829.  
  830. material::material(){
  831.     memset(&this->ambient, 0, sizeof(this->ambient));
  832.     memset(&this->diffusal, 0, sizeof(this->diffusal));
  833.     memset(&this->specular, 0, sizeof(this->specular));
  834.    
  835.     /*this->ambP = new float[3];
  836.     this->specP = new float[3];
  837.     this->diffP= new float[3];*/
  838. }
  839.  
  840. void material::bindMaterial(bool force=true){
  841.     //...
  842. }
  843.  
  844. //float* material::getAmbientP(){
  845. //  //...
  846. //}
  847. //
  848. //float* material::getDiffusalP(){
  849. //  //...
  850. //}
  851. //
  852. //float* material::getSpecularP(){
  853. //  //...
  854. //}
  855.  
  856.  
  857.  
  858. int sm3DSLoad(FILE * fail, int WELD=0){
  859.     weld3dsfiled=WELD;
  860.     unsigned char * filedata;
  861.     if(!fail) return 0;
  862.     FILE * fp=fail;
  863.     fseek(fp, 0, SEEK_END);
  864.     long flen = ftell(fp);
  865.     m3dsfile_maxdata=flen;
  866.     fseek(fp, 0, SEEK_SET);
  867.     filedata = new unsigned char [flen];
  868.     fread(filedata, flen, 1, fp);
  869.     FWmodels::model3ds test(filedata, flen);
  870. //  FWmodels::model3ds * test=new model3ds (filedata, flen);  
  871.     delete[] filedata;
  872.  
  873.     number_of_mesh_3ds=0; // nullázás, hátha elkúrta a compiler
  874.     for(int i=0;i<511;i++){
  875.         osszes_polygon_3ds[i]=0;
  876.         polygon_3ds[i]=NULL;
  877.         uv_3ds[i]=NULL;
  878.         normal_3ds[i]=NULL;
  879.         texneve[i][0]='\0';
  880.     }
  881.  
  882.     for (int i=0; i<test.objects.size(); i++){
  883.         polygon_3ds[number_of_mesh_3ds]=(float*)malloc( (test.objects[i]).meshList.size() * sizeof(float)*9);
  884.         uv_3ds[number_of_mesh_3ds]=(float*)malloc( (test.objects[i]).meshList.size() * sizeof(float)*6);
  885.         normal_3ds[number_of_mesh_3ds]=(float*)malloc( (test.objects[i]).meshList.size() * sizeof(float)*9);
  886.  
  887.         if((test.objects[i]).matList.size()>0){
  888.             const char * material_neve=(test.objects[i]).matList[0].name.c_str();
  889.             if(!material_neve){ // ezen nincs textúra, ejnye
  890.                 texneve[number_of_mesh_3ds][0]='\0';
  891.             }else{ // ezen van textúra, meg kell keresni név alapján a hordozó materialt.
  892.                 if(ujmat>254) ujmat=254;
  893.                 for(int q=0;q<ujmat;q++){
  894.                     if(strstr(materialok_nevei[q], material_neve) && strstr(material_neve, materialok_nevei[q])){
  895.                         snprintf(texneve[number_of_mesh_3ds], 13, "%s", materialok_nevei_tex[q]);
  896.     //                  printf("megtalalva %s\n", texneve[i]);
  897.                     }
  898.                 }
  899.             }
  900.         }else{
  901.             texneve[number_of_mesh_3ds][0]='\0';
  902.         }
  903.  
  904.         for (int j=0; j<(test.objects[i]).meshList.size(); j++){
  905.             osszes_polygon_3ds[number_of_mesh_3ds]++;
  906.  
  907.             if (!(test.objects[i]).vertList.empty()){
  908.                 polygon_3ds[number_of_mesh_3ds][j*9  +0]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].x].x;
  909.                 polygon_3ds[number_of_mesh_3ds][j*9  +1]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].x].y;
  910.                 polygon_3ds[number_of_mesh_3ds][j*9  +2]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].x].z;
  911.                 polygon_3ds[number_of_mesh_3ds][j*9  +3]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].y].x;
  912.                 polygon_3ds[number_of_mesh_3ds][j*9  +4]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].y].y;
  913.                 polygon_3ds[number_of_mesh_3ds][j*9  +5]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].y].z;
  914.                 polygon_3ds[number_of_mesh_3ds][j*9  +6]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].z].x;
  915.                 polygon_3ds[number_of_mesh_3ds][j*9  +7]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].z].y;
  916.                 polygon_3ds[number_of_mesh_3ds][j*9  +8]=(test.objects[i]).vertList[(test.objects[i]).meshList[j].z].z;
  917.             }
  918.  
  919.             if (!(test.objects[i]).uvList.empty()){
  920.                 uv_3ds[number_of_mesh_3ds][j*6 +0]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].x].u;
  921.                 uv_3ds[number_of_mesh_3ds][j*6 +1]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].x].v;
  922.                 uv_3ds[number_of_mesh_3ds][j*6 +2]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].y].u;
  923.                 uv_3ds[number_of_mesh_3ds][j*6 +3]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].y].v;
  924.                 uv_3ds[number_of_mesh_3ds][j*6 +4]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].z].u;
  925.                 uv_3ds[number_of_mesh_3ds][j*6 +5]=(test.objects[i]).uvList[(test.objects[i]).meshList[j].z].v;
  926.             }
  927.  
  928.             if (!(test.objects[i]).normList.empty()){
  929. /*              normal_3ds[number_of_mesh_3ds][j*9  +0]=(test.objects[i]).normList[(test.objects[i]).meshList[j].x].x;
  930.                 normal_3ds[number_of_mesh_3ds][j*9  +1]=(test.objects[i]).normList[(test.objects[i]).meshList[j].x].y;
  931.                 normal_3ds[number_of_mesh_3ds][j*9  +2]=(test.objects[i]).normList[(test.objects[i]).meshList[j].x].z;
  932.                 normal_3ds[number_of_mesh_3ds][j*9  +3]=(test.objects[i]).normList[(test.objects[i]).meshList[j].y].x;
  933.                 normal_3ds[number_of_mesh_3ds][j*9  +4]=(test.objects[i]).normList[(test.objects[i]).meshList[j].y].y;
  934.                 normal_3ds[number_of_mesh_3ds][j*9  +5]=(test.objects[i]).normList[(test.objects[i]).meshList[j].y].z;
  935.                 normal_3ds[number_of_mesh_3ds][j*9  +6]=(test.objects[i]).normList[(test.objects[i]).meshList[j].z].x;
  936.                 normal_3ds[number_of_mesh_3ds][j*9  +7]=(test.objects[i]).normList[(test.objects[i]).meshList[j].z].y;
  937.                 normal_3ds[number_of_mesh_3ds][j*9  +8]=(test.objects[i]).normList[(test.objects[i]).meshList[j].z].z;*/
  938.  
  939. /*              normal_3ds[number_of_mesh_3ds][j*9  +0]=polygon_3ds[number_of_mesh_3ds][j*9  +0] - polygon_3ds[number_of_mesh_3ds][j*9  +3] - (polygon_3ds[number_of_mesh_3ds][j*9  +6] + polygon_3ds[number_of_mesh_3ds][j*9  +0]);
  940.                 normal_3ds[number_of_mesh_3ds][j*9  +1]=polygon_3ds[number_of_mesh_3ds][j*9  +1] - polygon_3ds[number_of_mesh_3ds][j*9  +4] - (polygon_3ds[number_of_mesh_3ds][j*9  +7] + polygon_3ds[number_of_mesh_3ds][j*9  +1]);
  941.                 normal_3ds[number_of_mesh_3ds][j*9  +2]=polygon_3ds[number_of_mesh_3ds][j*9  +2] - polygon_3ds[number_of_mesh_3ds][j*9  +5] - (polygon_3ds[number_of_mesh_3ds][j*9  +8] + polygon_3ds[number_of_mesh_3ds][j*9  +2]);
  942.                 normal_3ds[number_of_mesh_3ds][j*9  +3]=polygon_3ds[number_of_mesh_3ds][j*9  +3] - polygon_3ds[number_of_mesh_3ds][j*9  +6] - (polygon_3ds[number_of_mesh_3ds][j*9  +0] + polygon_3ds[number_of_mesh_3ds][j*9  +3]);
  943.                 normal_3ds[number_of_mesh_3ds][j*9  +4]=polygon_3ds[number_of_mesh_3ds][j*9  +4] - polygon_3ds[number_of_mesh_3ds][j*9  +7] - (polygon_3ds[number_of_mesh_3ds][j*9  +1] + polygon_3ds[number_of_mesh_3ds][j*9  +4]);
  944.                 normal_3ds[number_of_mesh_3ds][j*9  +5]=polygon_3ds[number_of_mesh_3ds][j*9  +5] - polygon_3ds[number_of_mesh_3ds][j*9  +8] - (polygon_3ds[number_of_mesh_3ds][j*9  +2] + polygon_3ds[number_of_mesh_3ds][j*9  +5]);
  945.                 normal_3ds[number_of_mesh_3ds][j*9  +6]=polygon_3ds[number_of_mesh_3ds][j*9  +6] - polygon_3ds[number_of_mesh_3ds][j*9  +0] - (polygon_3ds[number_of_mesh_3ds][j*9  +3] + polygon_3ds[number_of_mesh_3ds][j*9  +6]);
  946.                 normal_3ds[number_of_mesh_3ds][j*9  +7]=polygon_3ds[number_of_mesh_3ds][j*9  +7] - polygon_3ds[number_of_mesh_3ds][j*9  +1] - (polygon_3ds[number_of_mesh_3ds][j*9  +4] + polygon_3ds[number_of_mesh_3ds][j*9  +7]);
  947.                 normal_3ds[number_of_mesh_3ds][j*9  +8]=polygon_3ds[number_of_mesh_3ds][j*9  +8] - polygon_3ds[number_of_mesh_3ds][j*9  +2] - (polygon_3ds[number_of_mesh_3ds][j*9  +5] + polygon_3ds[number_of_mesh_3ds][j*9  +8]);
  948.                
  949.                 float nrm;
  950.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +0]*normal_3ds[number_of_mesh_3ds][j*9  +0]+normal_3ds[number_of_mesh_3ds][j*9  +1]*normal_3ds[number_of_mesh_3ds][j*9  +1]+normal_3ds[number_of_mesh_3ds][j*9  +2]*normal_3ds[number_of_mesh_3ds][j*9  +2]);
  951.                 normal_3ds[number_of_mesh_3ds][j*9  +0]*=nrm;
  952.                 normal_3ds[number_of_mesh_3ds][j*9  +1]*=nrm;
  953.                 normal_3ds[number_of_mesh_3ds][j*9  +2]*=nrm;
  954.  
  955.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +3]*normal_3ds[number_of_mesh_3ds][j*9  +3]+normal_3ds[number_of_mesh_3ds][j*9  +4]*normal_3ds[number_of_mesh_3ds][j*9  +4]+normal_3ds[number_of_mesh_3ds][j*9  +5]*normal_3ds[number_of_mesh_3ds][j*9  +5]);
  956.                 normal_3ds[number_of_mesh_3ds][j*9  +3]*=nrm;
  957.                 normal_3ds[number_of_mesh_3ds][j*9  +4]*=nrm;
  958.                 normal_3ds[number_of_mesh_3ds][j*9  +5]*=nrm;
  959.  
  960.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +6]*normal_3ds[number_of_mesh_3ds][j*9  +6]+normal_3ds[number_of_mesh_3ds][j*9  +7]*normal_3ds[number_of_mesh_3ds][j*9  +7]+normal_3ds[number_of_mesh_3ds][j*9  +8]*normal_3ds[number_of_mesh_3ds][j*9  +8]);
  961.                 normal_3ds[number_of_mesh_3ds][j*9  +6]*=nrm;
  962.                 normal_3ds[number_of_mesh_3ds][j*9  +7]*=nrm;
  963.                 normal_3ds[number_of_mesh_3ds][j*9  +8]*=nrm;*/
  964.  
  965.                 float v1_x=polygon_3ds[number_of_mesh_3ds][j*9  +3] - polygon_3ds[number_of_mesh_3ds][j*9  +0];
  966.                 float v1_y=polygon_3ds[number_of_mesh_3ds][j*9  +4] - polygon_3ds[number_of_mesh_3ds][j*9  +1];
  967.                 float v1_z=polygon_3ds[number_of_mesh_3ds][j*9  +5] - polygon_3ds[number_of_mesh_3ds][j*9  +2];
  968.                 float v2_x=polygon_3ds[number_of_mesh_3ds][j*9  +6] - polygon_3ds[number_of_mesh_3ds][j*9  +0];
  969.                 float v2_y=polygon_3ds[number_of_mesh_3ds][j*9  +7] - polygon_3ds[number_of_mesh_3ds][j*9  +1];
  970.                 float v2_z=polygon_3ds[number_of_mesh_3ds][j*9  +8] - polygon_3ds[number_of_mesh_3ds][j*9  +2];
  971.  
  972.                 float cx=v1_y*v2_z - v2_y*v1_z;
  973.                 float cy=v1_z*v2_x - v2_z*v1_x;
  974.                 float cz=v1_x*v2_y - v2_x*v1_y;
  975.    
  976.                 normal_3ds[number_of_mesh_3ds][j*9  +0]=cx;
  977.                 normal_3ds[number_of_mesh_3ds][j*9  +1]=cy;
  978.                 normal_3ds[number_of_mesh_3ds][j*9  +2]=cz;
  979.  
  980.                 normal_3ds[number_of_mesh_3ds][j*9  +3]=cx;
  981.                 normal_3ds[number_of_mesh_3ds][j*9  +4]=cy;
  982.                 normal_3ds[number_of_mesh_3ds][j*9  +5]=cz;
  983.  
  984.                 normal_3ds[number_of_mesh_3ds][j*9  +6]=cx;
  985.                 normal_3ds[number_of_mesh_3ds][j*9  +7]=cy;
  986.                 normal_3ds[number_of_mesh_3ds][j*9  +8]=cz;
  987.  
  988.  
  989.                 float nrm;
  990.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +0]*normal_3ds[number_of_mesh_3ds][j*9  +0]+normal_3ds[number_of_mesh_3ds][j*9  +1]*normal_3ds[number_of_mesh_3ds][j*9  +1]+normal_3ds[number_of_mesh_3ds][j*9  +2]*normal_3ds[number_of_mesh_3ds][j*9  +2]);
  991.                 normal_3ds[number_of_mesh_3ds][j*9  +0]*=nrm;
  992.                 normal_3ds[number_of_mesh_3ds][j*9  +1]*=nrm;
  993.                 normal_3ds[number_of_mesh_3ds][j*9  +2]*=nrm;
  994.  
  995.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +3]*normal_3ds[number_of_mesh_3ds][j*9  +3]+normal_3ds[number_of_mesh_3ds][j*9  +4]*normal_3ds[number_of_mesh_3ds][j*9  +4]+normal_3ds[number_of_mesh_3ds][j*9  +5]*normal_3ds[number_of_mesh_3ds][j*9  +5]);
  996.                 normal_3ds[number_of_mesh_3ds][j*9  +3]*=nrm;
  997.                 normal_3ds[number_of_mesh_3ds][j*9  +4]*=nrm;
  998.                 normal_3ds[number_of_mesh_3ds][j*9  +5]*=nrm;
  999.  
  1000.                 nrm=1.0f/sqrtf(normal_3ds[number_of_mesh_3ds][j*9  +6]*normal_3ds[number_of_mesh_3ds][j*9  +6]+normal_3ds[number_of_mesh_3ds][j*9  +7]*normal_3ds[number_of_mesh_3ds][j*9  +7]+normal_3ds[number_of_mesh_3ds][j*9  +8]*normal_3ds[number_of_mesh_3ds][j*9  +8]);
  1001.                 normal_3ds[number_of_mesh_3ds][j*9  +6]*=nrm;
  1002.                 normal_3ds[number_of_mesh_3ds][j*9  +7]*=nrm;
  1003.                 normal_3ds[number_of_mesh_3ds][j*9  +8]*=nrm;
  1004.             }
  1005.         }
  1006.         // normalok posztprocesszalasa
  1007.         if(weld3dsfiled){
  1008.             register int ipopt=osszes_polygon_3ds[number_of_mesh_3ds];
  1009.             for(register int i=0;i<ipopt;i++){
  1010.                 for(register int j=0;j<3;j++){
  1011.                     for(register int i2=0;i2<ipopt;i2++){
  1012.                         for(register int j2=0;j2<3;j2++){
  1013.                             register float t21=polygon_3ds[number_of_mesh_3ds][i2*9  +j2*3 +0];
  1014.                             register float t1=polygon_3ds[number_of_mesh_3ds][i*9  +j*3 +0];
  1015.                             if(t21==t1){
  1016.                                 float t22=polygon_3ds[number_of_mesh_3ds][i2*9  +j2*3 +1];
  1017.                                 float t2=polygon_3ds[number_of_mesh_3ds][i*9  +j*3 +1];
  1018.                                 if(t22==t2){
  1019.                                     float t23=polygon_3ds[number_of_mesh_3ds][i2*9  +j2*3 +2];
  1020.                                     float t3=polygon_3ds[number_of_mesh_3ds][i*9  +j*3 +2];
  1021.                                     if(t23==t3){
  1022.                                         // weld!
  1023.                                         normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +0]=normal_3ds[number_of_mesh_3ds][i*9  +j*3 +0]=(normal_3ds[number_of_mesh_3ds][i*9  +j*3 +0]+normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +0])*0.5;
  1024.                                         normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +1]=normal_3ds[number_of_mesh_3ds][i*9  +j*3 +1]=(normal_3ds[number_of_mesh_3ds][i*9  +j*3 +1]+normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +1])*0.5;
  1025.                                         normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +2]=normal_3ds[number_of_mesh_3ds][i*9  +j*3 +2]=(normal_3ds[number_of_mesh_3ds][i*9  +j*3 +2]+normal_3ds[number_of_mesh_3ds][i2*9  +j2*3 +2])*0.5;
  1026.                                     }
  1027.                                 }
  1028.                             }
  1029.                         }
  1030.                     }
  1031.                 }
  1032.             }
  1033.         }
  1034.         number_of_mesh_3ds++;
  1035.         if(number_of_mesh_3ds>253){ // híjnye, sok lesz
  1036.             return 1;
  1037.         }
  1038.     }
  1039.     return 1;
  1040. }
  1041.  
  1042. int sm3DSget_number_of_mesh(){
  1043.     return number_of_mesh_3ds;
  1044. }
  1045.  
  1046. int sm3DSget_texture_name(int mesh, char * a){
  1047. /*  for(int i=0;i<???;i++){
  1048.         if(objects[i].matlist.???==???){
  1049.             snprintf(a, 13, "%s", objects[i].matlist.???.nametex);
  1050.             return 1;
  1051.         }
  1052.     }*/
  1053.     if(strlen(texneve[mesh])>0){
  1054.         snprintf(a, 13, "%s", texneve[mesh]);
  1055.         return 1;
  1056.     }
  1057.     return 0;
  1058. }
  1059.  
  1060. float * sm3DSget_vtx(int mesh){
  1061.     return polygon_3ds[mesh];
  1062. }
  1063.  
  1064. float * sm3DSget_tex(int mesh){
  1065.     return uv_3ds[mesh];
  1066. }
  1067.  
  1068. float * sm3DSget_nrm(int mesh){
  1069.     return normal_3ds[mesh];
  1070. }
  1071.  
  1072. void sm3DS_die(){
  1073.     number_of_mesh_3ds=0;
  1074.     for(int i=0;i<511;i++){
  1075.         osszes_polygon_3ds[i]=0;
  1076.         if(polygon_3ds[i]) free(polygon_3ds[i]);
  1077.         polygon_3ds[i]=NULL;
  1078.         if(uv_3ds[i]) free(uv_3ds[i]);
  1079.         uv_3ds[i]=NULL;
  1080.         if(normal_3ds[i]) free(normal_3ds[i]);
  1081.         normal_3ds[i]=NULL;
  1082.         texneve[i][0]='\0';
  1083.     }
  1084.     ukchkFAIL=0;
  1085.     ujmat=0;
  1086.     return;
  1087. }
  1088.  
  1089. int sm3DSget_nmbr_faces(int mesh){
  1090.     return osszes_polygon_3ds[mesh];
  1091. }
  1092.  
  1093.  
  1094. //             *     ,MMM8&&&.            *
  1095. //                  MMMM88&&&&&    .
  1096. //                 MMMM88&&&&&&&
  1097. //     *           MMM88&&&&&&&&
  1098. //                 MMM88&&&&&&&&
  1099. //                 'MMM88&&&&&&'
  1100. //                   'MMM8&&&'      *
  1101. //          |\___/|
  1102. //          )     (             .              '
  1103. //         =\     /=
  1104. //           )===(       *
  1105. //          /     \
  1106. //          |     |
  1107. //         /       \
  1108. //         \       /
  1109. //  _/\_/\_/\__  _/_/\_/\_/\_/\_/\_/\_/\_/\_/\_
  1110. //  |  |  |  |( (  |  |  |  |  |  |  |  |  |  |
  1111. //  |  |  |  | ) ) |  |  |  |  |  |  |  |  |  |
  1112. //  |  |  |  |(_(  |  |  |  |  |  |  |  |  |  |
  1113. //  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  1114. //  jgs|  |  |  |  |  |  |  |  |  |  |  |  |  |
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement