1 package nl.tudelft.goal.ut2004.visualizer.timeline.map;
2
3 import static javax.media.opengl.GL.GL_COLOR_BUFFER_BIT;
4 import static javax.media.opengl.GL.GL_DEPTH_TEST;
5 import static javax.media.opengl.GL.GL_VIEWPORT;
6 import static javax.media.opengl.GL2.GL_RENDER;
7 import static javax.media.opengl.GL2.GL_SELECT;
8 import static javax.media.opengl.fixedfunc.GLLightingFunc.GL_LIGHTING;
9 import static javax.media.opengl.fixedfunc.GLLightingFunc.GL_SMOOTH;
10 import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_MODELVIEW;
11 import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_PROJECTION;
12
13 import java.awt.Point;
14 import java.nio.IntBuffer;
15 import java.util.Arrays;
16
17 import javax.media.opengl.GL;
18 import javax.media.opengl.GL2;
19 import javax.media.opengl.GLAutoDrawable;
20 import javax.media.opengl.GLEventListener;
21 import javax.media.opengl.glu.GLU;
22 import javax.vecmath.Vector3d;
23
24 import com.jogamp.common.nio.Buffers;
25 import com.jogamp.opengl.util.gl2.GLUT;
26
27 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
28 import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
29
30
31
32
33
34
35
36
37 public class EnvironmentRenderer implements GLEventListener {
38 private static GLU glu = new GLU();
39 private static GLUT glut = new GLUT();
40
41 private MapViewpoint viewpoint;
42 private CollectionRenderer<IPlayer> agentRenderes;
43 private CollectionRenderer<Object> objectRenderes;
44 private MapRenderer mapRenderer;
45
46 public EnvironmentRenderer(MapViewpoint viewpoint, CollectionRenderer<IPlayer> agentRenderes,
47 CollectionRenderer<Object> objectRenderes, MapRenderer mapRenderer) {
48 this.viewpoint = viewpoint;
49 this.agentRenderes = agentRenderes;
50 this.objectRenderes = objectRenderes;
51 this.mapRenderer = mapRenderer;
52
53 }
54
55 @Override
56 public synchronized void init(GLAutoDrawable glDrawable) {
57 GL2 gl = glDrawable.getGL().getGL2();
58
59 gl.glMatrixMode(GL_PROJECTION);
60 gl.glLoadIdentity();
61 gl.glMatrixMode(GL_MODELVIEW);
62 gl.glLoadIdentity();
63
64 gl.glEnable(GL.GL_DEPTH_TEST);
65 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
66
67 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
68 gl.glClearDepth(1.0);
69 gl.glShadeModel(GL_SMOOTH);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 gl.glDisable(GL_LIGHTING);
87
88
89 agentRenderes.prepare(gl);
90 objectRenderes.prepare(gl);
91 mapRenderer.prepare(gl);
92 }
93
94 @Override
95 public synchronized void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
96 GL2 gl = drawable.getGL().getGL2();
97 setProjection(gl, x, y, width, height);
98 }
99
100
101
102
103 private synchronized void setProjection(GL2 gl, int x, int y, int width, int height) {
104 if (height <= 0) {
105 height = 1;
106 }
107 float h = (float) width / (float) height;
108 gl.glViewport(0, 0, width, height);
109 gl.glMatrixMode(GL_PROJECTION);
110 gl.glLoadIdentity();
111 glu.gluPerspective(viewpoint.getViewAngle(), h, 10.0, 100000.0);
112 gl.glMatrixMode(GL_MODELVIEW);
113 gl.glLoadIdentity();
114 }
115
116
117
118
119
120
121 private synchronized void prepareCanvas(GL2 gl) {
122
123 gl.glEnable(GL_DEPTH_TEST);
124 gl.glClearColor(0.45f, 0.45f, 0.45f, 0f);
125
126 gl.glClear(GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
127
128
129 gl.glMatrixMode(GL_MODELVIEW);
130 gl.glLoadIdentity();
131
132
133 Location observerLoc = viewpoint.getLocation();
134 Location eyeLoc = viewpoint.getEye();
135 Vector3d upVec = viewpoint.getUp();
136
137
138 glu.gluLookAt(eyeLoc.x, eyeLoc.y, eyeLoc.z, observerLoc.x, observerLoc.y, observerLoc.z, upVec.x, upVec.y,
139 upVec.z);
140
141 gl.glScaled(1., 1., -1.);
142 }
143
144 @Override
145 public synchronized void display(GLAutoDrawable glDrawable) {
146 GL2 gl = glDrawable.getGL().getGL2();
147
148 int viewport[] = new int[4];
149 gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
150
151
152
153
154
155 int[] selectBufferArray = new int[512];
156 IntBuffer selectBuffer = Buffers.newDirectIntBuffer(selectBufferArray.length);
157
158 gl.glSelectBuffer(selectBufferArray.length, selectBuffer);
159
160
161
162
163 gl.glRenderMode(GL_SELECT);
164
165
166
167 gl.glInitNames();
168 gl.glPushName(-1);
169
170
171
172 gl.glMatrixMode(GL_PROJECTION);
173 gl.glPushMatrix();
174 {
175 gl.glLoadIdentity();
176
177
178 glu.gluPickMatrix(selectPoint.x, viewport[3] - selectPoint.y, 1.0f, 1.0f, viewport, 0);
179 glu.gluPerspective(viewpoint.getViewAngle(), (float) viewport[2] / (float) viewport[3], 10.0, 100000.0);
180
181 prepareCanvas(gl);
182 mapRenderer.render(gl);
183 agentRenderes.render(gl);
184 objectRenderes.render(gl);
185 gl.glMatrixMode(GL_PROJECTION);
186 }
187 gl.glPopMatrix();
188
189 gl.glMatrixMode(GL_MODELVIEW);
190
191
192
193
194
195
196 int numOfHits = gl.glRenderMode(GL_RENDER);
197 selectBuffer.get(selectBufferArray);
198
199
200 selectedObjects = new int[numOfHits];
201 for (int hitIndex = 0; hitIndex < numOfHits; hitIndex++) {
202 selectedObjects[hitIndex] = selectBufferArray[hitIndex * 4 + 3];
203 }
204
205 this.selectDirtyFlag = false;
206
207 prepareCanvas(gl);
208
209
210 mapRenderer.render(gl);
211 agentRenderes.render(gl);
212 objectRenderes.render(gl);
213
214
215
216 String res = "[" + selectPoint.x + ", " + selectPoint.y + "] # " + numOfHits;
217 for (int objId : selectedObjects) {
218 res += ":" + objId;
219 }
220 renderText(gl, res, 0, 0, GLUT.BITMAP_HELVETICA_12);
221
222 }
223
224 private void renderText(GL2 gl, String text, int x, int y, int font) {
225 int viewport[] = new int[4];
226 gl.glGetIntegerv(GL_VIEWPORT, viewport, 0);
227
228 gl.glMatrixMode(GL_MODELVIEW);
229 gl.glPushMatrix();
230 gl.glLoadIdentity();
231 gl.glMatrixMode(GL_PROJECTION);
232 gl.glPushMatrix();
233 gl.glLoadIdentity();
234 {
235 gl.glOrtho(0, viewport[2], 0, viewport[3], -1, 1);
236 gl.glColor3d(1, 1, 1);
237 gl.glRasterPos3d(0, 0, 0);
238 glut.glutBitmapString(font, text);
239 }
240 gl.glMatrixMode(GL_PROJECTION);
241 gl.glPopMatrix();
242 gl.glMatrixMode(GL_MODELVIEW);
243 gl.glPopMatrix();
244
245 }
246
247
248 private boolean selectDirtyFlag = true;
249
250
251 private Point selectPoint = new Point();
252
253 private int[] selectedObjects = new int[0];
254
255 public synchronized int[] getSelectedObjects() throws IllegalStateException {
256 if (selectDirtyFlag) {
257 throw new IllegalStateException(
258 "Not objects from selected point. Did you call display() after setSelectPoint()?");
259 }
260
261 return Arrays.copyOf(selectedObjects, selectedObjects.length);
262 }
263
264
265
266
267
268
269
270 public synchronized void setSelectPoint(Point point) {
271 if (point == null) {
272 throw new IllegalArgumentException("Point cannot be null");
273 }
274
275 this.selectPoint = new Point(point);
276 this.selectDirtyFlag = true;
277 }
278
279
280 @Override
281 public void dispose(GLAutoDrawable arg0) {
282
283
284 }
285 }