compasslayer.java

来自「world wind java sdk 源码」· Java 代码 · 共 667 行 · 第 1/2 页

JAVA
667
字号
            Vec4 locationSW = this.computeLocation(viewport, scale);            double heading = this.computeHeading(dc.getView());            double pitch = this.computePitch(dc.getView());            gl.glTranslated(locationSW.x, locationSW.y, locationSW.z);            gl.glScaled(scale, scale, 1);            if (!dc.isPickingMode())            {                gl.glTranslated(width / 2, height / 2, 0);                if (this.showTilt) // formula contributed by Ty Hayden                    gl.glRotated(70d * (pitch / 90.0), 1d, 0d, 0d);                gl.glRotated(heading, 0d, 0d, 1d);                gl.glTranslated(-width / 2, -height / 2, 0);                Texture iconTexture = dc.getTextureCache().get(this);                if (iconTexture == null)                {                    this.initializeTexture(dc);                    iconTexture = dc.getTextureCache().get(this);                    if (iconTexture == null)                    {                        // TODO: log warning                    }                }                if (iconTexture != null)                {                    gl.glEnable(GL.GL_TEXTURE_2D);                    iconTexture.bind();                    gl.glColor4d(1d, 1d, 1d, this.getOpacity());                    gl.glEnable(GL.GL_BLEND);                    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);                    TextureCoords texCoords = iconTexture.getImageTexCoords();                    gl.glScaled(width, height, 1d);                    dc.drawUnitQuad(texCoords);                }            }            else            {                // Picking                this.pickSupport.clearPickList();                this.pickSupport.beginPicking(dc);                // Compute pick point 'heading' relative to compass center                Vec4 center = new Vec4(locationSW.x + width * scale / 2, locationSW.y + height * scale / 2, 0);                double px = dc.getPickPoint().x - center.x;                double py = viewport.getHeight() - dc.getPickPoint().y - center.y;                Angle pickHeading = Angle.fromRadians(Math.atan2(px, py));                pickHeading = pickHeading.degrees >= 0 ? pickHeading : pickHeading.addDegrees(360);                // Draw unique color across the compass area                Color color = dc.getUniquePickColor();                int colorCode = color.getRGB();                // Add our object(s) to the pickable list with pick heading value                PickedObject po = new PickedObject(colorCode, this, null, false);                po.setValue("Heading", pickHeading);                this.pickSupport.addPickableObject(po);                // Draw                gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue());                gl.glScaled(width, height, 1d);                dc.drawUnitQuad();                // Done picking                this.pickSupport.endPicking(dc);                this.pickSupport.resolvePick(dc, dc.getPickPoint(), this);            }        }        finally        {            if (projectionPushed)            {                gl.glMatrixMode(GL.GL_PROJECTION);                gl.glPopMatrix();            }            if (modelviewPushed)            {                gl.glMatrixMode(GL.GL_MODELVIEW);                gl.glPopMatrix();            }            if (attribsPushed)                gl.glPopAttrib();        }    }//    private void drawOld(DrawContext dc)//    {//        if (this.iconFilePath == null)//            return;////        GL gl = dc.getGL();////        boolean attribsPushed = false;//        boolean modelviewPushed = false;//        boolean projectionPushed = false;////        try//        {//            gl.glPushAttrib(GL.GL_DEPTH_BUFFER_BIT//                | GL.GL_COLOR_BUFFER_BIT//                | GL.GL_ENABLE_BIT//                | GL.GL_TEXTURE_BIT//                | GL.GL_TRANSFORM_BIT//                | GL.GL_VIEWPORT_BIT//                | GL.GL_CURRENT_BIT);//            attribsPushed = true;////            Texture iconTexture = dc.getTextureCache().get(this);//            if (iconTexture == null)//            {//                this.initializeTexture(dc);//                iconTexture = dc.getTextureCache().get(this);//                if (iconTexture == null)//                {//                    // TODO: log warning//                    return;//                }//            }////            gl.glEnable(GL.GL_TEXTURE_2D);//            iconTexture.bind();////            gl.glColor4d(1d, 1d, 1d, this.getOpacity());//            gl.glEnable(GL.GL_BLEND);//            gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);//            gl.glDisable(GL.GL_DEPTH_TEST);////            double width = this.getScaledIconWidth();//            double height = this.getScaledIconHeight();////            // Load a parallel projection with xy dimensions (viewportWidth, viewportHeight)//            // into the GL projection matrix.//            java.awt.Rectangle viewport = dc.getView().getViewport();//            gl.glMatrixMode(javax.media.opengl.GL.GL_PROJECTION);//            gl.glPushMatrix();//            projectionPushed = true;//            gl.glLoadIdentity();//            double maxwh = width > height ? width : height;//            gl.glOrtho(0d, viewport.width, 0d, viewport.height, -0.6 * maxwh, 0.6 * maxwh);////            gl.glMatrixMode(GL.GL_MODELVIEW);//            gl.glPushMatrix();//            modelviewPushed = true;//            gl.glLoadIdentity();////            double scale = this.computeScale(viewport);//            Vec4 locationSW = this.computeLocation(viewport, scale);//            double heading = this.computeHeading(dc.getView());//            double pitch = this.computePitch(dc.getView());////            gl.glTranslated(locationSW.x, locationSW.y, locationSW.z);//            gl.glScaled(scale, scale, 1);////            gl.glTranslated(width / 2, height / 2, 0);//            if (this.showTilt) // formula contributed by Ty Hayden//                gl.glRotated(70d * (pitch / 90.0), 1d, 0d, 0d);//            gl.glRotated(heading, 0d, 0d, 1d);//            gl.glTranslated(-width / 2, -height / 2, 0);////            TextureCoords texCoords = iconTexture.getImageTexCoords();//            gl.glScaled(width, height, 1d);//            dc.drawUnitQuad(texCoords);//        }//        finally//        {//            if (projectionPushed)//            {//                gl.glMatrixMode(GL.GL_PROJECTION);//                gl.glPopMatrix();//            }//            if (modelviewPushed)//            {//                gl.glMatrixMode(GL.GL_MODELVIEW);//                gl.glPopMatrix();//            }//            if (attribsPushed)//                gl.glPopAttrib();//        }//    }    private double computeScale(java.awt.Rectangle viewport)    {        if (this.resizeBehavior.equals(AVKey.RESIZE_SHRINK_ONLY))        {            return Math.min(1d, (this.compassToViewportScale) * viewport.width / this.getScaledIconWidth());        }        else if (this.resizeBehavior.equals(AVKey.RESIZE_STRETCH))        {            return (this.compassToViewportScale) * viewport.width / this.getScaledIconWidth();        }        else if (this.resizeBehavior.equals(AVKey.RESIZE_KEEP_FIXED_SIZE))        {            return 1d;        }        else        {            return 1d;        }    }    private double getScaledIconWidth()    {        return this.iconWidth * this.iconScale;    }    private double getScaledIconHeight()    {        return this.iconHeight * this.iconScale;    }    private Vec4 computeLocation(java.awt.Rectangle viewport, double scale)    {        double width = this.getScaledIconWidth();        double height = this.getScaledIconHeight();        double scaledWidth = scale * width;        double scaledHeight = scale * height;        double x;        double y;        if (this.locationCenter != null)        {            x = this.locationCenter.x - scaledWidth / 2;            y = this.locationCenter.y - scaledHeight / 2;        }        else if (this.position.equals(AVKey.NORTHEAST))        {            x = viewport.getWidth() - scaledWidth - this.borderWidth;            y = viewport.getHeight() - scaledHeight - this.borderWidth;        }        else if (this.position.equals(AVKey.SOUTHEAST))        {            x = viewport.getWidth() - scaledWidth - this.borderWidth;            y = 0d + this.borderWidth;        }        else if (this.position.equals(AVKey.NORTHWEST))        {            x = 0d + this.borderWidth;            y = viewport.getHeight() - scaledHeight - this.borderWidth;        }        else if (this.position.equals(AVKey.SOUTHWEST))        {            x = 0d + this.borderWidth;            y = 0d + this.borderWidth;        }        else // use North East as default        {            x = viewport.getWidth() - scaledWidth - this.borderWidth;            y = viewport.getHeight() - scaledHeight - this.borderWidth;        }        if (this.locationOffset != null)        {            x += this.locationOffset.x;            y += this.locationOffset.y;        }        return new Vec4(x, y, 0);    }    private double computeHeading(View view)    {        if (view == null)            return 0.0;        if (!(view instanceof OrbitView))            return 0.0;        OrbitView orbitView = (OrbitView) view;        return orbitView.getHeading().getDegrees();    }    private double computePitch(View view)    {        if (view == null)            return 0.0;        if (!(view instanceof OrbitView))            return 0.0;        OrbitView orbitView = (OrbitView) view;        return orbitView.getPitch().getDegrees();    }    private void initializeTexture(DrawContext dc)    {        Texture iconTexture = dc.getTextureCache().get(this);        if (iconTexture != null)            return;        try        {            InputStream iconStream = this.getClass().getResourceAsStream("/" + this.iconFilePath);            if (iconStream == null)            {                File iconFile = new File(this.iconFilePath);                if (iconFile.exists())                {                    iconStream = new FileInputStream(iconFile);                }            }            iconTexture = TextureIO.newTexture(iconStream, false, null);            iconTexture.bind();            this.iconWidth = iconTexture.getWidth();            this.iconHeight = iconTexture.getHeight();            dc.getTextureCache().put(this, iconTexture);        }        catch (IOException e)        {            String msg = Logging.getMessage("layers.IOExceptionDuringInitialization");            Logging.logger().severe(msg);            throw new WWRuntimeException(msg, e);        }        GL gl = dc.getGL();        gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);//_MIPMAP_LINEAR);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);        // Enable texture anisotropy, improves "tilted" compass quality.        int[] maxAnisotropy = new int[1];        gl.glGetIntegerv(GL.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy, 0);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy[0]);    }    @Override    public String toString()    {        return Logging.getMessage("layers.CompassLayer.Name");    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?