vendredi 6 mars 2015

opengl - unique color for each triangle


I am trying to shade my model such that each triangle is a solid, unique color based on its position in the buffer. I would like to know if this is a proper method of doing so.


You can skim to the last paragraph if you need no information about OpenGL


This is my Fragment Shader that will color based on the triangles' index in the buffer.



#version 420 core
out vec3 color
void main(){
float r, g, b;
r = (gl_PrimitiveID % 255) / 255.0f;
g = ((gl_PrimitiveID / 255) % 255) / 255.0f;
b = ((gl_PrimitiveID / (255 * 255)) % 255) / 255.0f;
color = vec3(r, g, b);
}


It uses gl_PrimitiveID which essentially gives the location in the buffer of the triangle it is shading. So if I do a command to draw a buffer and say there are triangles in the buffer, gl_PrimitiveID will say that the first one is 0, then second triangle is 1, and the nth triangle is n-1.


OpenGL has its RGB values such that they are floats between 0.0f and 1.0f (I do not know if 1.0f is inclusive and is part of my problem).


If 1.0f end is inclusive I assume the method I have displayed above will work accurately, but if it is exclusive I think that I may have to divide by 256.0f instead of 255.0f. Which one is proper?


Example of replacement:



r = (gl_PrimitiveID % 255) / 256.0f; // notice 256.0f instead of 255.0f



Aucun commentaire:

Enregistrer un commentaire