normal.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. /*
  31. ** Author: Eric Veach, July 1994.
  32. **
  33. */
  34. #include "../prboom/SDL_opengl.h" // JDC
  35. //#include "gluos.h"
  36. #include "mesh.h"
  37. #include "tess.h"
  38. #include "normal.h"
  39. #include <math.h>
  40. #include <assert.h>
  41. #define TRUE 1
  42. #define FALSE 0
  43. #define Dot(u,v) (u[0]*v[0] + u[1]*v[1] + u[2]*v[2])
  44. #if 0
  45. static void Normalize( GLdouble v[3] )
  46. {
  47. GLdouble len = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  48. assert( len > 0 );
  49. len = sqrt( len );
  50. v[0] /= len;
  51. v[1] /= len;
  52. v[2] /= len;
  53. }
  54. #endif
  55. #undef ABS
  56. #define ABS(x) ((x) < 0 ? -(x) : (x))
  57. static int LongAxis( GLdouble v[3] )
  58. {
  59. int i = 0;
  60. if( ABS(v[1]) > ABS(v[0]) ) { i = 1; }
  61. if( ABS(v[2]) > ABS(v[i]) ) { i = 2; }
  62. return i;
  63. }
  64. static void ComputeNormal( GLUtesselator *tess, GLdouble norm[3] )
  65. {
  66. GLUvertex *v, *v1, *v2;
  67. GLdouble c, tLen2, maxLen2;
  68. GLdouble maxVal[3], minVal[3], d1[3], d2[3], tNorm[3];
  69. GLUvertex *maxVert[3], *minVert[3];
  70. GLUvertex *vHead = &tess->mesh->vHead;
  71. int i;
  72. maxVal[0] = maxVal[1] = maxVal[2] = -2 * GLU_TESS_MAX_COORD;
  73. minVal[0] = minVal[1] = minVal[2] = 2 * GLU_TESS_MAX_COORD;
  74. for( v = vHead->next; v != vHead; v = v->next ) {
  75. for( i = 0; i < 3; ++i ) {
  76. c = v->coords[i];
  77. if( c < minVal[i] ) { minVal[i] = c; minVert[i] = v; }
  78. if( c > maxVal[i] ) { maxVal[i] = c; maxVert[i] = v; }
  79. }
  80. }
  81. /* Find two vertices separated by at least 1/sqrt(3) of the maximum
  82. * distance between any two vertices
  83. */
  84. i = 0;
  85. if( maxVal[1] - minVal[1] > maxVal[0] - minVal[0] ) { i = 1; }
  86. if( maxVal[2] - minVal[2] > maxVal[i] - minVal[i] ) { i = 2; }
  87. if( minVal[i] >= maxVal[i] ) {
  88. /* All vertices are the same -- normal doesn't matter */
  89. norm[0] = 0; norm[1] = 0; norm[2] = 1;
  90. return;
  91. }
  92. /* Look for a third vertex which forms the triangle with maximum area
  93. * (Length of normal == twice the triangle area)
  94. */
  95. maxLen2 = 0;
  96. v1 = minVert[i];
  97. v2 = maxVert[i];
  98. d1[0] = v1->coords[0] - v2->coords[0];
  99. d1[1] = v1->coords[1] - v2->coords[1];
  100. d1[2] = v1->coords[2] - v2->coords[2];
  101. for( v = vHead->next; v != vHead; v = v->next ) {
  102. d2[0] = v->coords[0] - v2->coords[0];
  103. d2[1] = v->coords[1] - v2->coords[1];
  104. d2[2] = v->coords[2] - v2->coords[2];
  105. tNorm[0] = d1[1]*d2[2] - d1[2]*d2[1];
  106. tNorm[1] = d1[2]*d2[0] - d1[0]*d2[2];
  107. tNorm[2] = d1[0]*d2[1] - d1[1]*d2[0];
  108. tLen2 = tNorm[0]*tNorm[0] + tNorm[1]*tNorm[1] + tNorm[2]*tNorm[2];
  109. if( tLen2 > maxLen2 ) {
  110. maxLen2 = tLen2;
  111. norm[0] = tNorm[0];
  112. norm[1] = tNorm[1];
  113. norm[2] = tNorm[2];
  114. }
  115. }
  116. if( maxLen2 <= 0 ) {
  117. /* All points lie on a single line -- any decent normal will do */
  118. norm[0] = norm[1] = norm[2] = 0;
  119. norm[LongAxis(d1)] = 1;
  120. }
  121. }
  122. static void CheckOrientation( GLUtesselator *tess )
  123. {
  124. GLdouble area;
  125. GLUface *f, *fHead = &tess->mesh->fHead;
  126. GLUvertex *v, *vHead = &tess->mesh->vHead;
  127. GLUhalfEdge *e;
  128. /* When we compute the normal automatically, we choose the orientation
  129. * so that the the sum of the signed areas of all contours is non-negative.
  130. */
  131. area = 0;
  132. for( f = fHead->next; f != fHead; f = f->next ) {
  133. e = f->anEdge;
  134. if( e->winding <= 0 ) continue;
  135. do {
  136. area += (e->Org->s - e->Dst->s) * (e->Org->t + e->Dst->t);
  137. e = e->Lnext;
  138. } while( e != f->anEdge );
  139. }
  140. if( area < 0 ) {
  141. /* Reverse the orientation by flipping all the t-coordinates */
  142. for( v = vHead->next; v != vHead; v = v->next ) {
  143. v->t = - v->t;
  144. }
  145. tess->tUnit[0] = - tess->tUnit[0];
  146. tess->tUnit[1] = - tess->tUnit[1];
  147. tess->tUnit[2] = - tess->tUnit[2];
  148. }
  149. }
  150. #ifdef FOR_TRITE_TEST_PROGRAM
  151. #include <stdlib.h>
  152. extern int RandomSweep;
  153. #define S_UNIT_X (RandomSweep ? (2*drand48()-1) : 1.0)
  154. #define S_UNIT_Y (RandomSweep ? (2*drand48()-1) : 0.0)
  155. #else
  156. #if defined(SLANTED_SWEEP)
  157. /* The "feature merging" is not intended to be complete. There are
  158. * special cases where edges are nearly parallel to the sweep line
  159. * which are not implemented. The algorithm should still behave
  160. * robustly (ie. produce a reasonable tesselation) in the presence
  161. * of such edges, however it may miss features which could have been
  162. * merged. We could minimize this effect by choosing the sweep line
  163. * direction to be something unusual (ie. not parallel to one of the
  164. * coordinate axes).
  165. */
  166. #define S_UNIT_X 0.50941539564955385 /* Pre-normalized */
  167. #define S_UNIT_Y 0.86052074622010633
  168. #else
  169. #define S_UNIT_X 1.0
  170. #define S_UNIT_Y 0.0
  171. #endif
  172. #endif
  173. /* Determine the polygon normal and project vertices onto the plane
  174. * of the polygon.
  175. */
  176. void __gl_projectPolygon( GLUtesselator *tess )
  177. {
  178. GLUvertex *v, *vHead = &tess->mesh->vHead;
  179. GLdouble norm[3];
  180. GLdouble *sUnit, *tUnit;
  181. int i, computedNormal = FALSE;
  182. norm[0] = tess->normal[0];
  183. norm[1] = tess->normal[1];
  184. norm[2] = tess->normal[2];
  185. if( norm[0] == 0 && norm[1] == 0 && norm[2] == 0 ) {
  186. ComputeNormal( tess, norm );
  187. computedNormal = TRUE;
  188. }
  189. sUnit = tess->sUnit;
  190. tUnit = tess->tUnit;
  191. i = LongAxis( norm );
  192. #if defined(FOR_TRITE_TEST_PROGRAM) || defined(TRUE_PROJECT)
  193. /* Choose the initial sUnit vector to be approximately perpendicular
  194. * to the normal.
  195. */
  196. Normalize( norm );
  197. sUnit[i] = 0;
  198. sUnit[(i+1)%3] = S_UNIT_X;
  199. sUnit[(i+2)%3] = S_UNIT_Y;
  200. /* Now make it exactly perpendicular */
  201. w = Dot( sUnit, norm );
  202. sUnit[0] -= w * norm[0];
  203. sUnit[1] -= w * norm[1];
  204. sUnit[2] -= w * norm[2];
  205. Normalize( sUnit );
  206. /* Choose tUnit so that (sUnit,tUnit,norm) form a right-handed frame */
  207. tUnit[0] = norm[1]*sUnit[2] - norm[2]*sUnit[1];
  208. tUnit[1] = norm[2]*sUnit[0] - norm[0]*sUnit[2];
  209. tUnit[2] = norm[0]*sUnit[1] - norm[1]*sUnit[0];
  210. Normalize( tUnit );
  211. #else
  212. /* Project perpendicular to a coordinate axis -- better numerically */
  213. sUnit[i] = 0;
  214. sUnit[(i+1)%3] = S_UNIT_X;
  215. sUnit[(i+2)%3] = S_UNIT_Y;
  216. tUnit[i] = 0;
  217. tUnit[(i+1)%3] = (norm[i] > 0) ? -S_UNIT_Y : S_UNIT_Y;
  218. tUnit[(i+2)%3] = (norm[i] > 0) ? S_UNIT_X : -S_UNIT_X;
  219. #endif
  220. /* Project the vertices onto the sweep plane */
  221. for( v = vHead->next; v != vHead; v = v->next ) {
  222. v->s = Dot( v->coords, sUnit );
  223. v->t = Dot( v->coords, tUnit );
  224. }
  225. if( computedNormal ) {
  226. CheckOrientation( tess );
  227. }
  228. }