1 package de.lathanda.eos.robot.gui;
4 import java.awt.Dimension;
5 import java.awt.Toolkit;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseListener;
8 import java.awt.event.MouseMotionListener;
9 import java.awt.event.MouseWheelEvent;
10 import java.awt.event.MouseWheelListener;
11 import java.util.Map.Entry;
12 import com.jogamp.opengl.GL;
13 import com.jogamp.opengl.GL2;
14 import com.jogamp.opengl.GLAutoDrawable;
15 import com.jogamp.opengl.GLCapabilities;
16 import com.jogamp.opengl.GLEventListener;
17 import com.jogamp.opengl.GLProfile;
18 import com.jogamp.opengl.awt.GLCanvas;
19 import com.jogamp.opengl.glu.GLU;
20 import com.jogamp.opengl.util.FPSAnimator;
21 import de.lathanda.eos.robot.Column;
22 import de.lathanda.eos.robot.Coordinate;
23 import de.lathanda.eos.robot.Cube;
24 import de.lathanda.eos.robot.Entrance;
25 import de.lathanda.eos.robot.Robot;
26 import de.lathanda.eos.robot.World;
27 import de.lathanda.eos.robot.World.IntRange;
28 import de.lathanda.eos.robot.exceptions.RobotException;
29 import de.lathanda.eos.robot.geom3d.Polyhedron;
30 import static de.lathanda.eos.robot.obj.Models.*;
40 implements GLEventListener, MouseListener, MouseMotionListener, MouseWheelListener {
41 private static final long serialVersionUID = -7254900679929645124L;
43 private FPSAnimator animator;
47 private int lastMouseX, lastMouseY;
48 private int lastButton;
52 super(
new GLCapabilities(GLProfile.get(GLProfile.GL2)));
55 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
56 setPreferredSize(
new Dimension(screen.width / 2, screen.height / 2));
65 addMouseListener(
this);
66 addMouseMotionListener(
this);
67 addMouseWheelListener(
this);
68 addGLEventListener(
this);
71 animator =
new FPSAnimator(
this, 30,
true);
77 public void display(GLAutoDrawable glad) {
78 GL2 gl = glad.getGL().getGL2();
81 gl.glClearColor(0.25f, 0.6f, 1f, 0f);
85 gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
92 gl.glEnable(GL.GL_BLEND);
93 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
94 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
96 gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR,
new float[] { 0.2f, 0.2f, 0.2f, 1f }, 0);
97 gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT,
new float[] { 0.2f, 0.2f, 0.2f, 1f }, 0);
98 gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE,
new float[] { 0.8f, 0.8f, 0.8f, 1f }, 0);
99 gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION,
new float[] { -0.5f, -0.3f, 1f, 0f }, 0);
104 gl.glEnable(GL.GL_DEPTH_TEST);
105 gl.glDepthFunc(GL.GL_LEQUAL);
126 public void init(GLAutoDrawable glad) {
130 public void reshape(GLAutoDrawable glad,
int x,
int y,
int width,
int height) {
131 GL2 gl = glad.getGL().getGL2();
132 double ratio = (double) width / (
double) height;
135 gl.glEnable(GL2.GL_LIGHTING);
136 gl.glEnable(GL2.GL_LIGHT0);
139 gl.glViewport(0, 0, width, height);
140 gl.glMatrixMode(GL2.GL_PROJECTION);
142 glu.gluPerspective(60, ratio, 0.1d, 200d);
144 gl.glMatrixMode(GL2.GL_MODELVIEW);
149 private static final double ROTATION_SPEED_SIDE = 0.15d;
150 private static final double ROTATION_SPEED_UP_DOWN = 0.4d;
151 private static final double MOVE_SPEED_SIDE = 0.14d;
152 private static final double MOVE_SPEED_UP_DOWN = 0.07d;
153 private static final double MOVE_SPEED = 2d;
157 int dX = e.getX() - lastMouseX;
158 int dY = e.getY() - lastMouseY;
159 switch (lastButton) {
160 case MouseEvent.BUTTON1:
164 case MouseEvent.BUTTON2:
168 case MouseEvent.BUTTON3:
169 cam.
rotateCamera(dY * ROTATION_SPEED_UP_DOWN, dX * ROTATION_SPEED_SIDE);
173 lastMouseX = e.getX();
174 lastMouseY = e.getY();
177 private void restrictCamera() {
193 lastButton = e.getButton();
194 lastMouseX = e.getX();
195 lastMouseY = e.getY();
200 lastMouseX = e.getX();
201 lastMouseY = e.getY();
214 double mouseRotation = e.getPreciseWheelRotation();
215 moveCamera(MOVE_SPEED * mouseRotation);
218 private void moveCamera(
double distance) {
225 private static double sin(
double degree) {
226 return Math.sin(degree / 180 * Math.PI);
229 private static double cos(
double degree) {
230 return Math.cos(degree / 180 * Math.PI);
234 private static Color CURSOR_COLOR =
new Color(1f, 1f, 0f, .5f);
239 private void renderSkybox(GL2 gl) {
240 renderPolyhedron(SKYBOX, Color.BLACK, gl);
247 gl.glBegin(GL2.GL_QUADS);
248 gl.glNormal3f(0f, 0f, 1f);
251 double left = rangeX.
getMin() - 0.5d;
252 double width = rangeX.
size();
253 double bottom = rangeY.
getMin() - 0.5d;
254 double height = rangeY.
size();
255 gl.glTexCoord2d(0, 0);
256 gl.glVertex3d(left *
WIDTH, bottom *
DEPTH, 0);
257 gl.glTexCoord2d(0, height);
258 gl.glVertex3d(left *
WIDTH, (bottom + height) *
DEPTH, 0);
259 gl.glTexCoord2d(width, height);
260 gl.glVertex3d((left + width) *
WIDTH, (bottom + height) *
DEPTH, 0);
261 gl.glTexCoord2d(width, 0);
262 gl.glVertex3d((left + width) *
WIDTH, bottom *
DEPTH, 0);
268 for (Entry<Coordinate, Column> col : world.
getColumns().entrySet()) {
272 renderColumn(col.getValue(), gl);
286 renderEntrance(e, gl);
291 private void renderCursor(GL2 gl) {
294 renderPolyhedron(CURSOR, CURSOR_COLOR, gl);
298 private void renderEntrance(
Entrance e, GL2 gl) {
301 gl.glRotatef(e.
d.
getAngle(), 0f, 0f, 1f);
302 renderPolyhedron(ROBOT,
new Color(0.5f, 0.5f, 0.5f, 0.5f), gl);
306 private void renderRobot(Robot r, GL2 gl) {
309 gl.glRotatef(r.getDirection().getAngle(), 0f, 0f, 1f);
310 renderPolyhedron(ROBOT, r.getRobotColor().getColor(), gl);
314 private void renderColumn(Column column, GL2 gl) {
316 boolean doMark =
true;
319 cubes = column.getCubes();
320 for (
int i = 0; i < cubes.length; i++) {
321 if (!cubes[i].isEmpty()) {
322 renderCube(cubes[i], gl);
324 }
else if (column.isMarked() && doMark) {
325 renderPolyhedron(MARK, column.getMark().getColor(), gl);
328 gl.glTranslated(0d, 0d,
HEIGHT);
330 if (column.isMarked() && doMark) {
331 renderPolyhedron(MARK, column.getMark().getColor(), gl);
333 }
catch (RobotException e) {
338 private void renderCube(Cube cube, GL2 gl) {
339 switch (cube.getType()) {
345 renderPolyhedron(CUBE, cube.getColor().getColor(), gl);
348 renderPolyhedron(ROCK, cube.getColor().getColor(), gl);
353 private void renderPolyhedron(Polyhedron poly, Color base, GL2 gl) {
354 GLObjectBuffer.get(poly).render(base, gl);
LinkedList< Entrance > getEntrances()
LinkedList< Robot > getRobots()
TreeMap< Coordinate, Column > getColumns()
double getCameraPositionX()
void moveCamera(double dx, double dy, double dz)
double getCameraRotationX()
void rotateCamera(double rx, double rz)
double getCameraPositionY()
double getCameraPositionZ()
double getCameraRotationZ()
void setCameraRotationZ(double cameraRotationZ)
static synchronized void clear(GL gl)
void closeMaterial(GL2 gl)
static synchronized GLTextureBuffer get(Material m, GL gl)
static synchronized void clear(GL gl)
void openMaterial(Color base, GL2 gl)
void mouseClicked(MouseEvent e)
void dispose(GLAutoDrawable glad)
void mouseDragged(MouseEvent e)
void reshape(GLAutoDrawable glad, int x, int y, int width, int height)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void display(GLAutoDrawable glad)
void mouseWheelMoved(MouseWheelEvent e)
void mouseReleased(MouseEvent e)
void mouseEntered(MouseEvent e)
WorldPanelOpenGLNoShader(World world)
void init(GLAutoDrawable glad)
void mouseMoved(MouseEvent e)