blob: a1b3fdeca7ad2aecc79ed46742df2f4d858e470e [file] [log] [blame]
Brian Paul5b0a7f32000-06-27 16:52:38 +00001/**
2(c) Copyright 1993, Silicon Graphics, Inc.
3
4ALL RIGHTS RESERVED
5
6Permission to use, copy, modify, and distribute this software
7for any purpose and without fee is hereby granted, provided
8that the above copyright notice appear in all copies and that
9both the copyright notice and this permission notice appear in
10supporting documentation, and that the name of Silicon
11Graphics, Inc. not be used in advertising or publicity
12pertaining to distribution of the software without specific,
13written prior permission.
14
15THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
16"AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
17OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
18MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO
19EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE
20ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
21CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
22INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
23SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
24NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY
25OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
27PERFORMANCE OF THIS SOFTWARE.
28
29US Government Users Restricted Rights
30
31Use, duplication, or disclosure by the Government is subject to
32restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
33(c)(1)(ii) of the Rights in Technical Data and Computer
34Software clause at DFARS 252.227-7013 and/or in similar or
35successor clauses in the FAR or the DOD or NASA FAR
36Supplement. Unpublished-- rights reserved under the copyright
37laws of the United States. Contractor/manufacturer is Silicon
38Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA
3994039-7311.
40
41OpenGL(TM) is a trademark of Silicon Graphics, Inc.
42*/
43
44/* Taken from the projshadow.c - by Tom McReynolds, SGI */
45
46/* Modified by David Bucciarelli */
47
48/* Rendering shadows using projective shadows. */
49
50#include <GL/glut.h>
51
52enum {
53 X, Y, Z, W
54};
55enum {
56 A, B, C, D
57};
58
59/* create a matrix that will project the desired shadow */
60void
61shadowmatrix(GLfloat shadowMat[4][4],
62 GLfloat groundplane[4],
63 GLfloat lightpos[4])
64{
65 GLfloat dot;
66
67 /* find dot product between light position vector and ground plane normal */
68 dot = groundplane[X] * lightpos[X] +
69 groundplane[Y] * lightpos[Y] +
70 groundplane[Z] * lightpos[Z] +
71 groundplane[W] * lightpos[W];
72
73 shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
74 shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
75 shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
76 shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
77
78 shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
79 shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
80 shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
81 shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
82
83 shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
84 shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
85 shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
86 shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
87
88 shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
89 shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
90 shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
91 shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
92
93}
94
95/* find the plane equation given 3 points */
96void
97findplane(GLfloat plane[4],
98 GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])
99{
100 GLfloat vec0[3], vec1[3];
101
102 /* need 2 vectors to find cross product */
103 vec0[X] = v1[X] - v0[X];
104 vec0[Y] = v1[Y] - v0[Y];
105 vec0[Z] = v1[Z] - v0[Z];
106
107 vec1[X] = v2[X] - v0[X];
108 vec1[Y] = v2[Y] - v0[Y];
109 vec1[Z] = v2[Z] - v0[Z];
110
111 /* find cross product to get A, B, and C of plane equation */
112 plane[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];
113 plane[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);
114 plane[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X];
115
116 plane[D] = -(plane[A] * v0[X] + plane[B] * v0[Y] + plane[C] * v0[Z]);
117}