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