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