/* 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.Quaternion;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import ponkOut.graphics.resources.Shader;
import ponkOut.graphics.resources.ShaderManager;
/**
* A glowing object orbiting the board
*/
public class Satellite extends WorldObject {
private Vector3f position;
private float time;
private Light light;
private float a; // lenth of semi-major axis
private float e; // eccentricity
private float period;
private Quaternion rotation;
private static Shader ballGlowVShader = ShaderManager.getInstance().getShader("ballGlow.arbvp1");
private static Shader ballGlowFShader = ShaderManager.getInstance().getShader("ballGlow.arbfp1");
private int displayList;
public Satellite(float radius, float red, float green, float blue, float a, float e, float initialTime,
float period, float rotX, float rotY, float rotZ, float angle, GraphicsObjectsManager goManager) {
super(new Color(red, green, blue), goManager);
this.a = a;
this.e = e;
time = 0.0f;
this.period = period;
rotation = new Quaternion();
rotation.setFromAxisAngle(new Vector4f(rotX, rotY, rotZ, angle));
position = new Vector3f();
light = Lighting.addLight(position, 0.1f * red, 0.1f * green, 0.1f * blue, red, green, blue, red, green, blue);
// create display list
displayList = GL11.glGenLists(1);
GL11.glNewList(displayList, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_QUADS);
// draw back (visible in reflection)
GL11.glVertex3f(-radius, -radius, 0.0f); // bottom left
GL11.glVertex3f(-radius, radius, 0.0f); // top left
GL11.glVertex3f(radius, radius, 0.0f); // top right
GL11.glVertex3f(radius, -radius, 0.0f); // bottom right
// draw front
GL11.glVertex3f(-radius, -radius, 0.0f); // bottom left
GL11.glVertex3f(radius, -radius, 0.0f); // bottom right
GL11.glVertex3f(radius, radius, 0.0f); // top right
GL11.glVertex3f(-radius, radius, 0.0f); // top left
GL11.glEnd();
GL11.glEndList();
// call move to get correction position
move(initialTime);
}
public void move(float elapsedTime) {
time += elapsedTime;
time %= period;
// http://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion#Position_as_a_function_of_time
double M = time * (2.0 * Math.PI) / period;
// find root of f(E) = E - e*sin(E) - M using Newton's method
// f'(E) = 1 - e*cos(E)
// first guess, compare
// http://de.wikipedia.org/wiki/Kepler-Gleichung#Berechnung_der_exzentrischen_Anomalie
double E = (M + e * Math.sin(M) + 0.5 * e * e * Math.sin(2.0 * M));
double f = E - e * Math.sin(E) - M;
for (int i = 0; i < 30 && Math.abs(f) > 0.001; ++i) {
E -= f / (1.0 - e * Math.cos(E));
f = E - e * Math.sin(E) - M;
}
double phi = 2.0 * Math.atan(Math.sqrt((1.0 + e) / (1.0 - e)) * Math.tan(E / 2.0));
double r = a * (1.0 - e * e) / (1.0 + e * Math.cos(phi));
// rotate position
Quaternion pos = new Quaternion((float) (r * Math.cos(phi)), (float) (r * Math.sin(phi)), 0.0f, 0.0f);
Quaternion.mul(rotation, pos, pos);
Quaternion.mulInverse(pos, rotation, pos);
position.x = pos.x;
position.y = pos.y;
position.z = pos.z;
light.setPosition(position);
}
@Override
public void draw() {
// store current transformation matrix
GL11.glPushMatrix();
// translate
GL11.glTranslatef(position.x, position.y, position.z);
// set color
appearance.use();
// enable shaders
ballGlowVShader.enable();
ballGlowFShader.enable();
// draw
GL11.glCallList(displayList);
// 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;
}
}