Example usage for javax.media.j3d Transform3D lookAt

List of usage examples for javax.media.j3d Transform3D lookAt

Introduction

In this page you can find the example usage for javax.media.j3d Transform3D lookAt.

Prototype

public void lookAt(Point3d eye, Point3d center, Vector3d up) 

Source Link

Document

Helping function that specifies the position and orientation of a view matrix.

Usage

From source file:ucar.unidata.idv.flythrough.Flythrough.java

/**
 * _more_//w w  w  . j  av a  2s  .  c om
 *
 * @param pt1 _more_
 * @param xyz1 _more_
 * @param xyz2 _more_
 * @param actualPoint _more_
 * @param animateMove _more_
 */
protected void goTo(FlythroughPoint pt1, double[] xyz1, double[] xyz2, double[] actualPoint,
        boolean animateMove) {

    currentHeading = 180;
    if (actualPoint == null) {
        actualPoint = xyz2;
    }
    NavigatedDisplay navDisplay = viewManager.getNavigatedDisplay();
    MouseBehavior mouseBehavior = navDisplay.getMouseBehavior();
    double[] currentMatrix = navDisplay.getProjectionMatrix();
    double[] aspect = navDisplay.getDisplayAspect();
    try {

        if (pt1.getDescription() != null) {
            getHtmlView().setText(pt1.getDescription());
        } else {
            getHtmlView().setText("");
        }

        processReadout(pt1);

        float x1 = (float) xyz1[0];
        float y1 = (float) xyz1[1];
        float z1 = (float) xyz1[2];

        float x2 = (float) xyz2[0];
        float y2 = (float) xyz2[1];
        float z2 = (float) xyz2[2];

        double zoom = (pt1.hasZoom() ? pt1.getZoom() : getZoom());
        if (zoom == 0) {
            zoom = 0.1;
        }

        double tiltx = (pt1.hasTiltX() ? pt1.getTiltX() : tilt[0]);
        double tilty = (pt1.hasTiltY() ? pt1.getTiltY() : tilt[1]);
        double tiltz = (pt1.hasTiltZ() ? pt1.getTiltZ() : tilt[2]);

        //Check for nans
        if ((x2 != x2) || (y2 != y2) || (z2 != z2)) {
            return;
        }

        if ((x1 != x1) || (y1 != y1) || (z1 != z1)) {
            return;
        }

        double[] m = pt1.getMatrix();
        if (m == null) {
            m = new double[16];

            Transform3D t = new Transform3D();
            if (orientation.equals(ORIENT_UP)) {
                y2 = y1 + 100;
                x2 = x1;
            } else if (orientation.equals(ORIENT_DOWN)) {
                y2 = y1 - 100;
                x2 = x1;
            } else if (orientation.equals(ORIENT_LEFT)) {
                x2 = x1 - 100;
                y2 = y1;
            } else if (orientation.equals(ORIENT_RIGHT)) {
                x2 = x1 + 100;
                y2 = y1;
            }

            if ((x1 == x2) && (y1 == y2) && (z1 == z2)) {
                return;
            }

            Vector3d upVector;
            if (doGlobe()) {
                upVector = new Vector3d(x1, y1, z1);
            } else {
                upVector = new Vector3d(0, 0, 1);
            }

            //Keep flat in z for non globe
            Point3d p1 = new Point3d(x1, y1, z1);
            Point3d p2 = new Point3d(x2, y2, ((!getUseFixedZ() || doGlobe()) ? z2 : z1));
            t.lookAt(p1, p2, upVector);

            t.get(m);

            EarthLocation el1 = navDisplay.getEarthLocation(p1.x, p1.y, p1.z, false);
            EarthLocation el2 = navDisplay.getEarthLocation(p2.x, p2.y, p2.z, false);
            Bearing bearing = Bearing.calculateBearing(new LatLonPointImpl(getLat(el1), getLon(el1)),
                    new LatLonPointImpl(getLat(el2), getLon(el2)), null);
            currentHeading = bearing.getAngle();

            double[] tiltMatrix = mouseBehavior.make_matrix(tiltx, tilty, tiltz, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0);
            m = mouseBehavior.multiply_matrix(tiltMatrix, m);
            if (aspect != null) {
                double[] aspectMatrix = mouseBehavior.make_matrix(0.0, 0.0, 0.0, aspect[0], aspect[1],
                        aspect[2], 0.0, 0.0, 0.0);
                //                    m = mouseBehavior.multiply_matrix(aspectMatrix, m);
            }

            double[] scaleMatrix = mouseBehavior.make_matrix(0.0, 0.0, 0.0, zoom, 0.0, 0.0, 0.0);

            m = mouseBehavior.multiply_matrix(scaleMatrix, m);
        }

        currentPoint = pt1;
        location = currentPoint.getEarthLocation();

        if (doGlobe()) {
            setPts(locationLine, 0, x1 * 2, 0, y1 * 2, 0, z1 * 2);
            //                setPts(locationLine2, 0, x2 * 2, 0, y2 * 2, 0, z2 * 2);
        } else {
            setPts(locationLine, x1, x1, y1, y1, 1, -1);
        }

        RealTuple markerLocation = new RealTuple(RealTupleType.SpatialCartesian3DTuple,
                new double[] { x1, y1, z1 });

        if (xyz1[0] != xyz2[0]) {
            Transform3D rotTransform;
            VisADGeometryArray marker = (VisADGeometryArray) getMarker().clone();
            double rotx = 0;
            double roty = 0;
            double rotz = 0;
            rotz = -Math.toDegrees(Math.atan2(actualPoint[1] - xyz1[1], actualPoint[0] - xyz1[0])) + 90;

            if (doGlobe()) {
                Vector3d upVector = new Vector3d(x1, y1, z1);
                rotTransform = new Transform3D();
                rotTransform.lookAt(new Point3d(x1, y1, z1), new Point3d(x2, y2, z2), upVector);
                Matrix3d m3d = new Matrix3d();
                rotTransform.get(m3d);
                rotTransform = new Transform3D(m3d, new Vector3d(0, 0, 0), 1);
                rotTransform.invert();
                //                    ShapeUtility.rotate(marker, rotTransform,(float)x1,(float)y1,(float)z1);
                ShapeUtility.rotate(marker, rotTransform);

            } else {
                double[] markerM = navDisplay.getMouseBehavior().make_matrix(rotx, roty, rotz, 1.0, 0.0, 0.0,
                        0.0);
                rotTransform = new Transform3D(markerM);
                ShapeUtility.rotate(marker, rotTransform);
            }

            locationMarker.setPoint(markerLocation, marker);
        } else {
            locationMarker.setPoint(markerLocation);
        }

        locationLine.setVisible(showLine);
        //            locationLine2.setVisible(showLine);
        locationMarker.setVisible(showMarker);

        if (hasTimes && getShowTimes()) {
            DateTime dttm = pt1.getDateTime();
            if (dttm != null) {
                viewManager.getAnimationWidget().setTimeFromUser(dttm);
            }

        }

        if (changeViewpointCbx.isSelected()) {
            if (animateMove) {
                navDisplay.animateMatrix(m, animationSpeed);
            } else {
                navDisplay.setProjectionMatrix(m);
            }
        }

        if (!Misc.equals(lastLocation, pt1.getEarthLocation())) {
            lastLocation = pt1.getEarthLocation();
            EarthLocationTuple tuplePosition = new EarthLocationTuple(lastLocation.getLatitude(),
                    lastLocation.getLongitude(), lastLocation.getAltitude());
            doShare(ucar.unidata.idv.control.ProbeControl.SHARE_POSITION, tuplePosition);
        }

    } catch (NumberFormatException exc) {
        logException("Error parsing number:" + exc, exc);
    } catch (javax.media.j3d.BadTransformException bte) {
        try {
            navDisplay.setProjectionMatrix(currentMatrix);
        } catch (Exception ignore) {
        }
    } catch (Exception exc) {
        logException("Error", exc);
        if (animationWidget != null) {
            animationWidget.setRunning(false);
        }
        return;
    }

}