📄 screenimage.java
字号:
/*Copyright (C) 2001, 2008 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.render;import gov.nasa.worldwind.pick.Pickable;import gov.nasa.worldwind.pick.PickSupport;import gov.nasa.worldwind.util.WWIO;import gov.nasa.worldwind.util.Logging;import gov.nasa.worldwind.exception.WWRuntimeException;import gov.nasa.worldwind.layers.Layer;import javax.media.opengl.GL;import java.awt.*;import java.awt.image.BufferedImage;import java.util.logging.Level;import java.io.InputStream;import java.io.IOException;import com.sun.opengl.util.texture.Texture;import com.sun.opengl.util.texture.TextureCoords;import com.sun.opengl.util.texture.TextureIO;/** * @author tag * @version $Id: ScreenImage.java 5955 2008-08-13 18:32:59Z tgaskins $ */public class ScreenImage implements Renderable, Pickable // TODO: arg checking{ private Object imageSource; private OrderedImage orderedImage = new OrderedImage(); private PickSupport pickSupport = new PickSupport(); private int imageWidth; private int imageHeight; private Point screenLocation; private double opacity = 1d; private Layer client; private class OrderedImage implements OrderedRenderable { public double getDistanceFromEye() { return 0; } public void pick(DrawContext dc, Point pickPoint) { ScreenImage.this.draw(dc); } public void render(DrawContext dc) { ScreenImage.this.draw(dc); } } public Point getScreenLocation() { return this.screenLocation; } public void setScreenLocation(Point screenLocation) { this.screenLocation = screenLocation; } public Object getImageSource() { return this.imageSource; } public void setImageSource(Object imageSource) { this.imageSource = imageSource; } public Layer getClient() { return client; } public void setClient(Layer client) { this.client = client; } public double getOpacity() { return opacity; } public void setOpacity(double opacity) { this.opacity = opacity; } public void render(DrawContext dc) { this.doRender(dc); } public void pick(DrawContext dc, Point pickPoint) { this.doRender(dc); } protected void doRender(DrawContext dc) { dc.addOrderedRenderable(this.orderedImage); } private void draw(DrawContext dc) { if (this.screenLocation == null || this.imageSource == 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; // Don't depth buffer. gl.glDisable(GL.GL_DEPTH_TEST); // Suppress any fully transparent image pixels gl.glEnable(GL.GL_ALPHA_TEST); gl.glAlphaFunc(GL.GL_GREATER, 0.001f); Texture texture = null; if (!dc.isPickingMode()) { texture = dc.getTextureCache().get(this.imageSource); if (texture == null) { this.initializeTexture(dc); texture = dc.getTextureCache().get(this.imageSource); if (texture == null) return; } } java.awt.Rectangle viewport = dc.getView().getViewport(); gl.glMatrixMode(javax.media.opengl.GL.GL_PROJECTION); gl.glPushMatrix(); projectionPushed = true; gl.glLoadIdentity(); gl.glOrtho(0d, viewport.width, 0d, viewport.height, -1, 1); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPushMatrix(); modelviewPushed = true; gl.glLoadIdentity(); gl.glTranslated(this.screenLocation.x - this.imageWidth / 2, viewport.height - (this.screenLocation.y + this.imageHeight / 2), 0d); if (!dc.isPickingMode()) { if (texture != null) { gl.glEnable(GL.GL_TEXTURE_2D); texture.bind(); gl.glColor4d(1d, 1d, 1d, this.opacity); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); TextureCoords texCoords = texture.getImageTexCoords(); gl.glScaled(this.imageWidth, this.imageHeight, 1d); dc.drawUnitQuad(texCoords); } } else { this.pickSupport.clearPickList(); this.pickSupport.beginPicking(dc); Color color = dc.getUniquePickColor(); int colorCode = color.getRGB(); this.pickSupport.addPickableObject(colorCode, this, null, false); gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()); gl.glScaled(this.imageWidth, this.imageHeight, 1d); dc.drawUnitQuad(); this.pickSupport.endPicking(dc); this.pickSupport.resolvePick(dc, dc.getPickPoint(), this.client); } } 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 initializeTexture(DrawContext dc) { Texture iconTexture = dc.getTextureCache().get(this.imageSource); if (iconTexture != null) return; try { if (this.imageSource instanceof BufferedImage) { iconTexture = TextureIO.newTexture((BufferedImage) this.imageSource, true); iconTexture.bind(); this.imageWidth = iconTexture.getWidth(); this.imageHeight = iconTexture.getHeight(); dc.getTextureCache().put(this.imageSource, iconTexture); } else if (this.imageSource instanceof String) { // Handle local file or resource String imagePath = (String) this.imageSource; Object streamOrException = WWIO.getFileOrResourceAsStream(imagePath, this.getClass()); if (streamOrException == null || streamOrException instanceof Exception) { Logging.logger().log(Level.SEVERE, "layers.TextureLayer.ExceptionAttemptingToReadTextureFile", streamOrException != null ? streamOrException : ""); return; } iconTexture = TextureIO.newTexture((InputStream) streamOrException, true, null); iconTexture.bind(); this.imageWidth = iconTexture.getWidth(); this.imageHeight = iconTexture.getHeight(); dc.getTextureCache().put(imagePath, iconTexture); } else { Logging.logger().log(java.util.logging.Level.SEVERE, "generic.UnrecognizedImageSourceTypeeType", this.imageSource.getClass().getName()); return; } } 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -