Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / WorldWindowGLAutoDrawable.java
blob6829f0a8ff9842b13743c9d83f9283126142bf36
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.pick.PickedObject;
13 import gov.nasa.worldwind.util.*;
15 import javax.media.opengl.*;
16 import java.beans.PropertyChangeEvent;
17 import java.util.logging.Level;
19 /**
20 * A non-platform specific {@link WorldWindow} class. This class can be aggregated into platform-specific classes to
21 * provide the core functionality of World Wind.
23 * @author Tom Gaskins
24 * @version $Id: WorldWindowGLAutoDrawable.java 2471 2007-07-31 21:50:57Z tgaskins $
26 public class WorldWindowGLAutoDrawable extends WorldWindowImpl implements WorldWindowGLDrawable, GLEventListener
28 private GLAutoDrawable drawable;
30 /**
31 * Construct a new <code>WorldWindowGLCanvase</code> for a specified {@link GLDrawable}.
33 public WorldWindowGLAutoDrawable()
35 SceneController sc = this.getSceneController();
36 if (sc != null)
38 sc.addPropertyChangeListener(this);
42 public void initDrawable(GLAutoDrawable glAutoDrawable)
44 if (glAutoDrawable == null)
46 String msg = Logging.getMessage("nullValue.DrawableIsNull");
47 Logging.logger().severe(msg);
48 throw new IllegalArgumentException(msg);
51 this.drawable = glAutoDrawable;
52 this.drawable.setAutoSwapBufferMode(false);
53 this.drawable.addGLEventListener(this);
56 public void initTextureCache(TextureCache textureCache)
58 if (textureCache == null)
60 String msg = Logging.getMessage("nullValue.TextureCacheIsNull");
61 Logging.logger().severe(msg);
62 throw new IllegalArgumentException(msg);
65 this.setTextureCache(textureCache);
68 @Override
69 public void propertyChange(PropertyChangeEvent propertyChangeEvent)
71 if (propertyChangeEvent == null)
73 String msg = Logging.getMessage("nullValue.PropertyChangeEventIsNull");
74 Logging.logger().severe(msg);
75 throw new IllegalArgumentException(msg);
78 if (this.drawable != null)
79 this.drawable.repaint(); // Queue a JOGL repaint request.
82 /**
83 * See {@link GLEventListener#init(GLAutoDrawable)}.
85 * @param glAutoDrawable the drawable
87 public void init(GLAutoDrawable glAutoDrawable)
89 // This GLEventListener callback method is not used.
90 // this.drawable.setGL(new DebugGL(this.drawable.getGL()));
93 /**
94 * See {@link GLEventListener#display(GLAutoDrawable)}.
96 * @param glAutoDrawable the drawable
97 * @throws IllegalStateException if no {@link SceneController} exists for this canvas
99 public void display(GLAutoDrawable glAutoDrawable)
101 // System.out.printf("Caches: Memory %d (%d) %d items, Texture %d (%d) %d items, free memory %d\n",
102 // WorldWind.memoryCache().getUsedCapacity(), WorldWind.memoryCache().getCapacity(), WorldWind.memoryCache().getNumObjects(),
103 // this.getTextureCache().getUsedCapacity(), this.getTextureCache().getCapacity(), this.getTextureCache().getNumObjects(),
104 // Runtime.getRuntime().freeMemory());
107 SceneController sc = this.getSceneController();
108 if (sc == null)
110 Logging.logger().severe("WorldWindowGLCanvas.ScnCntrllerNullOnRepaint");
111 throw new IllegalStateException(Logging.getMessage("WorldWindowGLCanvas.ScnCntrllerNullOnRepaint"));
114 Position positionAtStart = this.getCurrentPosition();
115 PickedObject selectionAtStart = this.getCurrentSelection();
119 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.BEFORE_RENDERING));
121 catch (Exception e)
123 Logging.logger().log(Level.SEVERE,
124 Logging.getMessage("WorldWindowGLAutoDrawable.ExceptionDuringGLEventListenerDisplay"), e);
127 this.doDisplay();
131 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.BEFORE_BUFFER_SWAP));
133 catch (Exception e)
135 Logging.logger().log(Level.SEVERE,
136 Logging.getMessage("WorldWindowGLAutoDrawable.ExceptionDuringGLEventListenerDisplay"), e);
139 this.doSwapBuffers(this.drawable);
141 Double frameTime = sc.getFrameTime();
142 if (frameTime != null)
143 this.setValue(PerformanceStatistic.FRAME_TIME, frameTime);
145 Double frameRate = sc.getFramesPerSecond();
146 if (frameTime != null)
147 this.setValue(PerformanceStatistic.FRAME_RATE, frameRate);
149 this.callRenderingListeners(new RenderingEvent(this.drawable, RenderingEvent.AFTER_BUFFER_SWAP));
151 // Position and selection notification occurs only on triggering conditions, not same-state conditions:
152 // start == null, end == null: nothing selected -- don't notify
153 // start == null, end != null: something now selected -- notify
154 // start != null, end == null: something was selected but no longer is -- notify
155 // start != null, end != null, start != end: something new was selected -- notify
156 // start != null, end != null, start == end: same thing is selected -- don't notify
158 Position positionAtEnd = this.getCurrentPosition();
159 if (positionAtStart != null || positionAtEnd != null)
161 if (positionAtStart != positionAtEnd)
162 this.callPositionListeners(new PositionEvent(this.drawable, sc.getPickPoint(),
163 positionAtStart, positionAtEnd));
166 PickedObject selectionAtEnd = this.getCurrentSelection();
167 if (selectionAtStart != null || selectionAtEnd != null)
169 if (selectionAtStart != selectionAtEnd)
170 this.callSelectListeners(new SelectEvent(this.drawable, SelectEvent.ROLLOVER,
171 sc.getPickPoint(), sc.getPickedObjectList()));
174 catch (Exception e)
176 Logging.logger().log(Level.SEVERE, Logging.getMessage(
177 "WorldWindowGLCanvas.ExceptionAttemptingRepaintWorldWindow"), e);
181 protected void doDisplay()
183 this.getSceneController().repaint();
186 protected void doSwapBuffers(GLAutoDrawable drawable)
188 drawable.swapBuffers();
192 * See {@link GLEventListener#reshape(GLAutoDrawable,int,int,int,int)}.
194 * @param glAutoDrawable the drawable
196 public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int w, int h)
201 * See {@link GLEventListener#displayChanged(GLAutoDrawable,boolean,boolean)}.
203 * @param glAutoDrawable the drawable
205 public void displayChanged(GLAutoDrawable glAutoDrawable, boolean b, boolean b1)
207 Logging.logger().finest("WorldWindowGLCanvas.DisplayEventListenersDisplayChangedMethodCalled");
210 public void redrawNow()
212 if (this.drawable != null)
213 this.drawable.display();