Partial application of linux/alpha patch. Courtesy of Falk Hueffner <falk.hueffner...
[btb/d2x.git] / 3d / instance.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  * $Source: /cvs/cvsroot/d2x/3d/instance.c,v $
15  * $Revision: 1.2 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-31 15:17:48 $
18  * 
19  * Instancing routines
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  2001/01/19 03:29:58  bradleyb
23  * Import of d2x-0.0.8
24  *
25  * Revision 1.1.1.1  1999/06/14 21:57:45  donut
26  * Import of d1x 1.37 source.
27  *
28  * Revision 1.2  1995/06/12  12:36:57  allender
29  * fixed bug where g3_start_instance_angles recursively called itself
30  *
31  * Revision 1.1  1995/05/05  08:51:27  allender
32  * Initial revision
33  *
34  * Revision 1.1  1995/04/17  06:43:29  matt
35  * Initial revision
36  * 
37  * 
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <conf.h>
42 #endif
43
44 #ifdef RCS
45 static char rcsid[] = "$Id: instance.c,v 1.2 2001-01-31 15:17:48 bradleyb Exp $";
46 #endif
47
48 #include <stdlib.h>
49 #include "error.h"
50
51 #include "fix.h"
52 #include "vecmat.h"
53 #include "gr.h"
54 #include "3d.h"
55 #include "globvars.h"
56
57 #define MAX_INSTANCE_DEPTH      5
58
59 struct instance_context {
60         vms_matrix m;
61         vms_vector p;
62 } instance_stack[MAX_INSTANCE_DEPTH];
63
64 int instance_depth = 0;
65
66 //instance at specified point with specified orientation
67 //if matrix==NULL, don't modify matrix.  This will be like doing an offset   
68 void g3_start_instance_matrix(vms_vector *pos,vms_matrix *orient)
69 {
70         vms_vector tempv;
71         vms_matrix tempm,tempm2;
72
73 #ifdef D1XD3D
74         Win32_start_instance_matrix (pos, orient);
75 #endif
76
77         Assert(instance_depth<MAX_INSTANCE_DEPTH);
78
79         instance_stack[instance_depth].m = View_matrix;
80         instance_stack[instance_depth].p = View_position;
81         instance_depth++;
82
83         //step 1: subtract object position from view position
84
85         vm_vec_sub(&tempv,&View_position,pos);
86
87
88         if (orient) {
89
90                 //step 2: rotate view vector through object matrix
91
92                 vm_vec_rotate(&View_position,&tempv,orient);
93
94                 //step 3: rotate object matrix through view_matrix (vm = ob * vm)
95
96                 vm_copy_transpose_matrix(&tempm2,orient);
97
98                 vm_matrix_x_matrix(&tempm,&tempm2,&View_matrix);
99                 View_matrix = tempm;
100         }
101 }
102
103
104 //instance at specified point with specified orientation
105 //if angles==NULL, don't modify matrix.  This will be like doing an offset
106 void g3_start_instance_angles(vms_vector *pos,vms_angvec *angles)
107 {
108         vms_matrix tm;
109
110         if (angles==NULL) {
111                 g3_start_instance_matrix(pos,NULL);
112                 return;
113         }
114
115         vm_angles_2_matrix(&tm,angles);
116
117         g3_start_instance_matrix(pos,&tm);
118
119 }
120
121
122 //pops the old context
123 void g3_done_instance()
124 {
125 #ifdef D1XD3D
126         Win32_done_instance ();
127 #endif
128
129         instance_depth--;
130
131         Assert(instance_depth >= 0);
132
133         View_position = instance_stack[instance_depth].p;
134         View_matrix = instance_stack[instance_depth].m;
135 }
136
137