http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[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 #include "config.h"
15 #include <GL/gl.h>
16 #include "normals.h"
17
18 /* Calculate the unit normal at p given two other points p1,p2 on the
19    surface. The normal points in the direction of p1 crossproduct p2
20  */
21 XYZ
22 calc_normal (XYZ p, XYZ p1, XYZ p2)
23 {
24   XYZ n, pa, pb;
25   pa.x = p1.x - p.x;
26   pa.y = p1.y - p.y;
27   pa.z = p1.z - p.z;
28   pb.x = p2.x - p.x;
29   pb.y = p2.y - p.y;
30   pb.z = p2.z - p.z;
31   n.x = pa.y * pb.z - pa.z * pb.y;
32   n.y = pa.z * pb.x - pa.x * pb.z;
33   n.z = pa.x * pb.y - pa.y * pb.x;
34   return (n);
35 }
36
37 /* Call glNormal3f() with a normal of the indicated triangle.
38  */
39 void
40 do_normal(GLfloat x1, GLfloat y1, GLfloat z1,
41           GLfloat x2, GLfloat y2, GLfloat z2,
42           GLfloat x3, GLfloat y3, GLfloat z3)
43 {
44   XYZ p1, p2, p3, p;
45   p1.x = x1; p1.y = y1; p1.z = z1;
46   p2.x = x2; p2.y = y2; p2.z = z2;
47   p3.x = x3; p3.y = y3; p3.z = z3;
48   p = calc_normal (p1, p2, p3);
49   glNormal3f (p.x, p.y, p.z);
50 }