glfile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qbsp.h"
  19. int c_glfaces;
  20. int PortalVisibleSides (portal_t *p)
  21. {
  22. int fcon, bcon;
  23. if (!p->onnode)
  24. return 0; // outside
  25. fcon = p->nodes[0]->opaque;
  26. bcon = p->nodes[1]->opaque;
  27. // same contents never create a face
  28. if (fcon == bcon)
  29. return 0;
  30. if (!fcon)
  31. return 1;
  32. if (!bcon)
  33. return 2;
  34. return 0;
  35. }
  36. void OutputWinding (winding_t *w, FILE *glview)
  37. {
  38. static int level = 128;
  39. vec_t light;
  40. int i;
  41. fprintf (glview, "%i\n", w->numpoints);
  42. level+=28;
  43. light = (level&255)/255.0;
  44. for (i=0 ; i<w->numpoints ; i++)
  45. {
  46. fprintf (glview, "%6.3f %6.3f %6.3f %6.3f %6.3f %6.3f\n",
  47. w->p[i][0],
  48. w->p[i][1],
  49. w->p[i][2],
  50. light,
  51. light,
  52. light);
  53. }
  54. fprintf (glview, "\n");
  55. }
  56. /*
  57. =============
  58. OutputPortal
  59. =============
  60. */
  61. void OutputPortal (portal_t *p, FILE *glview)
  62. {
  63. winding_t *w;
  64. int sides;
  65. sides = PortalVisibleSides (p);
  66. if (!sides)
  67. return;
  68. c_glfaces++;
  69. w = p->winding;
  70. if (sides == 2) // back side
  71. w = ReverseWinding (w);
  72. OutputWinding (w, glview);
  73. if (sides == 2)
  74. FreeWinding(w);
  75. }
  76. /*
  77. =============
  78. WriteGLView_r
  79. =============
  80. */
  81. void WriteGLView_r (node_t *node, FILE *glview)
  82. {
  83. portal_t *p, *nextp;
  84. if (node->planenum != PLANENUM_LEAF)
  85. {
  86. WriteGLView_r (node->children[0], glview);
  87. WriteGLView_r (node->children[1], glview);
  88. return;
  89. }
  90. // write all the portals
  91. for (p=node->portals ; p ; p=nextp)
  92. {
  93. if (p->nodes[0] == node)
  94. {
  95. OutputPortal (p, glview);
  96. nextp = p->next[0];
  97. }
  98. else
  99. nextp = p->next[1];
  100. }
  101. }
  102. /*
  103. =============
  104. WriteGLView
  105. =============
  106. */
  107. void WriteGLView (tree_t *tree, char *source)
  108. {
  109. char name[1024];
  110. FILE *glview;
  111. c_glfaces = 0;
  112. sprintf (name, "%s%s.gl",outbase, source);
  113. _printf ("Writing %s\n", name);
  114. glview = fopen (name, "w");
  115. if (!glview)
  116. Error ("Couldn't open %s", name);
  117. WriteGLView_r (tree->headnode, glview);
  118. fclose (glview);
  119. _printf ("%5i c_glfaces\n", c_glfaces);
  120. }