Dynamic tile map lighting.

OMG, was that you?! HAVE MY BABIES

I’ve been messing around, and I already have a few questions. :S

First of all, what’s the difference in GL11-GL42?

Second of all, how do I even reference from a method like so:

public void render(GameContainer gc, Graphics g){
		Input input = gc.getInput();
		manageInput(input);

		GL11 gl = ???
		
		drawMap(gc, g);
	}

Thirdly, is there anything else I should know before I dive in?

Thanks for the help guys. I seem dumb when I start this stuff, but once I get the concept I’m pretty decent at programming this stuff.

Lwjgl puts opengl functions in a class corresponding to the version (or extension) that the function belongs to. So GL11 contains all the functions defined by OpenGL v1.1 (ie. base opengl). All additional functions introduced in OpenGL v1.2 are in GL12, etc. Similarly any functions defined as an extension go in a class named after the extension (eg. ARBVertexBufferObject).

If a computer supports OpenGL, then it will always support GL11 functions. For anything above that you should check ContextCapabilities.

Often it’s useful to choose a suitable base level (eg. GL20), check if that’s available on start-up and then use whatever GL20 and lower functions you need afterwards.

[quote]Second of all, how do I even reference from a method like so:
[/quote]
You don’t, you just call the functions directly since they’re static.

http://dl.dropbox.com/u/66337942/dynamicLightingRev1.bmp

Can someone explain this?

The code looks like this…

GL11.glViewport(0,0,GAME_WIDTH,GAME_HEIGHT);
	         GL11.glMatrixMode(GL11.GL_PROJECTION);
	         GL11.glLoadIdentity();
	         GLU.gluPerspective(45.0f, ((float)GAME_WIDTH/(float)GAME_HEIGHT),0.1f,100.0f);
	         GL11.glMatrixMode(GL11.GL_MODELVIEW);
	         GL11.glLoadIdentity();
	         GL11.glShadeModel(GL11.GL_SMOOTH);
	         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	         GL11.glClearDepth(1.0f);
	         GL11.glEnable(GL11.GL_DEPTH_TEST);
	         GL11.glDepthFunc(GL11.GL_LEQUAL);
	         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,
	         GL11.GL_NICEST);

Someone needs to find me a literal full explanation of how this thing works, I have no idea what I’m doing. Most of the tutorials I’ve found assume I know all of the terminology and what-not. It usually involves “conceptual code” as if I know how to convert to actual code. lol.

EDIT: For anyone who may read this, I found this. It’s helping me a bit with terminology, but the above problem still persists at the moment :stuck_out_tongue:
Here it is: http://www.arcsynthesis.org/gltut/

One thing you can probably get rid of is that last glHint, since it’s meaningless in every modern driver I’m aware of. The rest, you’ll need to pick up an OpenGL tutorial to learn what everything does. It’s doing a lot, nothing that can be answered in a few forum posts.

That’s the thing, all of the tutorials explain everything in the most complicated way possible with absolutely nothing visual for me to mentally grasp. Most of the time the shader tutorials don’t have screenshots of what you’re making either, so you have no idea on whether or not you’re wasting your time or not.

I wish I had comforting words there, but learning OpenGL from online tutorials really is a damned tricky thing, requiring you to go between lots of different ones to pick up basics. Then there’s learning it in Java, which have hardly any tutorials written using it. While LWJGL clones the OpenGL C API almost perfectly in java, the various books still tend to assume C++, and porting things like matrix classes from them can be very tricky if you don’t already have C++ experience.

I found the original Red Book, obsolete as it is, to be an immensely helpful resource, but it’s no help for learning the programmable pipeline. It could be that learning the fixed function pipeline then unlearning it for shaders is still the best way to ease into it.

Well, the lighting I want is really simple. I just want black if there’s no light, and circles of light where there is. If you can just point out functions in the direction of doing this, I can just kind of bullcrap my way through.

I could do this the simple way, and make shading voxel style myself, but it’d look bad with the sketched graphics.

Yes, that looks like you have mismatched lwjgl jar + source setup in eclipse. The debug info from the .class file it’s running says that the function is at line 2052, but when eclipse tries to find that in the source attachment you’ve given it it doesn’t exist.

I’d suggest upgrading your lwjgl (jars + source + docs) to the latest so you know they’re all the exact same version.

As for the actual null pointer error, I’d guess you’re trying to issue GL commands before you’ve created a display?

I just updated all of the jars and DLLs. I don’t use the sources or docs, so I leave them out. Same problem persists.

About 90% of the time though, things don’t work just because I’m the one using it ;p

EDIT:
Here’s my code for initializing the game window.

