lighting in a 2d game

Started by
17 comments, last by skjinedmjeet 18 years, 8 months ago
i'm having a little bit of trouble implementing opengl lighting into a 2d game, could someone point me in the right direction?
Advertisement
i'm sorry to be rude, but do you even need ligthing in a 2D game? also, can you post your exact problems? i don't want to answer you with something your not even looking for:-) so post the problems, and i'll get back to you:-)

later
Quote:Original post by lack the hack
i'm sorry to be rude, but do you even need ligthing in a 2D game? also, can you post your exact problems? i don't want to answer you with something your not even looking for:-) so post the problems, and i'll get back to you:-)

later


i guess my problem is that the lights seem too dark and i'm not sure of the best way to "brighten" things up


You can brithen things up by changing the Blending Mode. In DirectX if you set your Source to SrcAlpha,and your Destination to INVDESTCOLOR and change the Texture Stage State to D3DTOP_MODULATE2X on the COLOROP you can then draw your tiles brither than they look which in term means you are adding light. I hope that help
I would need to know how yo are doing lighting now to be able to tell you how to modify that to get better lighting.


Quote:Original post by kburkhart84
I would need to know how yo are doing lighting now to be able to tell you how to modify that to get better lighting.


GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 128.0f, 116.0f, 2.0f, 1.0f };

/\/\/\/\/\/\/\/\/
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);

/\/\/\/\/\/\/\/\/
glEnable(GL_LIGHTING);

You haven't shown any screenshots, so it's difficult to tell what should be changed.

IMHO you should play with ambient.
Other suggestions

1) Set up normals correctly ( for example set every normal as 0,0,1 )
2) Remember that by default OpenGL assumes your normals have unit length; if you glScale an object you have to 'renormalize' the normals accordingly.
3) Read the light tutorial on the red book (the first link I've found with google)
4) Do you really need lighting in a 2D game ? Can you simulate it ? (today you have no speed penalty in using real time light however...)
I've been doing light in just the way that BornToCode describes, with great results, with a generic light sprite that I draw over top of a tile map. I also split my tilemap into layers, and lightmap the floor layer. (With walls occluding light)

This is actually a lot easier to do then you might think, just look into line/box intersection.

For a tilebased game, I think this is a cool way to do it, but I have no idea if your game is tilebased or not.
OpenGL lights work too.

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

This ambient light will be a grayish light. Maybe if you made it like this.

GLfloat LightAmbient[]= { 0.8f, 0.8f, 0.8f, 1.0f };

That is a brighter white color, if that is what you are looking for. This is the ambient light that hits everything though.

**EDIT**

Also, check all the normals and make sure they are what they should be. The next step for cool lighting will be setting up spotlights in the 2d world. For example, Inside a room, put a spotlight at the position(0, 0, 5) and set the cone angle right. Have it point at your tile map, vector(0, 0, -1), and if the angle is right, you get a circle look, or at least the center will be brighter than the outside areas by the walls.


This topic is closed to new replies.

Advertisement