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