I am trying to figure out how to optimize lighting in opengl 2. I am using 4 lights per object, with the lights sorted by distance. It works well with just one light, but with any more than that It starts to slow down. I would like a way to speed up my lighting while getting similar visual results. Here is my lighting code:

The obvious optimisation is that for each dtXX vector you call both length() and normalize() on it, which means you're calculating the length twice. You can save four square roots by just dividing by the length you already have.

You may want to pull out the common 'ambient+lightcolor' into a temporary, but I'd hope the compiler is already doing that optimisation.

Edit: Double-also, you calculate the length(), then do disX*disX to get the length squared. You could just manually calculate that, then sqrt to get the length to save you some computation. That may or may not be an improvement depending on how smart length() is handled under the hood.

Double-double also: you're going to be completely vector op bound (obviously) while the texture unit sits idle. It may be worth moving some of your computation into a texture lookup. Possibly the light attenuation calculation?

Double-double also: you're going to be completely vector op bound (obviously) while the texture unit sits idle. It may be worth moving some of your computation into a texture lookup. Possibly the light attenuation calculation?

So:- For loops get automatically unrolled by the compiler, if constant loop count and if this constant is not to big- many loops instead of one big prevents read after write delays.- only real speed up I did, is getting rid of the sqrt in the length function(there is a distance(p0, p1) function in glsl when you need the distance between two points in a shader next time

ps:perhaps you noticed that I changed the name to lambert, because the dot product between the normal and lightdirection is the Lambert Term. Phong describes only how to calculate the specular reflection not the diffuse. Where Blin-Phong should be prefered over standart Phong.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org