I'm making an Android game using OpenGL ES 2.0, and I have a question on what would be the best strategy:
I want to have a plane (the ground) made of multiple triangles (like a chessboard, with each case as 2 triangles), and I want each triangle to have a color that will be updated each frame depending on its distance to my "light" (using the center of the triangle probably). But I don't want a regular illumination with interpolation, the whole triangle has to be the same color.
So I was wondering which strategy would be the best:
- Have a Java class for each triangle that update its color at runtime and then draw a triangle with a basic fragment shader, passing the color as a uniform,
- still have a Triangle class but that pass parameters to the shader (distance to the light, etc...) so the color can be determined in the shader,
- or draw all the triangles at once, with a huge vertex buffer, and a "draw order" buffer, but in this case, I don't know if I it's possible to have the triangle center position in the fragment shader.
I'm concerned that the first solution might have low performances, if I have, say, 1000 triangles that must be updated each frame.
I hope my question isn't too messy, I'll try illustrating it if necessary.
Thanks !
EDIT:
I tryed implementing the second solution, and I have a fragment shader that look like that:
uniform vec4 vMinColor;
uniform vec4 vMaxColor;
uniform vec3 vLightPosition;
uniform vec4 vCenterPosition;
uniform float vLightRadius;
uniform float vNoise;
void main(){
float dist = distance(vLightPosition, vCenterPosition);
if(dist > vLightRadius){
gl_FragColor = vMaxColor;
} else {
vec4 colorDiff = vMaxColor - vMinColor;
float shade = floor(10*dist/LightRadius)/10;
shade = shade + vNoise;
vec4 color = vMinColor + shade*vMaxColor;
gl_FragColor = color;
}
}
I'm not sure it works though. (Actually I'm sure, I get an error 502 when passing uniforms using glGetUniformLocation...)