0

I have a mesh that consists of several triangles (order 100). I would like to define a different fragment shader for each of them. So to be able to show different kind of reflection behaviour for each triangle.

How should I approach this problem? Should I start defining a GLSL program and try to distinguish between different triangles? this answer is suggesting me that this is not the right approach glDrawElements and flat shading . Even this Using a different vertex and fragment shader for each object in webgl seems not the right approach since I do not want to have multiple objects, but just one with different materials(fragment shaders) on it.

Community
  • 1
  • 1
BiA
  • 364
  • 2
  • 6
  • 23

1 Answers1

1

My suggestion would be to create a super shader which can handle all the different scenarios you desire.

In order to set this up you'll need attributes that dictate which part of the shader to use.

e.g. in your vertex or fragment shader:

attribute bool flatShading;
attribute bool phongShading;
if (flatShading) {
  // perform flat shading
} else if (phongShading) {
  // perform phong shading
}

Then setup your buffers as so that the vertices in each triangle have a certain shading attribute applied.

Brendan Annable
  • 2,637
  • 24
  • 37
  • Thanks for you answer. I got the idea and I think I will go in this direction. The only perplexity that I have is how can I set an attribute for triangle: since every triangle has 3 vertex, 2 of them are shared with another triangle. Setting proprieties for vertex seems a wrong way. But maybe I lack some opengl theory here – BiA Mar 01 '15 at 11:46
  • Essentially you will need to duplicate vertices which are on a boundary of separate shading (it sounds like all of them in you case). It's a lot of redundancy I know. – Brendan Annable Mar 01 '15 at 21:23