//Set up display
		try {
			 Display.setInitialBackground(1, 1, 1);
			 
	         JFrame frame = new JFrame("Nusarium");
	         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	         CanvasGameContainer container;
	         container = new CanvasGameContainer(new ScalableGame(new Game(), GAME_WIDTH, GAME_HEIGHT));
	         container.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); //NOTE: Usable 640x480
	         container.getContainer().setTargetFrameRate(60);
	         container.getContainer().setShowFPS(true);
	         
	         	GL11.glViewport(0,0,GAME_WIDTH,GAME_HEIGHT);
	            GL11.glMatrixMode(GL11.GL_PROJECTION);
	            GL11.glLoadIdentity();
	            GLU.gluPerspective(45.0f, ((float)GAME_WIDTH/(float)GAME_HEIGHT),0.1f,100.0f);
	            GL11.glMatrixMode(GL11.GL_MODELVIEW);
	            GL11.glLoadIdentity();
	            GL11.glShadeModel(GL11.GL_SMOOTH);
	            GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	            GL11.glClearDepth(1.0f);
	            GL11.glEnable(GL11.GL_DEPTH_TEST);
	            GL11.glDepthFunc(GL11.GL_LEQUAL);
	            GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,
	            GL11.GL_NICEST);
	         
	         if (!stretchScreen){
	        	GridBagConstraints c = new GridBagConstraints();
	         	c.fill = GridBagConstraints.CENTER;
	         	frame.getContentPane().setLayout(new GridBagLayout());
	         	frame.getContentPane().add(container, c);
	         }
	         
	         	frame.getContentPane().setBackground(java.awt.Color.black);
	         	frame.getContentPane().add(container);
	         	frame.pack();
	         	frame.setLocationRelativeTo(null);
	         	frame.setResizable(true);
	         	frame.setMinimumSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
	         	container.requestFocusInWindow();
	         	frame.setVisible(true);
	         	container.start();
	         
	         frame.addWindowListener(new WindowAdapter(){
	        	  public void windowClosing(WindowEvent we){
	        	  Display.destroy();
	        	  System.exit(0);
	        	  }
	         });
	    
	         
	      } catch (SlickException ex) {
	         ex.printStackTrace();
	      }

You are trying to use OpenGL before it’s been initialized (this also happens when you try to render from a different thread). GL calls should be made from the same (rendering) thread.

The solution to your problem is to move your GL11 calls to your game’s init() method.

Why the ScalableGame? Also, if you haven’t already, be sure to update to the latest slick dev branch code. Then you can use getContainer().exit() instead of manually destroying the display/AL. I’ll try to put up a tutorial on pulling dev branch code in the next couple days, and hopefully get a merge in soon so the code is downloadable in binary form on Slick’s website.

Okay. Scalable is so I can resize the window, and stretch it if I want too. Weird situation. lol. It needs A LOT of fine tuning.

Anyway, since you seem to know a lot about this stuff, can you answer a question I put on here earlier?

“Well, the lighting I want is really simple. I just want black if there’s no light, and circles of light where there is. If you can just point out functions in the direction of doing this, I can just kind of bullcrap my way through.”

I’d appreciate it.

Also, why the need for 3D perspective, depth testing, etc? Are you making a 3D game? You’ll have a rough time with Slick2D if you try to integrate certain utilities (like, say, Image or ScalableGame) alongside low-level GL calls. If you plan to do 3D, I’d recommend either avoiding Slick2D altogether (instead choosing Slick-Util or a different library), or using SlickCallable to be safe. Even then, ScalableGame + GL calls will give you trouble (as it warns in ScalableGame JavaDoc).

[quote]“Well, the lighting I want is really simple. I just want black if there’s no light, and circles of light where there is. If you can just point out functions in the direction of doing this, I can just kind of bullcrap my way through.”
[/quote]
I’m just jumping into this thread now, but there are some ways to do this in Slick2D without relying on low-level OpenGL calls.

The easiest is to use an alpha map, and you won’t need to worry about math or combining lights or what have you. One of the downsides to this is that it requires extra passes, which may lead you to fillrate limitations. The result is nice, though:

Is that what you’re looking for? Or are you looking for a more “low-res” / retro look? e.g. In Terraria; each tile is affected.

I’m just jumping into this thread now, but there are some ways to do this in Slick2D without relying on low-level OpenGL calls.

The easiest is to use an alpha map, and you won’t need to worry about math or combining lights or what have you. One of the downsides to this is that it requires extra passes, which may lead you to fillrate limitations. The result is nice, though:

Is that what you’re looking for? Or are you looking for a more “low-res” / retro look? e.g. In Terraria; each tile is affected.


[/quote]
The game is 2D, and all the graphics are sketched out/pixel drawn by me, so I need something that would fit this:

http://desmond.imageshack.us/Himg7/scaled.php?server=7&filename=newbitmapimagehq.png&res=landing

I’ve consider both styles of shading, but I think the alpha map would go best. What do you think? They need to be dynamic and re-sizable on the go.

What do you mean “dynamic”? Do they need to interact with walls? (i.e. will walls affect the lighting)

Alpha maps should be easier, and resizing is just a matter of drawing the map at a different size. If I get a chance I’ll throw together an example, although there should be some rudimentary code lying around slick forums already.

This thread may be of interest: http://www.java-gaming.org/topics/realistic-lighting-in-a-2d-game/20161/view.html

I’d like walls to affect lighting, but, if it’s one of those overcomplicated not worth it type deals I think I’ll just go with alpha maps. Haha. I’d appreciate that example too, if you get around to it.

Eventually this will make its way into the Slick wiki. If I’m ambitious, I’ll also add tutorials/code for two other examples: lighting tiles per-vertex and lighting via shaders.

The gist of the technique:

for each light {
    set draw mode to MODE_ALPHA_MAP
    clear alpha map
    draw alpha map image at light position
    
    set draw mode to MODE_ALPHA_BLEND
    set clip (glScissor) to ignore pixels outside of the lit area
    draw tile map as per usual
    clear clip
}
set draw mode to MODE_NORMAL 

Alpha maps won’t be very efficient if you plan to have many lights at once (in that instance, you could improve efficiency by “baking” the lights, i.e. rendering static lights to an offscreen image). Also, the lighting won’t interact with walls or corridors. If your game is very particular, you might be able to hack around with some different alpha maps to fake that “dynamic” aspect, but it would probably be easier to just use per-vertex lighting or shaders.

Code:
http://www.java-gaming.org/?action=pastebin&id=76

Sprite sheet: (right click + save as)

(credits to Lost Garden for the awesome and free ground tiles)

I’m going to use this more than likely, but if you happen to know of any good shader tutorials that’s as informative and helpful for visual learners as yours is, please do share. Haha

Thank you!