Hello,
With apologies, I think that I originally posted this under the wrong section, so I moved my question here. I am having difficulty creating a lighting system for my game. The way I plan to render lighting is like this:
- Create a lighting map by using additive blending and radial gradients from all of the lightsources
- Combine the lightmap with the rendered game using multiplicative blending
The issue with my code so far is creating a radial gradient class. I am using Slick2D, which doesn’t include a radial gradient class that implements ShapeFill, the interface used to fill shapes. I decided to create one myself, which looks like this:
`import org.newdawn.slick.;
import org.newdawn.slick.geom.;
import org.newdawn.slick.svg.*;
public class RadialFill implements ShapeFill {
Color start;
Color end;
float centerX;
float centerY;
public RadialFill(Color s, Color e){
start = s;
end = e;
}
@Override
public Color colorAt(Shape shape, float x, float y) {
float e = (float) (Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY))) / ((shape.getHeight() > shape.getWidth() ? shape.getHeight() : shape.getWidth()) / 2 );
float s = 1 - e;
Color col = new Color(1,1,1,1);
col.r = (e * end.r) + (s * start.r);
col.b = (e * end.b) + (s * start.b);
col.g = (e * end.g) + (s * start.g);
col.a = (e * end.a) + (s * start.a);
return col;
}
public void setCenter(float x, float y){
centerX = x;
centerY = y;
}
public float getCenterX(){
return centerX;
}
public float getCenterY(){
return centerY;
}
public void setStartColor(Color s){
start = s;
}
public Color getStartColor(){
return start;
}
public void setEndColor(Color e){
end = e;
}
public Color getEndColor(){
return end;
}
@Override
public Vector2f getOffsetAt(Shape shape, float x, float y) {
// TODO Auto-generated method stub
return new Vector2f(0,0);
}
}
Here is the test code that I made:
import org.newdawn.slick.;
import org.newdawn.slick.fills.;
import org.newdawn.slick.geom.;
import org.newdawn.slick.state.;
import org.newdawn.slick.svg.*;
public class LightTestState extends BasicGameState {
Input input;
GradientFill grad;
RadialFill gradFill;
Ellipse mouseLight;
int mouseX;
int mouseY;
boolean ready = false;
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
input = container.getInput();
grad = new GradientFill(0, 0, Color.yellow, 100, 100, Color.black, false);
gradFill = new RadialFill(Color.yellow, Color.black);
ready = true;
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
if (ready){
g.setAntiAlias(true);
g.setDrawMode(Graphics.MODE_ADD);
mouseLight = new Ellipse(mouseX, mouseY, 100, 100);
gradFill.setCenter(mouseX, mouseY);
g.fill(mouseLight, grad);
}
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
mouseX = input.getMouseX();
mouseY = input.getMouseY();
}
@Override
public int getID() {
// TODO Auto-generated method stub
return 1;
}
}
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tests.*;
public class Noteblock extends StateBasedGame {
public Noteblock(){
super("Noteblock");
}
@Override
public void initStatesList(GameContainer container) throws SlickException {
// TODO Auto-generated method stub
container.setShowFPS(false);
addState(new LightTestState());
}
/**
* @param args
*/
public static void main(String[] args) {
//GradientTest.main(null);
try {
AppGameContainer container = new AppGameContainer(new Noteblock());
container.setDisplayMode(800,600,false);
container.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
`
However, when I try to create a radial gradient with this code, it doesn’t work. At best, it can only make a fill of one color. ??? Any ideas? Any help would be appreciated.