View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.timeline.map;
2   
3   import java.awt.Color;
4   import java.util.prefs.Preferences;
5   
6   import javax.media.opengl.GL;
7   import javax.media.opengl.GL2;
8   import javax.media.opengl.glu.GLU;
9   import javax.media.opengl.glu.GLUquadric;
10  
11  import com.jogamp.opengl.util.gl2.GLUT;
12  
13  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
15  
16  /**
17   * Sub renderer for Flag objects.
18   * 
19   * 
20   * @author M.P. Korstanje
21   * 
22   */
23  public class FlagRenderer implements ISubGLRenderer<FlagInfo> {
24  
25  	private static final double SPHERE_RADIUS = 64;
26  	private static final int SPHERE_SLICES = 32;
27  	private static final int SPHERE_STACKS = 32;
28  	private static final double CYLINDER_RADIUS = 16;
29  	private static final int CYLINDER_SLICES = 5;
30  	private static final int CYLINDER_STACKS = 1;
31  	private static final double CYLINDER_HEIGHT = SPHERE_RADIUS * 3;
32  
33  	private static final Color BLUE_FLAG_COLOR_DEFAULT = Color.BLUE;
34  	private static final Color RED_FLAG_COLOR_DEFAULT = Color.RED;
35  	private static final Color OTHER_FLAG_COLOR_DEFAULT = Color.ORANGE;
36  
37  	private static final String BLUE_FLAG_COLOR_KEY = "BLUE_FLAG_COLOR_KEY";
38  	private static final String RED_FLAG_COLOR_KEY = "RED_FLAG_COLOR_KEY";
39  	private static final String OTHER_FLAG_COLOR_KEY = "OTHER_FLAG_COLOR_KEY";
40  
41  	private static final Preferences preferences = Preferences.userNodeForPackage(FlagRenderer.class);
42  
43  	private final FlagInfo flag;
44  	private final int glName;
45  
46  	/**
47  	 * Create a new subrenderer with passed agent as source of data.
48  	 * 
49  	 * @param renderableUTAgent
50  	 *            agent used as source of data.
51  	 */
52  	public FlagRenderer(FlagInfo flag, int glName) {
53  		this.flag = flag;
54  		this.glName = glName;
55  	}
56  
57  	@Override
58  	public void prepare(GL gl) {
59  
60  	}
61  
62  	@Override
63  	public void render(GL gla) {
64  		GL2 gl = gla.getGL2();
65  
66  		try {
67  			Location flagLocation = flag.getLocation();
68  			if (flagLocation == null) {
69  				return;
70  			}
71  			Location center = new Location(flagLocation.x, flagLocation.y, flagLocation.z);
72  			GlColor color = new GlColor(getColor());
73  			gl.glLoadName(glName);
74  			renderFlag(gl, color, center);
75  			gl.glLoadName(-1);
76  
77  		} catch (RuntimeException e) {
78  			e.printStackTrace();
79  		}
80  
81  	}
82  
83  	private void renderFlag(GL gla, GlColor color, Location position) {
84  		GLU glu = new GLU();
85  		GLUT glut = new GLUT();
86  		GLUquadric quadratic = glu.gluNewQuadric();
87  
88  		GL2 gl = gla.getGL2();
89  		gl.glPushMatrix();
90  		{
91  			gl.glTranslated(position.x, position.y, position.z);
92  			gl.glColor4d(color.r, color.g, color.b, color.a);
93  			glu.gluCylinder(quadratic, CYLINDER_RADIUS, CYLINDER_RADIUS, CYLINDER_HEIGHT, CYLINDER_SLICES, CYLINDER_STACKS);
94  			gl.glTranslated(0, 0, CYLINDER_HEIGHT);
95  			glu.gluSphere(quadratic, SPHERE_RADIUS, SPHERE_SLICES, SPHERE_STACKS);
96  		}
97  		gl.glPopMatrix();
98  	}
99  
100 	@Override
101 	public FlagInfo getObject() {
102 		return flag;
103 	}
104 
105 	public static Color getRedFlagColor() {
106 		int rgb = preferences.getInt(RED_FLAG_COLOR_KEY, RED_FLAG_COLOR_DEFAULT.getRGB());
107 		return new Color(rgb);
108 	}
109 
110 	public static void setRedFlagColor(Color c) {
111 		preferences.putInt(RED_FLAG_COLOR_KEY, c.getRGB());
112 	}
113 
114 	public static Color getBlueFlagColor() {
115 		int rgb = preferences.getInt(BLUE_FLAG_COLOR_KEY, BLUE_FLAG_COLOR_DEFAULT.getRGB());
116 		return new Color(rgb);
117 	}
118 
119 	public static void setBlueFlagColor(Color c) {
120 		preferences.putInt(BLUE_FLAG_COLOR_KEY, c.getRGB());
121 	}
122 
123 	public static Color getOtherFlagColor() {
124 		int rgb = preferences.getInt(OTHER_FLAG_COLOR_KEY, OTHER_FLAG_COLOR_DEFAULT.getRGB());
125 		return new Color(rgb);
126 	}
127 
128 	public static void setOtherFlagColor(Color c) {
129 		preferences.putInt(OTHER_FLAG_COLOR_KEY, c.getRGB());
130 	}
131 
132 	public Color getColor() {
133 		switch (flag.getTeam()) {
134 		case 0:
135 			return getRedFlagColor();
136 		case 1:
137 			return getBlueFlagColor();
138 		default:
139 			return getOtherFlagColor();
140 		}
141 	}
142 
143 	@Override
144 	public int getGLName() {
145 		return glName;
146 	}
147 
148 	@Override
149 	public void destroy() {
150 		// Does nothing.
151 	}
152 
153 }