hw3file.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Hw3file implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. /* Don't build for now */
  10. #if 0
  11. /* This file is parsing a text format from another raytracer I made in the past. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include <ctype.h>
  17. #include <math.h>
  18. #define IS_CMD(_cmd) (strncmp(buffer, _cmd, sizeof(_cmd)) == 0)
  19. typedef void (*cmdFunc)(scene *sc, uint32_t curLine, char *first, float argv[15]);
  20. typedef struct cmd_def
  21. {
  22. const char *name;
  23. int8_t numparam; /* -1 == one string, else >0 = number of argv */
  24. cmdFunc f;
  25. } cmd_def;
  26. void cmdCamera (scene *sc, uint32_t curLine, char *first, float argv[15])
  27. {
  28. sc->cam.seteye(point(argv[0], argv[1], argv[2]));
  29. sc->cam.setat(point(argv[3], argv[4], argv[5]));
  30. sc->cam.setup(vector(argv[6], argv[7], argv[8]));
  31. sc->cam.setfovy(argv[9]);
  32. }
  33. void cmdSphere (scene *sc, uint32_t curLine, char *first, float argv[15])
  34. {
  35. /* Instanciate a sphere */
  36. sphere *sp = new sphere(point(argv[0], argv[1], argv[2]), argv[3], sc->getMatrix());
  37. sp->ambient = sc->ambient;
  38. sp->diffuse = sc->diffuse;
  39. sp->specular = sc->specular;
  40. sp->emission = sc->emission;
  41. sp->shininess = sc->shininess;
  42. sp->sourceLine = curLine;
  43. sc->o[sc->o_count] = sp;
  44. sc->o_count++;
  45. }
  46. void cmdCube (scene *sc, uint32_t curLine, char *first, float argv[15])
  47. {
  48. }
  49. #if 0
  50. void cmdMaxVerts (scene *sc, uint32_t curLine, char *first, float argv[15])
  51. {
  52. sc->vertexCount = (uint32_t) argv[0];
  53. sc->vertex = new point[sc->vertexCount];
  54. sc->curVertex = 0;
  55. }
  56. void cmdMaxVertN (scene *sc, uint32_t curLine, char *first, float argv[15])
  57. {
  58. /* ignore for now */
  59. }
  60. void cmdVertex (scene *sc, uint32_t curLine, char *first, float argv[15])
  61. {
  62. sc->vertex[sc->curVertex].set(argv[0], argv[1], argv[2]);
  63. sc->curVertex++;
  64. }
  65. void cmdVertexN (scene *sc, uint32_t curLine, char *first, float argv[15])
  66. {
  67. /* ignore for now */
  68. }
  69. void cmdTri (scene *sc, uint32_t curLine, char *first, float argv[15])
  70. {
  71. /* arg are vertex numbers */
  72. triangle *tr = new triangle(sc->vertex[(int)argv[0]], sc->vertex[(int)argv[1]], sc->vertex[(int)argv[2]], sc->getMatrix());
  73. tr->ambient = sc->ambient;
  74. tr->diffuse = sc->diffuse;
  75. tr->specular = sc->specular;
  76. tr->emission = sc->emission;
  77. tr->shininess = sc->shininess;
  78. tr->sourceLine = curLine;
  79. sc->o[sc->o_count] = tr;
  80. sc->o_count++;
  81. }
  82. void cmdTriN (scene *sc, uint32_t curLine, char *first, float argv[15])
  83. {
  84. /* ignore for noz */
  85. }
  86. #endif
  87. void cmdTrans (scene *sc, uint32_t curLine, char *first, float argv[15])
  88. {
  89. matrix m;
  90. m.translation(argv[0], argv[1], argv[2]);
  91. sc->applyTransform(m);
  92. }
  93. void cmdRotate (scene *sc, uint32_t curLine, char *first, float argv[15])
  94. {
  95. matrix m;
  96. vector axis;
  97. axis.set(argv[0], argv[1], argv[2]);
  98. m.rotation(axis, argv[3]);
  99. sc->applyTransform(m);
  100. }
  101. void cmdScale (scene *sc, uint32_t curLine, char *first, float argv[15])
  102. {
  103. matrix m;
  104. m.scale(argv[0], argv[1], argv[2]);
  105. sc->applyTransform(m);
  106. }
  107. void cmdPushT (scene *sc, uint32_t curLine, char *first, float argv[15])
  108. {
  109. sc->pushMatrix();
  110. }
  111. void cmdPopT (scene *sc, uint32_t curLine, char *first, float argv[15])
  112. {
  113. sc->popMatrix();
  114. }
  115. #if 0
  116. void cmdLDire (scene *sc, uint32_t curLine, char *first, float argv[15])
  117. {
  118. /* create directional light */
  119. light *cur = new light();
  120. cur->type = LIGHT_DIRECTIONAL;
  121. cur->attenuation = sc->attenuation;
  122. cur->position.set(argv[0], argv[1], argv[2]);
  123. cur->lightcolor.set(argv[3], argv[4], argv[5]);
  124. sc->l[sc->l_count] = cur;
  125. sc->l_count++;
  126. }
  127. #endif
  128. void cmdLPoint (scene *sc, uint32_t curLine, char *first, float argv[15])
  129. {
  130. /* create point light */
  131. light *cur = new light();
  132. cur->type = LIGHT_POINT;
  133. cur->attenuation = sc->attenuation;
  134. cur->position.set(argv[0], argv[1], argv[2]);
  135. cur->lightcolor.set(argv[3], argv[4], argv[5]);
  136. sc->l[sc->l_count] = cur;
  137. sc->l_count++;
  138. }
  139. #if 0
  140. void cmdAtten (scene *sc, uint32_t curLine, char *first, float argv[15])
  141. {
  142. sc->attenuation = vector(argv[0], argv[1], argv[2]);
  143. }
  144. #endif
  145. void cmdAmbient (scene *sc, uint32_t curLine, char *first, float argv[15])
  146. {
  147. sc->ambient = color(argv[0], argv[1], argv[2]);
  148. }
  149. void cmdDiffuse (scene *sc, uint32_t curLine, char *first, float argv[15])
  150. {
  151. sc->diffuse = color(argv[0], argv[1], argv[2]);
  152. }
  153. void cmdSpecular (scene *sc, uint32_t curLine, char *first, float argv[15])
  154. {
  155. sc->specular = color(argv[0], argv[1], argv[2]);
  156. }
  157. void cmdShine (scene *sc, uint32_t curLine, char *first, float argv[15])
  158. {
  159. sc->shininess = argv[0];
  160. }
  161. void cmdEmission (scene *sc, uint32_t curLine, char *first, float argv[15])
  162. {
  163. sc->emission = color(argv[0], argv[1], argv[2]);
  164. }
  165. cmd_def commandList[] =
  166. {
  167. /* Camera */
  168. { "camera", 10, cmdCamera },
  169. /* Geometry */
  170. { "sphere", 4, cmdSphere },
  171. //{ "maxverts", 1, cmdMaxVerts },
  172. //{ "maxvertnorms", 1, cmdMaxVertN },
  173. //{ "vertex", 3, cmdVertex },
  174. //{ "vertexnormal", 6, cmdVertexN },
  175. //{ "tri", 3, cmdTri },
  176. //{ "trinormal", 3, cmdTriN },
  177. //{ "cube", 1, cmdCube },
  178. /* transformation */
  179. { "translate", 3, cmdTrans },
  180. { "rotate", 4, cmdRotate },
  181. { "scale", 3, cmdScale },
  182. { "pushTransform", 0, cmdPushT },
  183. { "popTransform", 0, cmdPopT },
  184. /* Lights */
  185. //{ "directional", 6, cmdLDire },
  186. { "point", 6, cmdLPoint },
  187. //{ "attenuation", 3, cmdAtten },
  188. /* Materials */
  189. { "ambient", 3, cmdAmbient },
  190. { "diffuse", 3, cmdDiffuse },
  191. { "specular", 3, cmdSpecular },
  192. { "shininess", 1, cmdShine },
  193. { "emission", 3, cmdEmission },
  194. };
  195. #define CMD_COUNT (sizeof(commandList) / sizeof(cmd_def))
  196. scene *readfile::read(char *filename)
  197. {
  198. FILE *fp;
  199. scene *sc = new scene;
  200. if (sc != NULL)
  201. {
  202. fp = fopen(filename, "rt");
  203. if (fp == NULL)
  204. {
  205. delete sc;
  206. sc = NULL;
  207. }
  208. else
  209. {
  210. char buffer[512];
  211. int line = 0;
  212. while(!feof(fp))
  213. {
  214. uint32_t i;
  215. line++;
  216. memset(buffer, 0, 512);
  217. fgets(buffer, 512, fp);
  218. if ((buffer[0] == '#') || (strlen(buffer) < 2))
  219. continue; /* Ingore empty line or commented lines */
  220. //printf("::%d:> %s", strlen(buffer), buffer);
  221. for (i = 0; i < CMD_COUNT; i++)
  222. {
  223. if (strncmp(buffer, commandList[i].name, strlen(commandList[i].name)) == 0)
  224. {
  225. char first[512];
  226. float value[15];
  227. if (commandList[i].numparam != 0)
  228. {
  229. size_t j;
  230. int k = 0, l = 0;
  231. char buff[512];
  232. for(j = strlen(commandList[i].name); j < strlen(buffer); j++)
  233. {
  234. if (!isspace(buffer[j]))
  235. {
  236. buff[l++] = buffer[j];
  237. }
  238. else
  239. {
  240. buff[l] = 0;
  241. l = 0;
  242. if (k == 0)
  243. {
  244. strcpy(first, buff);
  245. }
  246. if (strlen(buff) > 0)
  247. {
  248. value[k++] = atof(buff);
  249. }
  250. }
  251. }
  252. if (k != abs(commandList[i].numparam))
  253. {
  254. printf("line %d malformed: given %d parameter, expected %d\n%s", line, k, abs(commandList[i].numparam), buffer);
  255. sc = NULL;
  256. goto exit;
  257. }
  258. }
  259. commandList[i].f(sc, line, first, value);
  260. break;
  261. }
  262. }
  263. }
  264. #ifdef USE_OCTREE
  265. sc->createOctree();
  266. #endif
  267. }
  268. }
  269. exit:
  270. return sc;
  271. }
  272. #endif