Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

load3ds.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "load3ds.h"
  2. #include "../punkt3d.h"
  3. Load3ds::Load3ds(std::string _fname, bool _parse) {
  4. filename = _fname;
  5. parsed = false;
  6. error = false;
  7. // if(_parse)
  8. // parse();
  9. }
  10. bool Load3ds::parse(Model *m) {
  11. if(parsed)
  12. unload();
  13. std::ifstream mfile(filename.c_str(), std::ios::binary);
  14. if(!mfile) {
  15. return false;
  16. }
  17. unsigned short ident;
  18. unsigned int len;
  19. bool modelloaded = false;
  20. bool materialloaded = false;
  21. Material mat;
  22. ModelObject modobj;
  23. m->clear();
  24. while(!mfile.eof()) {
  25. ident = len = 0;
  26. mfile.read((char *)&ident, 2);
  27. mfile.read((char *)&len, 4);
  28. std::cout << "Chunk: 0x" << std::hex << ident << " (" << std::dec << len << ")" << std::endl;
  29. switch(ident) {
  30. case 0x4d4d:
  31. // Main Chunk
  32. break;
  33. case 0x3d3d:
  34. // 3D Editor Chunk
  35. break;
  36. case 0x4000:
  37. {
  38. if(modelloaded) {
  39. modobj.calcNormales();
  40. m->addObject(modobj);
  41. modobj.clear();
  42. }
  43. modelloaded = true;
  44. // std::cout << "namechunk" << std::endl;
  45. std::string name;
  46. char c;
  47. do {
  48. mfile.read(&c, 1);
  49. name += c;
  50. } while(c!=0);
  51. std::cout << "Name: " << name << std::endl;
  52. modobj.name = name;
  53. }
  54. break;
  55. case 0x4100:
  56. break;
  57. case 0x4110:{
  58. unsigned short panz;
  59. Punkt3D p;
  60. mfile.read((char*)&panz, 2);
  61. // std::cout << "\t\t\tVertexanz: " << panz << std::endl;
  62. glBegin(GL_LINE_LOOP);
  63. for(unsigned int i=0; i<panz; i++) {
  64. mfile.read((char*)&p.x, sizeof(float));
  65. mfile.read((char*)&p.y, sizeof(float));
  66. mfile.read((char*)&p.z, sizeof(float));
  67. // p.print("Punkt");
  68. modobj.vertex.push_back(p);
  69. glVertex3f(p);
  70. }
  71. glEnd();
  72. }
  73. break;
  74. case 0x4120:
  75. {
  76. unsigned short panz, tp;
  77. Punkt3D p;
  78. mfile.read((char*)&panz, 2);
  79. // std::cout << "\t\t\tPolygonanz: " << panz << std::endl;
  80. for(unsigned int i=0; i<panz; i++) {
  81. mfile.read((char*)&tp, 2);
  82. p.x = tp;
  83. mfile.read((char*)&tp, 2);
  84. p.y = tp;
  85. mfile.read((char*)&tp, 2);
  86. p.z = tp;
  87. mfile.read((char*)&tp, 2);
  88. tp; // flags
  89. // p.print("Polygon");
  90. modobj.polygon.push_back(p);
  91. }
  92. }
  93. break;
  94. // Komischer Chunk, ein haufen Zahlen sind sich selber zugewiesen
  95. case 0x4130:
  96. {
  97. std::string name;
  98. char c;
  99. do {
  100. mfile.read(&c, 1);
  101. name += c;
  102. } while(c!=0);
  103. modobj.mat = m->findMaterial(name);
  104. std::cout << "Mat Name: " << name << std::endl;
  105. unsigned short panz, tp;
  106. mfile.read((char*)&panz, 2);
  107. mfile.ignore(panz*2);
  108. // for(unsigned int i=0; i<panz; i++) {
  109. // mfile.read((char*)&tp, 2);
  110. // std::cout << i << " ==> " << tp << std::endl;
  111. // }
  112. }
  113. break;
  114. case 0x4140:
  115. {
  116. unsigned short panz;
  117. Punkt2D p;
  118. mfile.read((char*)&panz, 2);
  119. // std::cout << "\t\t\tUV-Anz: " << panz << std::endl;
  120. for(unsigned int i=0; i<panz; i++) {
  121. mfile.read((char*)&p.x, sizeof(float));
  122. mfile.read((char*)&p.y, sizeof(float));
  123. modobj.mapcoord.push_back(p);
  124. }
  125. }
  126. break;
  127. case 0xafff:
  128. // Materialchunk
  129. break;
  130. case 0xa000:
  131. {
  132. if(materialloaded) {
  133. m->addMaterial(mat);
  134. mat.clear();
  135. }
  136. materialloaded = true;
  137. std::string name;
  138. char c;
  139. do {
  140. mfile.read(&c, 1);
  141. name += c;
  142. } while(c!=0);
  143. mat.name = name;
  144. std::cout << "Mat Name: " << name << std::endl;
  145. }
  146. break;
  147. case 0xa010:
  148. {
  149. GLColor col;
  150. col = readColorChunk(&mfile);
  151. mat.ambient = col;
  152. std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
  153. }
  154. break;
  155. case 0xa020:
  156. {
  157. GLColor col;
  158. col = readColorChunk(&mfile);
  159. mat.diffuse = col;
  160. std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
  161. }
  162. break;
  163. case 0xa030:
  164. {
  165. GLColor col;
  166. col = readColorChunk(&mfile);
  167. mat.specular = col;
  168. std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
  169. }
  170. break;
  171. default:
  172. //Switch Chunk
  173. // std::cout << "einfach ignorieren..." << std::endl;
  174. mfile.ignore(len-6);
  175. // std::cout << "moep" << std::endl;
  176. break;
  177. }
  178. }
  179. if(materialloaded)
  180. m->addMaterial(mat);
  181. if(modelloaded) {
  182. modobj.calcNormales();
  183. // modobj.mat = m->findMaterial(modobj.getName());
  184. m->addObject(modobj);
  185. }
  186. std::cout << "Ende Load" << std::endl;
  187. mfile.close();
  188. return true;
  189. }
  190. GLColor Load3ds::readColorChunk(std::ifstream *file) {
  191. GLColor col;
  192. unsigned short id, tp;
  193. unsigned int clen;
  194. file->read((char*)&id, 2); // Color, warscheinlich nur bytes, wenn nich implementiere funktion
  195. file->read((char*)&clen, 4); // len, gleiches wie oben
  196. switch(id) {
  197. case 0x11:
  198. file->read((char*)&tp, 1);
  199. col.r = tp/255.0f;
  200. file->read((char*)&tp, 1);
  201. col.g = tp/255.0f;
  202. file->read((char*)&tp, 1);
  203. col.b = tp/255.0f;
  204. break;
  205. default:
  206. file->ignore(clen-6);
  207. break;
  208. }
  209. return col;
  210. }
  211. void Load3ds::unload() {
  212. parsed = false;
  213. error = false;
  214. }