2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Render/3D.H $
15 * Include file for 3d rendering functions
18 * Revision 1.2 2002/06/09 04:41:12 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:12 root
25 * 10 9/06/99 3:23p Andsager
26 * Make fireball and weapon expl ani LOD choice look at resolution of the
29 * 9 8/27/99 9:07p Dave
30 * LOD explosions. Improved beam weapon accuracy.
32 * 8 7/09/99 9:51a Dave
33 * Added thick polyline code.
35 * 7 7/02/99 3:05p Anoop
36 * Oops. Fixed g3_draw_2d_poly() so that it properly handles poly bitmap
37 * and LFB bitmap calls.
39 * 6 6/16/99 4:06p Dave
40 * New pilot info popup. Added new draw-bitmap-as-poly function.
42 * 5 6/03/99 6:37p Dave
43 * More TNT fun. Made perspective bitmaps more flexible.
45 * 4 5/27/99 6:17p Dave
46 * Added in laser glows.
48 * 3 4/07/99 6:22p Dave
49 * Fred and Freespace support for multiple background bitmaps and suns.
50 * Fixed link errors on all subprojects. Moved encrypt_init() to
51 * cfile_init() and lcl_init(), since its safe to call twice.
53 * 2 10/07/98 10:53a Dave
56 * 1 10/07/98 10:51a Dave
58 * 28 5/05/98 11:15p Lawrance
59 * Optimize weapon flyby sound determination
61 * 27 3/16/98 5:02p John
64 * 26 3/16/98 4:51p John
65 * Added low-level code to clip all polygons against an arbritary plane.
66 * Took out all old model_interp_zclip and used this new method instead.
68 * 25 1/29/98 9:46a John
69 * Capped debris length
71 * 24 1/06/98 6:18p John
72 * Made debris render as a blur. Had to make g3_draw_laser take tmap
75 * 23 12/30/97 6:44p John
76 * Made g3_Draw_bitmap functions account for aspect of bitmap.
78 * 22 12/15/97 11:32a John
81 * 21 10/20/97 4:49p John
82 * added weapon trails.
83 * added facing bitmap code to 3d lib.
85 * 20 7/11/97 11:54a John
86 * added rotated 3d bitmaps.
88 * 19 4/29/97 12:24p Adam
89 * JAS: Added code for delayed point to vec. Fixed some FRED
90 * sequencing problems with g3_start_frame / g3_end_frame.
92 * 18 4/29/97 9:55a John
94 * 17 4/08/97 5:18p John
95 * First rev of decent (dynamic, correct) lighting in FreeSpace.
97 * 16 3/05/97 12:49p John
98 * added Viewer_obj. Took out the interp_??? variables for turning
99 * outline,etc on and put them in flags you pass to model_render.
100 * Cleaned up model_interp code to fit new coding styles.
102 * 15 2/26/97 5:24p John
103 * Added g3_draw_sphere_ez
105 * 14 2/17/97 5:18p John
106 * Added a bunch of RCS headers to a bunch of old files that don't have
118 //flags for point structure
119 #define PF_PROJECTED 1 //has been projected, so sx,sy valid
120 #define PF_OVERFLOW 2 //can't project
121 #define PF_TEMP_POINT 4 //created during clip
123 //clipping codes flags
124 #define CC_OFF_LEFT 1
125 #define CC_OFF_RIGHT 2
128 #define CC_OFF_USER 16
129 #define CC_BEHIND 0x80
132 #define CC_OFF (CC_OFF_LEFT | CC_OFF_RIGHT | CC_OFF_BOT | CC_OFF_TOP | CC_OFF_USER | CC_BEHIND)
135 //Functions in library
137 //Frame setup functions:
138 //start the frame. Pass non-zero to enable zbuffering
139 #define g3_start_frame(zbuffer_flag) g3_start_frame_func(zbuffer_flag, __FILE__, __LINE__ )
141 // use the g3_start_frame macro instead of calling this directly.
142 extern void g3_start_frame_func(int zbuffer_flag, const char * filename, int lineno);
145 //set view from x,y,z & p,b,h, zoom. Must call one of g3_set_view_*()
146 void g3_set_view_angles(vector *view_pos,angles *view_orient,float zoom);
148 //set view from x,y,z, viewer matrix, and zoom. Must call one of g3_set_view_*()
149 void g3_set_view_matrix(vector *view_pos,matrix *view_matrix,float zoom);
152 extern matrix View_matrix; // The matrix to convert local coordinates to screen
153 extern vector View_position; // The offset to convert local coordinates to screen
155 extern matrix Light_matrix; // Used to rotate world points into current local coordinates
156 extern vector Light_base; // Used to rotate world points into current local coordinates
158 extern matrix Eye_matrix; // Where the viewer's eye is pointing in World coordinates
159 extern vector Eye_position; // Where the viewer's eye is at in World coordinates
163 void g3_end_frame(void);
166 void g3_draw_horizon(int sky_color,int ground_color);
168 //draws a line representing the horizon
169 void g3_draw_horizon_line();
171 //get vectors that are edge of horizon
172 int g3_compute_sky_polygon(float *points_2d,vector *vecs);
176 //instance at specified point with specified orientation
177 void g3_start_instance_matrix(vector *pos,matrix *orient);
179 //instance at specified point with specified orientation
180 void g3_start_instance_angles(vector *pos,angles *orient);
182 //pops the old context
183 void g3_done_instance();
185 //Misc utility functions:
187 //get current field of view. Fills in angle for x & y
188 void g3_get_FOV(float *fov_x,float *fov_y);
190 //get zoom. For a given window size, return the zoom which will achieve
191 //the given FOV along the given axis.
192 float g3_get_zoom(char axis,float fov,int window_width,int window_height);
194 //returns the normalized, unscaled view vectors
195 void g3_get_view_vectors(vector *forward,vector *up,vector *right);
197 //returns true if a plane is facing the viewer. takes the unrotated surface
198 //normal of the plane, and a point on it. The normal need not be normalized
199 int g3_check_normal_facing(vector *v,vector *norm);
201 //Point definition and rotation functions:
203 //specify the arrays refered to by the 'pointlist' parms in the following
204 //functions. I'm not sure if we will keep this function, but I need
206 //void g3_set_points(g3s_point *points,vms_vector *vecs);
208 //returns codes_and & codes_or of a list of points numbers
209 ccodes g3_check_codes(int nv,vertex **pointlist);
211 //rotates a point. returns codes. does not check if already rotated
212 ubyte g3_rotate_vertex(vertex *dest,vector *src);
214 // same as above, only ignores the current instancing
215 ubyte g3_rotate_vertex_popped(vertex *dest,vector *src);
217 //use this for stars, etc
218 ubyte g3_rotate_faraway_vertex(vertex *dest,vector *src);
222 int g3_project_vertex(vertex *point);
225 ubyte g3_project_vector(vector *p, float *sx, float *sy );
227 //rotates a point. returns codes.
228 ubyte g3_rotate_vector(vector *dest,vector *src);
230 //Codes a vector. Returns the codes of a point.
231 ubyte g3_code_vector(vector * p);
233 //calculate the depth of a point - returns the z coord of the rotated point
234 float g3_calc_point_depth(vector *pnt);
236 //From a 2d point, compute the vector through that point
237 void g3_point_to_vec(vector *v,int sx,int sy);
239 //From a 2d point, compute the vector through that point.
240 // This can be called outside of a g3_start_frame/g3_end_frame
241 // pair as long g3_start_frame was previously called.
242 void g3_point_to_vec_delayed(vector *v,int sx,int sy);
244 //code a point. fills in the p3_codes field of the point, and returns the codes
245 ubyte g3_code_vertex(vertex *point);
247 //delta rotation functions
248 vector *g3_rotate_delta_x(vector *dest,float dx);
249 vector *g3_rotate_delta_y(vector *dest,float dy);
250 vector *g3_rotate_delta_z(vector *dest,float dz);
251 vector *g3_rotate_delta_vec(vector *dest,vector *src);
252 ubyte g3_add_delta_vec(vertex *dest,vertex *src,vector *deltav);
257 //Set TMAP_FLAG_TEXTURED in the tmap_flags to texture map it with current texture.
258 //returns 1 if off screen, 0 if drew
259 int g3_draw_poly(int nv,vertex **pointlist,uint tmap_flags);
261 // Draw a polygon. Same as g3_draw_poly, but it bashes sw to a constant value
262 // for all vertexes. Needs to be done after clipping to get them all.
263 //Set TMAP_FLAG_TEXTURED in the tmap_flags to texture map it with current texture.
264 //returns 1 if off screen, 0 if drew
265 int g3_draw_poly_constant_sw(int nv,vertex **pointlist,uint tmap_flags, float constant_sw);
267 //like g3_draw_poly(), but checks to see if facing. If surface normal is
268 //NULL, this routine must compute it, which will be slow. It is better to
269 //pre-compute the normal, and pass it to this function. When the normal
270 //is passed, this function works like g3_check_normal_facing() plus
272 //Set TMAP_FLAG_TEXTURED in the tmap_flags to texture map it with current texture.
273 //returns -1 if not facing, 1 if off screen, 0 if drew
274 int g3_draw_poly_if_facing(int nv,vertex **pointlist,uint tmap_flags,vector *norm,vector *pnt);
276 //draws a line. takes two points.
277 int g3_draw_line(vertex *p0,vertex *p1);
279 // Draws a polygon always facing the viewer.
280 // compute the corners of a rod. fills in vertbuf.
281 // Verts has any needs uv's or l's or can be NULL if none needed.
282 int g3_draw_rod(vector *p0,float width1,vector *p1,float width2, vertex * verts, uint tmap_flags);
284 //draws a bitmap with the specified 3d width & height
285 //Set TMAP_FLAG_TEXTURED in the tmap_flags to texture map it with current texture.
286 //returns 1 if off screen, 0 if drew
287 // If bitmap is not square, rad will be the 3d size of the smallest dimension.
288 // orient flips the bitmap in some way. Pass 0 for normal or else pass a
289 // random nuber between 0 and 7, inclusive.
290 int g3_draw_bitmap(vertex *pos,int orient, float radius, uint tmap_flags);
292 // get bitmap dims onscreen as if g3_draw_bitmap() had been called
293 int g3_get_bitmap_dims(int bitmap, vertex *pos, float radius, int *x, int *y, int *w, int *h, int *size);
295 //draw a sortof sphere - i.e., the 2d radius is proportional to the 3d
296 //radius, but not to the distance from the eye. Uses the current 2d color.
297 int g3_draw_sphere(vertex *pnt,float rad);
299 // Same as g3_draw_sphere, but you pass a vector and this rotates
300 // and projects it and then call g3_draw_sphere.
301 int g3_draw_sphere_ez(vector *pnt,float rad);
303 //Draw a laser shaped 3d looking thing.
304 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels
305 float g3_draw_laser(vector *headp, float head_width, vector *tailp, float tail_width, uint tmap_flags = TMAP_FLAG_TEXTURED, float max_len = 0.0f );
307 // Draw a laser shaped 3d looking thing using vertex coloring (useful for things like colored laser glows)
308 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels
309 float g3_draw_laser_rgb(vector *headp, float head_width, vector *tailp, float tail_width, int r, int g, int b, uint tmap_flags = TMAP_FLAG_TEXTURED | TMAP_FLAG_RGB, float max_len = 0.0f );
311 // Draw a bitmap that is always facing, but rotates.
312 // If bitmap is not square, rad will be the 3d size of the smallest dimension.
313 int g3_draw_rotated_bitmap(vertex *pnt,float angle, float radius, uint tmap_flags);
315 // draw a perspective bitmap based on angles and radius
316 int g3_draw_perspective_bitmap(angles *a, float scale_x, float scale_y, int div_x, int div_y, uint tmap_flags);
318 // draw a 2d bitmap on a poly
319 int g3_draw_2d_poly_bitmap(int x, int y, int w, int h, uint additional_tmap_flags = 0);
321 // Enables clipping with an arbritary plane. This will be on
322 // until g3_stop_clip_plane is called or until next frame.
323 // The points passed should be relative to the instance. Probably
324 // that means world coordinates.
326 This works like any other clip plane... if this is enabled and you
327 rotate a point, the CC_OFF_USER bit will be set in the clipping codes.
328 It is completely handled by most g3_draw primitives, except maybe lines.
330 As far as performance, when enabled, it will slow down each point
331 rotation (or g3_code_vertex call) by a vector subtraction and dot
332 product. It won't slow anything down for polys that are completely
333 clipped on or off by the plane, and will slow each clipped polygon by
334 not much more than any other clipping we do.
336 void g3_start_user_clip_plane( vector *plane_point, vector *plane_normal );
338 // Stops arbritary plane clipping
339 void g3_stop_user_clip_plane();