1
0

READ_JAW.C 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /******************************************************************************
  2. Plush Version 1.2
  3. read_jaw.c
  4. Jaw3D Object Reader
  5. Copyright (c) 1996-2000, Justin Frankel
  6. *******************************************************************************
  7. Notes on .JAW files:
  8. This is a file format created by Jawed Karim for Jaw3D
  9. (http://jaw3d.home.ml.org).
  10. -- updated 11/6/00 - www.jawed.com
  11. It is very simple, and lets one easily create ones own models using only
  12. a text editor. The format is pretty simple:
  13. The first line must be "Light: (x,y,z)" where x,y, and z are the x y and
  14. z components of the lightsource vector (I think ;)
  15. A series of lines, numbered 0 to n, in the format of
  16. "i: x y z", where i is the vertex number (which should be listed in
  17. order, and x y and z are the coordinates of that vertex.
  18. A series of lines, having the format "tri a, b, c" where a b and c are
  19. the vertices that the face uses. It is unclear at this time which
  20. way the vertices are listed (ccw or cw), so just make em consistent
  21. and you can always use plFlipObjectNormals() on the loaded object.
  22. That is it! (I told ya it was simple).
  23. ******************************************************************************/
  24. #include "plush.h"
  25. pl_Obj *plReadJAWObj(char *filename, pl_Mat *m) {
  26. FILE *jawfile;
  27. pl_Obj *obj;
  28. pl_uInt32 i;
  29. pl_sInt crap;
  30. char line[256];
  31. pl_uInt32 total_points = 0, total_polys = 0;
  32. if ((jawfile = fopen(filename, "r")) == NULL) return 0;
  33. fgets(line, 256, jawfile); /* Ignores lightsource info */
  34. while (fgets(line, 256, jawfile) != NULL)
  35. if (strstr(line, ":") != NULL) total_points++;
  36. rewind(jawfile); fgets(line, 256, jawfile);
  37. while (fgets(line, 256, jawfile) != NULL)
  38. if (strstr(line, "tri") != NULL) total_polys++;
  39. rewind(jawfile); fgets(line, 256, jawfile);
  40. obj = plObjCreate(total_points,total_polys);
  41. i = 0;
  42. while (fgets(line, 256, jawfile) != NULL) if (strstr(line, ":") != NULL) {
  43. float x, y, z;
  44. sscanf(line, "%d: %f %f %f",&crap,&x,&y,&z);
  45. obj->Vertices[i].x = (pl_Float) x;
  46. obj->Vertices[i].y = (pl_Float) y;
  47. obj->Vertices[i].z = (pl_Float) z;
  48. i++;
  49. }
  50. rewind(jawfile); fgets(line, 256, jawfile);
  51. i = 0;
  52. while (fgets(line, 256, jawfile) != NULL) if (strstr(line, "tri") != NULL) {
  53. pl_uInt32 a,b,c;
  54. sscanf(line, "tri %ld, %ld, %ld", &a, &b, &c);
  55. obj->Faces[i].Vertices[0] = obj->Vertices + a;
  56. obj->Faces[i].Vertices[1] = obj->Vertices + c;
  57. obj->Faces[i].Vertices[2] = obj->Vertices + b;
  58. obj->Faces[i].Material = m;
  59. i++;
  60. }
  61. fclose(jawfile);
  62. plObjCalcNormals(obj);
  63. return obj;
  64. }