Sign in to follow this  
Miguel71

OpenGL Lighting - Lightmaps and bumpmaps

Recommended Posts

Miguel71    116

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)?

Share this post


Link to post
Share on other sites
kauna    2922

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!

Share this post


Link to post
Share on other sites
3TATUK2    714

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

Share this post


Link to post
Share on other sites
L. Spiro    25638

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

Share this post


Link to post
Share on other sites
Miguel71    116

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.

Share this post


Link to post
Share on other sites
L. Spiro    25638
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

Share this post


Link to post
Share on other sites
Miguel71    116

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?

Edited by Miguel71

Share this post


Link to post
Share on other sites
3TATUK2    714

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.

 

http://www.youtube.com/watch?v=XM5jVwk_oP8

 

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now