]> icculus.org git repositories - btb/d2x.git/blob - 3d/rod.c
updated documentation
[btb/d2x.git] / 3d / rod.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/rod.c,v $
15  * $Revision: 1.1.1.1 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-19 03:29:58 $
18  * 
19  * Rod routines
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.2  1999/09/21 04:05:55  donut
23  * mostly complete OGL implementation (still needs bitmap handling (reticle), and door/fan textures are corrupt)
24  *
25  * Revision 1.1.1.1  1999/06/14 21:57:50  donut
26  * Import of d1x 1.37 source.
27  *
28  * Revision 1.2  1995/09/13  11:31:46  allender
29  * removed checkmuldiv in PPC implemenation
30  *
31  * Revision 1.1  1995/05/05  08:52:45  allender
32  * Initial revision
33  *
34  * Revision 1.1  1995/04/17  06:42:08  matt
35  * Initial revision
36  * 
37  * 
38  */
39
40 #ifdef RCS
41 static char rcsid[] = "$Id: rod.c,v 1.1.1.1 2001-01-19 03:29:58 bradleyb Exp $";
42 #endif
43
44 #include <conf.h>
45 #include "fix.h"
46 #include "vecmat.h"
47 #include "gr.h"
48 #include "3d.h"
49 #include "globvars.h"
50
51 grs_point blob_vertices[4];
52 g3s_point rod_points[4];
53 g3s_point *rod_point_list[] = {&rod_points[0],&rod_points[1],&rod_points[2],&rod_points[3]};
54
55 g3s_uvl uvl_list[4] = { { 0x0200,0x0200,0 },
56                                                                 { 0xfe00,0x0200,0 },
57                                                                 { 0xfe00,0xfe00,0 },
58                                                                 { 0x0200,0xfe00,0 }};
59
60 //compute the corners of a rod.  fills in vertbuf.
61 int calc_rod_corners(g3s_point *bot_point,fix bot_width,g3s_point *top_point,fix top_width)
62 {
63         vms_vector delta_vec,top,tempv,rod_norm;
64         ubyte codes_and;
65         int i;
66
67         //compute vector from one point to other, do cross product with vector
68         //from eye to get perpendiclar
69
70         vm_vec_sub(&delta_vec,&bot_point->p3_vec,&top_point->p3_vec);
71
72         //unscale for aspect
73
74         delta_vec.x = fixdiv(delta_vec.x,Matrix_scale.x);
75         delta_vec.y = fixdiv(delta_vec.y,Matrix_scale.y);
76
77         //calc perp vector
78
79         //do lots of normalizing to prevent overflowing.  When this code works,
80         //it should be optimized
81
82         vm_vec_normalize(&delta_vec);
83
84         vm_vec_copy_normalize(&top,&top_point->p3_vec);
85
86         vm_vec_cross(&rod_norm,&delta_vec,&top);
87
88         vm_vec_normalize(&rod_norm);
89
90         //scale for aspect
91
92         rod_norm.x = fixmul(rod_norm.x,Matrix_scale.x);
93         rod_norm.y = fixmul(rod_norm.y,Matrix_scale.y);
94
95         //now we have the usable edge.  generate four points
96
97         //top points
98
99         vm_vec_copy_scale(&tempv,&rod_norm,top_width);
100         tempv.z = 0;
101
102         vm_vec_add(&rod_points[0].p3_vec,&top_point->p3_vec,&tempv);
103         vm_vec_sub(&rod_points[1].p3_vec,&top_point->p3_vec,&tempv);
104
105         vm_vec_copy_scale(&tempv,&rod_norm,bot_width);
106         tempv.z = 0;
107
108         vm_vec_sub(&rod_points[2].p3_vec,&bot_point->p3_vec,&tempv);
109         vm_vec_add(&rod_points[3].p3_vec,&bot_point->p3_vec,&tempv);
110
111
112         //now code the four points
113
114         for (i=0,codes_and=0xff;i<4;i++)
115                 codes_and &= g3_code_point(&rod_points[i]);
116
117         if (codes_and)
118                 return 1;               //1 means off screen
119
120         //clear flags for new points (not projected)
121
122         for (i=0;i<4;i++)
123                 rod_points[i].p3_flags = 0;
124
125         return 0;
126 }
127
128 //draw a polygon that is always facing you
129 //returns 1 if off screen, 0 if drew
130 bool g3_draw_rod_flat(g3s_point *bot_point,fix bot_width,g3s_point *top_point,fix top_width)
131 {
132         if (calc_rod_corners(bot_point,bot_width,top_point,top_width))
133                 return 0;
134
135         return g3_draw_poly(4,rod_point_list);
136
137 }
138
139 //draw a bitmap object that is always facing you
140 //returns 1 if off screen, 0 if drew
141 bool g3_draw_rod_tmap(grs_bitmap *bitmap,g3s_point *bot_point,fix bot_width,g3s_point *top_point,fix top_width,fix light)
142 {
143         if (calc_rod_corners(bot_point,bot_width,top_point,top_width))
144                 return 0;
145
146         uvl_list[0].l = uvl_list[1].l = uvl_list[2].l = uvl_list[3].l = light;
147
148         return g3_draw_tmap(4,rod_point_list,uvl_list,bitmap);
149 }
150
151 #ifndef __powerc
152 int checkmuldiv(fix *r,fix a,fix b,fix c);
153 #endif
154
155 #if (!(defined(D1XD3D) || defined(OGL)))
156 //draws a bitmap with the specified 3d width & height 
157 //returns 1 if off screen, 0 if drew
158 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
159 {
160 #ifndef __powerc
161         g3s_point pnt;
162         fix t,w,h;
163
164         if (g3_rotate_point(&pnt,pos) & CC_BEHIND)
165                 return 1;
166
167         g3_project_point(&pnt);
168
169         if (pnt.p3_flags & PF_OVERFLOW)
170                 return 1;
171
172         if (checkmuldiv(&t,width,Canv_w2,pnt.p3_z))
173                 w = fixmul(t,Matrix_scale.x);
174         else
175                 return 1;
176
177         if (checkmuldiv(&t,height,Canv_h2,pnt.p3_z))
178                 h = fixmul(t,Matrix_scale.y);
179         else
180                 return 1;
181
182         blob_vertices[0].x = pnt.p3_sx - w;
183         blob_vertices[0].y = blob_vertices[1].y = pnt.p3_sy - h;
184         blob_vertices[1].x = blob_vertices[2].x = pnt.p3_sx + w;
185         blob_vertices[2].y = pnt.p3_sy + h;
186
187         scale_bitmap(bm,blob_vertices,0);
188
189         return 0;
190 #else
191         g3s_point pnt;
192         fix w,h;
193         double fz;
194
195         if (g3_rotate_point(&pnt,pos) & CC_BEHIND)
196                 return 1;
197
198         g3_project_point(&pnt);
199
200         if (pnt.p3_flags & PF_OVERFLOW)
201                 return 1;
202
203         if (pnt.p3_z == 0)
204                 return 1;
205                 
206         fz = f2fl(pnt.p3_z);
207         w = fixmul(fl2f(((f2fl(width)*fCanv_w2) / fz)), Matrix_scale.x);
208         h = fixmul(fl2f(((f2fl(height)*fCanv_h2) / fz)), Matrix_scale.y);
209
210         blob_vertices[0].x = pnt.p3_sx - w;
211         blob_vertices[0].y = blob_vertices[1].y = pnt.p3_sy - h;
212         blob_vertices[1].x = blob_vertices[2].x = pnt.p3_sx + w;
213         blob_vertices[2].y = pnt.p3_sy + h;
214
215         scale_bitmap(bm,blob_vertices);
216
217         return 0;
218 #endif
219 }
220 #endif
221
222
223