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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
/**
* @(#) pointblast.java
* @(#) author: Mark J. Kilgard (converted to Java by Ron Cemer)
*/
/* Copyright (c) Mark J. Kilgard, 1997. */
/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */
/* This example demonstrates how to render particle effects
with OpenGL. A cloud of pinkish/orange particles explodes with the
particles bouncing off the ground. When the EXT_point_parameters
is present , the particle size is attenuated based on eye distance. */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.*;
import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;
import gl4java.applet.SimpleGLAnimApplet1;
public class pointblast extends SimpleGLAnimApplet1
{
/* Initialize the applet */
public void init()
{
super.init();
Dimension d = getSize();
canvas = new pointblastCanvas(d.width, d.height);
add("Center", canvas);
}
/* Local GLAnimCanvas extension class */
private class pointblastCanvas extends GLAnimCanvas
implements MouseListener, MouseMotionListener, ActionListener
{
private final double M_PI = 3.14159265;
private final String MENU_PAUSE = "Pause";
private final String MENU_RESUME = "Resume";
private final String MENU_RESET_TIME = "Reset time";
// private final String MENU_CONSTANT = "Constant";
// private final String MENU_LINEAR = "Linear";
// private final String MENU_QUADRATIC = "Quadratic";
private final String MENU_BLEND_ON = "Blend on";
private final String MENU_BLEND_OFF = "Blend off";
// private final String MENU_THRESHOLD_1 = "Threshold 1";
// private final String MENU_THRESHOLD_10 = "Threshold 10";
private final String MENU_POINT_SMOOTH_ON = "Point smooth on";
private final String MENU_POINT_SMOOTH_OFF = "Point smooth off";
private final String MENU_POINT_SIZE_2 = "Point size 2";
private final String MENU_POINT_SIZE_4 = "Point size 4";
private final String MENU_POINT_SIZE_8 = "Point size 8";
private final String MENU_ENABLE_SPIN = "Enable spin";
private final String MENU_DISABLE_SPIN = "Disable spin";
private final String MENU_200_POINTS = "200 points";
private final String MENU_500_POINTS = "500 points";
private final String MENU_1000_POINTS = "1000 points";
private final String MENU_2000_POINTS = "2000 points";
private final String MENU_ENABLE_TEXTURING = "Enable texturing";
private final String MENU_DISABLE_TEXTURING = "Disable texturing";
private final String MENU_LINEAR_TEXTURE_FILTERING = "Linear texture filtering";
private final String MENU_POINT_TEXTURE_FILTERING = "Point texture filtering";
private PopupMenu menu = null;
private boolean menu_showing = false;
private boolean save_suspended = false;
private float angle = -150; /* in degrees */
private boolean spin = false;
private boolean moving = false;
private int begin;
private boolean newModel = true;
private float theTime;
private boolean repeat = true;
private boolean blend = true;
private boolean texturing = true;
// private boolean useMipmaps = true;
private boolean linearFiltering = true;
private float constant[] = { 1.0f/5.0f, 0.0f, 0.0f };
private float linear[] = { 0.0f, 1.0f/5.0f, 0.0f };
private float theQuad[] = { 0.25f, 0.0f, 1.0f/60.0f };
private final int MAX_POINTS = 2000;
private int numPoints = 200;
private float pointList[][] = new float[MAX_POINTS][3];
private float pointTime[] = new float[MAX_POINTS];
private float pointVelocity[][] = new float[MAX_POINTS][2];
private float pointDirection[][] = new float[MAX_POINTS][2];
private int colorList[] = new int[MAX_POINTS];
private boolean motion = false;
private Random random = new Random(System.currentTimeMillis());
private final float MEAN_VELOCITY = 3.0f;
private final float GRAVITY = 2.0f;
private final float TIME_DELTA = 0.025f; /* The speed of time. */
/* Modeling units of ground extent in each X and Z direction. */
private final int EDGE = 12;
private final float colorSet[][] =
{
/* Shades of red. */
{ 0.7f, 0.2f, 0.4f, 0.5f },
{ 0.8f, 0.0f, 0.7f, 0.5f },
{ 1.0f, 0.0f, 0.0f, 0.5f },
{ 0.9f, 0.3f, 0.6f, 0.5f },
{ 1.0f, 0.4f, 0.0f, 0.5f },
{ 1.0f, 0.0f, 0.5f, 0.5f }
};
private final int NUM_COLORS = colorSet.length;
private final int DEAD = NUM_COLORS;
public pointblastCanvas(int w, int h)
{
super(w, h);
GLContext.gljNativeDebug = false;
GLContext.gljClassDebug = false;
setAnimateFps(30.0);
}
public void preInit()
{
doubleBuffer = true;
stereoView = false;
}
public void init()
{
System.out.println("init(): " + this);
reshape(getSize().width, getSize().height);
gl.glShadeModel(GL_FLAT);
gl.glEnable(GL_DEPTH_TEST);
gl.glEnable(GL_POINT_SMOOTH);
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.glPointSize(8.0f);
// gl.glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, theQuad);
gl.glMatrixMode(GL_PROJECTION);
glu.gluPerspective
(/* field of view in degree */ 40.0,
/* aspect ratio */ 1.0,
/* Z near */ 0.5,
/* Z far */ 40.0);
gl.glMatrixMode(GL_MODELVIEW);
glu.gluLookAt
(0.0f, 1.0f, 8.0f, /* eye location */
0.0f, 1.0f, 0.0f, /* center is at (0,0,0) */
0.0f, 1.0f, 0.0f); /* up is in postivie Y direction */
gl.glPushMatrix(); /* dummy push so we can pop on model
recalc */
makePointList();
makeFloorTexture();
glj.gljCheckGL();
menu = new PopupMenu("Options");
menu.add(MENU_PAUSE);
menu.add(MENU_RESUME);
menu.add(MENU_RESET_TIME);
// menu.add(MENU_CONSTANT);
// menu.add(MENU_LINEAR);
// menu.add(MENU_QUADRATIC);
menu.add(MENU_BLEND_ON);
menu.add(MENU_BLEND_OFF);
// menu.add(MENU_THRESHOLD_1);
// menu.add(MENU_THRESHOLD_10);
menu.add(MENU_POINT_SMOOTH_ON);
menu.add(MENU_POINT_SMOOTH_OFF);
menu.add(MENU_POINT_SIZE_2);
menu.add(MENU_POINT_SIZE_4);
menu.add(MENU_POINT_SIZE_8);
menu.add(MENU_ENABLE_SPIN);
menu.add(MENU_DISABLE_SPIN);
menu.add(MENU_200_POINTS);
menu.add(MENU_500_POINTS);
menu.add(MENU_1000_POINTS);
menu.add(MENU_2000_POINTS);
menu.add(MENU_ENABLE_TEXTURING);
menu.add(MENU_DISABLE_TEXTURING);
menu.add(MENU_LINEAR_TEXTURE_FILTERING);
menu.add(MENU_POINT_TEXTURE_FILTERING);
menu.addActionListener(this);
add(menu);
addMouseListener(this);
addMouseMotionListener(this);
}
public void doCleanup()
{
System.out.println("destroy(): " + this);
removeMouseListener(this);
removeMouseMotionListener(this);
menu.removeActionListener(this);
}
public void reshape(int width, int height)
{
gl.glViewport(0,0,width,height);
}
public void display()
{
if (glj.gljMakeCurrent() == false) return;
if (!isSuspended())
{
if (spin)
{
angle += 0.3f;
newModel = true;
}
updatePointList();
}
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (newModel) recalcModelView();
gl.glDepthMask(false);
/* Draw the floor. */
if (texturing) gl.glEnable(GL_TEXTURE_2D);
gl.glColor3f(0.5f, 1.0f, 0.5f);
gl.glBegin(GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-EDGE, -0.05f, -EDGE);
gl.glTexCoord2f(20.0f, 0.0f);
gl.glVertex3f(EDGE, -0.05f, -EDGE);
gl.glTexCoord2f(20.0f, 20.0f);
gl.glVertex3f(EDGE, -0.05f, EDGE);
gl.glTexCoord2f(0.0f, 20.0f);
gl.glVertex3f(-EDGE, -0.05f, EDGE);
gl.glEnd();
/* Allow particles to blend with each other. */
gl.glDepthMask(true);
if (blend) gl.glEnable(GL_BLEND);
if (texturing) gl.glDisable(GL_TEXTURE_2D);
gl.glBegin(GL_POINTS);
for (int i = 0; i < numPoints; i++)
{
/* Draw alive particles. */
int c = colorList[i];
if ( (c < 0) || (c >= DEAD) ) continue;
gl.glColor4fv(colorSet[colorList[i]]);
gl.glVertex3fv(pointList[i]);
}
gl.glEnd();
gl.glDisable(GL_BLEND);
glj.gljSwap();
glj.gljCheckGL();
glj.gljFree();
//if (!isSuspended()) repaint(); // Animate at full speed.
}
// Methods required for the implementation of MouseListener
public void mouseEntered(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{
}
public void mousePressed(MouseEvent evt)
{
// If user presses right mouse button within canvas area,
// suspend animation and pop up menu.
// If menu was already popped up and user presses either
// mouse button within canvas area, resume animation
// because the menu will have been removed automatically.
if (!menu_showing)
{
if ((evt.getModifiers() & evt.BUTTON3_MASK) != 0)
{
menu_showing = true;
save_suspended = isSuspended();
if (!save_suspended)
{
setSuspended(true);
repaint(100);
try
{
Thread.currentThread().sleep(200);
}
catch (Exception e)
{ }
}
menu.show(this,evt.getX(),evt.getY());
}
else
{
// Must be left button.
moving = true;
begin = evt.getX();
}
}
else
{
menu_showing = false;
setSuspended(save_suspended);
}
}
public void mouseReleased(MouseEvent evt)
{
if ((evt.getModifiers() & evt.BUTTON3_MASK) == 0)
{
// Must be left button.
moving = false;
}
}
public void mouseClicked(MouseEvent evt)
{
}
// Methods required for the implementation of MouseMotionListener
public void mouseDragged(MouseEvent evt)
{
if (moving)
{
int x = evt.getX();
angle = angle + (x - begin);
begin = x;
newModel = true;
if (isSuspended()) repaint();
}
}
public void mouseMoved(MouseEvent evt)
{
}
// Method required for the implementation of ActionListener
public void actionPerformed(ActionEvent evt)
{
if (glj.gljMakeCurrent() == false) return;
boolean dorepaint = false;
String c = evt.getActionCommand();
if (c.equals(MENU_PAUSE))
{
if (menu_showing)
save_suspended = true;
else if (!isSuspended())
setSuspended(true);
dorepaint = true;
}
else if (c.equals(MENU_RESUME))
{
if (menu_showing)
save_suspended = false;
else if (isSuspended())
setSuspended(false);
dorepaint = true;
}
else if (c.equals(MENU_RESET_TIME))
{
makePointList();
dorepaint = true;
}
// else if (c.equals(MENU_CONSTANT))
// {
// gl.glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, constant);
// dorepaint = true;
// }
// else if (c.equals(MENU_LINEAR))
// {
// gl.glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, linear);
// dorepaint = true;
// }
// else if (c.equals(MENU_QUADRATIC))
// {
// gl.glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, theQuad);
// dorepaint = true;
// }
else if (c.equals(MENU_BLEND_ON))
{
blend = true;
dorepaint = true;
}
else if (c.equals(MENU_BLEND_OFF))
{
blend = false;
dorepaint = true;
}
// else if (c.equals(MENU_THRESHOLD_1))
// {
// gl.glPointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT, 1.0f);
// dorepaint = true;
// }
// else if (c.equals(MENU_THRESHOLD_10))
// {
// gl.glPointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT, 10.0f);
// dorepaint = true;
// }
else if (c.equals(MENU_POINT_SMOOTH_ON))
{
gl.glEnable(GL_POINT_SMOOTH);
dorepaint = true;
}
else if (c.equals(MENU_POINT_SMOOTH_OFF))
{
gl.glDisable(GL_POINT_SMOOTH);
dorepaint = true;
}
else if (c.equals(MENU_POINT_SIZE_2))
{
gl.glPointSize(2.0f);
dorepaint = true;
}
else if (c.equals(MENU_POINT_SIZE_4))
{
gl.glPointSize(4.0f);
dorepaint = true;
}
else if (c.equals(MENU_POINT_SIZE_8))
{
gl.glPointSize(8.0f);
dorepaint = true;
}
else if (c.equals(MENU_ENABLE_SPIN))
{
spin = true;
dorepaint = true;
}
else if (c.equals(MENU_DISABLE_SPIN))
{
spin = false;
dorepaint = true;
}
else if (c.equals(MENU_200_POINTS))
{
numPoints = 200;
makePointList();
dorepaint = true;
}
else if (c.equals(MENU_500_POINTS))
{
numPoints = 500;
makePointList();
dorepaint = true;
}
else if (c.equals(MENU_1000_POINTS))
{
numPoints = 1000;
makePointList();
dorepaint = true;
}
else if (c.equals(MENU_2000_POINTS))
{
numPoints = 2000;
makePointList();
dorepaint = true;
}
else if (c.equals(MENU_ENABLE_TEXTURING))
{
texturing = true;
dorepaint = true;
}
else if (c.equals(MENU_DISABLE_TEXTURING))
{
texturing = false;
dorepaint = true;
}
else if (c.equals(MENU_LINEAR_TEXTURE_FILTERING))
{
linearFiltering = true;
makeFloorTexture();
dorepaint = true;
}
else if (c.equals(MENU_POINT_TEXTURE_FILTERING))
{
linearFiltering = false;
makeFloorTexture();
dorepaint = true;
}
glj.gljFree();
if (menu_showing)
{
menu_showing = false;
setSuspended(save_suspended);
}
if ( (isSuspended()) && (dorepaint) ) repaint();
}
private float float_rand()
{
return random.nextFloat();
}
private float RANDOM_RANGE(float lo, float hi)
{
return lo + ((hi - lo) * float_rand());
}
private void makePointList()
{
float angle, velocity, direction;
motion = true;
for (int i = 0; i < numPoints; i++)
{
pointList[i][0] = 0.0f;
pointList[i][1] = 0.0f;
pointList[i][2] = 0.0f;
pointTime[i] = 0.0f;
angle = (RANDOM_RANGE(60.0f, 70.0f)) * ((float)M_PI)/180.0f;
direction = RANDOM_RANGE(0.0f, 360.0f) * ((float)M_PI)/180.0f;
pointDirection[i][0] = (float)Math.cos(direction);
pointDirection[i][1] = (float)Math.sin(direction);
velocity = MEAN_VELOCITY + RANDOM_RANGE(-0.8f, 1.0f);
pointVelocity[i][0] = velocity * (float)Math.cos(angle);
pointVelocity[i][1] = velocity * (float)Math.sin(angle);
colorList[i] = random.nextInt() % NUM_COLORS;
}
theTime = 0.0f;
}
private void updatePointList()
{
float distance;
motion = false;
for (int i = 0; i < numPoints; i++)
{
distance = pointVelocity[i][0] * theTime;
/* X and Z */
pointList[i][0] = pointDirection[i][0] * distance;
pointList[i][2] = pointDirection[i][1] * distance;
/* Z */
pointList[i][1] =
(pointVelocity[i][1] - 0.5f * GRAVITY * pointTime[i])*pointTime[i];
/* If we hit the ground, bounce the point upward again. */
if (pointList[i][1] <= 0.0f)
{
if (distance > EDGE)
{
/* Particle has hit ground past the distance duration of
the particles. Mark particle as dead. */
colorList[i] = DEAD; /* Not moving. */
continue;
}
pointVelocity[i][1] *= 0.8f; /* 80% of previous up velocity. */
pointTime[i] = 0.0f; /* Reset the particles sense of up time. */
}
motion = true;
pointTime[i] += TIME_DELTA;
}
theTime += TIME_DELTA;
if ( (repeat) && (!motion) ) makePointList();
}
private void recalcModelView()
{
gl.glPopMatrix();
gl.glPushMatrix();
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);
newModel = false;
}
private void makeFloorTexture()
{
/* Nice floor texture tiling pattern. */
String circles[] =
{
"....xxxx........",
"..xxxxxxxx......",
".xxxxxxxxxx.....",
".xxx....xxx.....",
"xxx......xxx....",
"xxx......xxx....",
"xxx......xxx....",
"xxx......xxx....",
".xxx....xxx.....",
".xxxxxxxxxx.....",
"..xxxxxxxx......",
"....xxxx........",
"................",
"................",
"................",
"................"
};
byte floorTexture[] = new byte[16*16*3];
/* Setup RGB image for the texture. */
int idx = 0;
for (int t = 0; t < 16; t++)
{
for (int s = 0; s < 16; s++)
{
if (circles[t].charAt(s) == 'x')
{
/* Nice blue. */
floorTexture[idx++] = (byte)0x1f;
floorTexture[idx++] = (byte)0x1f;
floorTexture[idx++] = (byte)0x8f;
}
else
{
/* Light gray. */
floorTexture[idx++] = (byte)0xca;
floorTexture[idx++] = (byte)0xca;
floorTexture[idx++] = (byte)0xca;
}
}
}
gl.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// if (useMipmaps)
// {
// gl.glTexParameteri
// (GL_TEXTURE_2D,
// GL_TEXTURE_MIN_FILTER,
// GL_LINEAR_MIPMAP_LINEAR);
// glu.gluBuild2DMipmaps
// (GL_TEXTURE_2D,3,16,16,GL_RGB,GL_UNSIGNED_BYTE,floorTexture);
// }
// else
{
if (linearFiltering)
{
gl.glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
gl.glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl.glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
gl.glTexImage2D
(GL_TEXTURE_2D,0,3,16,16,0,GL_RGB,GL_UNSIGNED_BYTE,floorTexture);
}
}
}
}
|