/* Copyright 2010-2011 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut. If not, see <http://www.gnu.org/licenses/>.
*/
package ponkOut.graphics;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import ponkOut.graphics.resources.Shader;
import ponkOut.graphics.resources.ShaderManager;
public class Cursor extends WorldObject {
public enum State {
NORMAL, HOVER, APPLY_HOVER, DISCARD_HOVER
};
private static final float HEAD_RADIUS = 0.2f;
private static final float TAIL_RADIUS = 0.3f;
private static final float MAX_X = 10.6f;
private static final float MIN_X = -10.6f;
private static final float MAX_Y = 7.9f;
private static final float MIN_Y = -7.9f;
/** position of cursor if camera looked at xy-plane */
private Vector3f pos2d;
/** position of cursor after rotation */
private Vector3f position;
private Vector3f tailDistance;
private Vector3f tailPosition;
Matrix4f rotationMatrix;
private Light headLight;
private Light tailLight;
private Shader ballGlowVShader;
private Shader ballGlowFShader;
private int headDisplayList;
private int tailDisplayList;
private Color tailColor;
private Color normalColor;
private Color hoverColor;
private Color applyColor;
private Color discardColor;
private float ambientFactor = 0.4f;
private float diffuseFactor = 0.9f;
private float textDist;
private State state;
public Cursor(Color color, Color hoverColor, float x, float y, float z, float textDist,
GraphicsObjectsManager goManager) {
super(color, goManager);
ballGlowVShader = ShaderManager.getInstance().getShader("ballGlow.arbvp1");
ballGlowFShader = ShaderManager.getInstance().getShader("ballGlow.arbfp1");
pos2d = new Vector3f(x, y, z);
position = new Vector3f(x, y, z);
this.textDist = textDist;
tailDistance = new Vector3f(0.0f, 0.0f, 2.0f);
rotationMatrix = new Matrix4f();
state = State.NORMAL;
normalColor = new Color(1.0f, 0.9f, 0.7f);
this.hoverColor = hoverColor;
applyColor = new Color(0.3f, 1.0f, 0.0f);
discardColor = new Color(1.0f, 0.2f, 0.0f);
tailColor = normalColor;
float red = color.getR();
float green = color.getG();
float blue = color.getB();
// create head light
headLight = Lighting.addLight(position, 0.1f * red, 0.1f * green, 0.1f * blue, red, green, blue, red, green,
blue);
// create head display list
headDisplayList = GL11.glGenLists(1);
GL11.glNewList(headDisplayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(-HEAD_RADIUS, -HEAD_RADIUS, 0.0f); // bottom left
GL11.glVertex3f(HEAD_RADIUS, -HEAD_RADIUS, 0.0f); // bottom right
GL11.glVertex3f(HEAD_RADIUS, HEAD_RADIUS, 0.0f); // top right
GL11.glVertex3f(-HEAD_RADIUS, HEAD_RADIUS, 0.0f); // top left
GL11.glEnd();
GL11.glEndList();
// create tail light
tailPosition = new Vector3f();
Vector3f.add(position, tailDistance, tailPosition);
tailLight = Lighting.addLight(tailPosition, ambientFactor * normalColor.getR(),
ambientFactor * normalColor.getG(), ambientFactor * normalColor.getB(),
diffuseFactor * normalColor.getR(), diffuseFactor * normalColor.getG(),
diffuseFactor * normalColor.getB(), diffuseFactor * normalColor.getR(),
diffuseFactor * normalColor.getG(), diffuseFactor * normalColor.getB());
// create head display list
tailDisplayList = GL11.glGenLists(1);
GL11.glNewList(tailDisplayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(-TAIL_RADIUS, -TAIL_RADIUS, 0.0f); // bottom left
GL11.glVertex3f(TAIL_RADIUS, -TAIL_RADIUS, 0.0f); // bottom right
GL11.glVertex3f(TAIL_RADIUS, TAIL_RADIUS, 0.0f); // top right
GL11.glVertex3f(-TAIL_RADIUS, TAIL_RADIUS, 0.0f); // top left
GL11.glEnd();
GL11.glEndList();
}
@Override
public void draw() {
// store current transformation matrix
GL11.glPushMatrix();
// first draw head
// translate
GL11.glTranslatef(position.x, position.y, position.z);
// set color
appearance.use();
// enable shaders
ballGlowVShader.enable();
ballGlowFShader.enable();
// draw
GL11.glCallList(headDisplayList);
// disable shaders
ballGlowFShader.disable();
ballGlowVShader.disable();
GL11.glPopMatrix();
// now draw tail
GL11.glPushMatrix();
// translate
GL11.glTranslatef(tailPosition.x, tailPosition.y, tailPosition.z);
// set color
tailColor.use();
// enable shaders
ballGlowVShader.enable();
ballGlowFShader.enable();
// draw
GL11.glCallList(tailDisplayList);
// disable shaders
ballGlowFShader.disable();
ballGlowVShader.disable();
// restore transformation matrix
GL11.glPopMatrix();
}
@Override
public Vector3f getPosition() {
return new Vector3f(position.x, position.y, position.z);
}
@Override
public boolean isOpaque() {
return false;
}
public void translate(float x, float y) {
pos2d.x += x;
pos2d.y += y;
// ensure cursors does not leave screen
if (pos2d.x > MAX_X)
pos2d.x = MAX_X;
if (pos2d.x < MIN_X)
pos2d.x = MIN_X;
if (pos2d.y > MAX_Y)
pos2d.y = MAX_Y;
if (pos2d.y < MIN_Y)
pos2d.y = MIN_Y;
applyRotation();
}
public void setRotation(float x, float y) {
rotationMatrix.setIdentity();
// convert angles from degrees to radians
rotationMatrix.rotate((float) (y * Math.PI / 180.0), new Vector3f(0.0f, 1.0f, 0.0f));
rotationMatrix.rotate((float) (x * Math.PI / 180.0), new Vector3f(1.0f, 0.0f, 0.0f));
applyRotation();
}
private void applyRotation() {
// rotate head
Vector4f v1 = new Vector4f(pos2d.x, pos2d.y, pos2d.z, 1.0f);
Vector4f v2 = Matrix4f.transform(rotationMatrix, v1, null);
position.x = v2.x;
position.y = v2.y;
position.z = v2.z;
// rotate tail
Vector3f.add(pos2d, tailDistance, tailPosition);
v1 = new Vector4f(tailPosition.x, tailPosition.y, tailPosition.z, 1.0f);
Matrix4f.transform(rotationMatrix, v1, v2);
tailPosition.x = v2.x;
tailPosition.y = v2.y;
tailPosition.z = v2.z;
headLight.setPosition(position);
tailLight.setPosition(tailPosition);
}
public void setState(State state) {
if (this.state != state) {
this.state = state;
switch (state) {
case NORMAL:
setTailColor(normalColor);
break;
case HOVER:
setTailColor(hoverColor);
break;
case APPLY_HOVER:
setTailColor(applyColor);
break;
case DISCARD_HOVER:
setTailColor(discardColor);
break;
}
}
}
public void setTailColor(Color color) {
tailColor = color;
float red = color.getR();
float green = color.getG();
float blue = color.getB();
tailLight.setColor(new float[] { ambientFactor * red, ambientFactor * green, ambientFactor * green, 1.0f },
new float[] { diffuseFactor * red, diffuseFactor * green, diffuseFactor * blue, 1.0f }, new float[] {
diffuseFactor * red, diffuseFactor * green, diffuseFactor * blue, 1.0f });
}
/**
* @return position of cursor projected on text plane
*/
public Vector3f getTextPosition() {
Vector4f v1 = new Vector4f(pos2d.x, pos2d.y, pos2d.z - textDist, 1.0f);
Vector4f v2 = Matrix4f.transform(rotationMatrix, v1, null);
return new Vector3f(v2.x, v2.y, v2.z);
}
}