1 /*
2 	sw32_rmisc.c
3 
4 	(description)
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #define NH_DEFINE
32 #include "namehack.h"
33 
34 #include "QF/cmd.h"
35 #include "QF/cvar.h"
36 #include "QF/draw.h"
37 #include "QF/render.h"
38 #include "QF/sys.h"
39 
40 #include "compat.h"
41 #include "r_internal.h"
42 #include "vid_internal.h"
43 
44 
45 static void
R_CheckVariables(void)46 R_CheckVariables (void)
47 {
48 }
49 
50 /*
51 	R_TimeRefresh_f
52 
53 	For program optimization
54 */
55 void
sw32_R_TimeRefresh_f(void)56 sw32_R_TimeRefresh_f (void)
57 {
58 	int         i;
59 	float       start, stop, time;
60 	int         startangle;
61 	vrect_t     vr;
62 
63 	startangle = r_refdef.viewangles[1];
64 
65 	start = Sys_DoubleTime ();
66 	for (i = 0; i < 128; i++) {
67 		r_refdef.viewangles[1] = i / 128.0 * 360.0;
68 
69 		VID_LockBuffer ();
70 
71 		sw32_R_RenderView ();
72 
73 		VID_UnlockBuffer ();
74 
75 		vr.x = r_refdef.vrect.x;
76 		vr.y = r_refdef.vrect.y;
77 		vr.width = r_refdef.vrect.width;
78 		vr.height = r_refdef.vrect.height;
79 		vr.next = NULL;
80 		VID_Update (&vr);
81 	}
82 	stop = Sys_DoubleTime ();
83 	time = stop - start;
84 	Sys_Printf ("%f seconds (%f fps)\n", time, 128 / time);
85 
86 	r_refdef.viewangles[1] = startangle;
87 }
88 
89 void
sw32_R_LoadSky_f(void)90 sw32_R_LoadSky_f (void)
91 {
92 	if (Cmd_Argc () != 2) {
93 		Sys_Printf ("loadsky <name> : load a skybox\n");
94 		return;
95 	}
96 
97 	sw32_R_LoadSkys (Cmd_Argv (1));
98 }
99 
100 void
sw32_R_PrintTimes(void)101 sw32_R_PrintTimes (void)
102 {
103 	float       r_time2;
104 	float       ms;
105 
106 	r_time2 = Sys_DoubleTime ();
107 
108 	ms = 1000 * (r_time2 - r_time1);
109 
110 	Sys_Printf ("%5.1f ms %3i/%3i/%3i poly %3i surf\n",
111 				ms, sw32_c_faceclip, sw32_r_polycount, sw32_r_drawnpolycount, sw32_c_surf);
112 	sw32_c_surf = 0;
113 }
114 
115 void
sw32_R_PrintAliasStats(void)116 sw32_R_PrintAliasStats (void)
117 {
118 	Sys_Printf ("%3i polygon model drawn\n", sw32_r_amodels_drawn);
119 }
120 
121 void
sw32_R_TransformFrustum(void)122 sw32_R_TransformFrustum (void)
123 {
124 	int         i;
125 	vec3_t      v, v2;
126 
127 	for (i = 0; i < 4; i++) {
128 		v[0] = sw32_screenedge[i].normal[2];
129 		v[1] = -sw32_screenedge[i].normal[0];
130 		v[2] = sw32_screenedge[i].normal[1];
131 
132 		v2[0] = v[1] * vright[0] + v[2] * vup[0] + v[0] * vpn[0];
133 		v2[1] = v[1] * vright[1] + v[2] * vup[1] + v[0] * vpn[1];
134 		v2[2] = v[1] * vright[2] + v[2] * vup[2] + v[0] * vpn[2];
135 
136 		VectorCopy (v2, sw32_view_clipplanes[i].normal);
137 
138 		sw32_view_clipplanes[i].dist = DotProduct (modelorg, v2);
139 	}
140 }
141 
142 void
sw32_TransformVector(const vec3_t in,vec3_t out)143 sw32_TransformVector (const vec3_t in, vec3_t out)
144 {
145 	out[0] = DotProduct (in, vright);
146 	out[1] = DotProduct (in, vup);
147 	out[2] = DotProduct (in, vpn);
148 }
149 
150 void
sw32_R_TransformPlane(plane_t * p,float * normal,float * dist)151 sw32_R_TransformPlane (plane_t *p, float *normal, float *dist)
152 {
153 	float       d;
154 
155 	d = DotProduct (r_origin, p->normal);
156 	*dist = p->dist - d;
157 // TODO: when we have rotating entities, this will need to use the view matrix
158 	sw32_TransformVector (p->normal, normal);
159 }
160 
161 static void
R_SetUpFrustumIndexes(void)162 R_SetUpFrustumIndexes (void)
163 {
164 	int         i, j, *pindex;
165 
166 	pindex = sw32_r_frustum_indexes;
167 
168 	for (i = 0; i < 4; i++) {
169 		for (j = 0; j < 3; j++) {
170 			if (sw32_view_clipplanes[i].normal[j] < 0) {
171 				pindex[j] = j;
172 				pindex[j + 3] = j + 3;
173 			} else {
174 				pindex[j] = j + 3;
175 				pindex[j + 3] = j;
176 			}
177 		}
178 
179 		// FIXME: do just once at start
180 		sw32_pfrustum_indexes[i] = pindex;
181 		pindex += 6;
182 	}
183 }
184 
185 void
sw32_R_SetupFrame(void)186 sw32_R_SetupFrame (void)
187 {
188 	int         edgecount;
189 	vrect_t     vrect;
190 	float       w, h;
191 
192 	// don't allow cheats in multiplayer
193 	Cvar_SetValue (r_ambient, 0);
194 	Cvar_SetValue (r_drawflat, 0);
195 
196 	if (r_numsurfs->int_val) {
197 		if ((sw32_surface_p - sw32_surfaces) > sw32_r_maxsurfsseen)
198 			sw32_r_maxsurfsseen = sw32_surface_p - sw32_surfaces;
199 
200 		Sys_Printf ("Used %ld of %ld surfs; %d max\n",
201 					(long) (sw32_surface_p - sw32_surfaces),
202 					(long) (sw32_surf_max - sw32_surfaces), sw32_r_maxsurfsseen);
203 	}
204 
205 	if (r_numedges->int_val) {
206 		edgecount = sw32_edge_p - sw32_r_edges;
207 
208 		if (edgecount > sw32_r_maxedgesseen)
209 			sw32_r_maxedgesseen = edgecount;
210 
211 		Sys_Printf ("Used %d of %d edges; %d max\n", edgecount,
212 					sw32_r_numallocatededges, sw32_r_maxedgesseen);
213 	}
214 
215 	r_refdef.ambientlight = max (r_ambient->value, 0);
216 
217 	R_CheckVariables ();
218 
219 	R_AnimateLight ();
220 	R_ClearEnts ();
221 	r_framecount++;
222 
223 	sw32_numbtofpolys = 0;
224 
225 	// debugging
226 #if 0
227 	r_refdef.vieworg[0] = 80;
228 	r_refdef.vieworg[1] = 64;
229 	r_refdef.vieworg[2] = 40;
230 	r_refdef.viewangles[0] = 0;
231 	r_refdef.viewangles[1] = 46.763641357;
232 	r_refdef.viewangles[2] = 0;
233 #endif
234 
235 	// build the transformation matrix for the given view angles
236 	VectorCopy (r_refdef.vieworg, modelorg);
237 	VectorCopy (r_refdef.vieworg, r_origin);
238 
239 	AngleVectors (r_refdef.viewangles, vpn, vright, vup);
240 	R_SetFrustum ();
241 
242 	// current viewleaf
243 	r_viewleaf = Mod_PointInLeaf (r_origin, r_worldentity.model);
244 
245 	sw32_r_dowarpold = sw32_r_dowarp;
246 	sw32_r_dowarp = r_waterwarp->int_val && (r_viewleaf->contents <=
247 										CONTENTS_WATER);
248 
249 	if ((sw32_r_dowarp != sw32_r_dowarpold) || sw32_r_viewchanged) {
250 		if (sw32_r_dowarp) {
251 			if ((vid.width <= vid.maxwarpwidth)
252 				&& (vid.height <= vid.maxwarpheight)) {
253 				vrect.x = 0;
254 				vrect.y = 0;
255 				vrect.width = vid.width;
256 				vrect.height = vid.height;
257 
258 				R_SetVrect (&vrect, &r_refdef.vrect, vr_data.lineadj);
259 				sw32_R_ViewChanged (vid.aspect);
260 			} else {
261 				w = vid.width;
262 				h = vid.height;
263 
264 				if (w > vid.maxwarpwidth) {
265 					h *= (float) vid.maxwarpwidth / w;
266 					w = vid.maxwarpwidth;
267 				}
268 
269 				if (h > vid.maxwarpheight) {
270 					h = vid.maxwarpheight;
271 					w *= (float) vid.maxwarpheight / h;
272 				}
273 
274 				vrect.x = 0;
275 				vrect.y = 0;
276 				vrect.width = (int) w;
277 				vrect.height = (int) h;
278 
279 				R_SetVrect (&vrect, &r_refdef.vrect,
280 							(int) ((float) vr_data.lineadj *
281 								   (h / (float) vid.height)));
282 				sw32_R_ViewChanged (vid.aspect * (h / w) * ((float) vid.width /
283 													   (float) vid.height));
284 			}
285 		} else {
286 			vrect.x = 0;
287 			vrect.y = 0;
288 			vrect.width = vid.width;
289 			vrect.height = vid.height;
290 
291 			r_refdef.vrect = scr_vrect;
292 			sw32_R_ViewChanged (vid.aspect);
293 		}
294 
295 		sw32_r_viewchanged = false;
296 	}
297 	// start off with just the four screen edge clip planes
298 	sw32_R_TransformFrustum ();
299 
300 	// save base values
301 	VectorCopy (vpn, base_vpn);
302 	VectorCopy (vright, base_vright);
303 	VectorCopy (vup, base_vup);
304 	VectorCopy (modelorg, base_modelorg);
305 
306 	sw32_R_SetSkyFrame ();
307 
308 	R_SetUpFrustumIndexes ();
309 
310 	r_cache_thrash = false;
311 
312 	// clear frame counts
313 	sw32_c_faceclip = 0;
314 	sw32_r_polycount = 0;
315 	sw32_r_drawnpolycount = 0;
316 	sw32_r_amodels_drawn = 0;
317 	sw32_r_outofsurfaces = 0;
318 	sw32_r_outofedges = 0;
319 
320 	sw32_D_SetupFrame ();
321 }
322