/*
* Copyright (C) 2014 MillerV
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package aspect.render;
import aspect.core.AspectRenderer;
import static aspect.core.AspectRenderer.*;
import aspect.entity.Entity;
import aspect.entity.behavior.Behavior;
import aspect.util.Angles;
import aspect.util.Color;
import aspect.util.Matrix4x4;
import aspect.util.Vector3;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Matrix4f;
/**
*
* @author MillerV
*/
public class Light extends Entity {
private final int index;
private final Color color;
public boolean directional = false;
private static int numLights = 0;
public static final Light[] lights = new Light[8];
public static void clear() {
numLights = 0;
}
public Light(Color color) {
lights[numLights] = this;
this.index = numLights++;
this.color = color;
glLight(GL_LIGHT0 + index, GL_DIFFUSE, color.getBuffer());
glLight(GL_LIGHT0 + index, GL_SPECULAR, color.getBuffer());
glLight(GL_LIGHT0 + index, GL_AMBIENT, Color.BLACK.getBuffer());
glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, 1.0f / 20.0f);
glEnable(GL_LIGHT0 + index);
}
public void setAttenuation(float constant, float linear, float quadratic) {
glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, constant);
glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, linear);
glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, quadratic);
}
public static int numLights() {
return numLights;
}
public static void drawLights() {
for (int i = 0; i < numLights; i++) {
lights[i].drawLight();
}
}
private void drawLight() {
FloatBuffer buff = BufferUtils.createFloatBuffer(4);
buff.put(transform.position.getBuffer());
buff.put(directional ? 0.0f : 1.0f);
buff.clear();
glLight(GL_LIGHT0 + index, GL_POSITION, buff);
}
}