Lighting - Lightmaps and bumpmaps

Started by
8 comments, last by 3TATUK2 10 years, 6 months ago

Hi, I am currently working on a game engine and I have a problem.

I want to introduce lighting to my engine. I know the opengl has only 8 lights so I need to implement my own lighting system. I've tried somee simple fragment shaders to do that but the performance is poor when i use many lights. So I think that I need to use lightmaps on the static geometry.

My question is how can I implement bumpmapping using lightmaps(or any other way to do lighting)?

Advertisement

hello, you can consider implementing deferred rendering for complex geometry with many lights. give it a chance.

Thanks for the reply, I will try to do some research on it as soon as possible, but, could you explain me more about how that works?

Just to point out when you say "8 lights" it means that for each rendering call you can have up to 8 lights defined. You should be able to find out which lights affect which objects (typically only few) and set the lights for each draw call.

Of course, all that fixed function stuff is implemented with shaders nowadays, and making your own lighting system isn't a bad idea.

Cheers!

Lightmaps are very suitable for static geometry lighting... and advanced directional lightmapping can even be used for semi-dynamic model lighting (light changes on model depending or their position in the map)

Basically you first have to know how to bake a lightmap.. I use the method described here: http://forum.unity3d.com/threads/8998-Making-lightmaps-in-Maya-(tutorial)

Then implementing it on the code side.. you basically have to have a "duplicate" model of your map, with identical geometry.. You use that to create the lightmap so it gets new uv's... You import the UV's from the lightmap, pass them to the fragment shader, use them to do a texture lookup with the lightmap texture using the same UV coords, and lightply in the result

kauna has the best reply.

Doing baked lighting or writing a deferred renderer is an extremely long way to go out of one’s way just because of a simple misunderstanding about how many lights are supported in OpenGL.

To be clear: The limit on the number of lights in OpenGL’s fixed-function pipeline is limited only by the amount of memory on your machine.

The limit is 8 at a time.

There is nothing stopping you from rendering using 8 lights, then again with 8 more, and again, etc., until all 20,000 lights in your scene have been processed (never try this at home kids).

Lighting is additive, so each pass simply adds on top of the previous.

Also mentioned was that you really should be using shaders anyway. Deferred renderers requires shaders in the first place, and it is still much easier to switch to shaders than to bake and use lightmaps.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks for the replies! Although I am going to use the old opengl(probably 2.1) I dont want to use the fixed-function functionality, and I have sucessfully implemented lighting by shaders. The thing is that I want to do per fragment lighting, wich I found to be slow when using multiple lights.

I've done some research about deferred rendering and it seams to be my best option. I've sucessfuly made the G-Buffer but i'm having trouble doing the light pass. I'm not sure if I should create another topic about that or not, but i will post part of my code and stuff.

normals

BFJCBl0.png

position

pVKulul.png

diffuse

Bslwqml.png

I think those are okay. Here is my shader that generates those:


varying vec4 color;
varying vec2 texCoords;

varying vec3 N;

varying vec3 pos;

void main() {
    color = gl_Color.rgba;
    gl_Position = ftransform();
    texCoords = gl_MultiTexCoord0.xy;

    N = normalize(gl_NormalMatrix * gl_Normal);
	
	pos = vec4(ftransform()).xyz;
}


varying vec4 color;
varying vec2 texCoords;
uniform sampler2D textureSampler;

varying vec3 N;
varying vec3 v;

varying vec3 pos;

void main()
{
	gl_FragData[0] = color * texture2D(textureSampler, texCoords);
	gl_FragData[1] = vec4(N, 1.0);
	gl_FragData[2] = vec4(pos, 1.0);
	gl_FragData[3] = vec4(texCoords, 0.0, 1.0);
}

and here is my lighting shader


varying vec2 texCoords;

void main() {
    gl_Position = ftransform();
    texCoords = gl_MultiTexCoord0.xy;
}

varying vec2 texCoords;
uniform sampler2D DSampler;//Diffuse
uniform sampler2D NSampler;//Normals
uniform sampler2D PSampler;//Positions
void main(){
	vec3 lpos = vec3(0, 0, 52);
	
	vec3 v = texture2D(PSampler, texCoords).rgb;
	vec3 N = texture2D(NSampler, texCoords).rgb;
	
	vec3 L = normalize(lpos - v);   
	vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);  
	Idiff = clamp(Idiff, 0.0, 1.0);
	
	gl_FragColor = Idiff * texture2D(DSampler, texCoords);
}

What is wrong with those. Everything is black.

Thanks.

First you do max(dot(N,L), 0.0);, which always produces a negative number or 0, and then you do clamp(Idiff, 0.0, 1.0);, which clamps between 0 and 1.

The only value that can get through that unharmed is 0, and all other values will become 0.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Well, to be honest I don't know how to fix that, i tried putting a minus before max(dot(N,L), 0.0), but that didnt work, so I removed clamp, but that didn't affect anything idk why.

Also, it actualy is working, I probably set the light in a bad position or something. My problem now is that it is upside down and the light is a bit strange. Here are some pictures of it in comparison with forward rendering:

Forward rendering

KJg5Im1.png

Deferred rendering

DVLdeIq.png

Idk what is wrong with it. Can somebody help me?

Thanks again. smile.png

PS:

Also, could someone tell me if you deferred rendering or forward rendering in your game/engine?

I use forward rendering with some nice tricks for simple&fast dynamic lighting. I find filling the gbuffer of deferred is just too large an overhead... So it's really only worth it if you have "A LOT" of *active* (on screen and visible) dynamic lights.

This is all done on a crappy card at ~200 FPS

This topic is closed to new replies.

Advertisement