/
View.java
2339 lines (1793 loc) · 81.5 KB
/
View.java
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
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.fixedfunc.GLLightingFunc;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.ImmModeSink;
import com.jogamp.opengl.util.awt.TextRenderer;
import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.util.glsl.ShaderState;
import java.awt.*;
import java.awt.event.MouseEvent;
public class View implements Runnable, GLEventListener {
private static int latticeSizeX = Wilsim.m.lattice_size_x;
private static int latticeSizeY = Wilsim.m.lattice_size_y;
public final GLCanvas canvas;
private final GLCapabilities caps;
private final ImmModeSink immModeSink = ImmModeSink.createFixed(Wilsim.m.oneDimSize,
3, GL.GL_FLOAT, // vertex
3, GL.GL_FLOAT, // color
3, GL.GL_FLOAT, // normal
0, GL.GL_FLOAT, // texCoords
GL.GL_DYNAMIC_DRAW);
private GL2 gl;
private static float[][][] topoColor;
private final CondVar newComputation;
private boolean newData;
private boolean newParams;
private boolean newMode;
private boolean newUI;
private GLUT glut;
// Modes
final static public int SPIN_MODE = 0;
final static public int PROFILE_MODE = 1;
final static public int FLY_MODE = 2; // Someday
final static public int XVISUALIZER_MODE = 3;
final static public int RIVER_PROFILE_MODE = 4;
private int viewMode = SPIN_MODE;
// Camera parameters
private final float[] cameraEye = {1, 0, 0};
private final float[] cameraAt = {0, 0, 0};
private final float[] cameraUp = {0, 1, 0};
private final float cameraFOV;
private final float cameraNear;
private final float cameraFar;
final private float cameraRadius = 32000.0F;
private ShaderState st;
private GLArrayDataServer colors;
private final float[] world2cam = new float[16];
private final float[] lightPosition = new float[4];
// X and Y screen extents at cameraNear distance
// Useful for positioning UI widgets
private float screenX;
private float screenY;
// Material parameters
private final float[] backgroundColor = {1.0f, 1.0f, 1.0f, 1.0f};
private final float[] directLight = {0.8f, 0.8f, 0.8f, 0.8f};
private final float[] ambientLight = {0.2f, 0.2f, 0.2f, 1.0f};
final private float gridHorizontalScaleFactor = 30.0f;
// for visualization - change later to take into account true
// ground scale factor and then throw in proper vertical scaling.
final private float gridHorizontalSpacingFactor = 720.0f;
// Spacing between points on ground
// Needed for cross section output.
// Intermediate viewing control parameters
private float viewLongitude;
private float viewLatitude;
// For text labels
private TextRenderer vertScaleTextEngine;
private TextRenderer scoreTextEngine;
private TextRenderer CompassTextEngine;
private boolean repaint;
private final float COLOR_MAX_HEIGHT = 3000.0f;
// private final float COLOR_MIN_HEIGHT = 1200.0f;
private final float COLOR_MIN_HEIGHT = 1400.0f;
private final float[] vertScaleSample = {1400.0f, 2000.0f, 2500.0f, 3000.0f};
private final int nVertScaleSamples = 4;
// Mouse state
private boolean buttonDownFlag = false;
private int mouseStartX, mouseStartY;
private int mouseEndX, mouseEndY;
private final float[] mouse2Data = new float[4];
public XSection tempXSection = new XSection();;
// XSection information
final private float profileScale = 1.1f; // For borders in profile view
public View() {
// System.out.println("View: View()\n");
// Log not available here
GLProfile glp = GLProfile.getDefault();
caps = new GLCapabilities(glp);
caps.setDoubleBuffered(true);
caps.setRedBits(8);
caps.setGreenBits(8);
caps.setBlueBits(8);
caps.setAlphaBits(8);
caps.setDepthBits(32);
caps.setHardwareAccelerated(true);
canvas = new GLCanvas(caps);
ViewEventHandler v = new ViewEventHandler();
canvas.addMouseListener(v);
canvas.addMouseMotionListener(v);
// Initialize camera parameters
viewLongitude = 180.0f;
viewLatitude = 45.0f;
cameraEye[0] = 0.0f;
cameraEye[1] = -cameraRadius;
cameraEye[2] = 0.0f;
cameraAt[0] = 0.0f;
cameraAt[1] = 0.0f;
cameraAt[2] = 0.0f;
cameraUp[0] = 0.0f;
cameraUp[1] = 0.0f;
cameraUp[2] = 1.0f;
cameraFOV = 50;
cameraNear = 2000.0f;
cameraFar = 50000.0f;
lightPosition[0] = cameraRadius;
lightPosition[1] = cameraRadius;
lightPosition[2] = cameraRadius;
lightPosition[3] = 0.0f;
// Create communication locks
newComputation = new CondVar(false);
newData = false;
newParams = true;
newMode = true;
newUI = false;
}
// inner class for handling events
private class ViewEventHandler extends ViewAdapter {
@Override
public void mousePressed(MouseEvent m) {
/* Wilsim.i.log.append("View : button_down() (" + m.getX() + ", "
+ m.getY() + ")\n");
*/
mouseEndX = mouseStartX = m.getX();
mouseEndY = mouseStartY = m.getY();
switch (viewMode) {
case SPIN_MODE:
return;
case PROFILE_MODE:
tempXSection.startX =
(int) (mouseStartX * mouse2Data[0] + mouse2Data[1]);
tempXSection.startY =
(int) (mouseStartY * mouse2Data[2] + mouse2Data[3]);
// Check for bounds
if (tempXSection.startX < 0
|| tempXSection.startX >= latticeSizeX)
return;
if (tempXSection.startY < 0
|| tempXSection.startY >= latticeSizeY)
return;
// For future implementation -- clipping of line outside
// bounds would be better.
/* Wilsim.i.log.append("View : button_down() mouse("
+ m.getX() + ", "+ m.getY() + ")\n");
Wilsim.i.log.append("View : button_down() data("
+ tempXSection.startX + ", "
+ tempXSection.startY + ")\n");
*/
break;
default:
return;
}
buttonDownFlag = true;
synchronized (newComputation) {
newUI = true;
newComputation.boolVal = true;
newComputation.notify();
}
}
@Override
public void mouseReleased(MouseEvent m) {
/* Wilsim.i.log.append("View : button_up() (" + m.getX() + ", "
+ m.getY() + ")\n"); */
mouseEndX = m.getX();
mouseEndY = m.getY();
switch (viewMode) {
case SPIN_MODE:
break;
case PROFILE_MODE:
if (!buttonDownFlag) return; // Invalid start point
tempXSection.endX =
(int) (mouseEndX * mouse2Data[0] + mouse2Data[1]);
tempXSection.endY =
(int) (mouseEndY * mouse2Data[2] + mouse2Data[3]);
// Check for bounds
if (tempXSection.startX < 0
|| tempXSection.endX >= latticeSizeX)
break;
if (tempXSection.startY < 0
|| tempXSection.endY >= latticeSizeY)
break;
// For future implementation -- clipping of line outside
// bounds would be better.
/*
Wilsim.i.log.append("View : button_up() mouse("
+ m.getX() + ", "+ m.getY() + ")\n");
Wilsim.i.log.append("View : button_up() data("
+ tempXSection.endX + ", "
+ tempXSection.endY + ")\n");
*/
// For future implementation -- clipping of line outside
// bounds would be better.
// Make a profile
int p_index = XSectionManager.addXSection();
XSection p = XSectionManager.getXSection(p_index);
p.startX = tempXSection.startX;
p.startY = tempXSection.startY;
p.endX = tempXSection.endX;
p.endY = tempXSection.endY;
break;
default:
break;
}
buttonDownFlag = false;
synchronized (newComputation) {
newUI = true;
newComputation.boolVal = true;
newComputation.notify();
}
}
@Override
public void mouseDragged(MouseEvent m) {
mouseEndX = m.getX();
mouseEndY = m.getY();
switch (viewMode) {
case SPIN_MODE:
return;
case PROFILE_MODE:
/* Wilsim.i.log.append("View : move() (" + m.getX() + ", "
+ m.getY() + ")\n");
*/
if (!buttonDownFlag) {
// Shouldn't happen unless events are processed
// out of order
Wilsim.i.log.append("View : move() : out of order\n");
return;
}
{
// Check for bounds
// For future implementation -- clipping of line outside
// bounds would be better.
}
break;
default:
return;
}
synchronized (newComputation) {
newUI = true;
newComputation.boolVal = true;
newComputation.notify();
}
}
}
public void init(final GLAutoDrawable glautodrawable) {;
// Wilsim.i.log.append("View : init()\n");
glut = new GLUT();
repaint = false;
gl = glautodrawable.getGL().getGL2();
//gl = glautodrawable.getGL().getGL2ES1();
gl.glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
if( !gl.hasGLSL() ) {
/* System.err.println("No GLSL available, no rendering.");
st = new ShaderState();
st.setVerbose(true);
final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader",
"shader/bin", "RedSquareShader", true);
final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader",
"shader/bin", "RedSquareShader", true);
vp0.defaultShaderCustomization(gl, true, true);
fp0.defaultShaderCustomization(gl, true, true);
final ShaderProgram sp0 = new ShaderProgram();
sp0.add(gl, vp0, System.err);
sp0.add(gl, fp0, System.err);
st.attachShaderProgram(gl, sp0, true);
// Allocate Color Array
colors= GLArrayDataServer.createGLSL("mgl_Color", 4, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1);
colors.putf(0); colors.putf(0); colors.putf(1); colors.putf(1);
colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1);
colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1);
colors.seal(gl, true);
st.ownAttribute(colors, true);
colors.enableBuffer(gl, false);*/
}
else
System.err.println("GLSL available");
float matDiffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
float matSpecular[] = {0.01f, 0.01f, 0.01f, 1.0f};
float matShininess[] = {30.0f, 30.0f, 30.0f, 1.0f};
gl.glShadeModel(GLLightingFunc.GL_SMOOTH);
gl.glMaterialfv(GL2.GL_FRONT, GLLightingFunc.GL_DIFFUSE,
matDiffuse, 0);
gl.glMaterialfv(GL2.GL_FRONT, GLLightingFunc.GL_SPECULAR,
matSpecular, 0);
gl.glMaterialfv(GL2.GL_FRONT, GLLightingFunc.GL_SHININESS,
matShininess, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE,
directLight, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR,
directLight, 0);
//gl.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE, 1);
gl.glLightModelf(GL2.GL_LIGHT_MODEL_TWO_SIDE, 1f);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT,
ambientLight, 0);
gl.glEnable(GLLightingFunc.GL_NORMALIZE);
// gl.glEnable(gl.GL_LIGHTING);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glEnable(GLLightingFunc.GL_LIGHT0);
gl.glEnable(GL2.GL_DEPTH_TEST);
// Set up text rendering capability
vertScaleTextEngine = new TextRenderer(new Font("SansSerif", Font.PLAIN, 14));
scoreTextEngine = new TextRenderer(new Font("SanSerif", Font.PLAIN, 28));
CompassTextEngine = new TextRenderer(new Font("SanSerif", Font.PLAIN, 20));
// Initialize XSection Manager
XSectionManager.init();
}
public void initModel() {
// Wilsim.i.log.append("View: initModel()\n");
latticeSizeX = Model.lattice_size_x;
latticeSizeY = Model.lattice_size_y;
//topoColor = new float[latticeSizeX + 1][latticeSizeY + 1][3];
/*String str = latticeSizeX + "x" + latticeSizeY;*/
// Wilsim.i.log.append("View: initModel(): " + str + '\n');
newComputation.boolVal = false;
}
public void loadModel(float[][] array) {
// System.out.println("View: loadModel()\n");
// Don't coppy an array in a for loop. Very slow!
/* for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++)
topo[i][j] = array[i][j];*/
//topo = array.clone(); // <== this should be 4x faster
//for(int j = 0; j <= latticeSizeX; j++)
//System.arraycopy(array[j], 0, topo[j], 0, array[0].length); //even faster
/* for (int j = 1; j <= Model.lattice_size_y; j++) {
for (int i = 1; i <= Model.lattice_size_x; i++) {
float t = (topo[i][j] - COLOR_MIN_HEIGHT) / (COLOR_MAX_HEIGHT - COLOR_MIN_HEIGHT);
// Clamp to range, just in case
if (t < 0.0) t = 0.0f;
if (t > 1.0) t = 1.0f;
if (t < 0.5 *//*&& t > 0.01*//*) {
topoColor[i][j][0] = 0.488f + t * 0.836f;
topoColor[i][j][1] = 0.164f + t * 0.953f;
topoColor[i][j][2] = 0.094f + t * 0.172f;
}
else {
topoColor[i][j][0] = 0.906f + (t - 0.5f) * 0.180f;
topoColor[i][j][1] = 0.640f + (t - 0.5f) * 0.680f;
topoColor[i][j][2] = 0.180f + (t - 0.5f) * 0.773f;
}
}
}*/
/* synchronized (newComputation) {
newData = true;
newComputation.boolVal = true;
newComputation.notify();
}*/
}
public void newComp(){
synchronized (newComputation) {
newData = true;
newComputation.boolVal = true;
newComputation.notify();
}
}
public void changeViewMode(int v) {
synchronized (newComputation) {
switch (v) {
case SPIN_MODE:
viewMode = SPIN_MODE;
break;
case PROFILE_MODE:
viewMode = PROFILE_MODE;
break;
case XVISUALIZER_MODE:
viewMode = XVISUALIZER_MODE;
break;
case RIVER_PROFILE_MODE:
viewMode = RIVER_PROFILE_MODE;
break;
default: //Unrecognized view mode - do nothing
break;
}
newMode = true;
newComputation.boolVal = true;
newComputation.notify();
}
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
// System.out.println("View : display(drawable)\n");
draw(drawable);
}
private void drawprofileMode() {
drawXSectionMode();
gl.glEnable(GLLightingFunc.GL_LIGHTING);
// Position lights
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
// Recenter and resposition grid
// gl.glRotatef(180f, 1.0f, 1.0f, .0f);
gl.glScalef(gridHorizontalScaleFactor, -gridHorizontalScaleFactor +2, 1.0f);
gl.glTranslatef(-latticeSizeX / 2, -latticeSizeY / 2, -1900);
// Z translation is currently a hack based on the grid. Roughly 1800 m for
// the Grand Canyon
drawTerrain();
gl.glDisable(GLLightingFunc.GL_LIGHTING);
drawXSections();
// Now draw UI stuff on top
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0f, canvas.getWidth(), 0.0f, canvas.getHeight(), 1.0f, 2.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
drawUI();
}
private void draw(GLAutoDrawable draw) {
// System.out.println("View: draw()\n");
gl.glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glColor3f(0.8f, 0.8f, 0.8f);
// Move more and more into separate drawing modes
// as functionality diverges
if (viewMode == PROFILE_MODE) drawprofileMode();
else if (viewMode == SPIN_MODE) drawSpinMode();
else if (viewMode == RIVER_PROFILE_MODE) drawRP();
else drawXVISUALIZER_MODE();
synchronized (newComputation) {
newData = false;
newComputation.boolVal = false; // ?? Should this be elsewhere?
}
gl.glFlush();
}
private void drawSpinMode() {
// Move more and more in here as draw mode methods diverge
if (newParams || newMode) {
computeView(viewLongitude, viewLatitude);
// Cleanup
newParams = false;
newMode = false;
}
// Set up 3D view
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustum(-screenX, screenX, -screenY, screenY,
cameraNear, cameraFar);
// Set up 3D rendering
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMultMatrixf(world2cam, 0);
// Spin mode
gl.glEnable(GLLightingFunc.GL_LIGHTING);
// Position lights
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
// Recenter and resposition grid
// gl.glRotatef(180f, 1.0f, 1.0f, .0f);
gl.glScalef(gridHorizontalScaleFactor, -gridHorizontalScaleFactor, 1.0f);
gl.glTranslatef(-latticeSizeX / 2, -latticeSizeY / 2, -1800);
// Z translation is currently a hack based on the grid. Roughly 1800 m for
// the Grand Canyon
drawTerrain();
gl.glDisable(GLLightingFunc.GL_LIGHTING);
drawXSections();
// Now draw UI stuff on top
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0f, canvas.getWidth(), 0.0f, canvas.getHeight(), 1.0f, 2.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
drawVertScale();
drawUI();
immModeSink.draw(gl, true);
}
private void drawXSectionMode() {
// Move more and more in here as draw mode methods diverge
if (newParams || newMode) {
float aspect = ((float) canvas.getWidth()) / canvas.getHeight();
float data_aspect = ((float) latticeSizeX + 1) / (latticeSizeY + 1);
if (aspect > data_aspect) {
// Wide screen
screenY = ((float) latticeSizeY + 1) / 2.0f
* gridHorizontalScaleFactor * profileScale;
screenX = screenY * aspect;
} else {
// Tall screen
screenX = ((float) latticeSizeX + 1) / 2.0f
* gridHorizontalScaleFactor * profileScale;
screenY = screenX / aspect;
}
computeXSectionView();
newParams = true;
newMode = true;
}
drawVertScale();
// Set up orthogonal view
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-screenX, screenX, -screenY, screenY,
cameraNear, cameraFar);
// Set up 3D rendering
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMultMatrixf(world2cam, 0);
}
private void drawXVISUALIZER_MODE() {
gl.glEnable(GLLightingFunc.GL_LIGHTING);
// Position lights
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
// Recenter and resposition grid
// gl.glRotatef(180f, 1.0f, 1.0f, .0f);
gl.glScalef(gridHorizontalScaleFactor, -gridHorizontalScaleFactor, 1.0f);
gl.glTranslatef(-latticeSizeX / 2, -latticeSizeY / 2, -1800);
// Z translation is currently a hack based on the grid. Roughly 1800 m for
// the Grand Canyon
//drawTerrain();
gl.glDisable(GLLightingFunc.GL_LIGHTING);
//drawXSections();
// Now draw UI stuff on top
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0f, canvas.getWidth(), 0.0f, canvas.getHeight(), 1.0f, 2.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
drawScaleBar();
drawXSectionVertScale();
drawUI();
}
private void drawRP() {
gl.glEnable(GLLightingFunc.GL_LIGHTING);
// Position lights
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
// Recenter and resposition grid
// gl.glRotatef(180f, 1.0f, 1.0f, .0f);
gl.glScalef(gridHorizontalScaleFactor, -gridHorizontalScaleFactor, 1.0f);
gl.glTranslatef(-latticeSizeX / 2, -latticeSizeY / 2, -1800);
// Z translation is currently a hack based on the grid. Roughly 1800 m for
// the Grand Canyon
//drawTerrain();
gl.glDisable(GLLightingFunc.GL_LIGHTING);
drawXSections();
// Now draw UI stuff on top
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0f, canvas.getWidth(), 0.0f, canvas.getHeight(), 1.0f, 2.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
drawRIVER_PROFILE_MODE();
drawUI();
}
private void drawRIVER_PROFILE_MODE() {
gl.glEnable(GLLightingFunc.GL_LIGHTING);
// Position lights
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
// Recenter and resposition grid
// gl.glRotatef(180f, 1.0f, 1.0f, .0f);
gl.glScalef(gridHorizontalScaleFactor, -gridHorizontalScaleFactor, 1.0f);
gl.glTranslatef(-latticeSizeX / 2, -latticeSizeY / 2, -1800);
// Z translation is currently a hack based on the grid. Roughly 1800 m for
// the Grand Canyon
//drawTerrain();
gl.glDisable(GLLightingFunc.GL_LIGHTING);
drawXSections();
// Now draw UI stuff on top
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0f, canvas.getWidth(), 0.0f, canvas.getHeight(), 1.0f, 2.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
drawProfileView();
drawRiverProfileVertScale();
drawUI();
}
private void drawXSections() {
int n = XSectionManager.nXSections();
// Wilsim.i.log.append("View : drawXSections() : " + n + "\n");
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glBegin(GL2GL3.GL_QUADS);
for (int i = 0; i < n; i++) {
XSection p = XSectionManager.getXSection(i);
gl.glColor4f(0.6f, 0.5f, 0.5f, 0.6f);
gl.glVertex3f(p.startX, p.startY, COLOR_MIN_HEIGHT);
gl.glVertex3f(p.endX, p.endY, COLOR_MIN_HEIGHT);
gl.glVertex3f(p.endX, p.endY, COLOR_MAX_HEIGHT);
gl.glVertex3f(p.startX, p.startY, COLOR_MAX_HEIGHT);
}
gl.glEnd();
gl.glDisable(GL.GL_BLEND);
// Draw top highlights
gl.glLineWidth(3.0f);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.0f, 0.0f, 0.0f);
for (int i = 0; i < n; i++) {
XSection p = XSectionManager.getXSection(i);
gl.glVertex3f(p.startX, p.startY, COLOR_MAX_HEIGHT);
gl.glVertex3f(p.endX, p.endY, COLOR_MAX_HEIGHT);
// Draw arrow
float dx, dy, dbarb;
dx = p.endX - p.startX;
dy = p.endY - p.startY;
float distance = (float) Math.sqrt(dx * dx + dy * dy);
// Some hackish numbers to keep barb size sane
dbarb = distance * 0.06f; // 10% / sqrt(3)
dbarb = Math.min(dbarb, 15.0f); // but no greater than 26.
dbarb = Math.max(dbarb, 2.0f); // but no smaller than 3
dx = dx * dbarb / distance;
dy = dy * dbarb / distance;
gl.glVertex3f(p.endX, p.endY, COLOR_MAX_HEIGHT);
gl.glVertex3f(p.endX - 2.0f * dx + dy, p.endY - dx - 2.0f * dy, COLOR_MAX_HEIGHT);
gl.glVertex3f(p.endX, p.endY, COLOR_MAX_HEIGHT);
gl.glVertex3f(p.endX - 2.0f * dx - dy, p.endY + dx - 2.0f * dy, COLOR_MAX_HEIGHT);
gl.glEnd();
vertScaleTextEngine.setColor(0.0f, 0.0f, 0.0f, 1.0f);
vertScaleTextEngine.beginRendering(canvas.getWidth(), canvas.getHeight());
gl.glMatrixMode(5888);
gl.glPushMatrix();
/* vertScaleTextEngine.draw("tempXSection.startX= " + Integer.toString(p.startX), 10, 12);
vertScaleTextEngine.draw("tempXSection.startY= " + Integer.toString(p.startY), 10, 0);
vertScaleTextEngine.draw("tempXSection.endX= " + Integer.toString(p.endX), 300, 12);
vertScaleTextEngine.draw("tempXSection.endY= " + Integer.toString(p.endY), 300, 0);*/
vertScaleTextEngine.endRendering();
}
gl.glEnd();
gl.glLineWidth(1.0f);
}
/*
private void drawTerrain2() {
// Need a separate vertex normal for each quad -- this implementation is inefficient
// Should be computed once and stored when grid is loaded
float[] v1 = new float[3];
float[] v2 = new float[3];
float[] norm = new float[3];
float[] color = new float[4];
v1[0] = gridHorizontalScaleFactor / 5.0f; // Scale factor is a hack
v1[1] = 0.0f;
v2[0] = 0.0f;
v2[1] = gridHorizontalScaleFactor / 5.0f; // Ditto on hack
// Get ready for varying material properties
gl.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK,
GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);
int interp = 2;
float mux = (2 % interp) * (1 / (float) (interp - 1));
float mux2 = (float) (1 - Math.cos(mux * Math.PI)) / 2;
for (int x = 2; x < (latticeSizeX - 2) * interp; x++) {
float muxb = ((x + 1) % interp) * (1 / (float) (interp - 1));
float muxb2 = (float) (1 - Math.cos(muxb * Math.PI)) / 2;
for (int y = 2; y < (latticeSizeY - 2) * interp; y++) {
float answerx = (
topo[x / interp][y / interp] * (1 - mux2) + topo[(x / interp) + 1][(y / interp)] * mux2);
float answerx2 = (
topo[x / interp][y / interp + 1] * (1 - mux2) + topo[(x / interp) + 1][(y / interp) + 1] * mux2);
float answerxb = (
topo[x / interp][y / interp] * (1 - muxb2) + topo[(x / interp) + 1][(y / interp)] * muxb2);
float answerxb2 = (
topo[x / interp][y / interp + 1] * (1 - muxb2) + topo[(x / interp) + 1][(y / interp) + 1] * muxb2);
float muy = (y % interp) * (1 / (float) (interp - 1));
float muy2 = (float) (1 - Math.cos(muy * Math.PI)) / 2;
float ybetweenx0x1 = (
answerx * (1 - muy2) + answerx2 * muy2);
float ybetweenx1x2 = (
answerxb * (1 - muy2) + answerxb2 * muy2);
float muyb = ((y + 1) % interp) * (1 / (float) (interp - 1));
float muyb2 = (float) (1 - Math.cos(muyb * Math.PI)) / 2;
float y1betweenx0x1 = (
answerx * (1 - muyb2) + answerx2 * muyb2);
float y1betweenx1x2 = (
answerxb * (1 - muyb2) + answerxb2 * muyb2);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
v1[2] = topo[(x / interp) + 1][(y / interp)] - topo[(x / interp) - 1][(y / interp)];
v2[2] = topo[(x / interp)][(y / interp) + 1] - topo[(x / interp)][(y / interp) - 1];
cross(norm, v2, v1);
gl.glNormal3fv(norm, 0);
map_color(ybetweenx0x1, color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x / interp + mux, y / interp + muy, ybetweenx0x1);
map_color(ybetweenx1x2, color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x / interp + mux + mux, y / interp + muy, ybetweenx1x2);
map_color(y1betweenx0x1, color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x / interp + mux, y / interp + muy + muy, y1betweenx0x1);
*//* v1[2] = topo[(x / interp) + 2][(y / interp) + 1] - topo[x / interp][y / interp + 1];
v2[2] = topo[(x / interp) + 1][(y / interp) + 2] - topo[(x / interp) + 1][y / interp];
cross(norm, v2, v1);*//*
map_color(y1betweenx1x2, color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x / interp + mux + mux, y / interp + muy + muy, y1betweenx1x2);
gl.glEnd();
}
mux = muxb;
mux2 = muxb2;
}
float km = 13.888888888f;
int offset = 2;
int verty = 270;
*//* gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(offset, 271, 1800.0f);
gl.glVertex3f(km * 5 + offset, 271, 1800.0f);
gl.glVertex3f(offset, 261, 1800.0f);
gl.glVertex3f(offset, 261, 1800.0f);
gl.glVertex3f(km * 5 + offset, 271, 1800.0f);
gl.glVertex3f(km * 5 + offset, 261, 1800.0f);
gl.glColor3f(1.75f, 1.75f, 1.75f);
gl.glVertex3f(km * 5 + offset, 271, 1800.0f);
gl.glVertex3f(km * 7 + offset, 271, 1800.0f);
gl.glVertex3f(km * 5 + offset, 261, 1800.0f);
gl.glVertex3f(km * 5 + offset, 261, 1800.0f);
gl.glVertex3f(km * 7 + offset, 271, 1800.0f);
gl.glVertex3f(km * 7 + offset, 261, 1800.0f);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(km * 7 + offset, 271, 1800.0f);
gl.glVertex3f(km * 8 + offset, 271, 1800.0f);
gl.glVertex3f(km * 7 + offset, 261, 1800.0f);
gl.glVertex3f(km * 7 + offset, 261, 1800.0f);
gl.glVertex3f(km * 8 + offset, 271, 1800.0f);
gl.glVertex3f(km * 8 + offset, 261, 1800.0f);
gl.glEnd();*//*
gl.glLineWidth(1.0f);
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(offset, verty - 5.0f, 1820.0f);
gl.glVertex3f(km * 10 + offset, verty - 5.0f, 1820.0f);
gl.glEnd();
// Draw label tics
gl.glColor3f(0.0f, 0.0f, 0.0f);
//gl.glColor4f(0.0f, 0.0f, 0.0f, .5f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(offset, verty + 0.0f, 1820.0f);
gl.glVertex3f(offset, verty - 10.0f, 1820.0f);
//System.out.println("Line 50 * drawScaleX " + (50 * drawScaleX));
gl.glVertex3f(km * 2.5f + offset, verty + 0.0f, 1820.0f);
gl.glVertex3f(km * 2.5f + offset, verty - 10.0f, 1820.0f);
//System.out.println("Line 20050 * drawScaleX " + (20050 * drawScaleX));
gl.glVertex3f(km * 5 + offset, verty + 0.0f, 1820.0f);
gl.glVertex3f(km * 5 + offset, verty - 10.0f, 1820.0f);
//System.out.println("Line 30050 * drawScaleX " + (30050 * drawScaleX));
gl.glVertex3f(km * 10 + offset, verty + 0.0f, 1820.0f);
gl.glVertex3f(km * 10 + offset, verty - 10.0f, 1820.0f);
*//* gl.glVertex3f(km * 15 + offset, verty + 0.0f, 1820.0f);
gl.glVertex3f(km * 15 + offset, verty - 10.0f, 1820.0f);*//*
//System.out.println("Line 40050 * drawScaleX " + (40050 * drawScaleX));
gl.glEnd();
vertScaleTextEngine.setColor(0.0f, 0.0f, 0.0f, 1.0f);
vertScaleTextEngine.beginRendering(canvas.getWidth(), canvas.getHeight());
*//* vertScaleTextEngine.draw("Horizontal Distance (km) ", 5, 5);
vertScaleTextEngine.draw("Horizontal Distance (km) " , 5, 5);
vertScaleTextEngine.draw("Horizontal Distance (km) " , 5, 5);*//*
*//* if (viewMode == SPIN_MODE) {*//*
vertScaleTextEngine.draw("0 km ", 5, 25);
vertScaleTextEngine.draw("25 km ", 55, 25);
vertScaleTextEngine.draw("50 km ", 105, 25);