tr_shadows.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 "tr_local.h"
  19. /*
  20. for a projection shadow:
  21. point[x] += light vector * ( z - shadow plane )
  22. point[y] +=
  23. point[z] = shadow plane
  24. 1 0 light[x] / light[z]
  25. */
  26. typedef struct {
  27. int i2;
  28. int facing;
  29. } edgeDef_t;
  30. #define MAX_EDGE_DEFS 32
  31. static edgeDef_t edgeDefs[SHADER_MAX_VERTEXES][MAX_EDGE_DEFS];
  32. static int numEdgeDefs[SHADER_MAX_VERTEXES];
  33. static int facing[SHADER_MAX_INDEXES/3];
  34. void R_AddEdgeDef( int i1, int i2, int facing ) {
  35. int c;
  36. c = numEdgeDefs[ i1 ];
  37. if ( c == MAX_EDGE_DEFS ) {
  38. return; // overflow
  39. }
  40. edgeDefs[ i1 ][ c ].i2 = i2;
  41. edgeDefs[ i1 ][ c ].facing = facing;
  42. numEdgeDefs[ i1 ]++;
  43. }
  44. void R_RenderShadowEdges( void ) {
  45. int i;
  46. #if 0
  47. int numTris;
  48. // dumb way -- render every triangle's edges
  49. numTris = tess.numIndexes / 3;
  50. for ( i = 0 ; i < numTris ; i++ ) {
  51. int i1, i2, i3;
  52. if ( !facing[i] ) {
  53. continue;
  54. }
  55. i1 = tess.indexes[ i*3 + 0 ];
  56. i2 = tess.indexes[ i*3 + 1 ];
  57. i3 = tess.indexes[ i*3 + 2 ];
  58. qglBegin( GL_TRIANGLE_STRIP );
  59. qglVertex3fv( tess.xyz[ i1 ] );
  60. qglVertex3fv( tess.xyz[ i1 + tess.numVertexes ] );
  61. qglVertex3fv( tess.xyz[ i2 ] );
  62. qglVertex3fv( tess.xyz[ i2 + tess.numVertexes ] );
  63. qglVertex3fv( tess.xyz[ i3 ] );
  64. qglVertex3fv( tess.xyz[ i3 + tess.numVertexes ] );
  65. qglVertex3fv( tess.xyz[ i1 ] );
  66. qglVertex3fv( tess.xyz[ i1 + tess.numVertexes ] );
  67. qglEnd();
  68. }
  69. #else
  70. int c, c2;
  71. int j, k;
  72. int i2;
  73. int c_edges, c_rejected;
  74. int hit[2];
  75. // an edge is NOT a silhouette edge if its face doesn't face the light,
  76. // or if it has a reverse paired edge that also faces the light.
  77. // A well behaved polyhedron would have exactly two faces for each edge,
  78. // but lots of models have dangling edges or overfanned edges
  79. c_edges = 0;
  80. c_rejected = 0;
  81. for ( i = 0 ; i < tess.numVertexes ; i++ ) {
  82. c = numEdgeDefs[ i ];
  83. for ( j = 0 ; j < c ; j++ ) {
  84. if ( !edgeDefs[ i ][ j ].facing ) {
  85. continue;
  86. }
  87. hit[0] = 0;
  88. hit[1] = 0;
  89. i2 = edgeDefs[ i ][ j ].i2;
  90. c2 = numEdgeDefs[ i2 ];
  91. for ( k = 0 ; k < c2 ; k++ ) {
  92. if ( edgeDefs[ i2 ][ k ].i2 == i ) {
  93. hit[ edgeDefs[ i2 ][ k ].facing ]++;
  94. }
  95. }
  96. // if it doesn't share the edge with another front facing
  97. // triangle, it is a sil edge
  98. if ( hit[ 1 ] == 0 ) {
  99. qglBegin( GL_TRIANGLE_STRIP );
  100. qglVertex3fv( tess.xyz[ i ] );
  101. qglVertex3fv( tess.xyz[ i + tess.numVertexes ] );
  102. qglVertex3fv( tess.xyz[ i2 ] );
  103. qglVertex3fv( tess.xyz[ i2 + tess.numVertexes ] );
  104. qglEnd();
  105. c_edges++;
  106. } else {
  107. c_rejected++;
  108. }
  109. }
  110. }
  111. #endif
  112. }
  113. /*
  114. =================
  115. RB_ShadowTessEnd
  116. triangleFromEdge[ v1 ][ v2 ]
  117. set triangle from edge( v1, v2, tri )
  118. if ( facing[ triangleFromEdge[ v1 ][ v2 ] ] && !facing[ triangleFromEdge[ v2 ][ v1 ] ) {
  119. }
  120. =================
  121. */
  122. void RB_ShadowTessEnd( void ) {
  123. int i;
  124. int numTris;
  125. vec3_t lightDir;
  126. // we can only do this if we have enough space in the vertex buffers
  127. if ( tess.numVertexes >= SHADER_MAX_VERTEXES / 2 ) {
  128. return;
  129. }
  130. if ( glConfig.stencilBits < 4 ) {
  131. return;
  132. }
  133. VectorCopy( backEnd.currentEntity->lightDir, lightDir );
  134. // project vertexes away from light direction
  135. for ( i = 0 ; i < tess.numVertexes ; i++ ) {
  136. VectorMA( tess.xyz[i], -512, lightDir, tess.xyz[i+tess.numVertexes] );
  137. }
  138. // decide which triangles face the light
  139. Com_Memset( numEdgeDefs, 0, 4 * tess.numVertexes );
  140. numTris = tess.numIndexes / 3;
  141. for ( i = 0 ; i < numTris ; i++ ) {
  142. int i1, i2, i3;
  143. vec3_t d1, d2, normal;
  144. float *v1, *v2, *v3;
  145. float d;
  146. i1 = tess.indexes[ i*3 + 0 ];
  147. i2 = tess.indexes[ i*3 + 1 ];
  148. i3 = tess.indexes[ i*3 + 2 ];
  149. v1 = tess.xyz[ i1 ];
  150. v2 = tess.xyz[ i2 ];
  151. v3 = tess.xyz[ i3 ];
  152. VectorSubtract( v2, v1, d1 );
  153. VectorSubtract( v3, v1, d2 );
  154. CrossProduct( d1, d2, normal );
  155. d = DotProduct( normal, lightDir );
  156. if ( d > 0 ) {
  157. facing[ i ] = 1;
  158. } else {
  159. facing[ i ] = 0;
  160. }
  161. // create the edges
  162. R_AddEdgeDef( i1, i2, facing[ i ] );
  163. R_AddEdgeDef( i2, i3, facing[ i ] );
  164. R_AddEdgeDef( i3, i1, facing[ i ] );
  165. }
  166. // draw the silhouette edges
  167. GL_Bind( tr.whiteImage );
  168. qglEnable( GL_CULL_FACE );
  169. GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO );
  170. qglColor3f( 0.2f, 0.2f, 0.2f );
  171. // don't write to the color buffer
  172. qglColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
  173. qglEnable( GL_STENCIL_TEST );
  174. qglStencilFunc( GL_ALWAYS, 1, 255 );
  175. // mirrors have the culling order reversed
  176. if ( backEnd.viewParms.isMirror ) {
  177. qglCullFace( GL_FRONT );
  178. qglStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
  179. R_RenderShadowEdges();
  180. qglCullFace( GL_BACK );
  181. qglStencilOp( GL_KEEP, GL_KEEP, GL_DECR );
  182. R_RenderShadowEdges();
  183. } else {
  184. qglCullFace( GL_BACK );
  185. qglStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
  186. R_RenderShadowEdges();
  187. qglCullFace( GL_FRONT );
  188. qglStencilOp( GL_KEEP, GL_KEEP, GL_DECR );
  189. R_RenderShadowEdges();
  190. }
  191. // reenable writing to the color buffer
  192. qglColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  193. }
  194. /*
  195. =================
  196. RB_ShadowFinish
  197. Darken everything that is is a shadow volume.
  198. We have to delay this until everything has been shadowed,
  199. because otherwise shadows from different body parts would
  200. overlap and double darken.
  201. =================
  202. */
  203. void RB_ShadowFinish( void ) {
  204. if ( r_shadows->integer != 2 ) {
  205. return;
  206. }
  207. if ( glConfig.stencilBits < 4 ) {
  208. return;
  209. }
  210. qglEnable( GL_STENCIL_TEST );
  211. qglStencilFunc( GL_NOTEQUAL, 0, 255 );
  212. qglDisable (GL_CLIP_PLANE0);
  213. qglDisable (GL_CULL_FACE);
  214. GL_Bind( tr.whiteImage );
  215. qglLoadIdentity ();
  216. qglColor3f( 0.6f, 0.6f, 0.6f );
  217. GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO );
  218. // qglColor3f( 1, 0, 0 );
  219. // GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO );
  220. qglBegin( GL_QUADS );
  221. qglVertex3f( -100, 100, -10 );
  222. qglVertex3f( 100, 100, -10 );
  223. qglVertex3f( 100, -100, -10 );
  224. qglVertex3f( -100, -100, -10 );
  225. qglEnd ();
  226. qglColor4f(1,1,1,1);
  227. qglDisable( GL_STENCIL_TEST );
  228. }
  229. /*
  230. =================
  231. RB_ProjectionShadowDeform
  232. =================
  233. */
  234. void RB_ProjectionShadowDeform( void ) {
  235. float *xyz;
  236. int i;
  237. float h;
  238. vec3_t ground;
  239. vec3_t light;
  240. float groundDist;
  241. float d;
  242. vec3_t lightDir;
  243. xyz = ( float * ) tess.xyz;
  244. ground[0] = backEnd.or.axis[0][2];
  245. ground[1] = backEnd.or.axis[1][2];
  246. ground[2] = backEnd.or.axis[2][2];
  247. groundDist = backEnd.or.origin[2] - backEnd.currentEntity->e.shadowPlane;
  248. VectorCopy( backEnd.currentEntity->lightDir, lightDir );
  249. d = DotProduct( lightDir, ground );
  250. // don't let the shadows get too long or go negative
  251. if ( d < 0.5 ) {
  252. VectorMA( lightDir, (0.5 - d), ground, lightDir );
  253. d = DotProduct( lightDir, ground );
  254. }
  255. d = 1.0 / d;
  256. light[0] = lightDir[0] * d;
  257. light[1] = lightDir[1] * d;
  258. light[2] = lightDir[2] * d;
  259. for ( i = 0; i < tess.numVertexes; i++, xyz += 4 ) {
  260. h = DotProduct( xyz, ground ) + groundDist;
  261. xyz[0] -= light[0] * h;
  262. xyz[1] -= light[1] * h;
  263. xyz[2] -= light[2] * h;
  264. }
  265. }