00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "lens.h"
00020 #include "throw_event.h"
00021 #include "compose_matrix.h"
00022 #include "look_at.h"
00023 #include "geomLinestrip.h"
00024 #include "boundingHexahedron.h"
00025 #include "indent.h"
00026 #include "config_gobj.h"
00027 #include "plane.h"
00028
00029 TypeHandle Lens::_type_handle;
00030
00031 const float Lens::_default_fov = 40.0f;
00032
00033
00034
00035
00036
00037
00038 Lens::
00039 Lens() {
00040 clear();
00041 }
00042
00043
00044
00045
00046
00047
00048 Lens::
00049 Lens(const Lens ©) {
00050 (*this) = copy;
00051 }
00052
00053
00054
00055
00056
00057
00058 void Lens::
00059 operator = (const Lens ©) {
00060 _change_event = copy._change_event;
00061 _cs = copy._cs;
00062 _film_size = copy._film_size;
00063 _film_offset = copy._film_offset;
00064 _focal_length = copy._focal_length;
00065 _fov = copy._fov;
00066 _aspect_ratio = copy._aspect_ratio;
00067 _near_distance = copy._near_distance;
00068 _far_distance = copy._far_distance;
00069 _user_flags = copy._user_flags;
00070 _comp_flags = 0;
00071
00072
00073
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083 void Lens::
00084 set_coordinate_system(CoordinateSystem cs) {
00085 _cs = cs;
00086 adjust_comp_flags(CF_mat | CF_view_hpr | CF_view_vector, 0);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 void Lens::
00096 clear() {
00097 _change_event = "";
00098 _cs = CS_default;
00099 _film_size.set(1.0f, 1.0f);
00100 _film_offset.set(0.0f, 0.0f);
00101 _focal_length = 1.0f;
00102 _fov.set(_default_fov, _default_fov);
00103 _aspect_ratio = 1.0f;
00104 _near_distance = default_near;
00105 _far_distance = default_far;
00106 _view_hpr.set(0.0f, 0.0f, 0.0f);
00107 _view_vector.set(0.0f, 1.0f, 0.0f);
00108 _up_vector.set(0.0f, 0.0f, 1.0f);
00109 _iod_offset = 0.0f;
00110 _user_flags = 0;
00111 _comp_flags = CF_fov;
00112
00113
00114 _film_size_seq = 0;
00115 _focal_length_seq = 1;
00116 _fov_seq = 2;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 void Lens::
00128 set_film_size(float width) {
00129 _film_size.set(width, width / get_aspect_ratio());
00130
00131
00132
00133 resequence_fov_triad(_film_size_seq, _focal_length_seq, _fov_seq);
00134
00135 if (_fov_seq == 0) {
00136
00137 adjust_user_flags(UF_hfov | UF_vfov | UF_film_height,
00138 UF_film_width);
00139 } else {
00140
00141 nassertv(_focal_length_seq == 0);
00142 adjust_user_flags(UF_focal_length | UF_film_height,
00143 UF_film_width);
00144 }
00145 adjust_comp_flags(CF_mat | CF_focal_length | CF_fov,
00146 CF_film_size);
00147 throw_change_event();
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 void Lens::
00172 set_film_size(const LVecBase2f &film_size) {
00173 _film_size = film_size;
00174
00175
00176
00177 resequence_fov_triad(_film_size_seq, _focal_length_seq, _fov_seq);
00178
00179 if (_fov_seq == 0) {
00180
00181 adjust_user_flags(UF_hfov | UF_vfov | UF_aspect_ratio,
00182 UF_film_width | UF_film_height);
00183 } else {
00184
00185 nassertv(_focal_length_seq == 0);
00186 adjust_user_flags(UF_focal_length | UF_vfov | UF_aspect_ratio,
00187 UF_film_width | UF_film_height);
00188 }
00189 adjust_comp_flags(CF_mat | CF_focal_length | CF_fov | CF_aspect_ratio,
00190 CF_film_size);
00191
00192
00193
00194 compute_aspect_ratio();
00195 adjust_user_flags(0, UF_aspect_ratio);
00196
00197 throw_change_event();
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 const LVecBase2f &Lens::
00207 get_film_size() const {
00208 if ((_comp_flags & CF_film_size) == 0) {
00209
00210
00211
00212 ((Lens *)this)->compute_film_size();
00213 }
00214 return _film_size;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 void Lens::
00228 set_focal_length(float focal_length) {
00229 _focal_length = focal_length;
00230
00231
00232
00233 resequence_fov_triad(_focal_length_seq, _film_size_seq, _fov_seq);
00234
00235 if (_film_size_seq == 0) {
00236
00237 adjust_user_flags(UF_film_width | UF_film_height,
00238 UF_focal_length);
00239 } else {
00240
00241 nassertv(_fov_seq == 0);
00242 adjust_user_flags(UF_hfov | UF_vfov,
00243 UF_focal_length);
00244 }
00245
00246 adjust_comp_flags(CF_mat | CF_fov | CF_film_size,
00247 CF_focal_length);
00248 throw_change_event();
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 float Lens::
00261 get_focal_length() const {
00262 if ((_comp_flags & CF_focal_length) == 0) {
00263 ((Lens *)this)->compute_focal_length();
00264 }
00265 return _focal_length;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 void Lens::
00276 set_fov(float hfov) {
00277 _fov[0] = hfov;
00278
00279
00280
00281 resequence_fov_triad(_fov_seq, _focal_length_seq, _film_size_seq);
00282
00283 if (_focal_length_seq == 0) {
00284
00285 adjust_user_flags(UF_focal_length | UF_vfov,
00286 UF_hfov);
00287 } else {
00288
00289 nassertv(_film_size_seq == 0);
00290 adjust_user_flags(UF_film_width | UF_film_height | UF_vfov,
00291 UF_hfov);
00292 }
00293 adjust_comp_flags(CF_mat | CF_focal_length | CF_fov | CF_film_size,
00294 0);
00295
00296
00297
00298 throw_change_event();
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 void Lens::
00314 set_fov(const LVecBase2f &fov) {
00315 _fov = fov;
00316
00317
00318
00319 resequence_fov_triad(_fov_seq, _focal_length_seq, _film_size_seq);
00320
00321 if (_focal_length_seq == 0) {
00322
00323 adjust_user_flags(UF_focal_length | UF_film_height | UF_aspect_ratio,
00324 UF_hfov | UF_vfov);
00325 } else {
00326
00327 nassertv(_film_size_seq == 0);
00328 adjust_user_flags(UF_film_width | UF_film_height | UF_aspect_ratio,
00329 UF_hfov | UF_vfov);
00330 }
00331 adjust_comp_flags(CF_mat | CF_focal_length | CF_film_size | CF_aspect_ratio,
00332 CF_fov);
00333
00334
00335
00336 compute_aspect_ratio();
00337 adjust_user_flags(0, UF_aspect_ratio);
00338
00339 throw_change_event();
00340 }
00341
00342
00343
00344
00345
00346
00347
00348 const LVecBase2f &Lens::
00349 get_fov() const {
00350 if ((_comp_flags & CF_fov) == 0) {
00351 ((Lens *)this)->compute_fov();
00352 }
00353 return _fov;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 void Lens::
00366 set_aspect_ratio(float aspect_ratio) {
00367 _aspect_ratio = aspect_ratio;
00368 adjust_user_flags(UF_film_height | UF_vfov,
00369 UF_aspect_ratio);
00370 adjust_comp_flags(CF_mat | CF_film_size | CF_fov,
00371 CF_aspect_ratio);
00372 throw_change_event();
00373 }
00374
00375
00376
00377
00378
00379
00380
00381
00382 float Lens::
00383 get_aspect_ratio() const {
00384 if ((_comp_flags & CF_aspect_ratio) == 0) {
00385 ((Lens *)this)->compute_aspect_ratio();
00386 }
00387 return _aspect_ratio;
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397 float Lens::
00398 get_default_near() {
00399 return default_near;
00400 }
00401
00402
00403
00404
00405
00406
00407
00408
00409 float Lens::
00410 get_default_far() {
00411 return default_far;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 void Lens::
00425 set_view_hpr(const LVecBase3f &view_hpr) {
00426 _view_hpr = view_hpr;
00427 adjust_user_flags(UF_view_vector | UF_view_mat,
00428 UF_view_hpr);
00429 adjust_comp_flags(CF_mat | CF_view_vector | CF_iod_offset,
00430 CF_view_hpr);
00431 throw_change_event();
00432 }
00433
00434
00435
00436
00437
00438
00439 const LVecBase3f &Lens::
00440 get_view_hpr() const {
00441 if ((_comp_flags & CF_view_hpr) == 0) {
00442 ((Lens *)this)->compute_view_hpr();
00443 }
00444 return _view_hpr;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456 void Lens::
00457 set_view_vector(const LVector3f &view_vector, const LVector3f &up_vector) {
00458 _view_vector = view_vector;
00459 _up_vector = up_vector;
00460 adjust_user_flags(UF_view_hpr | UF_view_mat,
00461 UF_view_vector);
00462 adjust_comp_flags(CF_mat | CF_view_hpr | CF_iod_offset,
00463 CF_view_vector);
00464 throw_change_event();
00465 }
00466
00467
00468
00469
00470
00471
00472 const LVector3f &Lens::
00473 get_view_vector() const {
00474 if ((_comp_flags & CF_view_vector) == 0) {
00475 ((Lens *)this)->compute_view_vector();
00476 }
00477 return _view_vector;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 const LVector3f &Lens::
00487 get_up_vector() const {
00488 if ((_comp_flags & CF_view_vector) == 0) {
00489 ((Lens *)this)->compute_view_vector();
00490 }
00491 return _up_vector;
00492 }
00493
00494
00495
00496
00497
00498
00499
00500 LPoint3f Lens::
00501 get_nodal_point() const {
00502 return get_view_mat().get_row3(3);
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 void Lens::
00517 set_iod_offset(float iod_offset) {
00518 _iod_offset = iod_offset;
00519 adjust_user_flags(UF_view_mat,
00520 UF_iod_offset);
00521 adjust_comp_flags(CF_mat | CF_view_hpr | CF_view_vector,
00522 CF_iod_offset);
00523 throw_change_event();
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533 float Lens::
00534 get_iod_offset() const {
00535 if ((_comp_flags & CF_iod_offset) == 0) {
00536 ((Lens *)this)->compute_iod_offset();
00537 }
00538 return _iod_offset;
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 void Lens::
00557 set_view_mat(const LMatrix4f &view_mat) {
00558 _lens_mat = view_mat;
00559 adjust_user_flags(UF_view_vector | UF_view_hpr | UF_iod_offset,
00560 UF_view_mat);
00561 adjust_comp_flags(CF_lens_mat_inv | CF_view_hpr | CF_view_vector | CF_iod_offset,
00562 CF_lens_mat);
00563 throw_change_event();
00564 }
00565
00566
00567
00568
00569
00570
00571 const LMatrix4f &Lens::
00572 get_view_mat() const {
00573 if ((_comp_flags & CF_lens_mat) == 0) {
00574 ((Lens *)this)->compute_lens_mat();
00575 }
00576 return _lens_mat;
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638 void Lens::
00639 set_frustum_from_corners(const LVecBase3f &ul, const LVecBase3f &ur,
00640 const LVecBase3f &ll, const LVecBase3f &lr,
00641 int flags) {
00642
00643
00644
00645
00646 const LMatrix4f &lens_mat_inv = get_lens_mat_inv();
00647 LVector3f eye_offset;
00648 lens_mat_inv.get_row3(eye_offset, 3);
00649
00650
00651
00652
00653
00654 LVector3f view_vector;
00655 if ((flags & FC_camera_plane) != 0) {
00656 view_vector = (ul + ur + ll + lr) * 0.25f;
00657 } else {
00658 Planef plane(ll, ul, ur);
00659 view_vector = plane.get_normal();
00660 }
00661
00662
00663
00664
00665
00666 LVector3f up_vector = LVector3f::up(_cs);
00667 if (view_vector == up_vector || ((flags & FC_roll) != 0)) {
00668 LVector3f top = ul - ur;
00669 up_vector = view_vector.cross(top);
00670 }
00671
00672
00673 LMatrix4f rot_mat;
00674 look_at(rot_mat, view_vector, up_vector, CS_zup_right);
00675
00676
00677 LMatrix4f inv_rot_mat;
00678 inv_rot_mat.invert_affine_from(rot_mat);
00679
00680
00681
00682 LPoint3f cul = inv_rot_mat.xform_point(ul);
00683 LPoint3f cur = inv_rot_mat.xform_point(ur);
00684 LPoint3f cll = inv_rot_mat.xform_point(ll);
00685 LPoint3f clr = inv_rot_mat.xform_point(lr);
00686
00687
00688
00689 cul /= cul[1];
00690 cur /= cur[1];
00691 cll /= cll[1];
00692 clr /= clr[1];
00693
00694 LMatrix4f shear_mat = LMatrix4f::ident_mat();
00695 LMatrix4f inv_shear_mat = LMatrix4f::ident_mat();
00696
00697
00698 if ((flags & FC_shear) != 0) {
00699 build_shear_mat(shear_mat, cul, cur, cll, clr);
00700 inv_shear_mat.invert_from(shear_mat);
00701 }
00702
00703
00704 LMatrix4f inv_view_mat =
00705 inv_rot_mat *
00706 inv_shear_mat;
00707
00708
00709 inv_view_mat.set_row(3, eye_offset);
00710
00711 LMatrix4f view_mat;
00712 view_mat.invert_from(inv_view_mat);
00713 set_view_mat(view_mat);
00714
00715 LPoint3f ful = inv_view_mat.xform_point(ul);
00716 LPoint3f fur = inv_view_mat.xform_point(ur);
00717 LPoint3f fll = inv_view_mat.xform_point(ll);
00718 LPoint3f flr = inv_view_mat.xform_point(lr);
00719
00720
00721 ful /= ful[1];
00722 fur /= fur[1];
00723 fll /= fll[1];
00724 flr /= flr[1];
00725
00726
00727
00728 float min_x = min(min(ful[0], fur[0]), min(fll[0], flr[0]));
00729 float max_x = max(max(ful[0], fur[0]), max(fll[0], flr[0]));
00730 float min_z = min(min(ful[2], fur[2]), min(fll[2], flr[2]));
00731 float max_z = max(max(ful[2], fur[2]), max(fll[2], flr[2]));
00732
00733 float x_spread, x_center, z_spread, z_center;
00734
00735 if ((flags & FC_off_axis) != 0) {
00736
00737
00738 x_center = (max_x + min_x) * 0.5f;
00739 z_center = (max_z + min_z) * 0.5f;
00740 x_spread = x_center - min_x;
00741 z_spread = z_center - min_z;
00742 } else {
00743
00744 x_center = 0.0f;
00745 z_center = 0.0f;
00746 x_spread = max(cabs(max_x), cabs(min_x));
00747 z_spread = max(cabs(max_z), cabs(min_z));
00748 }
00749
00750 float aspect_ratio = get_aspect_ratio();
00751 if ((flags & FC_aspect_ratio) == 0) {
00752
00753
00754 if (x_spread < z_spread * aspect_ratio) {
00755
00756 x_spread = z_spread * aspect_ratio;
00757 } else if (z_spread < x_spread / aspect_ratio) {
00758
00759 z_spread = x_spread / aspect_ratio;
00760 }
00761 }
00762
00763 float hfov = rad_2_deg(catan(x_spread)) * 2.0f;
00764 float vfov = rad_2_deg(catan(z_spread)) * 2.0f;
00765
00766 set_fov(hfov, vfov);
00767
00768 if ((flags & FC_aspect_ratio) == 0) {
00769
00770
00771
00772 set_aspect_ratio(aspect_ratio);
00773 }
00774
00775 const LVecBase2f &film_size = get_film_size();
00776 set_film_offset(film_size[0] * x_center / (x_spread * 2.0f),
00777 film_size[1] * z_center / (z_spread * 2.0f));
00778 }
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788 void Lens::
00789 recompute_all() {
00790 _comp_flags = 0;
00791 }
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801 bool Lens::
00802 is_linear() const {
00803 return false;
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 PT(Geom) Lens::
00816 make_geometry() {
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826 int num_segments = define_geom_coords();
00827 if (num_segments == 0) {
00828
00829 _geom_coords.clear();
00830 return (Geom *)NULL;
00831 }
00832
00833
00834 PTA_ushort vindex;
00835 PTA_int lengths;
00836 PTA_Colorf colors;
00837
00838 int num_points = num_segments * 4;
00839
00840
00841 int i;
00842 for (i = 0; i < num_points; i++) {
00843 vindex.push_back(i * num_segments * 2);
00844 }
00845 vindex.push_back(0);
00846 lengths.push_back(num_points + 1);
00847
00848
00849 for (i = 0; i < num_points; i++) {
00850 vindex.push_back(i * num_segments * 2 + 1);
00851 }
00852 vindex.push_back(1);
00853 lengths.push_back(num_points + 1);
00854
00855
00856 vindex.push_back(0);
00857 vindex.push_back(1);
00858 lengths.push_back(2);
00859
00860 vindex.push_back(num_segments * 2 + 0);
00861 vindex.push_back(num_segments * 2+ 1);
00862 lengths.push_back(2);
00863
00864 vindex.push_back(num_segments * 4 + 0);
00865 vindex.push_back(num_segments * 4 + 1);
00866 lengths.push_back(2);
00867
00868 vindex.push_back(num_segments * 6 + 0);
00869 vindex.push_back(num_segments * 6 + 1);
00870 lengths.push_back(2);
00871
00872
00873 vindex.push_back(num_segments * 8 + 0);
00874 vindex.push_back(num_segments * 8 + 1);
00875 lengths.push_back(2);
00876
00877
00878 colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
00879
00880 GeomLinestrip *gline = new GeomLinestrip;
00881 gline->set_coords(_geom_coords, vindex);
00882 gline->set_colors(colors, G_OVERALL);
00883 gline->set_lengths(lengths);
00884 gline->set_num_prims(lengths.size());
00885
00886 return gline;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897 PT(BoundingVolume) Lens::
00898 make_bounds() const {
00899
00900
00901 LPoint3f fll, flr, ful, fur;
00902 LPoint3f nll, nlr, nul, nur;
00903 LPoint2f corner;
00904
00905 corner[0] = -1.0f; corner[1] = 1.0f;
00906
00907
00908 if (!extrude(corner, nul, ful)) {
00909 return (BoundingVolume *)NULL;
00910 }
00911
00912 corner[0] = 1.0f; corner[1] = 1.0f;
00913
00914
00915 if (!extrude(corner, nur, fur)) {
00916 return (BoundingVolume *)NULL;
00917 }
00918
00919 corner[0] = 1.0f; corner[1] = -1.0f;
00920
00921
00922 if (!extrude(corner, nlr, flr)) {
00923 return (BoundingVolume *)NULL;
00924 }
00925
00926 corner[0] = -1.0f; corner[1] = -1.0f;
00927
00928
00929 if (!extrude(corner, nll, fll)) {
00930 return (BoundingVolume *)NULL;
00931 }
00932
00933 return new BoundingHexahedron(fll, flr, fur, ful, nll, nlr, nur, nul);
00934 }
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944 const LMatrix4f &Lens::
00945 get_projection_mat() const {
00946 if ((_comp_flags & CF_projection_mat) == 0) {
00947 ((Lens *)this)->compute_projection_mat();
00948 }
00949 return _projection_mat;
00950 }
00951
00952
00953
00954
00955
00956
00957
00958
00959 const LMatrix4f &Lens::
00960 get_projection_mat_inv() const {
00961 if ((_comp_flags & CF_projection_mat_inv) == 0) {
00962 Lens *non_const = (Lens *)this;
00963 const LMatrix4f &projection_mat = get_projection_mat();
00964 non_const->_projection_mat_inv.invert_from(projection_mat);
00965 non_const->adjust_comp_flags(0, CF_projection_mat_inv);
00966 }
00967 return _projection_mat_inv;
00968 }
00969
00970
00971
00972
00973
00974
00975 void Lens::
00976 output(ostream &out) const {
00977 out << get_type();
00978 }
00979
00980
00981
00982
00983
00984
00985 void Lens::
00986 write(ostream &out, int indent_level) const {
00987 indent(out, indent_level) << get_type() << " fov = " << get_fov() << "\n";
00988 }
00989
00990
00991
00992
00993
00994
00995
00996 void Lens::
00997 throw_change_event() {
00998 ++_last_change;
00999
01000 if (!_change_event.empty()) {
01001 throw_event(_change_event, this);
01002 }
01003
01004
01005 if (!_geom_coords.is_null()) {
01006 if (_geom_coords.get_ref_count() == 1) {
01007
01008
01009 _geom_coords.clear();
01010 } else {
01011
01012
01013 define_geom_coords();
01014 }
01015 }
01016 }
01017
01018
01019
01020
01021
01022
01023
01024 const LMatrix4f &Lens::
01025 get_film_mat() const {
01026 if ((_comp_flags & CF_film_mat) == 0) {
01027 ((Lens *)this)->compute_film_mat();
01028 }
01029 return _film_mat;
01030 }
01031
01032
01033
01034
01035
01036
01037
01038 const LMatrix4f &Lens::
01039 get_film_mat_inv() const {
01040 if ((_comp_flags & CF_film_mat_inv) == 0) {
01041 Lens *non_const = (Lens *)this;
01042 const LMatrix4f &film_mat = get_film_mat();
01043 non_const->_film_mat_inv.invert_from(film_mat);
01044 non_const->adjust_comp_flags(0, CF_film_mat_inv);
01045 }
01046 return _film_mat_inv;
01047 }
01048
01049
01050
01051
01052
01053
01054
01055 const LMatrix4f &Lens::
01056 get_lens_mat() const {
01057 if ((_comp_flags & CF_lens_mat) == 0) {
01058 ((Lens *)this)->compute_lens_mat();
01059 }
01060 return _lens_mat;
01061 }
01062
01063
01064
01065
01066
01067
01068
01069 const LMatrix4f &Lens::
01070 get_lens_mat_inv() const {
01071 if ((_comp_flags & CF_lens_mat_inv) == 0) {
01072 Lens *non_const = (Lens *)this;
01073 const LMatrix4f &lens_mat = get_lens_mat();
01074 non_const->_lens_mat_inv.invert_from(lens_mat);
01075 non_const->adjust_comp_flags(0, CF_lens_mat_inv);
01076 }
01077 return _lens_mat_inv;
01078 }
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097 bool Lens::
01098 extrude_impl(const LPoint3f &point2d, LPoint3f &near_point, LPoint3f &far_point) const {
01099 const LMatrix4f &projection_mat_inv = get_projection_mat_inv();
01100 {
01101 LVecBase4f full(point2d[0], point2d[1], -1.0f, 1.0f);
01102 full = projection_mat_inv.xform(full);
01103 if (full[3] == 0.0f) {
01104 return false;
01105 }
01106
01107 float recip_full3 = 1.0f/full[3];
01108 near_point.set(full[0] * recip_full3, full[1] * recip_full3, full[2] * recip_full3);
01109 }
01110 {
01111 LVecBase4f full(point2d[0], point2d[1], 1.0f, 1.0f);
01112 full = projection_mat_inv.xform(full);
01113 if (full[3] == 0.0f) {
01114 return false;
01115 }
01116 float recip_full3 = 1.0f/full[3];
01117 far_point.set(full[0] * recip_full3, full[1] * recip_full3, full[2] * recip_full3);
01118 }
01119 return true;
01120 }
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140 bool Lens::
01141 project_impl(const LPoint3f &point3d, LPoint3f &point2d) const {
01142 const LMatrix4f &projection_mat = get_projection_mat();
01143 LVecBase4f full(point3d[0], point3d[1], point3d[2], 1.0f);
01144 full = projection_mat.xform(full);
01145 if (full[3] == 0.0f) {
01146 point2d.set(0.0f, 0.0f, 0.0f);
01147 return false;
01148 }
01149 float recip_full3 = 1.0f/full[3];
01150 point2d.set(full[0] * recip_full3, full[1] * recip_full3, full[2] * recip_full3);
01151 return
01152 (full[3] > 0.0f) &&
01153 (point2d[0] >= -1.0f) && (point2d[0] <= 1.0f) &&
01154 (point2d[1] >= -1.0f) && (point2d[1] <= 1.0f);
01155 }
01156
01157
01158
01159
01160
01161
01162
01163 void Lens::
01164 compute_film_size() {
01165 if ((_user_flags & UF_film_width) == 0) {
01166 if ((_user_flags & (UF_hfov | UF_focal_length)) == (UF_hfov | UF_focal_length)) {
01167 _film_size[0] = fov_to_film(_fov[0], _focal_length, true);
01168 } else {
01169 _film_size[0] = 1.0f;
01170 }
01171 }
01172
01173 if ((_user_flags & UF_film_height) == 0) {
01174 if ((_user_flags & (UF_vfov | UF_focal_length)) == (UF_vfov | UF_focal_length)) {
01175 _film_size[1] = fov_to_film(_fov[1], _focal_length, false);
01176
01177 } else if ((_user_flags & (UF_hfov | UF_vfov)) == (UF_hfov | UF_vfov)) {
01178
01179
01180
01181 if ((_comp_flags & CF_focal_length) == 0) {
01182 _focal_length = fov_to_focal_length(_fov[0], _film_size[0], true);
01183 adjust_comp_flags(0, CF_focal_length);
01184 }
01185 _film_size[1] = fov_to_film(_fov[1], _focal_length, false);
01186
01187 } else if ((_user_flags & UF_aspect_ratio) != 0) {
01188 _film_size[1] = _film_size[0] / _aspect_ratio;
01189
01190 } else {
01191
01192 _film_size[1] = _film_size[0];
01193 }
01194 }
01195
01196 adjust_comp_flags(0, CF_film_size);
01197 }
01198
01199
01200
01201
01202
01203
01204
01205
01206 void Lens::
01207 compute_focal_length() {
01208 if ((_user_flags & UF_focal_length) == 0) {
01209 const LVecBase2f &film_size = get_film_size();
01210 const LVecBase2f &fov = get_fov();
01211 _focal_length = fov_to_focal_length(fov[0], film_size[0], true);
01212 }
01213
01214 adjust_comp_flags(0, CF_focal_length);
01215 }
01216
01217
01218
01219
01220
01221
01222
01223 void Lens::
01224 compute_fov() {
01225 const LVecBase2f &film_size = get_film_size();
01226
01227 if ((_user_flags & UF_hfov) == 0) {
01228 if ((_user_flags & UF_focal_length) != 0) {
01229 _fov[0] = film_to_fov(film_size[0], _focal_length, true);
01230 } else {
01231 _fov[0] = _default_fov;
01232 }
01233 }
01234
01235 if ((_user_flags & UF_vfov) == 0) {
01236 if ((_user_flags & UF_focal_length) == 0 &&
01237 (_comp_flags & CF_focal_length) == 0) {
01238
01239
01240 _focal_length = fov_to_focal_length(_fov[0], film_size[0], true);
01241 adjust_comp_flags(0, CF_focal_length);
01242 }
01243 _fov[1] = film_to_fov(film_size[1], _focal_length, false);
01244 }
01245
01246 adjust_comp_flags(0, CF_fov);
01247 }
01248
01249
01250
01251
01252
01253
01254
01255 void Lens::
01256 compute_aspect_ratio() {
01257 if ((_user_flags & UF_aspect_ratio) == 0) {
01258 const LVecBase2f &film_size = get_film_size();
01259 if (film_size[1] == 0.0f) {
01260 _aspect_ratio = 1.0f;
01261 } else {
01262 _aspect_ratio = film_size[0] / film_size[1];
01263 }
01264 adjust_comp_flags(0, CF_aspect_ratio);
01265 }
01266 }
01267
01268
01269
01270
01271
01272
01273
01274 void Lens::
01275 compute_view_hpr() {
01276 if ((_user_flags & UF_view_hpr) == 0) {
01277 const LMatrix4f &view_mat = get_view_mat();
01278 LVecBase3f scale, translate;
01279 decompose_matrix(view_mat, scale, _view_hpr, translate, _cs);
01280 }
01281 adjust_comp_flags(0, CF_view_hpr);
01282 }
01283
01284
01285
01286
01287
01288
01289 void Lens::
01290 compute_view_vector() {
01291 if ((_user_flags & UF_view_vector) == 0) {
01292 const LMatrix4f &view_mat = get_view_mat();
01293 _view_vector = LVector3f::forward(_cs) * view_mat;
01294 _up_vector = LVector3f::up(_cs) * view_mat;
01295 }
01296 adjust_comp_flags(0, CF_view_vector);
01297 }
01298
01299
01300
01301
01302
01303
01304
01305 void Lens::
01306 compute_iod_offset() {
01307 if ((_user_flags & UF_iod_offset) == 0) {
01308 const LMatrix4f &lens_mat_inv = get_lens_mat_inv();
01309 LVector3f translate;
01310 lens_mat_inv.get_row3(translate, 3);
01311 _iod_offset = -translate.dot(LVector3f::right(_cs));
01312 }
01313 adjust_comp_flags(0, CF_iod_offset);
01314 }
01315
01316
01317
01318
01319
01320
01321
01322 void Lens::
01323 compute_projection_mat() {
01324 _projection_mat = LMatrix4f::ident_mat();
01325 _projection_mat_inv = _projection_mat;
01326 adjust_comp_flags(0, CF_projection_mat | CF_projection_mat_inv);
01327 }
01328
01329
01330
01331
01332
01333
01334
01335 void Lens::
01336 compute_film_mat() {
01337
01338
01339
01340
01341
01342
01343 LVecBase2f film_size = get_film_size();
01344 LVector2f film_offset = get_film_offset();
01345
01346
01347
01348
01349
01350
01351
01352 float scale_x = 2.0f / film_size[0];
01353 float scale_y = 2.0f / film_size[1];
01354 _film_mat.set(scale_x, 0.0f, 0.0f, 0.0f,
01355 0.0f, scale_y, 0.0f, 0.0f,
01356 0.0f, 0.0f, 1.0f, 0.0f,
01357 -film_offset[0] * scale_x, -film_offset[1] * scale_y, 0.0f, 1.0f);
01358
01359 adjust_comp_flags(CF_film_mat_inv,
01360 CF_film_mat);
01361 }
01362
01363
01364
01365
01366
01367
01368
01369 void Lens::
01370 compute_lens_mat() {
01371 if ((_user_flags & UF_view_mat) == 0) {
01372 if ((_user_flags & UF_view_hpr) != 0) {
01373 compose_matrix(_lens_mat,
01374 LVecBase3f(1.0f, 1.0f, 1.0f), _view_hpr,
01375 LVecBase3f(0.0f, 0.0f, 0.0f), _cs);
01376
01377 } else if ((_user_flags & UF_view_vector) != 0) {
01378 look_at(_lens_mat, _view_vector, _up_vector, _cs);
01379
01380 } else {
01381 _lens_mat = LMatrix4f::ident_mat();
01382 }
01383
01384 if ((_user_flags & UF_iod_offset) != 0) {
01385 LVector3f iod_vector = _iod_offset * LVector3f::right(_cs);
01386 _lens_mat = LMatrix4f::translate_mat(iod_vector) * _lens_mat;
01387 }
01388 }
01389 adjust_comp_flags(CF_lens_mat_inv,
01390 CF_lens_mat);
01391 }
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402 float Lens::
01403 fov_to_film(float, float, bool) const {
01404 return 1.0f;
01405 }
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416 float Lens::
01417 fov_to_focal_length(float, float, bool) const {
01418 return 1.0f;
01419 }
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430 float Lens::
01431 film_to_fov(float, float, bool) const {
01432 return _default_fov;
01433 }
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449 void Lens::
01450 resequence_fov_triad(char &newest, char &older_a, char &older_b) {
01451 switch (newest) {
01452 case 0:
01453 newest = 2;
01454 older_a--;
01455 older_b--;
01456 nassertv(older_a + older_b == 1);
01457 return;
01458
01459 case 1:
01460 newest = 2;
01461 if (older_a == 2) {
01462 nassertv(older_b == 0);
01463 older_a = 1;
01464 } else {
01465 nassertv(older_a == 0 && older_b == 2);
01466 older_b = 1;
01467 }
01468 return;
01469
01470 case 2:
01471 nassertv(older_a + older_b == 1);
01472 return;
01473
01474 default:
01475 gobj_cat.error()
01476 << "Invalid fov sequence numbers in lens: " << newest << ", " << older_a
01477 << ", " << older_b << "\n";
01478 nassertv(false);
01479 return;
01480 }
01481 }
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493 int Lens::
01494 define_geom_coords() {
01495 int num_segments = 1;
01496 if (!is_linear()) {
01497 num_segments = 10;
01498 }
01499
01500 PTA_Vertexf coords;
01501 LPoint3f near_point, far_point;
01502 for (int si = 0; si < num_segments; si++) {
01503 float t = 2.0f * (float)si / (float)num_segments;
01504
01505
01506 LPoint2f p1(-1.0f + t, 1.0f);
01507 if (!extrude(p1, near_point, far_point)) {
01508
01509 return 0;
01510 }
01511 coords.push_back(near_point);
01512 coords.push_back(far_point);
01513
01514
01515 LPoint2f p2(1.0f, 1.0f - t);
01516 if (!extrude(p2, near_point, far_point)) {
01517
01518 return 0;
01519 }
01520 coords.push_back(near_point);
01521 coords.push_back(far_point);
01522
01523
01524 LPoint2f p3(1.0f - t, -1.0f);
01525 if (!extrude(p3, near_point, far_point)) {
01526
01527 return 0;
01528 }
01529 coords.push_back(near_point);
01530 coords.push_back(far_point);
01531
01532
01533 LPoint2f p4(-1.0f, -1.0f + t);
01534 if (!extrude(p4, near_point, far_point)) {
01535
01536 return 0;
01537 }
01538 coords.push_back(near_point);
01539 coords.push_back(far_point);
01540 }
01541
01542
01543 LPoint3f near_axis = LPoint3f::origin(_cs) + LVector3f::forward(_cs) * _near_distance;
01544 LPoint3f far_axis = LPoint3f::origin(_cs) + LVector3f::forward(_cs) * _far_distance;
01545 const LMatrix4f &lens_mat = get_lens_mat();
01546 near_axis = near_axis * lens_mat;
01547 far_axis = far_axis * lens_mat;
01548 coords.push_back(near_axis);
01549 coords.push_back(far_axis);
01550
01551 if (_geom_coords.is_null()) {
01552
01553 _geom_coords = coords;
01554 } else {
01555
01556
01557 _geom_coords.v().swap(coords.v());
01558 }
01559
01560 return num_segments;
01561 }
01562
01563
01564
01565
01566
01567
01568
01569
01570 void Lens::
01571 build_shear_mat(LMatrix4f &shear_mat,
01572 const LPoint3f &cul, const LPoint3f &cur,
01573 const LPoint3f &cll, const LPoint3f &clr) {
01574
01575
01576
01577
01578 LPoint3f points[4] = {
01579 cul, cur, clr, cll
01580 };
01581
01582 float max_edge_length = -1.0f;
01583 int base_edge = -1;
01584 for (int i = 0; i < 4; i++) {
01585 LVector3f edge = points[(i + 1) % 4] - points[i];
01586 float length = edge.length_squared();
01587 if (length > max_edge_length) {
01588 base_edge = i;
01589 max_edge_length = length;
01590 }
01591 }
01592
01593 const LPoint3f &base_origin = points[base_edge];
01594 LVector3f base_vec = points[(base_edge + 1) % 4] - base_origin;
01595
01596 float base_edge_length = csqrt(max_edge_length);
01597
01598
01599
01600
01601 int a = (base_edge + 2) % 4;
01602 int b = (base_edge + 3) % 4;
01603
01604 float a_dist = sqr_dist_to_line(points[a], base_origin, base_vec);
01605 float b_dist = sqr_dist_to_line(points[b], base_origin, base_vec);
01606
01607 int far_point;
01608 float dist;
01609 if (a_dist > b_dist) {
01610 far_point = a;
01611 dist = csqrt(a_dist);
01612 } else {
01613 far_point = b;
01614 dist = csqrt(b_dist);
01615 }
01616
01617
01618
01619 LVector3f perpendic = base_vec.cross(LVector3f(0.0f, -1.0f, 0.0f));
01620 perpendic.normalize();
01621 perpendic *= dist;
01622 LPoint3f parallel_origin = points[base_edge] + perpendic;
01623
01624
01625
01626 LVector3f base_norm_vec = base_vec / base_edge_length;
01627
01628 LVector3f far_point_delta = points[far_point] - parallel_origin;
01629 float far_point_pos = far_point_delta.dot(base_norm_vec);
01630
01631 if (far_point_pos < 0.0f) {
01632
01633 parallel_origin += base_norm_vec * far_point_pos;
01634
01635 } else if (far_point_pos > base_edge_length) {
01636
01637
01638 parallel_origin += base_norm_vec * (far_point_pos - base_edge_length);
01639 }
01640
01641
01642 float t;
01643 float Ox = parallel_origin[0];
01644 float Oy = parallel_origin[2];
01645 float Vx = base_vec[0];
01646 float Vy = base_vec[2];
01647 float Ax, Ay, Bx, By;
01648
01649 if (far_point == a) {
01650
01651 LVector3f v = points[b] - base_origin;
01652 Ax = points[b][0];
01653 Ay = points[b][2];
01654 Bx = v[0];
01655 By = v[2];
01656 } else {
01657
01658 LVector3f v = points[a] - (base_origin + base_vec);
01659 Ax = points[a][0];
01660 Ay = points[a][2];
01661 Bx = v[0];
01662 By = v[2];
01663 }
01664 t = ((Ox - Ax) * By + (Ay - Oy) * Bx) / (Bx * Vy - By * Vx);
01665
01666 if (t < 0.0f) {
01667
01668
01669 parallel_origin += base_vec * t;
01670 } else if (t > 1.0f) {
01671
01672
01673 parallel_origin += base_vec * (1.0f - t);
01674 }
01675
01676 LVector3f adjacent_norm_vec = parallel_origin - base_origin;
01677 adjacent_norm_vec.normalize();
01678
01679
01680
01681 shear_mat = LMatrix4f::ident_mat();
01682
01683
01684 switch (base_edge) {
01685 case 0:
01686
01687
01688 shear_mat.set_row(0, base_norm_vec);
01689 shear_mat.set_row(2, -adjacent_norm_vec);
01690 break;
01691
01692 case 1:
01693
01694
01695 shear_mat.set_row(0, -adjacent_norm_vec);
01696 shear_mat.set_row(2, -base_norm_vec);
01697 break;
01698
01699 case 2:
01700
01701
01702 shear_mat.set_row(0, -base_norm_vec);
01703 shear_mat.set_row(2, adjacent_norm_vec);
01704 break;
01705
01706 case 3:
01707
01708
01709 shear_mat.set_row(0, adjacent_norm_vec);
01710 shear_mat.set_row(2, base_norm_vec);
01711 break;
01712
01713 default:
01714 nassertv(false);
01715 }
01716 }
01717
01718
01719
01720
01721
01722
01723
01724
01725 float Lens::
01726 sqr_dist_to_line(const LPoint3f &point, const LPoint3f &origin,
01727 const LVector3f &vec) {
01728 LVector3f norm = vec;
01729 norm.normalize();
01730 LVector3f d = point - origin;
01731 float hyp_2 = d.length_squared();
01732 float leg = d.dot(norm);
01733 return hyp_2 - leg * leg;
01734 }