2D game with Dynamic lighting possible?

Started by elias4444, December 09, 2004, 03:44:40

Previous topic - Next topic

elias4444

Yep, yet another question from the n00b (however, I would like to announce that I'm learning as quickly as I can):

I ported a game of mine from Java2D over to OpenGL (via lwjgl of course). I used the glOrtho trick to get my sprite images to display correctly (as opposed to the whole coordinate change thing of OpenGL which apparently caused all of my images to be upside-down):
GL11.glOrtho(0, WIDTH, HEIGHT, 0, -2, 2);

It all works great, only now that I'm using OpenGL, I'm wanting to get some dynamic lighting going. However, as I try to setup any kind of dynamic lighting, nothing seems to happen. Everything is perfectly visible even without setting any light. If I set lights, it doesn't seem to make any difference at all. Can this even be done? I'd love to find a way to have some of my sprites actually emit light from themselves.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

princec

Do not use GL lighting. Especially not for 2D. If you want to add lighting, just modulate the colours of the corners of your sprites.

Cas :)

elias4444

QuoteDo not use GL lighting. Especially not for 2D. If you want to add lighting, just modulate the colours of the corners of your sprites.

Now, I'm sure I'm supposed to understand how to do that... but, I don't.
Modulate the colours of the corners of my sprites? You mean in the textures? I already flip through textures in order to create animations.

Now, Cas, if you'd indulge me in asking another question of you (who I consider to be quite the lwjgl programmer extraordinaire) - What do you use in your games like AlienFlux and SuperElvis? Those are 2D type games, but built on openGL via lwjgl. You obviously have great dynamic lighting and some fantastic animations for sprites and things. Are you doing the same glOrtho trick? What are you doing for lighting?

Ok, that was a couple of questions, but I'd sure like to know. I'm really getting into this 3D programming thing now that I'm picking up some of the basics.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

princec

I actually use a perspective projection where the Z=0 plane just happens to be orthographic ;)

To modulate the colours of a sprite:

GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
GL11.glColor3f(1.0f, 0.0f, 0.0f);
GL11.glTexCoord2f(...);
GL11.glVertex2i(top_left_x, top_left_y);
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glTexCoord2f(...);
GL11.glVertex2i(top_right_x, top_right_y);


etc.

Then work out the lighting yourself simply by attenuating the light according the distance to the corner of the sprite to the light source.

Cas :)

elias4444

Ah ha! Got it! I'm now changing the color of the sprite itself. Although, is there a way to actually have the sprite emit light so as to illuminate the surface just below it and around it? (beyond the sprite's borders).
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

princec

No, you've got to draw that stuff yourself.

Cas :)

elias4444

Really? I always thought there was a way to create a point of light that diffuses across a space? Isn't there a way to do that?

But you're saying I'd actually need to create another translucent sprite to lay inbetween the main sprite and the background surface in order to create the effect?
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

Orangy Tang

Vanilla OpenGL lighting does that for you (you just have to poke in the right state) but because its per-vertex it tends to look like crap for 2d games. Its often easier and better looking to do it manually, either via vertex colours yourself or some glow-y sprites.

If you use OpenGL lighting you also have to remember to set normals, which is a whole load of extra redundant memory use (although you could probably jimmy it by just using glNormal3f(0,0,1) early on and leaving that as constantly set state).

oNyx

>But you're saying I'd actually need to create another translucent sprite to
>lay inbetween the main sprite and the background surface in order to create
>the effect?

Yes. Like a grayscale image with a radial gradient in it.

The other way would be a spotlight... but you can forget about that. As Orangy said it's per vertex. That means you would need really tiny tiles in order to get some kind of "natural" falloff. That's just not worth it.

Here is an image you can use:

elias4444

Thanks for the image (I'll try it if I can't figure out what's going on with the one I made myself). Everything seems to be working great now, except for this new issue. I now get the following problem when the "glow objects" overlap:



For some reason, when there are two glows under the blue sprite, the blue sprite's square no longer acts translucent. I'm using the following settings for my alpha blending:

GL11.glBlendFunc(GL11.GL_SRC_ALPHA, L11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_BLEND);

Any ideas? The glow-images I'm using are translucent PNGs. Could that be the problem perhaps?
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

elias4444

Ok, I seem to have found something, although it doesn't make much sense to me.

The problem goes away so long as I place the glow-sprite and the regular sprites on the same z-coordinate plane. I just have to draw the glow sprites first, and then the regular sprites (so the regular sprites appear "on top"). I don't understand though why if the glow-sprites are placed at a z-coordinate just under the regular sprites that it would cause that strange alpha issue to appear.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com