Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / WorldWindowGLAutoDrawable.java
blob87fc633c359313b73bef734f137a182faa3d1224
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind;
9 import gov.nasa.worldwind.cache.TextureCache;
10 import gov.nasa.worldwind.event.*;
11 import gov.nasa.worldwind.geom.Position;
12 import gov.nasa.worldwind.layers.Layer;
13 import gov.nasa.worldwind.pick.PickedObject;
14 import gov.nasa.worldwind.util.*;
16 import javax.media.opengl.*;
17 import java.beans.PropertyChangeEvent;
18 import java.util.logging.Level;
20 /**
21 * A non-platform specific {@link WorldWindow} class. This class can be aggregated into platform-specific classes to
22 * provide the core functionality of World Wind.
24 * @author Tom Gaskins
25 * @version $Id: WorldWindowGLAutoDrawable.java 2980 2007-09-22 01:07:06Z tgaskins $
27 public class WorldWindowGLAutoDrawable extends WorldWindowImpl implements WorldWindowGLDrawable, GLEventListener
29 private GLAutoDrawable drawable;
31 /**
32 * Construct a new <code>WorldWindowGLCanvase</code> for a specified {@link GLDrawable}.
34 public WorldWindowGLAutoDrawable()
36 SceneController sc = this.getSceneController();
37 if (sc != null)
39 sc.addPropertyChangeListener(this);
43 public void initDrawable(GLAutoDrawable glAutoDrawable)
45 if (glAutoDrawable == null)
47 String msg = Logging.getMessage("nullValue.DrawableIsNull");
48 Logging.logger().severe(msg);
49 throw new IllegalArgumentException(msg);
52 this.drawable = glAutoDrawable;
53 this.drawable.setAutoSwapBufferMode(false);
54 this.drawable.addGLEventListener(this);
57 public void initTextureCache(TextureCache textureCache)
59 if (textureCache == null)
61 String msg = Logging.getMessage("nullValue.TextureCacheIsNull");
62 Logging.logger().severe(msg);
63 throw new IllegalArgumentException(msg);
66 this.setTextureCache(textureCache);
69 @Override
70 public void propertyChange(PropertyChangeEvent propertyChangeEvent)
72 if (propertyChangeEvent == null)
74 String msg = Logging.getMessage("nullValue.PropertyChangeEventIsNull");
75 Logging.logger().severe(msg);
76 throw new IllegalArgumentException(msg);
79 if (this.drawable != null)
80 this.drawable.repaint(); // Queue a JOGL repaint request.
83 /**
84 * See {@link GLEventListener#init(GLAutoDrawable)}.
86 * @param glAutoDrawable the drawable
88 public void init(GLAutoDrawable glAutoDrawable)
90 // Clear the texture cache if the window is reinitializing, most likely with a new gl hardware context.
91 if (this.getTextureCache() != null)
92 this.getTextureCache().clear();
94 if (this.getModel() != null && this.getModel().getLayers() != null)
96 for (Layer layer : this.getModel().getLayers())
98 layer.dispose();
102 // this.drawable.setGL(new DebugGL(this.drawable.getGL()));
106 * See {@link GLEventListener#display(GLAutoDrawable)}.
108 * @param glAutoDrawable the drawable
109 * @throws IllegalStateException if no {@link SceneController} exists for this canvas
111 public void display(GLAutoDrawable glAutoDrawable)
113 // System.out.printf("Caches: Memory %d (%d) %d items, Texture %d (%d) %d items, free memory %d\n",
114 // WorldWind.memoryCache().getUsedCapacity(), WorldWind.memoryCache().getCapacity(), WorldWind.memoryCache().getNumObjects(),
115 // this.getTextureCache().getUsedCapacity(), this.getTextureCache().getCapacity(), this.getTextureCache().getNumObjects(),
116 // Runtime.getRuntime().freeMemory());
119 SceneController sc = this.getSceneController();
120 if (sc == null)
122 Logging.logger().severe("WorldWindowGLCanvas.ScnCntrllerNullOnRepaint");
123 throw new IllegalStateException(Logging.getMessage("WorldWindowGLCanvas.ScnCntrllerNullOnRepaint"));
126 Position positionAtStart = this.getCurrentPosition();
127 PickedObject selectionAtStart = this.getCurrentSelection();
131 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.BEFORE_RENDERING));
133 catch (Exception e)
135 Logging.logger().log(Level.SEVERE,
136 Logging.getMessage("WorldWindowGLAutoDrawable.ExceptionDuringGLEventListenerDisplay"), e);
139 this.doDisplay();
143 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.BEFORE_BUFFER_SWAP));
145 catch (Exception e)
147 Logging.logger().log(Level.SEVERE,
148 Logging.getMessage("WorldWindowGLAutoDrawable.ExceptionDuringGLEventListenerDisplay"), e);
151 this.doSwapBuffers(this.drawable);
153 Double frameTime = sc.getFrameTime();
154 if (frameTime != null)
155 this.setValue(PerformanceStatistic.FRAME_TIME, frameTime);
157 Double frameRate = sc.getFramesPerSecond();
158 if (frameTime != null)
159 this.setValue(PerformanceStatistic.FRAME_RATE, frameRate);
161 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.AFTER_BUFFER_SWAP));
163 // Position and selection notification occurs only on triggering conditions, not same-state conditions:
164 // start == null, end == null: nothing selected -- don't notify
165 // start == null, end != null: something now selected -- notify
166 // start != null, end == null: something was selected but no longer is -- notify
167 // start != null, end != null, start != end: something new was selected -- notify
168 // start != null, end != null, start == end: same thing is selected -- don't notify
170 Position positionAtEnd = this.getCurrentPosition();
171 if (positionAtStart != null || positionAtEnd != null)
173 if (positionAtStart != positionAtEnd)
174 this.callPositionListeners(new PositionEvent(this.drawable, sc.getPickPoint(),
175 positionAtStart, positionAtEnd));
178 PickedObject selectionAtEnd = this.getCurrentSelection();
179 if (selectionAtStart != null || selectionAtEnd != null)
181 if (selectionAtStart != selectionAtEnd)
182 this.callSelectListeners(new SelectEvent(this.drawable, SelectEvent.ROLLOVER,
183 sc.getPickPoint(), sc.getPickedObjectList()));
186 catch (Exception e)
188 Logging.logger().log(Level.SEVERE, Logging.getMessage(
189 "WorldWindowGLCanvas.ExceptionAttemptingRepaintWorldWindow"), e);
193 protected void doDisplay()
195 this.getSceneController().repaint();
198 protected void doSwapBuffers(GLAutoDrawable drawable)
200 drawable.swapBuffers();
204 * See {@link GLEventListener#reshape(GLAutoDrawable,int,int,int,int)}.
206 * @param glAutoDrawable the drawable
208 public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int w, int h)
213 * See {@link GLEventListener#displayChanged(GLAutoDrawable,boolean,boolean)}.
215 * @param glAutoDrawable the drawable
217 public void displayChanged(GLAutoDrawable glAutoDrawable, boolean b, boolean b1)
219 Logging.logger().finest("WorldWindowGLCanvas.DisplayEventListenersDisplayChangedMethodCalled");
222 public void redrawNow()
224 if (this.drawable != null)
225 this.drawable.display();