/
Camera.java
1483 lines (1323 loc) · 50 KB
/
Camera.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
/*
* Copyright (c) 2009-2018 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer;
import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingVolume;
import com.jme3.export.*;
import com.jme3.math.*;
import com.jme3.util.TempVars;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* <code>Camera</code> is a standalone, purely mathematical class for doing
* camera-related computations.
*
* <p>
* Given input data such as location, orientation (direction, left, up),
* and viewport settings, it can compute data necessary to render objects
* with the graphics library. Two matrices are generated, the view matrix
* transforms objects from world space into eye space, while the projection
* matrix transforms objects from eye space into clip space.
* </p>
* <p>Another purpose of the camera class is to do frustum culling operations,
* defined by six planes which define a 3D frustum shape, it is possible to
* test if an object bounded by a mathematically defined volume is inside
* the camera frustum, and thus to avoid rendering objects that are outside
* the frustum
* </p>
*
* @author Mark Powell
* @author Joshua Slack
*/
public class Camera implements Savable, Cloneable {
private static final Logger logger = Logger.getLogger(Camera.class.getName());
/**
* The <code>FrustumIntersect</code> enum is returned as a result
* of a culling check operation,
* see {@link #contains(com.jme3.bounding.BoundingVolume) }
*/
public enum FrustumIntersect {
/**
* defines a constant assigned to spatials that are completely outside
* of this camera's view frustum.
*/
Outside,
/**
* defines a constant assigned to spatials that are completely inside
* the camera's view frustum.
*/
Inside,
/**
* defines a constant assigned to spatials that are intersecting one of
* the six planes that define the view frustum.
*/
Intersects;
}
/**
* LEFT_PLANE represents the left plane of the camera frustum.
*/
private static final int LEFT_PLANE = 0;
/**
* RIGHT_PLANE represents the right plane of the camera frustum.
*/
private static final int RIGHT_PLANE = 1;
/**
* BOTTOM_PLANE represents the bottom plane of the camera frustum.
*/
private static final int BOTTOM_PLANE = 2;
/**
* TOP_PLANE represents the top plane of the camera frustum.
*/
private static final int TOP_PLANE = 3;
/**
* FAR_PLANE represents the far plane of the camera frustum.
*/
private static final int FAR_PLANE = 4;
/**
* NEAR_PLANE represents the near plane of the camera frustum.
*/
private static final int NEAR_PLANE = 5;
/**
* FRUSTUM_PLANES represents the number of planes of the camera frustum.
*/
private static final int FRUSTUM_PLANES = 6;
/**
* MAX_WORLD_PLANES holds the maximum planes allowed by the system.
*/
private static final int MAX_WORLD_PLANES = 6;
/**
* Camera's location
*/
protected Vector3f location;
/**
* The orientation of the camera.
*/
protected Quaternion rotation;
/**
* Distance from camera to near frustum plane.
*/
protected float frustumNear;
/**
* Distance from camera to far frustum plane.
*/
protected float frustumFar;
/**
* Distance from camera to left frustum plane.
*/
protected float frustumLeft;
/**
* Distance from camera to right frustum plane.
*/
protected float frustumRight;
/**
* Distance from camera to top frustum plane.
*/
protected float frustumTop;
/**
* Distance from camera to bottom frustum plane.
*/
protected float frustumBottom;
//Temporary values computed in onFrustumChange that are needed if a
//call is made to onFrameChange.
protected float[] coeffLeft;
protected float[] coeffRight;
protected float[] coeffBottom;
protected float[] coeffTop;
//view port coordinates
/**
* Percent value on display where horizontal viewing starts for this camera.
* Default is 0.
*/
protected float viewPortLeft;
/**
* Percent value on display where horizontal viewing ends for this camera.
* Default is 1.
*/
protected float viewPortRight;
/**
* Percent value on display where vertical viewing ends for this camera.
* Default is 1.
*/
protected float viewPortTop;
/**
* Percent value on display where vertical viewing begins for this camera.
* Default is 0.
*/
protected float viewPortBottom;
/**
* Array holding the planes that this camera will check for culling.
*/
protected Plane[] worldPlane;
/**
* A mask value set during contains() that allows fast culling of a Node's
* children.
*/
private int planeState;
protected int width;
protected int height;
protected boolean viewportChanged = true;
/**
* store the value for field parallelProjection
*/
private boolean parallelProjection = true;
protected Matrix4f projectionMatrixOverride = new Matrix4f();
private boolean overrideProjection;
protected Matrix4f viewMatrix = new Matrix4f();
protected Matrix4f projectionMatrix = new Matrix4f();
protected Matrix4f viewProjectionMatrix = new Matrix4f();
private BoundingBox guiBounding = new BoundingBox();
/** The camera's name. */
protected String name;
/**
* Serialization only. Do not use.
*/
public Camera() {
worldPlane = new Plane[MAX_WORLD_PLANES];
for (int i = 0; i < MAX_WORLD_PLANES; i++) {
worldPlane[i] = new Plane();
}
}
/**
* Constructor instantiates a new <code>Camera</code> object. All
* values of the camera are set to default.
*/
public Camera(int width, int height) {
this();
location = new Vector3f();
rotation = new Quaternion();
frustumNear = 1.0f;
frustumFar = 2.0f;
frustumLeft = -0.5f;
frustumRight = 0.5f;
frustumTop = 0.5f;
frustumBottom = -0.5f;
coeffLeft = new float[2];
coeffRight = new float[2];
coeffBottom = new float[2];
coeffTop = new float[2];
viewPortLeft = 0.0f;
viewPortRight = 1.0f;
viewPortTop = 1.0f;
viewPortBottom = 0.0f;
this.width = width;
this.height = height;
onFrustumChange();
onViewPortChange();
onFrameChange();
logger.log(Level.FINE, "Camera created (W: {0}, H: {1})", new Object[]{width, height});
}
@Override
public Camera clone() {
try {
Camera cam = (Camera) super.clone();
cam.viewportChanged = true;
cam.planeState = 0;
cam.worldPlane = new Plane[MAX_WORLD_PLANES];
for (int i = 0; i < worldPlane.length; i++) {
cam.worldPlane[i] = worldPlane[i].clone();
}
cam.coeffLeft = new float[2];
cam.coeffRight = new float[2];
cam.coeffBottom = new float[2];
cam.coeffTop = new float[2];
cam.location = location.clone();
cam.rotation = rotation.clone();
if (projectionMatrixOverride != null) {
cam.projectionMatrixOverride = projectionMatrixOverride.clone();
}
cam.viewMatrix = viewMatrix.clone();
cam.projectionMatrix = projectionMatrix.clone();
cam.viewProjectionMatrix = viewProjectionMatrix.clone();
cam.guiBounding = (BoundingBox) guiBounding.clone();
cam.update();
return cam;
} catch (CloneNotSupportedException ex) {
throw new AssertionError();
}
}
/**
* This method copies the settings of the given camera.
*
* @param cam
* the camera we copy the settings from
*/
public void copyFrom(Camera cam) {
location.set(cam.location);
rotation.set(cam.rotation);
frustumNear = cam.frustumNear;
frustumFar = cam.frustumFar;
frustumLeft = cam.frustumLeft;
frustumRight = cam.frustumRight;
frustumTop = cam.frustumTop;
frustumBottom = cam.frustumBottom;
coeffLeft[0] = cam.coeffLeft[0];
coeffLeft[1] = cam.coeffLeft[1];
coeffRight[0] = cam.coeffRight[0];
coeffRight[1] = cam.coeffRight[1];
coeffBottom[0] = cam.coeffBottom[0];
coeffBottom[1] = cam.coeffBottom[1];
coeffTop[0] = cam.coeffTop[0];
coeffTop[1] = cam.coeffTop[1];
viewPortLeft = cam.viewPortLeft;
viewPortRight = cam.viewPortRight;
viewPortTop = cam.viewPortTop;
viewPortBottom = cam.viewPortBottom;
this.width = cam.width;
this.height = cam.height;
this.planeState = 0;
this.viewportChanged = true;
for (int i = 0; i < MAX_WORLD_PLANES; ++i) {
worldPlane[i].setNormal(cam.worldPlane[i].getNormal());
worldPlane[i].setConstant(cam.worldPlane[i].getConstant());
}
this.parallelProjection = cam.parallelProjection;
this.overrideProjection = cam.overrideProjection;
this.projectionMatrixOverride.set(cam.projectionMatrixOverride);
this.viewMatrix.set(cam.viewMatrix);
this.projectionMatrix.set(cam.projectionMatrix);
this.viewProjectionMatrix.set(cam.viewProjectionMatrix);
this.guiBounding.setXExtent(cam.guiBounding.getXExtent());
this.guiBounding.setYExtent(cam.guiBounding.getYExtent());
this.guiBounding.setZExtent(cam.guiBounding.getZExtent());
this.guiBounding.setCenter(cam.guiBounding.getCenter());
this.guiBounding.setCheckPlane(cam.guiBounding.getCheckPlane());
this.name = cam.name;
}
/**
* This method sets the cameras name.
* @param name the cameras name
*/
public void setName(String name) {
this.name = name;
}
/**
* This method returns the cameras name.
* @return the cameras name
*/
public String getName() {
return name;
}
/**
* Sets a clipPlane for this camera.
* The clipPlane is used to recompute the
* projectionMatrix using the plane as the near plane
* This technique is known as the oblique near-plane clipping method introduced by Eric Lengyel
* more info here
* <ul>
* <li><a href="http://www.terathon.com/code/oblique.html">http://www.terathon.com/code/oblique.html</a>
* <li><a href="http://aras-p.info/texts/obliqueortho.html">http://aras-p.info/texts/obliqueortho.html</a>
* <li><a href="http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html">http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html</a>
* </ul>
*
* Note that this will work properly only if it's called on each update, and be aware that it won't work properly with the sky bucket.
* if you want to handle the sky bucket, look at how it's done in SimpleWaterProcessor.java
* @param clipPlane the plane
* @param side the side the camera stands from the plane
*/
public void setClipPlane(Plane clipPlane, Plane.Side side) {
float sideFactor = 1;
if (side == Plane.Side.Negative) {
sideFactor = -1;
}
//we are on the other side of the plane no need to clip anymore.
if (clipPlane.whichSide(location) == side) {
return;
}
TempVars vars = TempVars.get();
try {
Matrix4f p = projectionMatrixOverride.set(projectionMatrix);
Matrix4f ivm = viewMatrix;
Vector3f point = clipPlane.getNormal().mult(clipPlane.getConstant(), vars.vect1);
Vector3f pp = ivm.mult(point, vars.vect2);
Vector3f pn = ivm.multNormal(clipPlane.getNormal(), vars.vect3);
Vector4f clipPlaneV = vars.vect4f1.set(pn.x * sideFactor, pn.y * sideFactor, pn.z * sideFactor, -(pp.dot(pn)) * sideFactor);
Vector4f v = vars.vect4f2.set(0, 0, 0, 0);
v.x = (Math.signum(clipPlaneV.x) + p.m02) / p.m00;
v.y = (Math.signum(clipPlaneV.y) + p.m12) / p.m11;
v.z = -1.0f;
v.w = (1.0f + p.m22) / p.m23;
float dot = clipPlaneV.dot(v);//clipPlaneV.x * v.x + clipPlaneV.y * v.y + clipPlaneV.z * v.z + clipPlaneV.w * v.w;
Vector4f c = clipPlaneV.multLocal(2.0f / dot);
p.m20 = c.x - p.m30;
p.m21 = c.y - p.m31;
p.m22 = c.z - p.m32;
p.m23 = c.w - p.m33;
setProjectionMatrix(p);
} finally {
vars.release();
}
}
/**
* Sets a clipPlane for this camera.
* The cliPlane is used to recompute the projectionMatrix using the plane as the near plane
* This technique is known as the oblique near-plane clipping method introduced by Eric Lengyel
* more info here
* <ul>
* <li><a href="http://www.terathon.com/code/oblique.html">http://www.terathon.com/code/oblique.html</a></li>
* <li><a href="http://aras-p.info/texts/obliqueortho.html">http://aras-p.info/texts/obliqueortho.html</a></li>
* <li><a href="http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html">
* http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html</a></li>
* </ul>
*
* Note that this will work properly only if it's called on each update, and be aware that it won't work properly with the sky bucket.
* if you want to handle the sky bucket, look at how it's done in SimpleWaterProcessor.java
* @param clipPlane the plane
*/
public void setClipPlane(Plane clipPlane) {
setClipPlane(clipPlane, clipPlane.whichSide(location));
}
/**
* Resize this camera's view for the specified display size. Invoked by an
* associated {@link RenderManager} to notify the camera of changes to the
* display dimensions.
*
* @param width the new width of the display, in pixels
* @param height the new height of the display, in pixels
* @param fixAspect if true, recompute the camera's frustum to preserve its
* prior aspect ratio
*/
public void resize(int width, int height, boolean fixAspect) {
this.width = width;
this.height = height;
onViewPortChange();
if (fixAspect) {
float h = height * (viewPortTop - viewPortBottom);
float w = width * (viewPortRight - viewPortLeft);
float aspectRatio = w / h;
frustumRight = frustumTop * aspectRatio;
frustumLeft = -frustumRight;
onFrustumChange();
}
}
/**
* <code>getFrustumBottom</code> returns the value of the bottom frustum
* plane.
*
* @return the value of the bottom frustum plane.
*/
public float getFrustumBottom() {
return frustumBottom;
}
/**
* <code>setFrustumBottom</code> sets the value of the bottom frustum
* plane.
*
* @param frustumBottom the value of the bottom frustum plane.
*/
public void setFrustumBottom(float frustumBottom) {
this.frustumBottom = frustumBottom;
onFrustumChange();
}
/**
* <code>getFrustumFar</code> gets the value of the far frustum plane.
*
* @return the value of the far frustum plane.
*/
public float getFrustumFar() {
return frustumFar;
}
/**
* <code>setFrustumFar</code> sets the value of the far frustum plane.
*
* @param frustumFar the value of the far frustum plane.
*/
public void setFrustumFar(float frustumFar) {
this.frustumFar = frustumFar;
onFrustumChange();
}
/**
* <code>getFrustumLeft</code> gets the value of the left frustum plane.
*
* @return the value of the left frustum plane.
*/
public float getFrustumLeft() {
return frustumLeft;
}
/**
* <code>setFrustumLeft</code> sets the value of the left frustum plane.
*
* @param frustumLeft the value of the left frustum plane.
*/
public void setFrustumLeft(float frustumLeft) {
this.frustumLeft = frustumLeft;
onFrustumChange();
}
/**
* <code>getFrustumNear</code> gets the value of the near frustum plane.
*
* @return the value of the near frustum plane.
*/
public float getFrustumNear() {
return frustumNear;
}
/**
* <code>setFrustumNear</code> sets the value of the near frustum plane.
*
* @param frustumNear the value of the near frustum plane.
*/
public void setFrustumNear(float frustumNear) {
this.frustumNear = frustumNear;
onFrustumChange();
}
/**
* <code>getFrustumRight</code> gets the value of the right frustum plane.
*
* @return frustumRight the value of the right frustum plane.
*/
public float getFrustumRight() {
return frustumRight;
}
/**
* <code>setFrustumRight</code> sets the value of the right frustum plane.
*
* @param frustumRight the value of the right frustum plane.
*/
public void setFrustumRight(float frustumRight) {
this.frustumRight = frustumRight;
onFrustumChange();
}
/**
* <code>getFrustumTop</code> gets the value of the top frustum plane.
*
* @return the value of the top frustum plane.
*/
public float getFrustumTop() {
return frustumTop;
}
/**
* <code>setFrustumTop</code> sets the value of the top frustum plane.
*
* @param frustumTop the value of the top frustum plane.
*/
public void setFrustumTop(float frustumTop) {
this.frustumTop = frustumTop;
onFrustumChange();
}
/**
* <code>getLocation</code> retrieves the location vector of the camera.
*
* @return the position of the camera.
* @see Camera#getLocation()
*/
public Vector3f getLocation() {
return location;
}
/**
* <code>getRotation</code> retrieves the rotation quaternion of the camera.
*
* @return the rotation of the camera.
*/
public Quaternion getRotation() {
return rotation;
}
/**
* <code>getDirection</code> retrieves the direction vector the camera is
* facing.
*
* @return the direction the camera is facing.
* @see Camera#getDirection()
*/
public Vector3f getDirection() {
return rotation.getRotationColumn(2);
}
/**
* <code>getLeft</code> retrieves the left axis of the camera.
*
* @return the left axis of the camera.
* @see Camera#getLeft()
*/
public Vector3f getLeft() {
return rotation.getRotationColumn(0);
}
/**
* <code>getUp</code> retrieves the up axis of the camera.
*
* @return the up axis of the camera.
* @see Camera#getUp()
*/
public Vector3f getUp() {
return rotation.getRotationColumn(1);
}
/**
* <code>getDirection</code> retrieves the direction vector the camera is
* facing.
*
* @return the direction the camera is facing.
* @see Camera#getDirection()
*/
public Vector3f getDirection(Vector3f store) {
return rotation.getRotationColumn(2, store);
}
/**
* <code>getLeft</code> retrieves the left axis of the camera.
*
* @return the left axis of the camera.
* @see Camera#getLeft()
*/
public Vector3f getLeft(Vector3f store) {
return rotation.getRotationColumn(0, store);
}
/**
* <code>getUp</code> retrieves the up axis of the camera.
*
* @return the up axis of the camera.
* @see Camera#getUp()
*/
public Vector3f getUp(Vector3f store) {
return rotation.getRotationColumn(1, store);
}
/**
* <code>setLocation</code> sets the position of the camera.
*
* @param location the position of the camera.
*/
public void setLocation(Vector3f location) {
this.location.set(location);
onFrameChange();
}
/**
* <code>setRotation</code> sets the orientation of this camera. This will
* be equivalent to setting each of the axes:
* <code><br>
* cam.setLeft(rotation.getRotationColumn(0));<br>
* cam.setUp(rotation.getRotationColumn(1));<br>
* cam.setDirection(rotation.getRotationColumn(2));<br>
* </code>
*
* @param rotation the rotation of this camera
*/
public void setRotation(Quaternion rotation) {
this.rotation.set(rotation);
onFrameChange();
}
/**
* <code>lookAtDirection</code> sets the direction the camera is facing
* given a direction and an up vector.
*
* @param direction the direction this camera is facing.
*/
public void lookAtDirection(Vector3f direction, Vector3f up) {
this.rotation.lookAt(direction, up);
onFrameChange();
}
/**
* <code>setAxes</code> sets the axes (left, up and direction) for this
* camera.
*
* @param left the left axis of the camera.
* @param up the up axis of the camera.
* @param direction the direction the camera is facing.
*
* @see Camera#setAxes(com.jme3.math.Quaternion)
*/
public void setAxes(Vector3f left, Vector3f up, Vector3f direction) {
this.rotation.fromAxes(left, up, direction);
onFrameChange();
}
/**
* <code>setAxes</code> uses a rotational matrix to set the axes of the
* camera.
*
* @param axes the matrix that defines the orientation of the camera.
*/
public void setAxes(Quaternion axes) {
this.rotation.set(axes);
onFrameChange();
}
/**
* normalize normalizes the camera vectors.
*/
public void normalize() {
this.rotation.normalizeLocal();
onFrameChange();
}
/**
* <code>setFrustum</code> sets the frustum of this camera object.
*
* @param near the near plane.
* @param far the far plane.
* @param left the left plane.
* @param right the right plane.
* @param top the top plane.
* @param bottom the bottom plane.
* @see Camera#setFrustum(float, float, float, float,
* float, float)
*/
public void setFrustum(float near, float far, float left, float right,
float top, float bottom) {
frustumNear = near;
frustumFar = far;
frustumLeft = left;
frustumRight = right;
frustumTop = top;
frustumBottom = bottom;
onFrustumChange();
}
/**
* <code>setFrustumPerspective</code> defines the frustum for the camera. This
* frustum is defined by a viewing angle, aspect ratio, and near/far planes
*
* @param fovY Frame of view angle along the Y in degrees.
* @param aspect Width:Height ratio
* @param near Near view plane distance
* @param far Far view plane distance
*/
public void setFrustumPerspective(float fovY, float aspect, float near,
float far) {
if (Float.isNaN(aspect) || Float.isInfinite(aspect)) {
// ignore.
logger.log(Level.WARNING, "Invalid aspect given to setFrustumPerspective: {0}", aspect);
return;
}
float h = FastMath.tan(fovY * FastMath.DEG_TO_RAD * .5f) * near;
float w = h * aspect;
frustumLeft = -w;
frustumRight = w;
frustumBottom = -h;
frustumTop = h;
frustumNear = near;
frustumFar = far;
// Camera is no longer parallel projection even if it was before
parallelProjection = false;
onFrustumChange();
}
/**
* <code>setFrame</code> sets the orientation and location of the camera.
*
* @param location the point position of the camera.
* @param left the left axis of the camera.
* @param up the up axis of the camera.
* @param direction the facing of the camera.
* @see Camera#setFrame(com.jme3.math.Vector3f,
* com.jme3.math.Vector3f, com.jme3.math.Vector3f, com.jme3.math.Vector3f)
*/
public void setFrame(Vector3f location, Vector3f left, Vector3f up,
Vector3f direction) {
this.location = location;
this.rotation.fromAxes(left, up, direction);
onFrameChange();
}
/**
* <code>lookAt</code> is a convenience method for auto-setting the frame
* based on a world position the user desires the camera to look at. It
* repoints the camera towards the given position using the difference
* between the position and the current camera location as a direction
* vector and the worldUpVector to compute up and left camera vectors.
*
* @param pos where to look at in terms of world coordinates
* @param worldUpVector a normalized vector indicating the up direction of the world.
* (typically {0, 1, 0} in jME.)
*/
public void lookAt(Vector3f pos, Vector3f worldUpVector) {
TempVars vars = TempVars.get();
Vector3f newDirection = vars.vect1;
Vector3f newUp = vars.vect2;
Vector3f newLeft = vars.vect3;
newDirection.set(pos).subtractLocal(location).normalizeLocal();
newUp.set(worldUpVector).normalizeLocal();
if (newUp.equals(Vector3f.ZERO)) {
newUp.set(Vector3f.UNIT_Y);
}
newLeft.set(newUp).crossLocal(newDirection).normalizeLocal();
if (newLeft.equals(Vector3f.ZERO)) {
if (newDirection.x != 0) {
newLeft.set(newDirection.y, -newDirection.x, 0f);
} else {
newLeft.set(0f, newDirection.z, -newDirection.y);
}
}
newUp.set(newDirection).crossLocal(newLeft).normalizeLocal();
this.rotation.fromAxes(newLeft, newUp, newDirection);
this.rotation.normalizeLocal();
vars.release();
onFrameChange();
}
/**
* <code>setFrame</code> sets the orientation and location of the camera.
*
* @param location
* the point position of the camera.
* @param axes
* the orientation of the camera.
*/
public void setFrame(Vector3f location, Quaternion axes) {
this.location = location;
this.rotation.set(axes);
onFrameChange();
}
/**
* <code>update</code> updates the camera parameters by calling
* <code>onFrustumChange</code>,<code>onViewPortChange</code> and
* <code>onFrameChange</code>.
*
* @see Camera#update()
*/
public void update() {
onFrustumChange();
onViewPortChange();
//...this is always called by onFrustumChange()
//onFrameChange();
}
/**
* <code>getPlaneState</code> returns the state of the frustum planes. So
* checks can be made as to which frustum plane has been examined for
* culling thus far.
*
* @return the current plane state int.
*/
public int getPlaneState() {
return planeState;
}
/**
* <code>setPlaneState</code> sets the state to keep track of tested
* planes for culling.
*
* @param planeState the updated state.
*/
public void setPlaneState(int planeState) {
this.planeState = planeState;
}
/**
* <code>getViewPortLeft</code> gets the left boundary of the viewport
*
* @return the left boundary of the viewport
*/
public float getViewPortLeft() {
return viewPortLeft;
}
/**
* <code>setViewPortLeft</code> sets the left boundary of the viewport
*
* @param left the left boundary of the viewport
*/
public void setViewPortLeft(float left) {
viewPortLeft = left;
onViewPortChange();
}
/**
* <code>getViewPortRight</code> gets the right boundary of the viewport
*
* @return the right boundary of the viewport
*/
public float getViewPortRight() {
return viewPortRight;
}
/**
* <code>setViewPortRight</code> sets the right boundary of the viewport
*
* @param right the right boundary of the viewport
*/
public void setViewPortRight(float right) {
viewPortRight = right;
onViewPortChange();
}
/**
* <code>getViewPortTop</code> gets the top boundary of the viewport
*
* @return the top boundary of the viewport
*/
public float getViewPortTop() {
return viewPortTop;
}
/**
* <code>setViewPortTop</code> sets the top boundary of the viewport
*
* @param top the top boundary of the viewport
*/
public void setViewPortTop(float top) {
viewPortTop = top;
onViewPortChange();
}
/**
* <code>getViewPortBottom</code> gets the bottom boundary of the viewport
*
* @return the bottom boundary of the viewport
*/
public float getViewPortBottom() {
return viewPortBottom;
}
/**
* <code>setViewPortBottom</code> sets the bottom boundary of the viewport
*
* @param bottom the bottom boundary of the viewport
*/
public void setViewPortBottom(float bottom) {
viewPortBottom = bottom;
onViewPortChange();
}
/**
* <code>setViewPort</code> sets the boundaries of the viewport
*
* @param left the left boundary of the viewport (default: 0)
* @param right the right boundary of the viewport (default: 1)
* @param bottom the bottom boundary of the viewport (default: 0)
* @param top the top boundary of the viewport (default: 1)
*/
public void setViewPort(float left, float right, float bottom, float top) {
this.viewPortLeft = left;
this.viewPortRight = right;
this.viewPortBottom = bottom;
this.viewPortTop = top;
onViewPortChange();
}
/**
* Returns the pseudo distance from the given position to the near
* plane of the camera. This is used for render queue sorting.
* @param pos The position to compute a distance to.
* @return Distance from the near plane to the point.
*/
public float distanceToNearPlane(Vector3f pos) {
return worldPlane[NEAR_PLANE].pseudoDistance(pos);
}