aboutsummaryrefslogtreecommitdiffstats
path: root/test/jake2/render/DebugCulling.java
blob: b85fb457dbe47f1d59f487b26cd2ecca185ebda7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * DebugCulling.java
 * Copyright (C) 2003
 *
 * $Id: DebugCulling.java,v 1.1 2004-07-07 19:59:58 hzi Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
package jake2.render;

import jake2.Defines;
import jake2.Globals;
import jake2.client.VID;
import jake2.client.cparticle_t;
import jake2.client.entity_t;
import jake2.client.lightstyle_t;
import jake2.client.particle_t;
import jake2.client.refdef_t;
import jake2.client.refexport_t;
import jake2.client.refimport_t;
import jake2.client.viddef_t;
import jake2.game.Cmd;
import jake2.game.cvar_t;
import jake2.qcommon.Cbuf;
import jake2.qcommon.Cvar;
import jake2.qcommon.FS;
import jake2.qcommon.Qcommon;
import jake2.qcommon.qfiles;
import jake2.qcommon.xcommand_t;
import jake2.sys.KBD;
import jake2.util.Lib;
import jake2.util.Math3D;
import jake2.util.Vargs;

import java.awt.Dimension;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

/**
 * DebugCulling
 *  
 * @author cwei
 */
public class DebugCulling
{

	static final float INSTANT_PARTICLE = -10000.0f;
	static final float PARTICLE_GRAVITY = 40.0f;

	String[] args;

	refexport_t re;
	refimport_t ri;
	viddef_t viddef;
	int framecount = 0;

	public DebugCulling(String[] args)
	{
		this.args = args;
	}

	public static void main(String[] args)
	{

		DebugCulling test = new DebugCulling(args);
		test.init();
		test.run();
	}

	void init()
	{

		// only for testing
		// a simple refimport_t implementation
		ri = new refimport_t()
		{
			public void Sys_Error(int err_level, String str)
			{
				VID.Error(err_level, str, null);
			}

			public void Sys_Error(int err_level, String str, Vargs vargs)
			{
				VID.Error(err_level, str, vargs);
			}

			public void Cmd_AddCommand(String name, xcommand_t cmd)
			{
				Cmd.AddCommand(name, cmd);
			}

			public void Cmd_RemoveCommand(String name)
			{
				Cmd.RemoveCommand(name);
			}

			public int Cmd_Argc()
			{
				return Cmd.Argc();
			}

			public String Cmd_Argv(int i)
			{
				return Cmd.Argv(i);
			}

			public void Cmd_ExecuteText(int exec_when, String text)
			{
				Cbuf.ExecuteText(exec_when, text);
			}

			public void Con_Printf(int print_level, String str)
			{
				VID.Printf(print_level, str, null);
			}

			public void Con_Printf(int print_level, String str, Vargs vargs)
			{
				VID.Printf(print_level, str, vargs);
			}

			public byte[] FS_LoadFile(String name)
			{
				return FS.LoadFile(name);
			}

			public int FS_FileLength(String name)
			{
				return FS.FileLength(name);
			}

			public void FS_FreeFile(byte[] buf)
			{
				FS.FreeFile(buf);
			}

			public String FS_Gamedir()
			{
				return FS.Gamedir();
			}

			public cvar_t Cvar_Get(String name, String value, int flags)
			{
				return Cvar.Get(name, value, flags);
			}

			public cvar_t Cvar_Set(String name, String value)
			{
				return Cvar.Set(name, value);
			}

			public void Cvar_SetValue(String name, float value)
			{
				Cvar.SetValue(name, value);
			}

			public boolean Vid_GetModeInfo(Dimension dim, int mode)
			{
				return VID.GetModeInfo(dim, mode);
			}

			public void Vid_MenuInit()
			{
				VID.MenuInit();
			}

			public void Vid_NewWindow(int width, int height)
			{
				VID.NewWindow(width, height);
			}

			public void updateScreenCallback()
			{
				DebugCulling.this.updateScreen();
			}
		};

		Qcommon.Init(new String[] { "$Id: DebugCulling.java,v 1.1 2004-07-07 19:59:58 hzi Exp $" });
		// sehr wichtig !!!
		VID.Shutdown();

		this.re = Renderer.getDriver("jogl", ri);

		re.Init(0, 0);

		viddef = Globals.viddef;
	}

