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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
|
/*
* Portions Copyright (C) 2003 Sun Microsystems, Inc.
* All rights reserved.
*/
/*
*
* COPYRIGHT NVIDIA CORPORATION 2003. ALL RIGHTS RESERVED.
* BY ACCESSING OR USING THIS SOFTWARE, YOU AGREE TO:
*
* 1) ACKNOWLEDGE NVIDIA'S EXCLUSIVE OWNERSHIP OF ALL RIGHTS
* IN AND TO THE SOFTWARE;
*
* 2) NOT MAKE OR DISTRIBUTE COPIES OF THE SOFTWARE WITHOUT
* INCLUDING THIS NOTICE AND AGREEMENT;
*
* 3) ACKNOWLEDGE THAT TO THE MAXIMUM EXTENT PERMITTED BY
* APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND
* THAT NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES,
* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY
* SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
* WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
* OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
* INFORMATION, OR ANY OTHER PECUNIARY LOSS), INCLUDING ATTORNEYS'
* FEES, RELATING TO THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
*/
package demos.proceduralTexturePhysics;
import com.jogamp.opengl.util.FileUtil;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
import demos.util.Cubemap;
import gleem.linalg.Mat4f;
import gleem.linalg.Rotf;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLPbuffer;
import javax.media.opengl.glu.GLU;
import com.jogamp.opengl.util.BufferUtil;
/**
* Auxiliary Water simulation class used by ProceduralTexturePhysics
* main loop. Demonstration by NVidia Corporation.
*
* <P>
*
* Ported to Java and ARB_fragment_program by Kenneth Russell
*/
public class Water {
// Note: this class is organized differently than most of the demos
// due to the fact that it is used for two purposes: when the
// pbuffer's context is current it is used to update the cellular
// automata, and when the parent drawable's context is current it is
// used to render the water geometry (with the parent drawable's GL
// object).
private GLU glu = new GLU();
// Rendering modes
public static final int CA_FULLSCREEN_REFLECT = 0;
public static final int CA_FULLSCREEN_FORCE = 1;
public static final int CA_FULLSCREEN_HEIGHT = 2;
public static final int CA_FULLSCREEN_NORMALMAP = 3;
public static final int CA_TILED_THREE_WINDOWS = 4;
public static final int CA_DO_NOT_RENDER = 5;
private int[] initialMapDimensions = new int[2];
private TextureData initialMapData;
private String tmpSpinFilename;
private String tmpDropletFilename;
private String tmpCubeMapFilenamePrefix;
private String tmpCubeMapFilenameSuffix;
private GLPbuffer pbuffer;
private Rotf cameraOrientation = new Rotf();
// Dynamic texture names
private static final int CA_TEXTURE_FORCE_INTERMEDIATE = 0;
private static final int CA_TEXTURE_FORCE_TARGET = 1;
private static final int CA_TEXTURE_VELOCITY_SOURCE = 2;
private static final int CA_TEXTURE_VELOCITY_TARGET = 3;
private static final int CA_TEXTURE_HEIGHT_SOURCE = 4;
private static final int CA_TEXTURE_HEIGHT_TARGET = 5;
private static final int CA_TEXTURE_NORMAL_MAP = 6;
private static final int CA_NUM_DYNAMIC_TEXTURES = 7;
// List names
private static final int CA_FRAGMENT_PROGRAM_EQ_WEIGHT_COMBINE = 0;
private static final int CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_1 = 1;
private static final int CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_2 = 2;
private static final int CA_FRAGMENT_PROGRAM_APPLY_FORCE = 3;
private static final int CA_FRAGMENT_PROGRAM_APPLY_VELOCITY = 4;
private static final int CA_FRAGMENT_PROGRAM_CREATE_NORMAL_MAP = 5;
private static final int CA_FRAGMENT_PROGRAM_REFLECT = 6;
private static final int CA_DRAW_SCREEN_QUAD = 7;
private static final int CA_NUM_LISTS = 8;
// Static textures
private Texture initialMapTex;
private Texture spinTex;
private Texture dropletTex;
private Texture cubemap;
private Texture[] dynamicTextures = new Texture[CA_NUM_DYNAMIC_TEXTURES];
private int texHeightInput; // current input height texture ID.
private int texHeightOutput; // current output height texture ID.
private int texVelocityInput; // current input velocity texture ID.
private int texVelocityOutput; // current output velocity texture ID.
private int texForceStepOne; // intermediate force computation result texture ID.
private int texForceOutput; // current output force texture ID.
private int[] displayListIDs = new int[CA_NUM_LISTS];
private int vertexProgramID; // one vertex program is used to choose the texcoord offset
private int flipState; // used to flip target texture configurations.
private boolean wrap; // CA can either wrap its borders, or clamp (clamp by default)
private boolean reset = true; // are we resetting this frame? (user hit reset).
private boolean singleStep; // animation step on keypress.
private boolean animate = true; // continuous animation.
private boolean slow = true; // run slow.
private boolean wireframe; // render in wireframe mode
private boolean applyInteriorBoundaries = true; // enable / disable "boundary" image drawing.
private boolean spinLogo = true; // draw spinning logo.
private boolean createNormalMap = true; // enable / disable normal map creation.
private float perTexelWidth; // width of a texel (percentage of texture)
private float perTexelHeight; // height of a texel
private float blurDist = 0.5f; // distance over which to blur.
private boolean mustUpdateBlurOffsets; // flag indicating blurDist was set last tick
private float normalSTScale = 0.8f; // scale of normals in normal map.
private float bumpScale = 0.25f; // scale of bumps in water.
private float dropletFrequency = 0.175f; // frequency at which droplets are drawn in water...
private int slowDelay = 1; // amount (milliseconds) to delay when running slow.
private int skipInterval; // frames to skip simulation.
private int skipCount; // frame count for skipping rendering
private int angle; // angle in degrees for spinning logo
private List/*<Droplet>*/ droplets = new ArrayList/*<Droplet>*/(); // array of droplets
private int renderMode;
// Constant memory locations
private static final int CV_UV_OFFSET_TO_USE = 0;
private static final int CV_UV_T0_NO_OFFSET = 1;
private static final int CV_UV_T0_TYPE1 = 2;
private static final int CV_UV_T0_TYPE2 = 3;
private static final int CV_UV_T0_TYPE3 = 4;
private static final int CV_UV_T0_TYPE4 = 5;
private static final int CV_UV_T1_NO_OFFSET = 6;
private static final int CV_UV_T1_TYPE1 = 7;
private static final int CV_UV_T1_TYPE2 = 8;
private static final int CV_UV_T1_TYPE3 = 9;
private static final int CV_UV_T1_TYPE4 = 10;
private static final int CV_UV_T2_NO_OFFSET = 11;
private static final int CV_UV_T2_TYPE1 = 12;
private static final int CV_UV_T2_TYPE2 = 13;
private static final int CV_UV_T2_TYPE3 = 14;
private static final int CV_UV_T2_TYPE4 = 15;
private static final int CV_UV_T3_NO_OFFSET = 16;
private static final int CV_UV_T3_TYPE1 = 17;
private static final int CV_UV_T3_TYPE2 = 18;
private static final int CV_UV_T3_TYPE3 = 19;
private static final int CV_UV_T3_TYPE4 = 20;
private static final int CV_CONSTS_1 = 21;
public void initialize(String initialMapFilename,
String spinFilename,
String dropletFilename,
String cubeMapFilenamePrefix,
String cubeMapFilenameSuffix,
GLAutoDrawable parentWindow) {
GLCapabilities caps = parentWindow.getChosenGLCapabilities();
loadInitialTexture(caps.getGLProfile(), initialMapFilename);
tmpSpinFilename = spinFilename;
tmpDropletFilename = dropletFilename;
tmpCubeMapFilenamePrefix = cubeMapFilenamePrefix;
tmpCubeMapFilenameSuffix = cubeMapFilenameSuffix;
// create the pbuffer. Will use this as an offscreen rendering buffer.
// it allows rendering a texture larger than our window.
caps.setDoubleBuffered(false);
if (!GLDrawableFactory.getFactory(caps.getGLProfile()).canCreateGLPbuffer()) {
throw new GLException("Pbuffers not supported with this graphics card");
}
pbuffer = GLDrawableFactory.getFactory(caps.getGLProfile()).createGLPbuffer(caps,
null,
initialMapDimensions[0],
initialMapDimensions[1],
parentWindow.getContext());
pbuffer.addGLEventListener(new Listener());
}
public void destroy() {
if (pbuffer != null) {
pbuffer.destroy();
pbuffer = null;
}
reset = true;
}
public void tick() {
pbuffer.display();
}
public void draw(GL2 gl, Rotf cameraOrientation) {
this.cameraOrientation.set(cameraOrientation);
if (skipCount >= skipInterval && renderMode != CA_DO_NOT_RENDER) {
skipCount = 0;
// Display the results of the rendering to texture
if (wireframe) {
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
// chances are the texture will be all dark, so lets not use a texture
gl.glDisable(GL2.GL_TEXTURE_2D);
} else {
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glEnable(GL2.GL_TEXTURE_2D);
}
switch (renderMode) {
case CA_FULLSCREEN_REFLECT: {
// include bump scale...
Mat4f bscale = new Mat4f();
bscale.makeIdent();
bscale.set(0, 0, bumpScale);
bscale.set(1, 1, bumpScale);
Mat4f rot = new Mat4f();
rot.makeIdent();
rot.setRotation(cameraOrientation);
Mat4f matRot = rot.mul(bscale);
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_REFLECT]);
// Draw quad over full display
gl.glActiveTexture(GL2.GL_TEXTURE0);
dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
dynamicTextures[CA_TEXTURE_NORMAL_MAP].disable();
gl.glActiveTexture(GL2.GL_TEXTURE3);
cubemap.bind();
cubemap.enable();
gl.glColor4f(1, 1, 1, 1);
gl.glBegin(GL2.GL_QUADS);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0,0);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE1, matRot.get(0,0), matRot.get(0,1), matRot.get(0,2), 1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE2, matRot.get(1,0), matRot.get(1,1), matRot.get(1,2), 1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE3, matRot.get(2,0), matRot.get(2,1), matRot.get(2,2), 1);
gl.glVertex2f(-1,-1);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1,0);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE1, matRot.get(0,0), matRot.get(0,1), matRot.get(0,2), -1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE2, matRot.get(1,0), matRot.get(1,1), matRot.get(1,2), 1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE3, matRot.get(2,0), matRot.get(2,1), matRot.get(2,2), 1);
gl.glVertex2f( 1,-1);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1,1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE1, matRot.get(0,0), matRot.get(0,1), matRot.get(0,2), -1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE2, matRot.get(1,0), matRot.get(1,1), matRot.get(1,2), -1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE3, matRot.get(2,0), matRot.get(2,1), matRot.get(2,2), 1);
gl.glVertex2f( 1, 1);
gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0,1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE1, matRot.get(0,0), matRot.get(0,1), matRot.get(0,2), 1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE2, matRot.get(1,0), matRot.get(1,1), matRot.get(1,2), -1);
gl.glMultiTexCoord4f(GL2.GL_TEXTURE3, matRot.get(2,0), matRot.get(2,1), matRot.get(2,2), 1);
gl.glVertex2f(-1, 1);
gl.glEnd();
cubemap.disable();
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
break;
}
case CA_FULLSCREEN_NORMALMAP: {
// Draw quad over full display
gl.glActiveTexture(GL2.GL_TEXTURE0);
dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
break;
}
case CA_FULLSCREEN_HEIGHT: {
// Draw quad over full display
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightOutput);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
break;
}
case CA_FULLSCREEN_FORCE: {
// Draw quad over full display
gl.glActiveTexture(GL2.GL_TEXTURE0);
dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
break;
}
case CA_TILED_THREE_WINDOWS: {
// Draw quad over full display
// lower left
gl.glActiveTexture(GL2.GL_TEXTURE0);
dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glTranslatef(-0.5f, -0.5f, 0);
gl.glScalef(0.5f, 0.5f, 1);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glPopMatrix();
// lower right
gl.glBindTexture(GL2.GL_TEXTURE_2D, texVelocityOutput);
gl.glPushMatrix();
gl.glTranslatef(0.5f, -0.5f, 0);
gl.glScalef(0.5f, 0.5f, 1);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glPopMatrix();
// upper left
dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glTranslatef(-0.5f, 0.5f, 0);
gl.glScalef(0.5f, 0.5f, 1);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glPopMatrix();
// upper right
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightOutput);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glTranslatef(0.5f, 0.5f, 0);
gl.glScalef(0.5f, 0.5f, 1);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glPopMatrix();
break;
}
}
} else {
// skip rendering this frame
skipCount++;
}
}
public void singleStep() { singleStep = true; }
public void enableAnimation(boolean enable) { animate = enable; }
public void enableSlowAnimation(boolean enable) { slow = enable; }
public void reset() { reset = true; }
public void setRenderMode(int mode) { renderMode = mode; }
public void enableWireframe(boolean enable) { wireframe = enable; }
public void enableBorderWrapping(boolean enable) { wrap = enable; }
public void enableBoundaryApplication(boolean enable) { applyInteriorBoundaries = enable; }
public void enableSpinningLogo(boolean enable) { spinLogo = enable; }
public void setBlurDistance(float distance) { blurDist = distance;
mustUpdateBlurOffsets = true; }
public float getBlurDistance() { return blurDist; }
public void setBumpScale(float scale) { bumpScale = scale; }
public float getBumpScale() { return bumpScale; }
public void setDropFrequency(float frequency) { dropletFrequency = frequency; }
public float getDropFrequency() { return dropletFrequency; }
public static class Droplet {
private float rX;
private float rY;
private float rScale;
Droplet(float rX, float rY, float rScale) {
this.rX = rX;
this.rY = rY;
this.rScale = rScale;
}
float rX() { return rX; }
float rY() { return rY; }
float rScale() { return rScale; }
}
public synchronized void addDroplet(Droplet drop) {
droplets.add(drop);
}
//----------------------------------------------------------------------
// Internals only below this point
//
class Listener implements GLEventListener {
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
initOpenGL(gl);
}
public void dispose(GLAutoDrawable drawable) {
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
if (mustUpdateBlurOffsets) {
updateBlurVertOffset(gl);
mustUpdateBlurOffsets = false;
}
// Take a single step in the cellular automaton
// Disable culling
gl.glDisable(GL2.GL_CULL_FACE);
if (reset) {
reset = false;
flipState = 0;
}
if (animate) {
// Update the textures for one step of the simulation
doSingleTimeStep(gl);
} else if (singleStep) {
doSingleTimeStep(gl);
singleStep = false;
}
// Force rendering to pbuffer to complete
gl.glFlush();
if (slow && (slowDelay > 0) ) {
try {
Thread.sleep(slowDelay);
} catch (InterruptedException e) {
}
}
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
// Unused routines
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
}
// We need to load the initial texture file early to get the width
// and height for the pbuffer
private void loadInitialTexture(GLProfile glp, String initialMapFilename) {
try {
initialMapData = TextureIO.newTextureData(glp,
getClass().getClassLoader().getResourceAsStream(initialMapFilename),
false,
FileUtil.getFileSuffix(initialMapFilename));
} catch (IOException e) {
throw new GLException(e);
}
initialMapDimensions[0] = initialMapData.getWidth();
initialMapDimensions[1] = initialMapData.getHeight();
}
private void initOpenGL(GL2 gl) {
try {
loadTextures(gl, tmpSpinFilename, tmpDropletFilename, tmpCubeMapFilenamePrefix, tmpCubeMapFilenameSuffix);
} catch (IOException e) {
throw new GLException(e);
}
tmpSpinFilename = null;
tmpDropletFilename = null;
tmpCubeMapFilenamePrefix = null;
tmpCubeMapFilenameSuffix = null;
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-1, 1, -1, 1);
gl.glClearColor(0, 0, 0, 0);
gl.glDisable(GL2.GL_LIGHTING);
gl.glDisable(GL2.GL_DEPTH_TEST);
createAndWriteUVOffsets(gl, initialMapDimensions[0], initialMapDimensions[1]);
checkExtension(gl, "GL_ARB_vertex_program");
checkExtension(gl, "GL_ARB_fragment_program");
checkExtension(gl, "GL_ARB_multitexture");
///////////////////////////////////////////////////////////////////////////
// UV Offset Vertex Program
///////////////////////////////////////////////////////////////////////////
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
vertexProgramID = tmpInt[0];
gl.glBindProgramARB(GL2.GL_VERTEX_PROGRAM_ARB, vertexProgramID);
String programBuffer =
"!!ARBvp1.0\n" +
"# Constant memory location declarations (must match those in Java sources)\n" +
"# CV_UV_OFFSET_TO_USE = 0\n" +
"\n" +
"# CV_UV_T0_NO_OFFSET = 1\n" +
"# CV_UV_T0_TYPE1 = 2\n" +
"# CV_UV_T0_TYPE2 = 3\n" +
"# CV_UV_T0_TYPE3 = 4\n" +
"# CV_UV_T0_TYPE4 = 5\n" +
"\n" +
"# CV_UV_T1_NO_OFFSET = 6\n" +
"# CV_UV_T1_TYPE1 = 7\n" +
"# CV_UV_T1_TYPE2 = 8\n" +
"# CV_UV_T1_TYPE3 = 9\n" +
"# CV_UV_T1_TYPE4 = 10\n" +
"\n" +
"# CV_UV_T2_NO_OFFSET = 11\n" +
"# CV_UV_T2_TYPE1 = 12\n" +
"# CV_UV_T2_TYPE2 = 13\n" +
"# CV_UV_T2_TYPE3 = 14\n" +
"# CV_UV_T2_TYPE4 = 15\n" +
"\n" +
"# CV_UV_T3_NO_OFFSET = 16\n" +
"# CV_UV_T3_TYPE1 = 17\n" +
"# CV_UV_T3_TYPE2 = 18\n" +
"# CV_UV_T3_TYPE3 = 19\n" +
"# CV_UV_T3_TYPE4 = 20\n" +
"\n" +
"# CV_CONSTS_1 = 21\n" +
"\n" +
"# Parameters\n" +
"PARAM mvp [4] = { state.matrix.mvp }; # modelview projection matrix\n" +
"PARAM uvOffsetToUse = program.env[0];\n" +
"PARAM uvOffsets[20] = { program.env[1..20] };\n" +
"\n" +
"# Addresses\n" +
"ADDRESS addr;\n" +
"\n" +
"# Per vertex inputs\n" +
"ATTRIB iPos = vertex.position; #position\n" +
"\n" +
"# Outputs\n" +
"OUTPUT oPos = result.position; #position\n" +
"\n" +
"# Transform vertex-position to clip-space\n" +
"DP4 oPos.x, iPos, mvp[0];\n" +
"DP4 oPos.y, iPos, mvp[1];\n" +
"DP4 oPos.z, iPos, mvp[2];\n" +
"DP4 oPos.w, iPos, mvp[3];\n" +
"\n" +
"# Read which set of offsets to use\n" +
"ARL addr.x, uvOffsetToUse.x;\n" +
"\n" +
"# c[CV_CONSTS_1] = c[28]\n" +
"# x = 0\n" +
"# y = 0.5\n" +
"# z = 1\n" +
"# w = 2.0f\n" +
"\n" +
"# Put a scale factor into r0 so the sample points\n" +
"# can be moved farther from the texel being written\n" +
"# MOV R0, c[28].z;\n" +
"\n" +
"# Add the offsets to the input texture\n" +
"# coordinate, creating 4 sets of independent\n" +
"# texture coordinates.\n" +
"ADD result.texcoord[0], uvOffsets[addr.x ], vertex.texcoord[0];\n" +
"ADD result.texcoord[1], uvOffsets[addr.x + 5 ], vertex.texcoord[0];\n" +
"ADD result.texcoord[2], uvOffsets[addr.x + 10], vertex.texcoord[0];\n" +
"ADD result.texcoord[3], uvOffsets[addr.x + 15], vertex.texcoord[0];\n" +
"\n" +
"END\n";
// set up constants (not currently used in the vertex program, though)
float[] rCVConsts = new float[] { 0, 0.5f, 1.0f, 2.0f };
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_CONSTS_1, rCVConsts, 0);
loadProgram(gl, GL2.GL_VERTEX_PROGRAM_ARB, programBuffer);
///////////////////////////////////////////////////////////////////////////
// fragment program setup for equal weight combination of texels
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_EQ_WEIGHT_COMBINE] = gl.glGenLists(1);
initEqWeightCombine_PostMult(gl, displayListIDs[CA_FRAGMENT_PROGRAM_EQ_WEIGHT_COMBINE]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup for computing force from neighbors (step 1)
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_1] = gl.glGenLists(1);
initNeighborForceCalcStep1(gl, displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_1]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup for computing force from neighbors (step 2)
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_2] = gl.glGenLists(1);
initNeighborForceCalcStep2(gl, displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_2]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup to apply force
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_FORCE] = gl.glGenLists(1);
initApplyForce(gl, displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_FORCE]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup to apply velocity
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_VELOCITY] = gl.glGenLists(1);
initApplyVelocity(gl, displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_VELOCITY]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup to create a normal map
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_CREATE_NORMAL_MAP] = gl.glGenLists(1);
initCreateNormalMap(gl, displayListIDs[CA_FRAGMENT_PROGRAM_CREATE_NORMAL_MAP]);
///////////////////////////////////////////////////////////////////////////
// fragment program setup for dot product reflection
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_FRAGMENT_PROGRAM_REFLECT] = gl.glGenLists(1);
initDotProductReflect(gl, displayListIDs[CA_FRAGMENT_PROGRAM_REFLECT]);
///////////////////////////////////////////////////////////////////////////
// display list to render a single screen space quad.
///////////////////////////////////////////////////////////////////////////
displayListIDs[CA_DRAW_SCREEN_QUAD] = gl.glGenLists(1);
gl.glNewList(displayListIDs[CA_DRAW_SCREEN_QUAD], GL2.GL_COMPILE);
gl.glColor4f(1, 1, 1, 1);
gl.glBegin(GL2.GL_TRIANGLE_STRIP);
gl.glTexCoord2f(0, 1); gl.glVertex2f(-1, 1);
gl.glTexCoord2f(0, 0); gl.glVertex2f(-1, -1);
gl.glTexCoord2f(1, 1); gl.glVertex2f( 1, 1);
gl.glTexCoord2f(1, 0); gl.glVertex2f( 1, -1);
gl.glEnd();
gl.glEndList();
}
private void checkExtension(GL gl, String extensionName) {
if (!gl.isExtensionAvailable(extensionName)) {
throw new GLException("Unable to initialize " + extensionName + " OpenGL extension");
}
}
private void doSingleTimeStep(GL2 gl) {
int temp;
// Swap texture source & target indices & pointers
// 0 = start from initial loaded texture
// 1/2 = flip flop back and forth between targets & sources
switch (flipState) {
case 0:
texHeightInput = dynamicTextures[CA_TEXTURE_HEIGHT_SOURCE].getTextureObject(); // initial height map.
texHeightOutput = dynamicTextures[CA_TEXTURE_HEIGHT_TARGET].getTextureObject(); // next height map.
texVelocityInput = dynamicTextures[CA_TEXTURE_VELOCITY_SOURCE].getTextureObject(); // initial velocity.
texVelocityOutput = dynamicTextures[CA_TEXTURE_VELOCITY_TARGET].getTextureObject(); // next velocity.
// Clear initial velocity texture to 0x80 == gray
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
// Now we need to copy the resulting pixels into the intermediate force field texture
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texVelocityInput);
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
break;
case 1:
temp = texHeightInput;
texHeightInput = texHeightOutput;
texHeightOutput = temp;
temp = texVelocityInput;
texVelocityInput = texVelocityOutput;
texVelocityOutput = temp;
break;
case 2:
temp = texHeightInput;
texHeightInput = texHeightOutput;
texHeightOutput = temp;
temp = texVelocityInput;
texVelocityInput = texVelocityOutput;
texVelocityOutput = temp;
break;
}
// even if wireframe mode, render to texture as solid
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
/////////////////////////////////////////////////////////////
// Render first 3 components of force from three neighbors
// Offsets selected are 1 center texel for center height
// and 3 of the 4 nearest neighbors. Texture selected
// is same for all stages as we're turning height difference
// of nearest neightbor texels into a force value.
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_1]);
// set current source texture for stage 0 texture
for (int i = 0; i < 4; i++)
{
gl.glActiveTexture(GL2.GL_TEXTURE0 + i);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightInput);
gl.glEnable(GL2.GL_TEXTURE_2D);
}
int wrapMode = wrap ? GL2.GL_REPEAT : GL2.GL_CLAMP_TO_EDGE;
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, wrapMode);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, wrapMode);
// disable blending
gl.glDisable(GL2.GL_BLEND);
// render using offset 1 (type 1 -- center + 3 of 4 nearest neighbors).
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 1, 0, 0, 0);
// bind the vertex program to be used for this step and the next one.
gl.glBindProgramARB(GL2.GL_VERTEX_PROGRAM_ARB, vertexProgramID);
gl.glEnable(GL2.GL_VERTEX_PROGRAM_ARB);
// render a screen quad. with texture coords doing difference of nearby texels for force calc.
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
// Now we need to copy the resulting pixels into the intermediate force field texture
gl.glActiveTexture(GL2.GL_TEXTURE2);
dynamicTextures[CA_TEXTURE_FORCE_INTERMEDIATE].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
////////////////////////////////////////////////////////////////
// Now add in last component of force for the 4th neighbor
// that we didn't have enough texture lookups to do in the
// first pass
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_NEIGHBOR_FORCE_CALC_2]);
// Cannot use additive blending as the force contribution might
// be negative and would have to subtract from the dest.
// We must instead use an additional texture as target and read
// the previous partial 3-neighbor result into the pixel shader
// for possible subtraction
// Alphablend must be false
//; t0 = center (same as last phase)
//; t1 = 2nd axis final point (same as last phase)
//; t2 = previous partial result texture sampled at center (result of last phase copied to texture)
//; t3 = not used (disable now)
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, wrapMode);
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, wrapMode);
gl.glActiveTexture(GL2.GL_TEXTURE3);
gl.glDisable(GL2.GL_TEXTURE_2D);
// vertex program already bound.
// render using offset 2 (type 2 -- final nearest neighbor plus center of previous result).
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 2, 0, 0, 0);
// render a screen quad
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
// Now we need to copy the resulting pixels into the intermediate force field texture
gl.glActiveTexture(GL2.GL_TEXTURE1);
dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
/////////////////////////////////////////////////////////////////
// Apply the force with a scale factor to reduce it's magnitude.
// Add this to the current texture representing the water height.
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_FORCE]);
// use offsets of zero
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 0, 0, 0, 0);
// bind the vertex program to be used for this step and the next one.
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texVelocityInput);
gl.glActiveTexture(GL2.GL_TEXTURE1);
dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glActiveTexture(GL2.GL_TEXTURE2);
gl.glDisable(GL2.GL_TEXTURE_2D);
gl.glActiveTexture(GL2.GL_TEXTURE3);
gl.glDisable(GL2.GL_TEXTURE_2D);
// Draw the quad to add in force.
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
///////////////////////////////////////////////////////////////////
// With velocity texture selected, render new excitation droplets
// at random freq.
float randomFrequency = (float) Math.random();
if (dropletFrequency > randomFrequency) {
// a drop falls - decide where
Droplet drop = new Droplet(2 * ((float)Math.random() - 0.5f),
2 * ((float)Math.random() - 0.5f),
0.02f + 0.1f * ((float)Math.random()));
addDroplet(drop);
}
// Now draw the droplets:
if (!droplets.isEmpty()) {
drawDroplets(gl);
droplets.clear();
}
// Now we need to copy the resulting pixels into the velocity texture
gl.glActiveTexture(GL2.GL_TEXTURE1);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texVelocityOutput);
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
//////////////////////////////////////////////////////////////////////
// Apply velocity to position
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_APPLY_VELOCITY]);
gl.glEnable(GL2.GL_VERTEX_PROGRAM_ARB);
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightInput);
gl.glActiveTexture(GL2.GL_TEXTURE1); // velocity output already bound
gl.glEnable(GL2.GL_TEXTURE_2D);
// use offsets of zero
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 0, 0, 0, 0);
// Draw the quad to add in force.
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
// Now we need to copy the resulting pixels into the input height texture
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightInput);
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
///////////////////////////////////////////////////////////////////
// blur positions to smooth noise & generaly dampen things
// degree of blur is controlled by magnitude of 4 neighbor texel
// offsets with bilinear on
for (int i = 1; i < 4; i++) {
gl.glActiveTexture(GL2.GL_TEXTURE0 + i);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightInput);
gl.glEnable(GL2.GL_TEXTURE_2D);
}
// use offsets of 3
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 3, 0, 0, 0);
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_EQ_WEIGHT_COMBINE]);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
// Draw the logo in the water.
if (applyInteriorBoundaries) {
gl.glDisable(GL2.GL_VERTEX_PROGRAM_ARB);
drawInteriorBoundaryObjects(gl);
}
// Now we need to copy the resulting pixels into the velocity texture
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightOutput);
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
///////////////////////////////////////////////////////////////////
// If selected, create a normal map from the height
if (createNormalMap) {
createNormalMap(gl);
}
///////////////////////////////////////////////////////////
// Flip the state variable for the next round of rendering
switch (flipState) {
case 0:
flipState = 1;
break;
case 1:
flipState = 2;
break;
case 2:
flipState = 1;
break;
}
}
private void createNormalMap(GL2 gl) {
// use the height output on all four texture stages
for (int i = 0; i < 4; i++) {
gl.glActiveTexture(GL2.GL_TEXTURE0 + i);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texHeightOutput);
gl.glEnable(GL2.GL_TEXTURE_2D);
}
// Set constants for red & green scale factors (also essential color masks)
// Red mask first
float[] pixMasks = new float[] { normalSTScale, 0.0f, 0.0f, 0.0f };
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 0, pixMasks, 0);
// Now green mask & scale:
pixMasks[0] = 0.0f;
pixMasks[1] = normalSTScale;
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 1, pixMasks, 0);
gl.glCallList(displayListIDs[CA_FRAGMENT_PROGRAM_CREATE_NORMAL_MAP]);
// set vp offsets to nearest neighbors
gl.glProgramEnvParameter4fARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_OFFSET_TO_USE, 4, 0, 0, 0);
gl.glEnable(GL2.GL_VERTEX_PROGRAM_ARB);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
// Now we need to copy the resulting pixels into the normal map
gl.glActiveTexture(GL2.GL_TEXTURE0);
dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
}
private void drawInteriorBoundaryObjects(GL2 gl) {
gl.glActiveTexture(GL2.GL_TEXTURE0);
initialMapTex.bind();
initialMapTex.enable();
gl.glEnable(GL2.GL_ALPHA_TEST);
// disable other texture units.
for (int i = 1; i < 4; i++) {
gl.glActiveTexture(GL2.GL_TEXTURE0 + i);
gl.glDisable(GL2.GL_TEXTURE_2D);
}
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL2.GL_BLEND);
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
if (spinLogo) {
gl.glActiveTexture(GL2.GL_TEXTURE0);
spinTex.bind();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glRotatef(angle, 0, 0, 1);
angle += 1;
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
gl.glPopMatrix();
}
gl.glDisable(GL2.GL_ALPHA_TEST);
gl.glDisable(GL2.GL_BLEND);
}
private void loadTextures(GL gl,
String spinFilename,
String dropletFilename,
String cubeMapFilenamePrefix,
String cubeMapFilenameSuffix) throws IOException {
if (initialMapData == null) {
throw new GLException("Must call loadInitialTexture ahead of time");
}
initialMapTex = TextureIO.newTexture(initialMapData);
spinTex = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream(spinFilename), false,
FileUtil.getFileSuffix(spinFilename));
dropletTex = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream(dropletFilename), false,
FileUtil.getFileSuffix(dropletFilename));
// load the cubemap texture
cubemap = Cubemap.loadFromStreams(getClass().getClassLoader(),
cubeMapFilenamePrefix,
cubeMapFilenameSuffix,
true);
// now create dummy intermediate textures from the initial map texture
for (int i = 0; i < CA_NUM_DYNAMIC_TEXTURES; i++) {
dynamicTextures[i] = TextureIO.newTexture(initialMapData);
}
initialMapData = null;
texHeightInput = initialMapTex.getTextureObject(); // initial height map.
texHeightOutput = dynamicTextures[CA_TEXTURE_HEIGHT_TARGET].getTextureObject(); // next height map.
texVelocityInput = dynamicTextures[CA_TEXTURE_VELOCITY_SOURCE].getTextureObject(); // initial velocity.
texVelocityOutput = dynamicTextures[CA_TEXTURE_VELOCITY_TARGET].getTextureObject(); // next velocity.
}
private void createAndWriteUVOffsets(GL2 gl, int width, int height) {
// This sets vertex shader constants used to displace the
// source texture over several additive samples. This is
// used to accumulate neighboring texel information that we
// need to run the game - the 8 surrounding texels, and the
// single source texel which will either spawn or die in the
// next generation.
// Label the texels as follows, for a source texel "e" that
// we want to compute for the next generation:
//
// abc
// def
// ghi:
// first the easy one: no offsets for sampling center
// occupied or unoccupied
// Use index offset value 0.0 to access these in the
// vertex shader.
perTexelWidth = 1.0f / width;
perTexelHeight = 1.0f / height;
// Offset set 0 : center texel sampling
float[] noOffsetX = new float[] { 0, 0, 0, 0 };
float[] noOffsetY = new float[] { 0, 0, 0, 0 };
// Offset set 1: For use with neighbor force pixel shader 1
// samples center with 0, +u, -u, and +v,
// ie the 'e','d', 'f', and 'h' texels
float dist = 1.5f;
float[] type1OffsetX = new float[] { 0.0f, -dist * perTexelWidth, dist * perTexelWidth, dist * perTexelWidth };
float[] type1OffsetY = new float[] { 0.0f, dist * perTexelHeight, dist * perTexelHeight, -dist * perTexelHeight };
// Offset set 2: for use with neighbor force pixel shader 2
// samples center with 0, and -v texels
// ie the 'e' and 'b' texels
// This completes a pattern of sampling center texel and it's
// 4 nearest neighbors to run the height-based water simulation
// 3rd must be 0 0 to sample texel center from partial result
// texture.
float[] type2OffsetX = new float[] { 0.0f, -dist * perTexelWidth, 0.0f, 0.0f };
float[] type2OffsetY = new float[] { 0.0f, -dist * perTexelHeight, 0.0f, 0.0f };
// type 3 offsets
updateBlurVertOffset(gl);
/////////////////////////////////////////////////////////////
// Nearest neighbor offsets:
float[] type4OffsetX = new float[] { -perTexelWidth, perTexelWidth, 0.0f, 0.0f };
float[] type4OffsetY = new float[] { 0.0f, 0.0f, -perTexelHeight, perTexelHeight };
// write all these offsets to constant memory
for (int i = 0; i < 4; ++i) {
float noOffset[] = { noOffsetX[i], noOffsetY[i], 0.0f, 0.0f };
float type1Offset[] = { type1OffsetX[i], type1OffsetY[i], 0.0f, 0.0f };
float type2Offset[] = { type2OffsetX[i], type2OffsetY[i], 0.0f, 0.0f };
float type4Offset[] = { type4OffsetX[i], type4OffsetY[i], 0.0f, 0.0f };
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_T0_NO_OFFSET + 5 * i, noOffset, 0);
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_T0_TYPE1 + 5 * i, type1Offset, 0);
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_T0_TYPE2 + 5 * i, type2Offset, 0);
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_T0_TYPE4 + 5 * i, type4Offset, 0);
}
}
private void updateBlurVertOffset(GL2 gl) {
float[] type3OffsetX = new float[] { -perTexelWidth * 0.5f,
perTexelWidth,
perTexelWidth * 0.5f,
-perTexelWidth
};
float[] type3OffsetY = new float[] { perTexelHeight,
perTexelHeight * 0.5f,
-perTexelHeight,
-perTexelHeight * 0.5f
};
float[] offsets = new float[] { 0, 0, 0, 0 };
for (int i = 0; i < 4; ++i) {
offsets[0] = blurDist * ( type3OffsetX[i]);
offsets[1] = blurDist * ( type3OffsetY[i]);
gl.glProgramEnvParameter4fvARB(GL2.GL_VERTEX_PROGRAM_ARB, CV_UV_T0_TYPE3 + 5 * i, offsets, 0);
}
}
private synchronized void drawDroplets(GL2 gl) {
gl.glDisable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glDisable(GL2.GL_VERTEX_PROGRAM_ARB);
gl.glActiveTexture(GL2.GL_TEXTURE0);
dropletTex.bind();
dropletTex.enable();
gl.glActiveTexture(GL2.GL_TEXTURE1);
gl.glDisable(GL2.GL_TEXTURE_2D);
gl.glBlendFunc(GL2.GL_ONE, GL2.GL_ONE);
gl.glEnable(GL2.GL_BLEND);
gl.glBegin(GL2.GL_QUADS);
gl.glColor4f(1, 1, 1, 1);
for (Iterator iter = droplets.iterator(); iter.hasNext(); ) {
Droplet droplet = (Droplet) iter.next();
// coords in [-1,1] range
// Draw a single quad to the texture render target
// The quad is textured with the initial droplet texture, and
// covers some small portion of the render target
// Draw the droplet
gl.glTexCoord2f(0, 0); gl.glVertex2f(droplet.rX() - droplet.rScale(), droplet.rY() - droplet.rScale());
gl.glTexCoord2f(1, 0); gl.glVertex2f(droplet.rX() + droplet.rScale(), droplet.rY() - droplet.rScale());
gl.glTexCoord2f(1, 1); gl.glVertex2f(droplet.rX() + droplet.rScale(), droplet.rY() + droplet.rScale());
gl.glTexCoord2f(0, 1); gl.glVertex2f(droplet.rX() - droplet.rScale(), droplet.rY() + droplet.rScale());
}
gl.glEnd();
gl.glDisable(GL2.GL_BLEND);
}
//----------------------------------------------------------------------
// Inlined register combiner and texture shader programs
// (don't want to port nvparse as it's a dead-end; we'll focus on Cg instead)
private void initEqWeightCombine_PostMult(GL2 gl, int displayListID) {
// Take samples of all four texture inputs and average them,
// adding on a bias
//
// Original register combiner program:
//
// Stage 0
// rgb
// {
// discard = half_bias(tex0);
// discard = half_bias(tex1);
// spare0 = sum();
// scale_by_one_half();
// }
// Stage 1
// rgb
// {
// discard = half_bias(tex2);
// discard = half_bias(tex3);
// spare1 = sum();
// scale_by_one_half();
// }
// Stage 2
// rgb
// {
// discard = spare0;
// discard = spare1;
// spare0 = sum();
// scale_by_one_half();
// }
// Stage 3
// rgb
// {
// discard = const0;
// discard = spare0;
// spare0 = sum();
// }
float[] const0 = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM const0 = program.env[0];\n" +
"PARAM oneQtr = { 0.25, 0.25, 0.25, 0.25 };\n" +
"PARAM two = { 2.0, 2.0, 2.0, 2.0 };\n" +
"TEMP texSamp0, texSamp1, texSamp2, texSamp3;\n" +
"TEMP spare0, spare1;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"TEX texSamp2, fragment.texcoord[2], texture[2], 2D;\n" +
"TEX texSamp3, fragment.texcoord[3], texture[3], 2D;\n" +
"ADD spare0, texSamp0, texSamp1;\n" +
"ADD spare1, texSamp2, texSamp3;\n" +
"ADD spare0, spare0, spare1;\n" +
"SUB spare0, spare0, two;\n" +
"MAD result.color, oneQtr, spare0, const0;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 0, const0, 0);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initNeighborForceCalcStep1(GL2 gl, int displayListID) {
// Step one in the nearest-neighbor force calculation for height-based water
// simulation. NeighborForceCalc2 is the second step.
//
// This step takes the center point and three neighboring points, and computes
// the texel difference as the "force" acting to pull the center texel.
//
// The amount to which the computed force is applied to the texel is controlled
// in a separate shader.
// get colors from all 4 texture stages
// tex0 = center texel
// tex1 = 1st neighbor
// tex2 = 2nd neighbor - same axis as 1st neighbor point
// so force for that axis == t1 - t0 + t2 - t0
// tex3 = 3rd neighbor on other axis
// Original register combiner program:
//
// Stage 0
// rgb
// {
// //s0 = t1 - t0;
// discard = -tex0;
// discard = tex1;
// spare0 = sum();
// }
// Stage 1
// rgb
// {
// //s1 = t2 - t0;
// discard = -tex0;
// discard = tex2;
// spare1 = sum();
// }
// Stage 2
// // 'force' for 1st axis
// rgb
// {
// //s0 = s0 + s1 = t1 - t0 + t2 - t0;
// discard = spare0;
// discard = spare1;
// spare0 = sum();
// }
// Stage 3
// // one more point for 2nd axis
// rgb
// {
// //s1 = t3 - t0;
// discard = -tex0;
// discard = tex3;
// spare1 = sum();
// }
// Stage 4
// rgb
// {
// //s0 = s0 + s1 = t3 - t0 + t2 - t0 + t1 - t0;
// discard = spare0;
// discard = spare1;
// spare0 = sum();
// }
// Stage 5
// // Now add in a force to gently pull the center texel's
// // value to 0.5. The strength of this is controlled by
// // the PCN_EQ_REST_FAC - restoration factor
// // Without this, the simulation will fade to zero or fly
// // away to saturate at 1.0
// rgb
// {
// //s1 = 0.5 - t0;
// discard = -tex0;
// discard = const0;
// spare1 = sum();
// }
// Stage 6
// {
// rgb
// {
// discard = spare1 * const0;
// discard = spare0;
// spare0 = sum();
// }
// }
// Stage 7
// rgb
// {
// discard = spare0;
// discard = const0;
// spare0 = sum();
// }
float[] const0 = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM const0 = program.env[0];\n" +
"PARAM three = { 3, 3, 3, 1.0 };\n" +
"TEMP texSamp0, texSamp1, texSamp2, texSamp3;\n" +
"TEMP spare0, spare1;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"TEX texSamp2, fragment.texcoord[2], texture[2], 2D;\n" +
"TEX texSamp3, fragment.texcoord[3], texture[3], 2D;\n" +
"ADD spare0, texSamp1, texSamp2;\n" +
"MAD spare1, const0, const0, const0;\n" +
"ADD spare0, texSamp3, spare0;\n" +
"ADD spare0, spare1, spare0;\n" +
"ADD spare1, three, const0;\n" +
"MAD result.color, -spare1, texSamp0, spare0;\n" +
// Faster version which hardcodes in value of const0:
//"ADD spare0, texSamp1, texSamp2;\n" +
//"ADD spare1, texSamp3, pointSevenFive;\n" +
//"ADD spare0, spare0, spare1;\n" +
//"MAD result.color, minusThreePointFive, texSamp0, spare0;\n" +
// Straightforward port:
//"SUB spare0, texSamp1, texSamp0;\n" +
//"SUB spare1, texSamp2, texSamp0;\n" +
//"ADD spare0, spare0, spare1;\n" +
//"SUB spare1, texSamp3, texSamp0;\n" +
//"ADD spare0, spare0, spare1;\n" +
//"SUB spare1, const0, texSamp0;\n" +
//"MAD spare0, const0, spare1, spare0;\n" +
//"ADD result.color, spare0, const0;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 0, const0, 0);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initNeighborForceCalcStep2(GL2 gl, int displayListID) {
// 2nd step of force calc for render-to-texture
// water simulation.
//
// Adds the 4th & final neighbor point to the
// force calc..
//
// Bias and scale the values so 0 force is 0.5,
// full negative force is 0.0, and full pos is
// 1.0
//
// tex0 Center texel
// tex1 2nd axis neighbor point
// tex2 previous partial force amount
// Result from t1 - t0 is added to this t2
// partial result & output
// Original register combiner program:
//
// Stage 0
// last element of neighbor force
// rgb
// {
// discard = -tex0;
// discard = tex1;
// spare0 = sum();
// }
// Stage 1
// add with previous partial force amount
// rgb
// {
// discard = spare0;
// discard = tex2;
// spare0 = sum();
// }
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM const0 = program.env[0];\n" +
"TEMP texSamp0, texSamp1, texSamp2;\n" +
"TEMP spare0;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"TEX texSamp2, fragment.texcoord[2], texture[2], 2D;\n" +
"SUB spare0, texSamp1, texSamp0;\n" +
"ADD result.color, spare0, texSamp2;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initApplyForce(GL2 gl, int displayListID) {
// This shader samples t1, biases its value to a signed number, and applies this
// value multiplied by a scale factor to the t0 sample.
//
// This is used to apply a "force" texture value to a "velocity" state texture
// for nearest-neighbor height-based water simulations. The output pixel is
// the new "velocity" value to replace the t0 sample in rendering to a new
// texture which will replace the texture selected into t0.
//
// A nearly identical shader using a different scaling constant is used to
// apply the "velocity" value to a "height" texture at each texel.
//
// t1 comes in the range [0,1] but needs to hold signed values, so a value of
// 0.5 in t1 represents zero force. This is biased to a signed value in
// computing the new velocity.
//
// tex0 = previous velocity
// tex1 = force
//
// Bias the force so that 0.5 input = no change in t0 value
// and 0.0 input means -0.5 * scale change in t0 value
//
// New velocity = force * scale + previous velocity
// Original register combiner program:
//
// Stage 0
// rgb
// {
// discard = expand(tex1) * const0;
// discard = expand(tex0);
// spare0 = sum();
// scale_by_one_half();
// }
// Stage 1
// rgb
// {
// discard = spare0;
// discard = const1;
// spare0 = sum();
// }
float[] const0 = new float[] { 0.25f, 0.25f, 0.25f, 1.0f };
float[] const1 = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM const0 = program.env[0];\n" +
"PARAM const1 = program.env[1];\n" +
"PARAM one = { 1.0, 1.0, 1.0, 0.0 };\n" +
"PARAM oneHalf = { 0.5, 0.5, 0.5, 1.0 };\n" +
"PARAM two = { 2.0, 2.0, 2.0, 1.0 };\n" +
"TEMP texSamp0, texSamp1;\n" +
"TEMP spare0, spare1;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"MAD spare0, two, texSamp1, -one;\n" +
"MAD spare1, two, texSamp0, -one;\n" +
"MAD spare0, spare0, const0, spare1;\n" +
"MAD result.color, oneHalf, spare0, const1;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 0, const0, 0);
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 1, const1, 0);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initApplyVelocity(GL2 gl, int displayListID) {
// This shader samples t1, biases its value to a signed number, and applies this
// value multiplied by a scale factor to the t0 sample.
//
// This is used to apply a "velocity" texture value to a "height" state texture
// for nearest-neighbor height-based water simulations. The output pixel is
// the new "height" value to replace the t0 sample in rendering to a new
// texture which will replace the texture selected into t0.
//
// A nearly identical shader using a different scaling constant is used to
// apply the "force" value to the "velocity" texture at each texel.
//
// t1 comes in the range [0,1] but needs to hold signed values, so a value of
// 0.5 in t1 represents zero velocity. This is biased to a signed value in
// computing the new position.
//
// tex0 = height field
// tex1 = velocity
//
// Bias the force/velocity to a signed value so we can subtract from
// the t0 position sample.
//
// New height = velocity * scale factor + old height
// Original register combiner program:
//
// Stage 0
// rgb
// {
// discard = expand(tex1) * const0;
// discard = expand(tex0);
// spare0 = sum();
// scale_by_one_half();
// }
// Stage 1
// rgb
// {
// discard = spare0;
// discard = const0;
// spare0 = sum();
// }
// }
float[] const0 = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM const0 = program.env[0];\n" +
"PARAM one = { 1.0, 1.0, 1.0, 0.0 };\n" +
"PARAM oneHalf = { 0.5, 0.5, 0.5, 1.0 };\n" +
"PARAM two = { 2.0, 2.0, 2.0, 1.0 };\n" +
"TEMP texSamp0, texSamp1;\n" +
"TEMP spare0, spare1;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"MAD spare0, two, texSamp1, -one;\n" +
"MAD spare1, two, texSamp0, -one;\n" +
"MAD spare0, spare0, const0, spare1;\n" +
"MAD result.color, oneHalf, spare0, const0;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glProgramEnvParameter4fvARB(GL2.GL_FRAGMENT_PROGRAM_ARB, 0, const0, 0);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initCreateNormalMap(GL2 gl, int displayListID) {
// Neighbor-differencing for RGB normal map creation. Scale factors for s and t
// axis components are set in program code.
// This does a crude 1-s^2-t^2 calculation for the blue component in order to
// approximately normalize the RGB normal map vector. For s^2+t^2 close to 1.0,
// this is a close approximation to blue = sqrt(1 - s^2 - t^2) which would give a
// normalized vector.
// An additional pass with a dependent texture lookup (alpha-red or green-blue)
// could be used to produce an exactly normalized normal.
// colors from all 4 texture stages
// tex0 = -s, 0
// tex1 = +s, 0
// tex2 = 0, +t
// tex3 = 0, -t
// Original register combiner program:
//
// Stage 0
// rgb
// {
// // (t0 - t1)*4 : 4 for higher scale
// discard = -tex1;
// discard = tex0;
// spare0 = sum();
// scale_by_four();
// }
// Stage 1
// rgb
// {
// // (t3 - t2)*4 : 4 for higher scale
// discard = -tex2;
// discard = tex3;
// spare1 = sum();
// scale_by_four();
// }
// Stage 2
// Define const0 in the third general combiner as RGBA = (scale, 0, 0, 0)
// Where scale [0,1] is applied to reduce the magnitude
// of the s axis component of the normal.
// Define const1 in the third combiner similarly to affect the t axis component
// define these by "ramboing" them in the C++ code that uses this combiner script.
// Note: these variables have been renamed to "redMask" and "greenMask" in
// the fragment program below.
// rgb
// {
// // see comment about consts above!
// // t0 = s result in red only
// discard = spare0 * const0;
// discard = spare1 * const1;
// spare0 = sum();
// }
// Stage 3
// rgb
// {
// tex1 = spare0 * spare0;
// scale_by_two();
// }
// Stage 4
// const0 = (1, 1, 0, 0);
// rgb
// {
// spare1 = unsigned_invert(tex1) . const0;
// scale_by_one_half();
// }
// Stage 5
// const0 = (0.5, 0.5, 0, 0);
// rgb
// {
// discard = spare0;
// discard = const0;
// spare0 = sum();
// }
// Stage 6
// const0 = (0, 0, 1, 1);
// rgb
// {
// discard = spare1 * const0;
// discard = spare0;
// spare0 = sum();
// }
float[] const0 = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM redMask = program.env[0];\n" +
"PARAM greenMask = program.env[1];\n" +
"PARAM const0 = { 1.0, 1.0, 0.0, 0.0 };\n" +
"PARAM const1 = { 0.5, 0.5, 0.0, 0.0 };\n" +
"PARAM const2 = { 0.0, 0.0, 1.0, 1.0 };\n" +
"PARAM one = { 1.0, 1.0, 1.0, 0.0 };\n" +
"PARAM oneHalf = { 0.5, 0.5, 0.5, 1.0 };\n" +
"PARAM two = { 2.0, 2.0, 2.0, 1.0 };\n" +
"PARAM four = { 4.0, 4.0, 4.0, 1.0 };\n" +
"TEMP texSamp0, texSamp1, texSamp2, texSamp3;\n" +
"TEMP spare0, spare1, spare2;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"TEX texSamp1, fragment.texcoord[1], texture[1], 2D;\n" +
"TEX texSamp2, fragment.texcoord[2], texture[2], 2D;\n" +
"TEX texSamp3, fragment.texcoord[3], texture[3], 2D;\n" +
"SUB spare0, texSamp0, texSamp1;\n" +
"MUL spare0, spare0, four;\n" +
"SUB spare1, texSamp3, texSamp2;\n" +
"MUL spare1, spare1, four;\n" +
"MUL spare0, spare0, redMask;\n" +
"MAD spare0, greenMask, spare1, spare0;\n" +
"MUL_SAT spare2, spare0, spare0;\n" +
"SUB spare2, one, spare2;\n" +
"DP3 spare1, spare2, const0;\n" +
"ADD spare0, spare0, const1;\n" +
"MAD result.color, const2, spare1, spare0;\n" +
"\n" +
"END\n";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void initDotProductReflect(GL2 gl, int displayListID) {
// Pseudocode for this operation, derived from the NVidia
// texture_shader.txt documentation at
// http://oss.sgi.com/projects/ogl-sample/registry/NV/texture_shader.txt
// TEX texSamp0, fragment.texcoord[0], texture[0], 2D;
// MAD texSamp0, two, texSamp0, minusOne;
// TEMP dotPP = texSamp0 . texcoord[1];
// TEMP dotP = texSamp0 . texcoord[2];
// TEMP dotC = texSamp0 . texcoord[3];
// TEMP R, N, E;
// N = [dotPP, dotP, dotC];
// ooNLength = N dot N;
// RCP ooNLength, ooNLength;
// E = [texcoord[1].w, texcoord[2].w, texcoord[3].w];
// nDotE = N dot E;
// MUL R, nDotE, N;
// MUL R, R, two;
// MUL R, R, ooNLength;
// SUB R, R, E;
// TEX result.color, R, texture[3], CUBE;
// This fragment program is pretty length-sensitive; making it too
// big causes the frame rate to be cut in half on my machine
// (Quadro FX Go700) due to sync-to-vertical-refresh. The program
// below is more optimized in its use of temporaries. Some of the
// scaling operations on the first component of the normal vector
// (before subtracting off the E vector) don't appear to make much
// of a visual difference so they are skipped as well.
int[] tmpInt = new int[1];
gl.glGenProgramsARB(1, tmpInt, 0);
int fragProg = tmpInt[0];
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
String program =
"!!ARBfp1.0\n" +
"PARAM minusOne = { -1.0, -1.0, -1.0, 0.0 };\n" +
"PARAM two = { 2.0, 2.0, 2.0, 0.0 };\n" +
"TEMP texSamp0, R, N, E;\n" +
"\n" +
"TEX texSamp0, fragment.texcoord[0], texture[0], 2D;\n" +
"MAD texSamp0, two, texSamp0, minusOne;\n" +
"DP3 N.x, texSamp0, fragment.texcoord[1];\n" +
"DP3 N.y, texSamp0, fragment.texcoord[2];\n" +
"DP3 N.z, texSamp0, fragment.texcoord[3];\n" +
"MOV E.x, fragment.texcoord[1].w;\n" +
"MOV E.y, fragment.texcoord[2].w;\n" +
"MOV E.z, fragment.texcoord[3].w;\n" +
"MUL N, N, two;\n" +
"SUB R, N, E;\n" +
"TEX result.color, R, texture[3], CUBE;\n" +
"\n" +
"END";
loadProgram(gl, GL2.GL_FRAGMENT_PROGRAM_ARB, program);
gl.glNewList(displayListID, GL2.GL_COMPILE);
gl.glBindProgramARB(GL2.GL_FRAGMENT_PROGRAM_ARB, fragProg);
gl.glEnable(GL2.GL_FRAGMENT_PROGRAM_ARB);
gl.glEndList();
}
private void loadProgram(GL2 gl,
int target,
String programBuffer) {
gl.glProgramStringARB(target, GL2.GL_PROGRAM_FORMAT_ASCII_ARB, programBuffer.length(), programBuffer);
int[] errPos = new int[1];
gl.glGetIntegerv(GL2.GL_PROGRAM_ERROR_POSITION_ARB, errPos, 0);
if (errPos[0] >= 0) {
String kind = "Program";
if (target == GL2.GL_VERTEX_PROGRAM_ARB) {
kind = "Vertex program";
} else if (target == GL2.GL_FRAGMENT_PROGRAM_ARB) {
kind = "Fragment program";
}
System.out.println(kind + " failed to load:");
String errMsg = gl.glGetString(GL2.GL_PROGRAM_ERROR_STRING_ARB);
if (errMsg == null) {
System.out.println("[No error message available]");
} else {
System.out.println("Error message: \"" + errMsg + "\"");
}
System.out.println("Error occurred at position " + errPos[0] + " in program:");
int endPos = errPos[0];
while (endPos < programBuffer.length() && programBuffer.charAt(endPos) != '\n') {
++endPos;
}
System.out.println(programBuffer.substring(errPos[0], endPos));
throw new GLException("Error loading " + kind);
} else {
if (target == GL2.GL_FRAGMENT_PROGRAM_ARB) {
int[] isNative = new int[1];
gl.glGetProgramivARB(GL2.GL_FRAGMENT_PROGRAM_ARB,
GL2.GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB,
isNative, 0);
if (isNative[0] != 1) {
System.out.println("WARNING: fragment program is over native resource limits");
Thread.dumpStack();
}
}
}
}
}
|