0
votes

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

1

1 Answers

0
votes

I think it would be better to use one vertex buffer. In pi3d the default arrangement is to either pass vertex, vertex+normal, vertex+normal+texture_uv so where I want to do some non-standard shading I can utilise the 'spare' values.

I realise it's probably foreign code (python) to wade through but I do some buffer info passing https://github.com/pi3d/pi3d_demos/blob/develop/SpriteMulti.py

Have you already got some code you want to modify or are you starting from scratch?

EDIT - to make it clearer, I would pass two extra float values representing the centre of each element which would be used in the vertex shader to calculate the distance from the light (uniform) this distance would be passed as varying to the fragment shader.