http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / glx / normals.c
1 /* normals, Copyright (c) 2002-2004 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  *
11  * Compute normal vectors for arbitrary triangles.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #include "normals.h"
19
20 /* Calculate the unit normal at p given two other points p1,p2 on the
21    surface. The normal points in the direction of p1 crossproduct p2
22  */
23 XYZ
24 calc_normal (XYZ p, XYZ p1, XYZ p2)
25 {
26   XYZ n, pa, pb;
27   pa.x = p1.x - p.x;
28   pa.y = p1.y - p.y;
29   pa.z = p1.z - p.z;
30   pb.x = p2.x - p.x;
31   pb.y = p2.y - p.y;
32   pb.z = p2.z - p.z;
33   n.x = pa.y * pb.z - pa.z * pb.y;
34   n.y = pa.z * pb.x - pa.x * pb.z;
35   n.z = pa.x * pb.y - pa.y * pb.x;
36   return (n);
37 }
38
39 /* Call glNormal3f() with a normal of the indicated triangle.
40  */
41 void
42 do_normal(GLfloat x1, GLfloat y1, GLfloat z1,
43           GLfloat x2, GLfloat y2, GLfloat z2,
44           GLfloat x3, GLfloat y3, GLfloat z3)
45 {
46   XYZ p1, p2, p3, p;
47   p1.x = x1; p1.y = y1; p1.z = z1;
48   p2.x = x2; p2.y = y2; p2.z = z2;
49   p3.x = x3; p3.y = y3; p3.z = z3;
50   p = calc_normal (p1, p2, p3);
51   glNormal3f (p.x, p.y, p.z);
52 }