View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.map;
2   
3   import java.awt.Point;
4   import java.util.HashSet;
5   import java.util.Set;
6   import java.util.logging.Logger;
7   
8   import javax.media.opengl.GLCapabilities;
9   import javax.media.opengl.GLProfile;
10  import javax.media.opengl.awt.GLJPanel;
11  
12  import nl.tudelft.goal.ut2004.visualizer.timeline.map.EnvironmentRenderer;
13  import nl.tudelft.goal.ut2004.visualizer.timeline.map.GLRendererCollection;
14  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableUTAgent;
15  import nl.tudelft.goal.ut2004.visualizer.timeline.map.IRenderableWorldObject;
16  import nl.tudelft.goal.ut2004.visualizer.timeline.map.ISubGLRenderer;
17  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapController;
18  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapRenderer;
19  import nl.tudelft.goal.ut2004.visualizer.timeline.map.MapViewpoint;
20  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
21  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
22  
23  /**
24   * This is a GLJPanel that displays UT2004Map. it is a base class, so it takes
25   * care of some things, while others are left for derived class to do.
26   * <p>
27   * What it does: * Can render passed map * Can render all stuff in
28   * agentRenderers * Takes care of user interaction for map viewpoint * Selection
29   * of object
30   * <p>
31   * What it doesn't do: * automatically display map, someone else has to take
32   * care of that (adding/removing) from agentRenderers * fill in agentRenderers,
33   * derived class has to do that.
34   * 
35   * In lookup are selected objects
36   * 
37   * @author Honza
38   */
39  public abstract class MapGLPanel extends GLJPanel implements
40  		MapViewpoint.ViewpointListener {
41  	protected Logger logger;
42  
43  	protected MapViewpoint mapViewpoint;
44  	protected MapController mapController;
45  
46  	protected MapRenderer mapRenderer;
47  	protected GLRendererCollection<IRenderableUTAgent> agentRenderes;
48  	protected GLRendererCollection<IRenderableWorldObject> objectRenderes;
49  
50  	protected EnvironmentRenderer environmentRenderer;
51  
52  	// iterator used to assign unique names for gl rendering, this enabling
53  	// selection of objects
54  	protected int lastGLName = 1;
55  
56  	private IUnrealMap map;
57  
58  	/**
59  	 * Create a panel for
60  	 * 
61  	 * @param caps
62  	 * @param map
63  	 * @param log
64  	 */
65  	protected MapGLPanel(GLCapabilities caps, IUnrealMap map, Logger log) {
66  		super(caps);
67  
68  		// if (Beans.isDesignTime()) {
69  		// Beans.setDesignTime(false);
70  		// }
71  
72  		this.map = map;
73  		this.logger = log;
74  
75  		Location mapFocus = new Location(map.getBox().getCenterX(), map
76  				.getBox().getCenterY(), map.getBox().getCenterZ());
77  		// Stuff for controlling viewpoint in map
78  		mapViewpoint = new MapViewpoint();
79  		mapController = new MapController(this, mapViewpoint, mapFocus);
80  		mapController.registerListeners();
81  
82  		// Create renderers
83  		mapRenderer = new MapRenderer(map, lastGLName++);
84  		agentRenderes = new GLRendererCollection<IRenderableUTAgent>();
85  		objectRenderes = new GLRendererCollection<IRenderableWorldObject>();
86  		environmentRenderer = new EnvironmentRenderer(mapViewpoint,
87  				agentRenderes, objectRenderes, mapRenderer);
88  
89  		// Add listener so this level is rendered
90  		this.addGLEventListener(environmentRenderer);
91  
92  		// Listen for changes in viewpoint
93  		mapViewpoint.addViewpointListener(this);
94  
95  		// Set initial position of view + thanks to listener display
96  		mapViewpoint.setFromViewedBox(map.getBox());
97  
98  	}
99  
100 	/**
101 	 * Create a pane showing passed map
102 	 * 
103 	 * @param map
104 	 *            Map this pane is supposed to show
105 	 */
106 	protected MapGLPanel(IUnrealMap map, Logger log) {
107 		this(getCapabilities(), map, log);
108 	}
109 
110 	/**
111 	 * I require HW acceleration and double buffering.
112 	 * 
113 	 * @return Set of required capabilities
114 	 */
115 	private static GLCapabilities getCapabilities() {
116 		GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
117 		caps.setHardwareAccelerated(true);
118 		caps.setDoubleBuffered(true);
119 		return caps;
120 	}
121 
122 	/**
123 	 * When viewpoint is changed, render the map (call display()).
124 	 * 
125 	 * @param viewpoint
126 	 */
127 	@Override
128 	public synchronized void onChangedViewpoint(MapViewpoint viewpoint) {
129 		display();
130 	}
131 
132 	/**
133 	 * Get agents at point p in the scene.
134 	 * 
135 	 * @param p
136 	 *            in window coordiates system, [0,0] is left top
137 	 * @return List of renderable agents that are at the passed point
138 	 */
139 	public synchronized Set<IRenderableWorldObject> getObjectsAt(Point p) {
140 		environmentRenderer.setSelectPoint(p);
141 		display();
142 		int[] list = environmentRenderer.getSelectedObjects();
143 
144 		Set<IRenderableWorldObject> selection = new HashSet<IRenderableWorldObject>();
145 
146 		// find that miserable renderer in agents
147 		for (ISubGLRenderer<IRenderableUTAgent> agentRenderer : agentRenderes.getSubRenderes()) {
148 			IRenderableUTAgent renderableAgent = agentRenderer.getObject();
149 			for (int glName : list) {
150 				if (glName == renderableAgent.getGLName()) {
151 					selection.add(renderableAgent);
152 				}
153 			}
154 		}
155 		// find that miserable renderer in world objects
156 		for (ISubGLRenderer<IRenderableWorldObject> objectRenderer : objectRenderes.getSubRenderes()) {
157 			IRenderableWorldObject renderableObject = objectRenderer.getObject();
158 			for (int glName : list) {
159 				if (glName == renderableObject.getGLName()) {
160 					selection.add(renderableObject);
161 				}
162 			}
163 		}
164 
165 		return selection;
166 	}
167 
168 	/**
169 	 * Remove listeners and basically clean up this map. Any call to this object
170 	 * after this method should invoke exception (it doesn't but I can always
171 	 * hope in NullPointerException).
172 	 */
173 	public synchronized void destroy() {
174 		this.removeGLEventListener(environmentRenderer);
175 		this.mapViewpoint.removeViewpointListener(this);
176 
177 		mapRenderer = null;
178 		environmentRenderer = null;
179 		agentRenderes = null;
180 
181 		mapController = null;
182 		mapViewpoint = null;
183 	}
184 
185 	protected IUnrealMap getMap() {
186 		return map;
187 	}
188 }