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
25
26
27
28
29
30
31
32
33
34
35
36
37
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
53
54 protected int lastGLName = 1;
55
56 private IUnrealMap map;
57
58
59
60
61
62
63
64
65 protected MapGLPanel(GLCapabilities caps, IUnrealMap map, Logger log) {
66 super(caps);
67
68
69
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
78 mapViewpoint = new MapViewpoint();
79 mapController = new MapController(this, mapViewpoint, mapFocus);
80 mapController.registerListeners();
81
82
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
90 this.addGLEventListener(environmentRenderer);
91
92
93 mapViewpoint.addViewpointListener(this);
94
95
96 mapViewpoint.setFromViewedBox(map.getBox());
97
98 }
99
100
101
102
103
104
105
106 protected MapGLPanel(IUnrealMap map, Logger log) {
107 this(getCapabilities(), map, log);
108 }
109
110
111
112
113
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
124
125
126
127 @Override
128 public synchronized void onChangedViewpoint(MapViewpoint viewpoint) {
129 display();
130 }
131
132
133
134
135
136
137
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
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
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
170
171
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 }