Working on 2D Escape game, contemplating lighting possiblities.

Started by KuDoKu, May 03, 2013, 01:39:12

Previous topic - Next topic

KuDoKu

Alright. So in LWJGL/openGL, a very simple way to do a "lighting shader" effect in a tilebased game, whether it be 32 x 32 or 16 x 16 or what have you, is to write the lighting for each vertex referenced for drawing, since there are only 4 vertices in a rectangle, and that isn't a ridiculous amount of math needed to be done when assigning light values to each vertex.

You can simply write a color value where R, G, and B are equal to whatever light value you want, the line before you draw each vertex of a quad, which would be the equivalent of tile-based lighting, just a lot more smooth. I.E:

GL11.glBegin(GL_QUADS);
GL11.glColor3f(lightForSpecificVertex, lightForSpecificVertex, lightForSpecificVertex);
GL11.glVertex2f(x, y);
GL11.glEnd();


instead of

GL11.glColor3f(lightForEntireSprite, lightForEntireSprite, lightForEntireSprite);
GL11.glBegin(GL_QUADS);
//Make your vertices
GL11.glEnd();


Example: (TLL = Top Left Light, TRL = Top Right Light, BLL = Bottom Left Light, BRL = Bottom Right Light)
public void render()
	{
		for (int i = 0; i < sprites.size(); i++) // Loop through an ArrayList of sprites
		{
			Sprite sprite = sprites.get(i);
			
			sprite.getTexture().bind(); // Bind texture to quad
			GL11.glColor4f(1f, 1f, 1f, sprite.alpha); // Alpha - Transparency
			GL11.glBegin(GL_QUADS);
				GL11.glColor3f(sprite.TLL, sprite.TLL, sprite.TLL); // Top left lighting of quad
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(-cx + main.currentDM.getWidth() / 2 + sprite.x, -cy + main.currentDM.getHeight() / 2 + sprite.y);
				
				GL11.glColor3f(sprite.TRL, sprite.TRL, sprite.TRL); // Top right lighting of quad
				GL11.glTexCoord2f(1, 0);
				GL11.glVertex2f(-cx + main.currentDM.getWidth() / 2 + sprite.x + sprite.getWidth(), -cy + main.currentDM.getHeight() / 2 + sprite.y);
				
				GL11.glColor3f(sprite.BRL, sprite.BRL, sprite.BRL); // Bottom left lighting of quad
				GL11.glTexCoord2f(1, 1);
				GL11.glVertex2f(-cx + main.currentDM.getWidth() / 2 + sprite.x + sprite.getWidth(), -cy + main.currentDM.getHeight() / 2 + sprite.y + sprite.getHeight());
				
				GL11.glColor3f(sprite.BLL, sprite.BLL, sprite.BLL); // Bottom right lighting of quad
				GL11.glTexCoord2f(0, 1);
				GL11.glVertex2f(-cx + main.currentDM.getWidth() / 2 + sprite.x, -cy + main.currentDM.getHeight() / 2 + sprite.y + sprite.getHeight());
			GL11.glEnd();
		}
	}


You can see TLL, TRL, BLL, and BRL designate the four corners of each sprite to begin a different light value from 0.0 - 1.0, which would be designated based off the light around it. This is one of the ways I'm considering writing lighting into my project, but if anyone else has a better idea, I would love to hear it. Hopefully this either helped someone who's looking for a way to do simple sprite-based lighting, or someone can help out with an even better idea. Thanks :)

quew8

Shaders? Faster, easier to implement, cooler.

Edit (Because links help):
http://www.lwjgl.org/wiki/index.php?title=GLSL_Shaders_with_LWJGL Setting up shaders (probably you shouldn't use the arb form)
http://www.lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Syntax Briefly on syntax.