コード例 #1
0
ファイル: Box.cs プロジェクト: zhj149/WorldWindJava.NET
//    public static void main(String[] args)
//    {
//        Box box = new Box(new Vec4[] {new Vec4(1, 0, 0), new Vec4(0, 1, 0), new Vec4(0, 0, 1)},
//            -.5, .5, -.5, .5, -.5, .5);
//        Line line = new Line(new Vec4(-1, 0.5, 0.5), new Vec4(1, 0, 0));
//        Intersection[] intersections = box.intersect(line);
//        if (intersections != null && intersections.Length > 0 && intersections[0] != null)
//            System.out.println(intersections[0]);
//        if (intersections != null && intersections.Length > 1 && intersections[1] != null)
//            System.out.println(intersections[1]);
//    }

//    /** {@inheritDoc} */
//    public Intersection[] intersect(Line line)
//    {
//        return WWMath.polytopeIntersect(line, this.planes);
//        // Algorithm from "3-D Computer Graphics" by Samuel R. Buss, 2005, Section X.1.4.
//
//        // Determine intersection with each plane and categorize the intersections as "front" if the line intersects
//        // the front side of the plane (dot product of line direction with plane normal is negative) and "back" if the
//        // line intersects the back side of the plane (dot product of line direction with plane normal is positive).
//
//        double fMax = -Double.MaxValue;
//        double bMin = Double.MaxValue;
//        bool isTangent = false;
//
//        Vec4 u = line.getDirection();
//        Vec4 p = line.getOrigin();
//
//        foreach (Plane plane in this.planes)
//        {
//            Vec4 n = plane.getNormal();
//            double d = -plane.getDistance();
//
//            double s = u.dot3(n);
//            if (s == 0) // line is parallel to plane
//            {
//                double pdn = p.dot3(n);
//                if (pdn > d) // is line in positive halfspace (in front of) of the plane?
//                    return null; // no intersection
//                else
//                {
//                    if (pdn == d)
//                        isTangent = true; // line coincident with plane
//                    continue; // line is in negative halfspace; possible intersection; check other planes
//                }
//            }
//
//            // Determine whether front or back intersection.
//            double a = (d - p.dot3(n)) / s;
//            if (u.dot3(n) < 0) // line intersects front face and therefore entering box
//            {
//                if (a > fMax)
//                {
//                    if (a > bMin)
//                        return null;
//                    fMax = a;
//                }
//            }
//            else // line intersects back face and therefore leaving box
//            {
//                if (a < bMin)
//                {
//                    if (a < 0 || a < fMax)
//                        return null;
//                    bMin = a;
//                }
//            }
//        }
//
//        // Compute the Cartesian intersection points. There will be no more than two.
//        if (fMax >= 0) // intersects frontface and backface; point origin is outside the box
//            return new Intersection[]
//                {
//                    new Intersection(p.add3(u.multiply3(fMax)), isTangent),
//                    new Intersection(p.add3(u.multiply3(bMin)), isTangent)
//                };
//        else // intersects backface only; point origin is within the box
//            return new Intersection[] {new Intersection(p.add3(u.multiply3(bMin)), isTangent)};
//    }

        /**
         * Draws a representation of the <code>Box</code>.
         *
         * @param dc the <code>DrawContext</code> to be used.
         */
        public void render(DrawContext dc)
        {
            if (dc == null)
            {
                String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            if (dc.isPickingMode())
            {
                return;
            }

            Vec4 a = this.s.add3(this.t).multiply3(-0.5);
            Vec4 b = this.s.subtract3(this.t).multiply3(0.5);
            Vec4 c = this.s.add3(this.t).multiply3(0.5);
            Vec4 d = this.t.subtract3(this.s).multiply3(0.5);

            GL2             gl   = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.
            OGLStackHandler ogsh = new OGLStackHandler();

            ogsh.pushAttrib(gl, GL2.GL_COLOR_BUFFER_BIT // For alpha enable, blend enable, alpha func, blend func.
                            | GL2.GL_CURRENT_BIT        // For current color.
                            | GL2.GL_LINE_BIT           // For line width.
                            | GL2.GL_TRANSFORM_BIT      // For matrix mode.
                            | GL2.GL_DEPTH_BUFFER_BIT); // For depth test enable, depth func.
            try
            {
                gl.glLineWidth(1f);
                gl.glEnable(GL.GL_BLEND);
                OGLUtil.applyBlending(gl, false);
                gl.glEnable(GL.GL_DEPTH_TEST);

                gl.glDepthFunc(GL.GL_LEQUAL);
                gl.glColor4f(1f, 1f, 1f, 0.5f);
                this.drawBox(dc, a, b, c, d);

                gl.glDepthFunc(GL.GL_GREATER);
                gl.glColor4f(1f, 0f, 1f, 0.4f);
                this.drawBox(dc, a, b, c, d);
            }
            finally
            {
                ogsh.pop(gl);
            }
        }
コード例 #2
0
ファイル: Cylinder.cs プロジェクト: zhj149/WorldWindJava.NET
        /**
         * Display the cylinder.
         *
         * @param dc the current draw context.
         *
         * @throws ArgumentException if the draw context is null.
         */
        public void render(DrawContext dc)
        {
            if (dc == null)
            {
                String msg = Logging.getMessage("nullValue.DrawContextIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            // Compute a matrix that will transform world coordinates to cylinder coordinates. The negative z-axis
            // will point from the cylinder's bottomCenter to its topCenter. The y-axis will be a vector that is
            // perpendicular to the cylinder's axisUnitDirection. Because the cylinder is symmetric, it does not matter
            // in what direction the y-axis points, as long as it is perpendicular to the z-axis.
            double tolerance = 1e-6;
            Vec4   upVector  = (this.axisUnitDirection.cross3(Vec4.UNIT_Y).getLength3() <= tolerance) ?
                               Vec4.UNIT_NEGATIVE_Z : Vec4.UNIT_Y;
            Matrix transformMatrix = Matrix.fromModelLookAt(this.bottomCenter, this.topCenter, upVector);

            double[] matrixArray = new double[16];
            transformMatrix.toArray(matrixArray, 0, false);

            GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.

            OGLStackHandler ogsh = new OGLStackHandler();

            ogsh.pushAttrib(gl, GL2.GL_CURRENT_BIT | GL2.GL_ENABLE_BIT | GL2.GL_TRANSFORM_BIT | GL2.GL_DEPTH_BUFFER_BIT);
            try
            {
                // The cylinder is drawn with as a wireframe plus a center axis. It's drawn in two passes in order to
                // visualize the portions of the cylinder above and below an intersecting surface.

                gl.glEnable(GL.GL_BLEND);
                OGLUtil.applyBlending(gl, false);
                gl.glEnable(GL.GL_DEPTH_TEST);

                // Draw the axis
                gl.glDepthFunc(GL.GL_LEQUAL); // draw the part that would normally be visible
                gl.glColor4f(1f, 1f, 1f, 0.4f);
                gl.glBegin(GL2.GL_LINES);
                gl.glVertex3d(this.bottomCenter.x, this.bottomCenter.y, this.bottomCenter.z);
                gl.glVertex3d(this.topCenter.x, this.topCenter.y, this.topCenter.z);
                gl.glEnd();

                gl.glDepthFunc(GL.GL_GREATER); // draw the part that is behind an intersecting surface
                gl.glColor4f(1f, 0f, 1f, 0.4f);
                gl.glBegin(GL2.GL_LINES);
                gl.glVertex3d(this.bottomCenter.x, this.bottomCenter.y, this.bottomCenter.z);
                gl.glVertex3d(this.topCenter.x, this.topCenter.y, this.topCenter.z);
                gl.glEnd();

                // Draw the exterior wireframe
                ogsh.pushModelview(gl);
                gl.glMultMatrixd(matrixArray, 0);

                GLUquadric quadric = dc.getGLU().gluNewQuadric();
                dc.getGLU().gluQuadricDrawStyle(quadric, GLU.GLU_LINE);

                gl.glDepthFunc(GL.GL_LEQUAL);
                gl.glColor4f(1f, 1f, 1f, 0.5f);
                dc.getGLU().gluCylinder(quadric, this.cylinderRadius, this.cylinderRadius, this.cylinderHeight, 30, 30);

                gl.glDepthFunc(GL.GL_GREATER);
                gl.glColor4f(1f, 0f, 1f, 0.4f);
                dc.getGLU().gluCylinder(quadric, this.cylinderRadius, this.cylinderRadius, this.cylinderHeight, 30, 30);

                dc.getGLU().gluDeleteQuadric(quadric);
            }
            finally
            {
                ogsh.pop(gl);
            }
        }