/* 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 java.nio.FloatBuffer;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
import ponkOut.graphics.resources.Shader;
import ponkOut.graphics.resources.ShaderManager;
public class Lighting {
// the same as in light.glslf
private static final int MAX_LIGHTS = 2;
private static Shader lightingVShader;
private static Shader lightingFShader;
private static Shader lightingTextureVShader;
private static Shader lightingTextureFShader;
private static Shader lightingTextureBumpVShader;
private static Shader lightingTextureBumpFShader;
private static List<Light> lights;
public static void init() {
// create shader objects
lightingVShader = ShaderManager.getInstance().getShader("lighting.arbvp1");
lightingFShader = ShaderManager.getInstance().getShader("lighting.arbfp1");
lightingTextureVShader = ShaderManager.getInstance().getShader("lightingTexture.arbvp1");
lightingTextureFShader = ShaderManager.getInstance().getShader("lightingTexture.arbfp1");
lightingTextureBumpVShader = ShaderManager.getInstance().getShader("lightingTextureBump.arbvp1");
lightingTextureBumpFShader = ShaderManager.getInstance().getShader("lightingTextureBump.arbfp1");
// set global light settings
FloatBuffer ambient = BufferUtils.createFloatBuffer(4);
ambient.put(new float[] { 0.0f, 0.0f, 0.0f, 1.0f }).flip();
GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, ambient);
lights = new LinkedList<Light>();
}
public static void enable(boolean texture) {
if (texture) {
lightingTextureVShader.enable();
lightingTextureFShader.enable();
} else {
lightingVShader.enable();
lightingFShader.enable();
}
}
public static void enableBumpMapped() {
lightingTextureBumpVShader.enable();
lightingTextureBumpFShader.enable();
}
public static void disable() {
// it does not matter which shader is disabled
lightingFShader.disable();
lightingVShader.disable();
}
public static Light addLight(Vector3f position, float ambientR, float ambientG, float ambientB, float diffuseR,
float diffuseG, float diffuseB, float specularR, float specularG, float specularB) {
int next = lights.size();
if (next >= MAX_LIGHTS)
return null;
Light light = new Light(GL11.GL_LIGHT0 + next, position, new float[] { ambientR, ambientG, ambientB, 1.0f },
new float[] { diffuseR, diffuseG, diffuseB, 1.0f },
new float[] { specularR, specularG, specularB, 1.0f });
lights.add(light);
return light;
}
public static void remove(Light light) {
lights.remove(light);
// actually disable it by setting position.w = 0.0
// (does not work anymore)
// FloatBuffer pos = BufferUtils.createFloatBuffer(4);
// pos.put(new float[] { 0.0f, 0.0f, 0.0f, 0.0f }).flip();
// GL11.glLight(light.getId(), GL11.GL_POSITION, pos);
}
public static void updatePositions() {
for (Light light : lights) {
light.updatePosition();
}
}
}