0

I have many objects in my scene and I want to colour every object with different colours. Now, my fragment shader consists of :

void main (void)\
{\
     gl_FragColor = vec4(0.82, 0.41, 0.12 ,1.0);\
}";

and the vertex shader consists of :

attribute highp vec4    myVertex;\
uniform mediump mat4    myPMVMatrix;\

void main(void)\
{\
gl_Position = myPMVMatrix * myVertex;\
}";

and hence it colours each object with the same colour. Can anyone tell how can I colour differently ? I have prepared a 2D array consisting of the colours for all the objects. I can't figure out how to pass them to the fragment shader or how to change the fragment shader and vertex shader code ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Hellboy
  • 1,199
  • 2
  • 15
  • 33

1 Answers1

1

The best way to color objects individually is to pass a uniform (like you did with myPMVMatrix) containing the color you want, for each object. You would have a uniform vec4 objectColor in the fragment shader that you can directly use inf gl_FragColor.

The fragment shader would look like:

uniform mediump vec4 myColor;\
void main (void)\
{\
     gl_FragColor = myColor;\
}";

and you would pass it exactly like you passed your myPMVMatrix, just with the word myColor instead of myPMVMatrix.

nbonneel
  • 3,286
  • 4
  • 29
  • 39
  • How will the vertex shader and fragment shader code change. Can you explain by writing both the shader's code ? Also, how will I pass the colour from my main function ? – Hellboy Jun 29 '13 at 13:23
  • I passed the matrix by : int i32Location = glGetUniformLocation(m_uiProgramObject, "myPMVMatrix"); glUniformMatrix4fv(i32Location, 1, GL_FALSE, PMVMatrix.ptr()); But, doesn't this pass the matrix to the vertex shader ? Don't I have to use a variable myColor in vertex shader also ? – Hellboy Jun 30 '13 at 09:51
  • you don't need to : the glGetUniformLocation can find the location of a variable in a fragment shader as well. If you just do a glGetUniformLocation(m_uiProgramObject, "myColor"); and glUniform3fv(...) this should send your variable to the correct shader. – nbonneel Jun 30 '13 at 16:59