	float fps = 0.0f;
	long start = 0;
	long startTime;

	void run()
	{
		startTime = System.currentTimeMillis();
		while (true)
		{
			re.updateScreen(null);
			KBD.Update();
//			try {
//				Thread.sleep(5);
//			}
//			catch (InterruptedException e) {
//			}
		}
	}

	int currentState = 0;

	void updateScreen()
	{
		re.BeginFrame(0.0f);
		
		switch (currentState)
		{
			case 0 :
				re.DrawStretchPic(0, 0, viddef.width, viddef.height, "conback");
				re.DrawPic(viddef.width / 2 - 50, viddef.height / 2, "loading");
				currentState = 1;
				break;
			case 1 :
				// register the map
				re.SetSky("space1", 0, new float[]{ 0, 0, 0 });
				re.BeginRegistration("ColorTest");
				re.EndRegistration();
				currentState = 2;
				//break;
			default :
				if (framecount % 500 == 0)
				{
					long time = System.currentTimeMillis();
					fps = 500000.0f / (time - start);
					start = time;
				}
				String text = fps + " fps";

				testMap();

				drawString(10, viddef.height - 16, text);
		}
		
		re.EndFrame();
		framecount++;
	}

	//		===================================================================

	private float yaw = 0;

	private float fov_x = 90;
	
	private refdef_t refdef;
	
	private void testMap()
	{

		if ( refdef == null ) {
			refdef = new refdef_t();

			refdef.x = 0;
			refdef.y = 0;
			refdef.width = viddef.width;
			refdef.height = viddef.height;
			refdef.fov_x = fov_x;
			refdef.fov_y = CalcFov(fov_x, refdef.width -10, refdef.height-10);
			refdef.vieworg = new float[] {0, 0, 0};

			refdef.viewangles[0] = 0;
			refdef.viewangles[1] = 90;
			refdef.viewangles[2] = 0;
			
			refdef.blend =  new float[] { 0.0f, 0.0f, 0.0f, 0.0f };

			refdef.areabits = null; // draw all
//			refdef.areabits = new byte[Defines.MAX_MAP_AREAS / 8];
//			Arrays.fill(refdef.areabits, (byte) 0xFF);


			refdef.num_entities = 0;
			refdef.entities = null;
			
			lightstyle_t light = new lightstyle_t();
			light.rgb = new float[] {1.0f, 1.0f, 1.0f};
			light.white = 3.0f;

			refdef.lightstyles = new lightstyle_t[Defines.MAX_LIGHTSTYLES];
			for (int i = 0; i < Defines.MAX_LIGHTSTYLES; i++)
			{
				refdef.lightstyles[i] = new lightstyle_t();
				refdef.lightstyles[i].rgb = new float[] { 1.0f, 1.0f, 1.0f };
				refdef.lightstyles[i].white = 3.0f; // r + g + b
			}

		}

		refdef.time = time() * 0.001f;

		refdef.viewangles[0] += KBD.my * 0.1f;
		refdef.viewangles[1] -= KBD.mx * 0.1f;

		refdef.vieworg[0] = 0; // + 30 * (float)Math.sin(time() * 0.0005f);
		refdef.vieworg[1] = -79;
		refdef.vieworg[2] = -131;
		
		// wichtig da aufloesung 1/8
		// --> ebenen schneiden nie genau die sicht
		refdef.vieworg[0] += 1.0f / 16;
		refdef.vieworg[1] += 1.0f / 16;
		refdef.vieworg[2] += 1.0f / 16;
		
		re.RenderFrame(refdef);
	}
	
	private float CalcFov(float fov_x, float width, float height)
	{
		double a;
		double x;

		if (fov_x < 1 || fov_x > 179)
			ri.Sys_Error(Defines.ERR_DROP, "Bad fov: " + fov_x);

		x = width / Math.tan(fov_x / 360 * Math.PI);

		a = Math.atan(height / x);

		a = a * 360 / Math.PI;

		return (float) a;
	}

	private void drawString(int x, int y, String text)
	{
		for (int i = 0; i < text.length(); i++)
		{
			re.DrawChar(x + 8 * i, y, (int) text.charAt(i));
		}
	}

	private final int time()
	{
		return (int) (System.currentTimeMillis() - startTime);
	}
	
}