Advertisement

Cg - Multiple Lights

Started by August 20, 2003 09:22 AM
13 comments, last by ZMaster 21 years, 6 months ago
Hi, I''m reading "The Cg Tutorial", a book about vertex/fragment shader programing using NVidia''s Cg language, at the moment. What I''m wondering about is: How would you do multiple Lights using your own vertex/fragment shader for per-vertex/per-pixel lighting. I mean, how would you for example realize 12 Lights (OpenGL is limited to 8, I know, but I''m talking about lights using shader programs). Is there a way to have more than one instance of a shader program, so, that you could use n instances of a lighting program with n different light parameters? Or do you have to write some kind of a for-loop in your program to render n lights (loops are only supported by NV30 and R300, aren''t they?). Or is the only possibility to render every light in a seperate rendering pass (I think this is damn slow if you render 12 lights with 12 passes ) I have really no idea how to do multiple lights the best way, but it would be really cool if I could render a (nearly) unlimited number of lights with a vertex program and a C++ class in a single pass TIA, Flo
Adjust your formula so that the lighting is additive - so different lights are blended with GL_ONE GL_ONE. When you want to render the scene, render an ambient pass, then render 1 pass for each light in the scene.
Advertisement
Yeah, my formula would be additive. So you''d advise to go a pass for every light visible. Isn''t this much slower than using OpenGL''s lighting? I could do 16 Lights with 2 passes and with a vertex program I would only have 2 Lights with 2 passes?
Well if you render several lights in 1 pass its sure faster but not as much as one might think. Remember when you only have to render 1 light, you only render those parts of the world that the light is actually interacting with. Another thing is that calculating multiple lights in the pixelshader/vertexshader also takes longer. I dunno what you want to achieve but if you want something like per pixel lighting, you cant use gl anyway...
Yes, you''re right, I have to use fragment programs or register combiners or something to achieve per-pixel lighting.

I just thought about writing my own lighting class with support for per-vertex and per-pixel lighting, visibility determination and depth-testing for lights and the possibility to add as many lights as needed and even if these would be more than 8.

I read some parts of Carmack''s .plan and he wrote that with the NV30, for example, there is most time only one rendering pass required in Doom III. He was talking about the vertex-programs he implemented and what extensions they use, so I don''t know if he meant there is only one pass for the vertex stuff required or one pass even for the per-pixel lighting he uses. So I thought ppl could give me their ideas, how they would implement lighting - that''s what I want
i read that to, basicly he was saying that he only needed one pass per light, when it comes to shaders lighting and stuff.

Allthough you can write a shader that works with more than one light at a time, you can only get shadows from one lightsource using stencil shadows at one time, well, that is unless you pre render all the shadow passes to individual textures, but even then you can''t do more than about 14 light''s per pass.
and it still is gonna be slow as hell and take lot''s of memory.
Advertisement
you can do it in one pass.

just calc for each light source the light attributes and apply to color.

then use fragment to render the color.

Bobby.

Did it with it multi pass and loss Frame per seconds.
Did it one pass and was happy.
Game Core
@BGCJR: But how exactly, would you implement the ligting operations with one pass?
I''m doing all the lighting calculations in my vertex/fragment program what actually is much faster on newer hardware than computing it on the CPU. So the final vertex/pixel color is determined by it''s program executed on the GPU. If I have more than 1 light I would have to compute the lighting term n (n = the number of lights) times. That''s the point. I need to execute a program more than one time (with multiple instances, multiple executions of the program or a loop within the program or whatever).
try to load 4 to 6 light coordinates


try to load the light coordinates with programinput(something like that)

then repeat the code for lights in vertex program.
no looping in vertex so just copy and paste so it passes 4 to 6.

blend into color array.

then output in fragment program or nvparse(register combiners).

anyway, your going to need to calculate each light.
gllights is predefined, but vertex program always customized lighting calcs.
you can read in glights parameters with vertex program, and do your calcs and ignore others.

Bobby

Game Core
Ok, thanks ppl. I''ll try this stuff then. I''d like to implement stencil shadow volumes anyway, so I won''t come around one pass per light.

This topic is closed to new replies.

Advertisement