1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.gui; |
---|
7 | |
---|
8 | import java.awt.geom.Point2D; |
---|
9 | import javax.media.opengl.GL; |
---|
10 | import javax.media.opengl.GLAutoDrawable; |
---|
11 | import javax.media.opengl.GLCanvas; |
---|
12 | import javax.media.opengl.GLCapabilities; |
---|
13 | import javax.media.opengl.GLEventListener; |
---|
14 | import javax.media.opengl.glu.GLU; |
---|
15 | import javax.swing.JPanel; |
---|
16 | |
---|
17 | import com.sun.opengl.util.Animator; |
---|
18 | import com.sun.opengl.util.GLUT; |
---|
19 | |
---|
20 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
21 | import vnsim.core.CarInfo; |
---|
22 | import vnsim.map.object.Cross; |
---|
23 | import vnsim.map.object.Globals; |
---|
24 | import vnsim.map.object.Point; |
---|
25 | import vnsim.map.object.Road; |
---|
26 | import vnsim.map.utils.GPSutil; |
---|
27 | import vnsim.vehicular.simulator.intersections.DirectedRoadSegment; |
---|
28 | import vnsim.vehicular.simulator.intersections.IntersectionWithTrafficLights; |
---|
29 | |
---|
30 | public class DriverView extends JPanel implements GLEventListener { |
---|
31 | |
---|
32 | /* |
---|
33 | * NEAR VIEW Panel that contains an OpenGL canvas |
---|
34 | */ |
---|
35 | |
---|
36 | private GLCanvas canvas; |
---|
37 | |
---|
38 | private int carList, carListOut, earthList; // display |
---|
39 | |
---|
40 | // lists(only |
---|
41 | // calculated at |
---|
42 | // compilation time) |
---|
43 | private float xCamera, yCamera, zCamera, xToSee, yToSee, distanceToLook, visualField; // camera |
---|
44 | |
---|
45 | private float previosPosX, previosPosY;// old position of the |
---|
46 | // carReceivingMsg and |
---|
47 | |
---|
48 | public SimulatedCarInfo currentCar; |
---|
49 | |
---|
50 | static final long serialVersionUID = 1; |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | * @param w |
---|
55 | * @param h |
---|
56 | */ |
---|
57 | public DriverView(int w, int h) { |
---|
58 | super(); |
---|
59 | // creates an OpenGL canvas and sets it's properties |
---|
60 | GLCapabilities capabilities = new GLCapabilities(); |
---|
61 | capabilities.setHardwareAccelerated(true); |
---|
62 | capabilities.setDoubleBuffered(true); |
---|
63 | canvas = new GLCanvas(); |
---|
64 | canvas.addGLEventListener(this); |
---|
65 | canvas.setSize(w, h); |
---|
66 | canvas.setVisible(true); |
---|
67 | Animator anim = new Animator(canvas); // for animating the display |
---|
68 | this.add(canvas); // adds the canvas to the container |
---|
69 | this.setSize(w, h); |
---|
70 | anim.start(); |
---|
71 | this.previosPosX = 0; // to remember the carReceivingMsg's previous |
---|
72 | // position |
---|
73 | this.previosPosY = 0; // helps determining the direction in witch the |
---|
74 | // carReceivingMsg moves |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * |
---|
79 | */ |
---|
80 | public void init(GLAutoDrawable glDrawable) { |
---|
81 | this.xCamera = 0.0f; // set the initial position |
---|
82 | // of the camera |
---|
83 | this.yCamera = 0.0f; |
---|
84 | this.zCamera = 20.0f; |
---|
85 | this.xToSee = 10.0f; |
---|
86 | this.yToSee = 10.0f; |
---|
87 | this.visualField = 2000.0f; |
---|
88 | this.distanceToLook = 50.0f; |
---|
89 | GL myGL = glDrawable.getGL(); // OpenGL initialization |
---|
90 | GLU myGLU = new GLU(); |
---|
91 | int width = canvas.getWidth(); |
---|
92 | int height = canvas.getHeight(); |
---|
93 | myGL.glClearColor(0.7f, 0.7f, 1.0f, 0.0f); // the ambiental color |
---|
94 | myGL.glShadeModel(GL.GL_FASTEST); |
---|
95 | myGL.glMatrixMode(GL.GL_PROJECTION); |
---|
96 | myGL.glLoadIdentity(); |
---|
97 | myGLU.gluPerspective(60.0f, (double) width / height, 0.01f, visualField); |
---|
98 | myGL.glMatrixMode(GL.GL_MODELVIEW); |
---|
99 | myGL.glLoadIdentity(); |
---|
100 | myGLU.gluLookAt(xCamera, yCamera, zCamera, xToSee, yToSee, 0.0, 0.0, 0.0, 1.0); |
---|
101 | glDrawable.setAutoSwapBufferMode(false); // i will call |
---|
102 | // swapBuffers |
---|
103 | // manualy in |
---|
104 | // display |
---|
105 | bildList(glDrawable); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * |
---|
110 | */ |
---|
111 | public void display(GLAutoDrawable glDrawable) { |
---|
112 | float x, y; |
---|
113 | Road currentRoad; |
---|
114 | Point currentPoint; |
---|
115 | short pt; |
---|
116 | int i; |
---|
117 | GL myGL = glDrawable.getGL(); |
---|
118 | glDrawable.swapBuffers(); |
---|
119 | myGL.glClear(GL.GL_COLOR_BUFFER_BIT); |
---|
120 | currentRoad = null; |
---|
121 | currentPoint = null; |
---|
122 | |
---|
123 | float auxx, auxy, x2, y2, sinAlfa, cosAlfa, dx, dy; |
---|
124 | pt = 0; |
---|
125 | // if i began to receive data from the gps |
---|
126 | if (Globals.demo.mv.currentCar != null) { |
---|
127 | try { |
---|
128 | // I get the current position of the carReceivingMsg on the road |
---|
129 | currentCar = Globals.demo.mv.currentCar; |
---|
130 | currentRoad = Globals.map.roads.get(currentCar.getRoadIdx()); |
---|
131 | currentPoint = (Point) currentRoad.points.get(currentCar.getPointIdx()); |
---|
132 | // I calculate the distance in meters on x and on y axis |
---|
133 | // from the point of minimum latitude and longitude |
---|
134 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
135 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
136 | // I use the last position and the current position to calculate |
---|
137 | // where to put the camera and in what |
---|
138 | // direction to look |
---|
139 | setCamera(glDrawable, (double) x, (double) y, (double) previosPosX, (double) previosPosY, |
---|
140 | currentRoad.laneNo, currentCar.getLane(), currentCar.getDirection()); |
---|
141 | // i draw the grass (green square in the xoy plane, centered on |
---|
142 | // the current position) |
---|
143 | myGL.glPushMatrix(); |
---|
144 | myGL.glTranslatef(x, y, -1.0f); |
---|
145 | myGL.glCallList(earthList); |
---|
146 | myGL.glPopMatrix(); |
---|
147 | /* |
---|
148 | * i = 0; while (i < currentRoad.crosses.size()) { // for every |
---|
149 | * road with witch the current road has a // cross, i draw that |
---|
150 | * road rd2 = (short) ((Cross) currentRoad.crosses .get((int) |
---|
151 | * i)).getCrossRoadIndex(); drawRoad(rd2, myGL); i++; } |
---|
152 | */ |
---|
153 | drawNearbyRoads(currentCar.getRoadIdx(), myGL); // last, i draw |
---|
154 | // the current |
---|
155 | // road |
---|
156 | // i draw the carReceivingMsg on it's current position |
---|
157 | myGL.glPushMatrix(); |
---|
158 | myGL.glTranslatef(x, y, 0.0f); |
---|
159 | myGL.glCallList(carList); |
---|
160 | myGL.glPopMatrix(); |
---|
161 | } catch (Exception e) { |
---|
162 | System.err.println("NearView Error: "); |
---|
163 | e.printStackTrace(); |
---|
164 | } |
---|
165 | // Semaphores |
---|
166 | Integer idx; |
---|
167 | IntersectionWithTrafficLights tpI; |
---|
168 | DirectedRoadSegment drS; |
---|
169 | int tempPointIdx; |
---|
170 | int laneNr; |
---|
171 | int dist = 300; |
---|
172 | for (i = 0; i < Globals.map.lightsIndices.size(); i++) { |
---|
173 | idx = Globals.map.lightsIndices.get(i); |
---|
174 | tpI = (IntersectionWithTrafficLights) Globals.map.allIntersections.get(idx.intValue()); |
---|
175 | currentRoad = (Road) Globals.map.roads.get(tpI.segments.get(0).roadIndex); |
---|
176 | currentPoint = currentRoad.points.get(tpI.segments.get(0).pointIndex); |
---|
177 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
178 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
179 | currentRoad = (Road) Globals.map.roads.get(Globals.demo.mv.currentCar.getRoadIdx()); |
---|
180 | currentPoint = (Point) currentRoad.points.get(Globals.demo.mv.currentCar.getPointIdx()); |
---|
181 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
182 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
183 | dist = (int) Math.sqrt(((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y))); |
---|
184 | if (dist > 300) { |
---|
185 | continue; |
---|
186 | } |
---|
187 | for (int j = 0; j < tpI.segments.size(); j++) { |
---|
188 | drS = tpI.segments.get(j); |
---|
189 | if (drS.isExitOnly()) |
---|
190 | continue; |
---|
191 | currentRoad = (Road) Globals.map.roads.get(drS.roadIndex); |
---|
192 | laneNr = currentRoad.laneNo; |
---|
193 | if (drS.direction) { |
---|
194 | tempPointIdx = drS.pointIndex + 4; |
---|
195 | while (currentRoad.points.size() <= tempPointIdx) { |
---|
196 | tempPointIdx--; |
---|
197 | } |
---|
198 | currentPoint = (Point) currentRoad.points.get(tempPointIdx); |
---|
199 | } else { |
---|
200 | tempPointIdx = drS.pointIndex - 4; |
---|
201 | while (tempPointIdx < 0) { |
---|
202 | tempPointIdx++; |
---|
203 | } |
---|
204 | currentPoint = (Point) currentRoad.points.get(tempPointIdx); |
---|
205 | } |
---|
206 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
207 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
208 | currentPoint = (Point) currentRoad.points.get(drS.pointIndex); |
---|
209 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
210 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
211 | if ((x2 != x) || (y2 != y)) { |
---|
212 | sinAlfa = (float) ((y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
213 | cosAlfa = (float) ((x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
214 | dx = (float) (laneNr * 20 * sinAlfa); |
---|
215 | dy = (float) (laneNr * 20 * cosAlfa); |
---|
216 | x2 = dx + x; |
---|
217 | y2 = y - dy; |
---|
218 | x = x - dx; |
---|
219 | y = y + dy; |
---|
220 | int color; |
---|
221 | int timp; |
---|
222 | myGL.glPushMatrix(); |
---|
223 | timp = (int) (Globals.engine.crtTime / Globals.engine.fps); |
---|
224 | color = tpI.segments.get(j).getLightInfo().getColor((timp % tpI.cycleLength)); |
---|
225 | MyColor semaphoreColor = MyColor.chooseSemaphoreColor(color); |
---|
226 | myGL.glColor3f(semaphoreColor.getRed(), semaphoreColor.getGreen(), semaphoreColor.getBlue()); |
---|
227 | myGL.glLineWidth(4.0f); |
---|
228 | myGL.glBegin(GL.GL_LINES); |
---|
229 | { |
---|
230 | myGL.glVertex3f(x, y, 3.0f); |
---|
231 | myGL.glVertex3f(x2, y2, 3.0f); |
---|
232 | } |
---|
233 | myGL.glEnd(); |
---|
234 | myGL.glPopMatrix(); |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | // //endSemaphores |
---|
240 | // print neighbors |
---|
241 | currentCar = Globals.demo.mv.currentCar; |
---|
242 | double offset; |
---|
243 | int dir; |
---|
244 | Point p2, p; |
---|
245 | for (i = 0; i < currentCar.trafficDB.size(); i++) { |
---|
246 | CarInfo elem = (CarInfo) currentCar.trafficDB.get(i); |
---|
247 | currentRoad = (Road) Globals.map.roads.get(elem.getRoadIdx()); |
---|
248 | pt = elem.getPointIdx(); |
---|
249 | offset = elem.getOffset(); |
---|
250 | p = (Point) currentRoad.points.get(pt); |
---|
251 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, p); |
---|
252 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, p); |
---|
253 | y2 = y; |
---|
254 | x2 = x; |
---|
255 | pt++; |
---|
256 | dir = (int) elem.getDirection(); |
---|
257 | try { |
---|
258 | p2 = (Point) currentRoad.points.get(pt); |
---|
259 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, p2); |
---|
260 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, p2); |
---|
261 | if (dir == 0) { |
---|
262 | auxx = x; |
---|
263 | auxy = y; |
---|
264 | x = x2; |
---|
265 | y = y2; |
---|
266 | x2 = auxx; |
---|
267 | y2 = auxy; |
---|
268 | offset = p2.getDistance() - p.getDistance() - offset; |
---|
269 | } |
---|
270 | offset = offset * 1000; |
---|
271 | laneNr = currentRoad.laneNo; |
---|
272 | int ln = elem.getLane(); |
---|
273 | if ((x2 != x) || (y2 != y)) { |
---|
274 | sinAlfa = (float) ((y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
275 | cosAlfa = (float) ((x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
276 | x = x + (float) (cosAlfa * offset); |
---|
277 | y = y + (float) (sinAlfa * offset); |
---|
278 | dx = (float) (((laneNr + 1 - ln) * 20 - 11) * sinAlfa); |
---|
279 | dy = (float) (((laneNr + 1 - ln) * 20 - 11) * cosAlfa); |
---|
280 | x = x + dx; |
---|
281 | y = y - dy; |
---|
282 | // System.out.println("Car "+sourceRoadIdx+":"+elem+" |
---|
283 | // road:"+currentRoad+"; point "+sourcePointIdx+" gets |
---|
284 | // printed at: |
---|
285 | // x="+x+"y="+y);sourceRoadIdx++; |
---|
286 | } |
---|
287 | } catch (Exception e) { |
---|
288 | } |
---|
289 | myGL.glPushMatrix(); |
---|
290 | myGL.glTranslatef(x, y, 0.0f); |
---|
291 | myGL.glScalef(2.0f, 2.0f, 2.0f); |
---|
292 | myGL.glCallList(carList); |
---|
293 | myGL.glPopMatrix(); |
---|
294 | } |
---|
295 | } |
---|
296 | // end neighbors |
---|
297 | synchronized (Globals.mutex) { |
---|
298 | Globals.flag = 0; |
---|
299 | Globals.mutex.notify(); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | /** |
---|
305 | * |
---|
306 | * @param myGL |
---|
307 | * @param myGLU |
---|
308 | * @param myGLUT |
---|
309 | */ |
---|
310 | private void buildList_Car(GL myGL, GLU myGLU, GLUT myGLUT) { |
---|
311 | carList = myGL.glGenLists(4); |
---|
312 | myGL.glNewList(carList, GL.GL_COMPILE); |
---|
313 | { |
---|
314 | myGL.glColor3f(0.2f, 0.2f, 1.0f); |
---|
315 | myGLUT.glutSolidSphere(2.0, 10, 10); |
---|
316 | } |
---|
317 | myGL.glEndList(); |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * The car out is a sphere. |
---|
322 | * |
---|
323 | * @param myGL |
---|
324 | * @param myGLU |
---|
325 | * @param myGLUT |
---|
326 | */ |
---|
327 | private void buildList_CarOut(GL myGL, GLU myGLU, GLUT myGLUT) { |
---|
328 | carListOut = carList + 1; |
---|
329 | myGL.glNewList(carListOut, GL.GL_COMPILE); |
---|
330 | { |
---|
331 | myGL.glColor3f(0.7f, 0.0f, 0.0f); |
---|
332 | myGLUT.glutSolidSphere(2.0f, 10, 10); |
---|
333 | } |
---|
334 | myGL.glEndList(); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * The grass is a green square. |
---|
339 | * |
---|
340 | * @param myGL |
---|
341 | * @param myGLU |
---|
342 | * @param myGLUT |
---|
343 | */ |
---|
344 | private void buildList_Earth(GL myGL, GLU myGLU, GLUT myGLUT) { |
---|
345 | earthList = carListOut + 1; |
---|
346 | myGL.glNewList(earthList, GL.GL_COMPILE); |
---|
347 | { |
---|
348 | // i build the "grass", a green square |
---|
349 | myGL.glColor3f(0.0f, 0.4f, 0.0f); |
---|
350 | myGL.glBegin(GL.GL_QUADS); |
---|
351 | myGL.glVertex3f(-1000.0f, -1000.0f, 0.0f); |
---|
352 | myGL.glVertex3f(1000.0f, -1000.0f, 0.0f); |
---|
353 | myGL.glVertex3f(1000.0f, 1000.0f, 0.0f); |
---|
354 | myGL.glVertex3f(-1000.0f, 1000.0f, 0.0f); |
---|
355 | myGL.glEnd(); |
---|
356 | } |
---|
357 | myGL.glEndList(); |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * |
---|
362 | * @param glDrawable |
---|
363 | */ |
---|
364 | private void bildList(GLAutoDrawable glDrawable) { |
---|
365 | GL myGL = glDrawable.getGL(); |
---|
366 | GLUT myGLUT = new GLUT(); |
---|
367 | GLU myGLU = new GLU(); |
---|
368 | buildList_Car(myGL, myGLU, myGLUT); |
---|
369 | buildList_CarOut(myGL, myGLU, myGLUT); |
---|
370 | buildList_Earth(myGL, myGLU, myGLUT); |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * |
---|
375 | * @param rd |
---|
376 | * @param myGL |
---|
377 | */ |
---|
378 | void drawNearbyRoads(int rd, GL myGL) { |
---|
379 | int ii, j; |
---|
380 | Road mainRoad = Globals.map.roads.get(rd); |
---|
381 | Road currentRoad; |
---|
382 | Point currentPoint; |
---|
383 | short rdIdx; |
---|
384 | ii = 0; |
---|
385 | byte[] ri; |
---|
386 | float x, y, x2, y2; |
---|
387 | while (ii <= mainRoad.crosses.size()) { |
---|
388 | // for every road with witch the current road has a cross, i |
---|
389 | // draw that road |
---|
390 | if (ii == mainRoad.crosses.size()) { |
---|
391 | currentRoad = Globals.map.roads.get(rd); |
---|
392 | } else { |
---|
393 | rdIdx = (short) ((Cross) mainRoad.crosses.get((int) ii)).getCrossRoadIndex(); |
---|
394 | currentRoad = Globals.map.roads.get(rdIdx); |
---|
395 | } |
---|
396 | ri = currentRoad.getRoadinfo(); |
---|
397 | MyColor roadColor = MyColor.chooseRoadColor(ri[1]); |
---|
398 | int laneNr = currentRoad.laneNo; |
---|
399 | y = 0.0f; |
---|
400 | x = 0.0f; |
---|
401 | j = 0; |
---|
402 | double sinAlfa, cosAlfa; |
---|
403 | if ((currentRoad.points.size() > 0) && (j == 0)) { |
---|
404 | currentPoint = (Point) currentRoad.points.get(0); |
---|
405 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
406 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
407 | } |
---|
408 | float dx, dy; |
---|
409 | while (j < currentRoad.points.size() - 1) { |
---|
410 | y2 = y; |
---|
411 | x2 = x; |
---|
412 | currentPoint = (Point) currentRoad.points.get(j + 1); |
---|
413 | y = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
414 | x = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
415 | currentPoint = (Point) currentRoad.points.get(j); |
---|
416 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
417 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
418 | sinAlfa = (y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x))); |
---|
419 | cosAlfa = (x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x))); |
---|
420 | dx = (float) (laneNr * 2 * sinAlfa); |
---|
421 | dy = (float) (laneNr * 2 * cosAlfa); |
---|
422 | |
---|
423 | myGL.glPushMatrix(); |
---|
424 | myGL.glColor3f(roadColor.getRed(), roadColor.getGreen(), roadColor.getBlue()); |
---|
425 | int laneWidth = 10; |
---|
426 | switch (currentRoad.oneWay) { |
---|
427 | case 0: |
---|
428 | myGL.glTranslatef(x, y, 0.0f); |
---|
429 | laneWidth = 20; |
---|
430 | break; |
---|
431 | case 1: |
---|
432 | myGL.glTranslatef(x - (dx / 2), y + (dy / 2), 0.0f); |
---|
433 | break; |
---|
434 | case 2: |
---|
435 | myGL.glTranslatef(x + (dx / 2), y - (dy / 2), 0.0f); |
---|
436 | break; |
---|
437 | } |
---|
438 | myGL.glBegin(GL.GL_POLYGON); |
---|
439 | { |
---|
440 | for (int ll = 0; ll < 8; ll++) { |
---|
441 | x2 = (float) (laneWidth * laneNr * (Math.cos((double) (45 * ll * Math.PI / 180)))); |
---|
442 | y2 = (float) (laneWidth * laneNr * (Math.sin((double) (45 * ll * Math.PI / 180)))); |
---|
443 | myGL.glVertex3f(x2, y2, 0.0f); |
---|
444 | } |
---|
445 | } |
---|
446 | myGL.glEnd(); |
---|
447 | myGL.glPopMatrix(); |
---|
448 | j++; |
---|
449 | } |
---|
450 | ii++; |
---|
451 | } |
---|
452 | ii = 0; |
---|
453 | while (ii <= mainRoad.crosses.size()) { |
---|
454 | // for every road with witch the current road has a cross, i |
---|
455 | // draw that road |
---|
456 | if (ii == mainRoad.crosses.size()) { |
---|
457 | currentRoad = Globals.map.roads.get(rd); |
---|
458 | } else { |
---|
459 | rdIdx = (short) ((Cross) mainRoad.crosses.get((int) ii)).getCrossRoadIndex(); |
---|
460 | currentRoad = Globals.map.roads.get(rdIdx); |
---|
461 | } |
---|
462 | ri = currentRoad.getRoadinfo(); |
---|
463 | MyColor roadColor = MyColor.chooseRoadColor(ri[1]); |
---|
464 | int laneNr = currentRoad.laneNo; |
---|
465 | x2 = 0; |
---|
466 | y2 = 0; |
---|
467 | j = 0; |
---|
468 | double sinAlfa, cosAlfa; |
---|
469 | if ((currentRoad.points.size() > 0) && (j == 0)) { |
---|
470 | currentPoint = (Point) currentRoad.points.get(0); |
---|
471 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
472 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
473 | } |
---|
474 | float dx, dy; |
---|
475 | while (j < currentRoad.points.size() - 1) { |
---|
476 | y = y2; |
---|
477 | x = x2; |
---|
478 | currentPoint = (Point) currentRoad.points.get(j + 1); |
---|
479 | y2 = (float) GPSutil.getMetersLatitude(Globals.map.minPoint, currentPoint); |
---|
480 | x2 = (float) GPSutil.getMetersLongitude(Globals.map.minPoint, currentPoint); |
---|
481 | if ((x2 != x) || (y2 != y)) { |
---|
482 | sinAlfa = (y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x))); |
---|
483 | cosAlfa = (x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x))); |
---|
484 | myGL.glPushMatrix(); |
---|
485 | myGL.glTranslatef(x, y, 0); |
---|
486 | myGL.glColor3f(roadColor.getRed(), roadColor.getGreen(), roadColor.getBlue()); |
---|
487 | myGL.glBegin(GL.GL_QUADS); |
---|
488 | { |
---|
489 | dx = (float) (laneNr * 20 * sinAlfa); |
---|
490 | dy = (float) (laneNr * 20 * cosAlfa); |
---|
491 | switch (currentRoad.oneWay) { |
---|
492 | case 0: |
---|
493 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
494 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
495 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
496 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
497 | break; |
---|
498 | |
---|
499 | case 1: |
---|
500 | myGL.glVertex3f(0.0f, 0.0f, 0.0f); |
---|
501 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
502 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
503 | myGL.glVertex3f(x2 - x, y2 - y, 0.0f); |
---|
504 | break; |
---|
505 | |
---|
506 | case 2: |
---|
507 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
508 | myGL.glVertex3f(0.0f, 0.0f, 0.0f); |
---|
509 | myGL.glVertex3f(x2 - x, y2 - y, 0.0f); |
---|
510 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
511 | break; |
---|
512 | |
---|
513 | } |
---|
514 | } |
---|
515 | myGL.glEnd(); |
---|
516 | |
---|
517 | myGL.glLineWidth(0.25f); |
---|
518 | myGL.glColor3f(0.3f, 0.3f, 0.3f); |
---|
519 | myGL.glBegin(GL.GL_LINES); |
---|
520 | { |
---|
521 | myGL.glVertex3f(0.0f, 0.0f, 0.0f); |
---|
522 | myGL.glVertex3f(x2 - x, y2 - y, 0.0f); |
---|
523 | for (int ln = 1; ln < laneNr; ln++) { |
---|
524 | dx = (float) (ln * 20 * sinAlfa); |
---|
525 | dy = (float) (ln * 20 * cosAlfa); |
---|
526 | if (j % 4 != 0) { |
---|
527 | myGL.glLineWidth(0.15f); |
---|
528 | switch (currentRoad.oneWay) { |
---|
529 | case 0: |
---|
530 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
531 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
532 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
533 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
534 | break; |
---|
535 | |
---|
536 | case 1: |
---|
537 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
538 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
539 | break; |
---|
540 | |
---|
541 | case 2: |
---|
542 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
543 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
544 | break; |
---|
545 | } |
---|
546 | } |
---|
547 | } |
---|
548 | dx = (float) (laneNr * 20 * sinAlfa); |
---|
549 | dy = (float) (laneNr * 20 * cosAlfa); |
---|
550 | myGL.glLineWidth(0.25f); |
---|
551 | switch (currentRoad.oneWay) { |
---|
552 | case 0: |
---|
553 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
554 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
555 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
556 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
557 | break; |
---|
558 | |
---|
559 | case 1: |
---|
560 | myGL.glVertex3f(dx, 0.0f - dy, 0.0f); |
---|
561 | myGL.glVertex3f(x2 - x + dx, y2 - y - dy, 0.0f); |
---|
562 | break; |
---|
563 | |
---|
564 | case 2: |
---|
565 | myGL.glVertex3f(0.0f - dx, dy, 0.0f); |
---|
566 | myGL.glVertex3f(x2 - x - dx, y2 - y + dy, 0.0f); |
---|
567 | break; |
---|
568 | } |
---|
569 | } |
---|
570 | myGL.glEnd(); |
---|
571 | myGL.glPopMatrix(); |
---|
572 | } |
---|
573 | j++; |
---|
574 | } |
---|
575 | ii++; |
---|
576 | } |
---|
577 | |
---|
578 | } |
---|
579 | |
---|
580 | /** |
---|
581 | * |
---|
582 | * @param A |
---|
583 | * @param B |
---|
584 | * @param C |
---|
585 | * @return |
---|
586 | */ |
---|
587 | private Point2D.Float solve2Degree(float A, float B, float C) { |
---|
588 | float delta = B * B - 4 * A * C; |
---|
589 | return new Point2D.Float((0 - B - delta) / 2 / A, (0 - B + delta) / 2 / A); |
---|
590 | } |
---|
591 | |
---|
592 | /** |
---|
593 | * |
---|
594 | * @param glDrawable |
---|
595 | * @param xc |
---|
596 | * @param yc |
---|
597 | * @param xp |
---|
598 | * @param yp |
---|
599 | * @param laneNr |
---|
600 | * @param ln |
---|
601 | * @param dir |
---|
602 | */ |
---|
603 | public void setCamera(GLAutoDrawable glDrawable, double xc, double yc, double xp, double yp, int laneNr, int ln, int dir) { |
---|
604 | // i place the camera on the current postion of the carReceivingMsg and |
---|
605 | // i make it look in front |
---|
606 | double A, B, C, a, b; |
---|
607 | double x1, x2, y1, y2; |
---|
608 | GL myGL = glDrawable.getGL(); |
---|
609 | GLU myGLU = new GLU(); |
---|
610 | double dx = 0, dy = 0, cosAlfa, sinAlfa; |
---|
611 | if ((xc != xp) || (yc != yp)) { |
---|
612 | sinAlfa = (float) ((yc - yp) / (Math.sqrt((yc - yp) * (yc - yp) + (xc - xp) * (xc - xp)))); |
---|
613 | cosAlfa = (float) ((xc - xp) / (Math.sqrt((yc - yp) * (yc - yp) + (xc - xp) * (xc - xp)))); |
---|
614 | dx = (float) (((laneNr + 1 - ln) * 20 - 11) * sinAlfa); |
---|
615 | dy = (float) (((laneNr + 1 - ln) * 20 - 11) * cosAlfa); |
---|
616 | } |
---|
617 | xCamera = (float) xc; |
---|
618 | yCamera = (float) yc; |
---|
619 | // i calculate the point at "distanceToLook" in front of the |
---|
620 | // carReceivingMsg(on |
---|
621 | // the direction determined by the previous position and the current |
---|
622 | // one) |
---|
623 | if ((xc == xp) && (yc == yp)) { |
---|
624 | // nothing to do. I am in the same spot |
---|
625 | } else { |
---|
626 | // xCamera=(float)(xp+((xc-xp)/2)); |
---|
627 | // yCamera=(float)(yp+((yc-yp)/2)); |
---|
628 | if (yc == yp) { |
---|
629 | // simpler cases |
---|
630 | if (xp < xc) { |
---|
631 | xToSee = (float) (xc + distanceToLook); |
---|
632 | yToSee = (float) (yc); |
---|
633 | } else { |
---|
634 | xToSee = (float) (xc - distanceToLook); |
---|
635 | yToSee = (float) (yc); |
---|
636 | } |
---|
637 | } else { |
---|
638 | if (xc == xp) { |
---|
639 | if (yp < yc) { |
---|
640 | xToSee = (float) (xc); |
---|
641 | yToSee = (float) (yc + distanceToLook); |
---|
642 | } else { |
---|
643 | xToSee = (float) xc; |
---|
644 | yToSee = (float) (yc - distanceToLook); |
---|
645 | } |
---|
646 | } else { |
---|
647 | // i solve a second degree equation to determine the |
---|
648 | // points at distanceToLook from the current |
---|
649 | // position and on the right direction |
---|
650 | a = (yp - yc) / (xp - xc); |
---|
651 | b = yc - xc * a; |
---|
652 | A = 1 + a * a; |
---|
653 | B = 2 * a * b - 2 * xc - 2 * yc * a; |
---|
654 | C = (double) xc * xc + b * b - 2 * yc * b + yc * yc - distanceToLook * distanceToLook; |
---|
655 | Point2D.Float sol = solve2Degree((float) A, (float) B, (float) C); |
---|
656 | x1 = sol.getX(); |
---|
657 | x2 = sol.getY(); |
---|
658 | y1 = a * x1 + b; |
---|
659 | y2 = a * x2 + b; |
---|
660 | // i choose the one that is front of the carReceivingMsg |
---|
661 | if (((yp >= yc) && (yc >= y1) && (xp <= xc) && (xc <= x1)) |
---|
662 | || ((yp >= yc) && (yc >= y1) && (xp >= xc) && (xc >= x1)) |
---|
663 | || ((yp <= yc) && (yc <= y1) && (xp <= xc) && (xc <= x1)) |
---|
664 | || ((yp <= yc) && (yc <= y1) && (xp >= xc) && (xc >= x1))) { |
---|
665 | xToSee = (float) x1; |
---|
666 | yToSee = (float) y1; |
---|
667 | } |
---|
668 | if (((yp >= yc) && (yc >= y2) && (xp <= xc) && (xc <= x2)) |
---|
669 | || ((yp >= yc) && (yc >= y2) && (xp >= xc) && (xc >= x2)) |
---|
670 | || ((yp <= yc) && (yc <= y2) && (xp <= xc) && (xc <= x2)) |
---|
671 | || ((yp <= yc) && (yc <= y2) && (xp >= xc) && (xc >= x2))) { |
---|
672 | xToSee = (float) x2; |
---|
673 | yToSee = (float) y2; |
---|
674 | } |
---|
675 | } |
---|
676 | } |
---|
677 | previosPosX = (float) xc; // the current position is saved for the |
---|
678 | // next iteration |
---|
679 | previosPosY = (float) yc; |
---|
680 | // The camera needs to be tilted in front, so now the normal |
---|
681 | // direction of the camera should also be calculated |
---|
682 | float D; |
---|
683 | float X, Y; |
---|
684 | X = xToSee; |
---|
685 | Y = yToSee; |
---|
686 | xc = (double) xToSee; |
---|
687 | yc = (double) yToSee; |
---|
688 | xp = (double) previosPosX; |
---|
689 | yp = (double) previosPosY; |
---|
690 | D = (float) (zCamera * zCamera) / distanceToLook; |
---|
691 | if (yc == yp) { |
---|
692 | // simpler cases |
---|
693 | if (xp < xc) { |
---|
694 | X = (float) (xc + D); |
---|
695 | Y = (float) yc; |
---|
696 | } else { |
---|
697 | X = (float) (xc - D); |
---|
698 | Y = (float) yc; |
---|
699 | } |
---|
700 | } else { |
---|
701 | if (xc == xp) { |
---|
702 | if (yp < yc) { |
---|
703 | X = (float) xc; |
---|
704 | Y = (float) (yc + D); |
---|
705 | } else { |
---|
706 | X = (float) xc; |
---|
707 | Y = (float) (yc + D); |
---|
708 | } |
---|
709 | } else { |
---|
710 | // i solve a second degree equation to determine the |
---|
711 | // points at distanceToLook from the current position |
---|
712 | // and on the right direction |
---|
713 | a = (yp - yc) / (xp - xc); |
---|
714 | b = yc - xc * a; |
---|
715 | A = 1 + a * a; |
---|
716 | B = 2 * a * b - 2 * xc - 2 * yc * a; |
---|
717 | C = (double) xc * xc + b * b - 2 * yc * b + yc * yc - D * D; |
---|
718 | Point2D.Float sol = solve2Degree((float) A, (float) B, (float) C); |
---|
719 | x1 = sol.getX(); |
---|
720 | x2 = sol.getY(); |
---|
721 | y1 = a * x1 + b; |
---|
722 | y2 = a * x2 + b; |
---|
723 | // i choose the one that is front of the carReceivingMsg |
---|
724 | if (((yp >= yc) && (yc >= y1) && (xp <= xc) && (xc <= x1)) |
---|
725 | || ((yp >= yc) && (yc >= y1) && (xp >= xc) && (xc >= x1)) |
---|
726 | || ((yp <= yc) && (yc <= y1) && (xp <= xc) && (xc <= x1)) |
---|
727 | || ((yp <= yc) && (yc <= y1) && (xp >= xc) && (xc >= x1))) { |
---|
728 | X = (float) x1; |
---|
729 | Y = (float) y1; |
---|
730 | } |
---|
731 | if (((yp >= yc) && (yc >= y2) && (xp <= xc) && (xc <= x2)) |
---|
732 | || ((yp >= yc) && (yc >= y2) && (xp >= xc) && (xc >= x2)) |
---|
733 | || ((yp <= yc) && (yc <= y2) && (xp <= xc) && (xc <= x2)) |
---|
734 | || ((yp <= yc) && (yc <= y2) && (xp >= xc) && (xc >= x2))) { |
---|
735 | X = (float) x2; |
---|
736 | Y = (float) y2; |
---|
737 | } |
---|
738 | } |
---|
739 | } |
---|
740 | myGL.glMatrixMode(GL.GL_MODELVIEW); |
---|
741 | myGL.glLoadIdentity(); |
---|
742 | myGLU.gluLookAt(xCamera + dx, yCamera - dy, zCamera, xToSee + dx, yToSee - dy, 0.0, (float) (xToSee - X), |
---|
743 | (float) (yToSee - Y), zCamera); |
---|
744 | } |
---|
745 | } |
---|
746 | |
---|
747 | public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) { |
---|
748 | // TODO Auto-generated method stub |
---|
749 | |
---|
750 | } |
---|
751 | |
---|
752 | public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { |
---|
753 | // TODO Auto-generated method stub |
---|
754 | |
---|
755 | } |
---|
756 | } |
---|