1 package nl.tudelft.goal.ut2004.visualizer.timeline.map;
2
3 import static javax.media.opengl.GL2.GL_COMPILE;
4
5 import java.awt.Color;
6 import java.util.prefs.PreferenceChangeEvent;
7 import java.util.prefs.PreferenceChangeListener;
8 import java.util.prefs.Preferences;
9
10 import javax.media.opengl.GL;
11 import javax.media.opengl.GL2;
12 import javax.media.opengl.glu.GLU;
13 import javax.media.opengl.glu.GLUquadric;
14
15 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
16 import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
17
18
19
20
21
22
23
24
25 public class WaypointRenderer implements ISubGLRenderer<IUnrealWaypoint> {
26
27
28 public static final double NAVPOINT_OUTER_RADIUS = 30;
29 public static final double NAVPOINT_INNER_RADIUS = 0;
30 public static final int NAVPOINT_SLICES = 32;
31 public static final int NAVPOINT_RINGS = 1;
32 public static final double NAVPOINT_FLOAT = 1;
33
34 private static final Color WAYPOINT_COLOR_DEFAULT = Color.BLUE;
35 private static final String WAYPOINT_COLOR_KEY = "WAYPOINT_COLOR_KEY";
36
37
38 private static final Preferences preferences = Preferences.userNodeForPackage(WaypointRenderer.class);
39 private PreferenceChangeListener changeListener = new PreferenceChangeListener() {
40
41 @Override
42 public void preferenceChange(PreferenceChangeEvent evt) {
43 dirty = true;
44 }
45 };
46 private final IUnrealWaypoint waypoint;
47 private final int glName;
48 private int displayList;
49 private boolean dirty = false;
50
51
52
53
54
55
56
57 public WaypointRenderer(IUnrealWaypoint waypoint, int glName) {
58 this.waypoint = waypoint;
59 this.glName = glName;
60 preferences.addPreferenceChangeListener(changeListener);
61 }
62
63 @Override
64 public void prepare(GL gl) {
65
66 }
67
68 @Override
69 public void render(GL gla) {
70 GL2 gl = gla.getGL2();
71
72 try {
73
74 if (dirty || !gl.glIsList(displayList)) {
75 updateDisplayLists(gl);
76 dirty = false;
77 }
78
79 gl.glLoadName(glName);
80 gl.glCallList(displayList);
81 gl.glLoadName(-1);
82
83 } catch (RuntimeException e) {
84 e.printStackTrace();
85 }
86
87 }
88
89 private void updateDisplayLists(GL2 gl) {
90
91 gl.glDeleteLists(displayList, 1);
92
93
94 displayList = gl.glGenLists(1);
95 gl.glNewList(displayList, GL_COMPILE);
96 renderWayPoint(gl);
97 gl.glEndList();
98 }
99
100 private void renderWayPoint(GL2 gl) {
101 GLU glu = new GLU();
102 GLUquadric qaudric = glu.gluNewQuadric();
103
104 GlColor color = new GlColor(getColor());
105 Location position = waypoint.getLocation();
106 if(position == null){
107 return;
108 }
109
110
111
112 gl.glPushMatrix();
113 {
114 gl.glTranslated(position.x, position.y, position.z + NAVPOINT_FLOAT);
115 gl.glColor4d(color.r, color.g, color.b, color.a);
116 glu.gluDisk(qaudric, NAVPOINT_INNER_RADIUS, NAVPOINT_OUTER_RADIUS, NAVPOINT_SLICES, NAVPOINT_RINGS);
117
118 }
119 gl.glPopMatrix();
120
121 }
122
123 @Override
124 public IUnrealWaypoint getObject() {
125 return waypoint;
126 }
127
128
129 public static Color getColor() {
130 int rgb = preferences.getInt(WAYPOINT_COLOR_KEY, WAYPOINT_COLOR_DEFAULT.getRGB());
131 return new Color(rgb);
132 }
133
134 public static void setColor(Color c){
135 preferences.putInt(WAYPOINT_COLOR_KEY, c.getRGB());
136 }
137
138 @Override
139 public int getGLName() {
140 return glName;
141 }
142 @Override
143 public void destroy() {
144 preferences.removePreferenceChangeListener(changeListener);
145 }
146 }