Slick Forums

Discuss the Slick 2D Library
It is currently Thu Apr 24, 2014 11:13 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: AlphaMapLightTest
PostPosted: Tue Apr 26, 2011 8:03 pm 
Offline

Joined: Sun Mar 27, 2011 6:44 pm
Posts: 87
Location: Finland
Tommy asked this and I have coding buzz so it was fast.
I just edited alphaMapTest with minor modifications. Alphamap.png was too ugly with this effect so I made new one. With this approach we could do easy to use light effect class to Slick.

Image

Code:
package org.newdawn.slick.tests;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

/**
 * A test to demonstrate alphamap light effects
 *
 * @author kalle hämäläinen aka pitbuller
 */
public class AlphaMapTest extends BasicGame {
   /** The alpha map being applied */
   private Image alphaMap;
   /** The texture to apply over the top */
   private Image sky;
   /** the horizontal coordinate */
   private float x;
   /** the vertical coordinate */
   private float y;
   /** boolean array for all the keys */
   private boolean keys[] = new boolean[256];

   private final static float speed = 0.2f;

   /**
    * Create a new tester for the clip plane based clipping
    */
   public AlphaMapTest() {
      super("AlphaMapLight Test");

   }

   /**
    * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
    */
   public void init(GameContainer container) throws SlickException {
      alphaMap = new Image("testdata/alphamini.png");
      sky = new Image("testdata/sky.jpg");
   }

   /**
    * @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer,
    *      int)
    */
   public void update(GameContainer container, int delta)
         throws SlickException {
      if (keys[Input.KEY_UP])
         y -= speed * delta;
      if (keys[Input.KEY_DOWN])
         y += speed * delta;
      if (keys[Input.KEY_LEFT])
         x -= speed * delta;
      if (keys[Input.KEY_RIGHT])
         x += speed * delta;
      if (keys[Input.KEY_ESCAPE])
         container.exit();

   }

   /**
    * @see org.newdawn.slick.Game#render(org.newdawn.slick.GameContainer,
    *      org.newdawn.slick.Graphics)
    */
   public void render(GameContainer container, Graphics g)
         throws SlickException {
      /** first draw everything as normal */
      sky.draw();
      /** then add lights */
      lights(g, 4);

      /** then draw UI */
      g.setColor(Color.white);
      g.drawString("AlphaMapTest", 270, 10);
   }

   private final void lights(Graphics g, int size) {

      /** Scaling stuff so we don't have to use big alpha map image */
      float invSize = 1f / size;
      g.scale(size, size);

      /** setting alpha channel ready so lights add up instead of clipping */
      g.clearAlphaMap();
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);

      /** actual lights, coordinates are transformed to scaled coordinates */
      alphaMap.drawCentered(x * invSize, y * invSize);
      alphaMap.drawCentered(200 * invSize, 200 * invSize);

      /** Scaling back stuff so we don't have to use big alpha map image */
      g.scale(invSize, invSize);

      /**
       * setting alpha channel for clearing everything but light maps just
       * added
       */
      GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_DST_ALPHA);

      /** paint everything else with black */
      g.fillRect(0, 0, 400, 400);
      /** setting drawing mode back to normal */
      g.setDrawMode(Graphics.MODE_NORMAL);

   }

   /**
    * @see org.newdawn.slick.BasicGame#keyPressed(int, char)
    */
   public void keyPressed(int key, char c) {
      keys[key] = true;
   }

   public void keyReleased(int key, char c) {
      keys[key] = false;
   }

   /**
    * Entry point to our test
    *
    * @param argv
    *            The arguments to pass into the test
    */
   public static void main(String[] argv) {
      try {
         AppGameContainer container = new AppGameContainer(
               new AlphaMapTest());
         container.setDisplayMode(400, 400, false);
         container.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }
}


Last edited by pitbuller on Wed Apr 27, 2011 7:36 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2011 8:05 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
tnx for sharing! Lighting is something that slick community needs to evolve...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2011 8:27 pm 
Offline

Joined: Sun Mar 27, 2011 6:44 pm
Posts: 87
Location: Finland
Code:
      /** paint everything else with black */
      g.setColor(Color.black);

I noticed that this is redudant.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2011 9:16 pm 
Offline

Joined: Tue Dec 07, 2010 9:36 pm
Posts: 47
Location: United States
Very nice,
Thanks for sharing!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 27, 2011 7:42 am 
Offline

Joined: Sun Mar 27, 2011 6:44 pm
Posts: 87
Location: Finland
Looked my code carefully and tried to understand everything. WIth help of this reference document http://pyopengl.sourceforge.net/documen ... nc.3G.html
I could edit these GL functions look symmetric. Nothing changes at output but code is much cleaner look now. Even if you don't understand anythin at openGL(like me) you can almoust understand what these lines are supposed to do.
Code:
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
//lightmaps there
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_DST_ALPHA);
//black rect there



I noticed that glBlendFunc have some parameters for colours is it possible to do colored light with this method?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group