Hey devs!
I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.
Vertex Shader
uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
attribute vec4 a_Position;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;
void main() {
v_Position = vec3(u_MVMatrix * a_Position);
v_TexCoordinate = a_TexCoordinate;
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
gl_Position = u_MVPMatrix * a_Position;
}
Fragment Shader
precision mediump float;
uniform vec4 u_LightPos["+numLights+"];
uniform vec4 u_LightColours["+numLights+"];
uniform float u_LightPower["+numLights+"];
uniform sampler2D u_Texture;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;
void main()
{
gl_FragColor = (texture2D(u_Texture, v_TexCoordinate));
float diffuse = 0.0;
vec4 colourSum = vec4(1.0);
for (int i = 0; i < "+numLights+"; i++) {
vec3 toPointLight = vec3(u_LightPos);
float distance = length(toPointLight - v_Position);
vec3 lightVector = normalize(toPointLight - v_Position);
float diffuseDiff = 0.0; // The diffuse difference contributed from current light
diffuseDiff = max(dot(v_Normal, lightVector), 0.0);
diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower)* distance * distance))); //Determine attenuatio
diffuse += diffuseDiff;
gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours))*diffuseDiff))); //The expensive part
}
diffuse += 0.1; //Add ambient light
gl_FragColor.rgb *= diffuse;
}
Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance