Colouring 3D Objects


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

you could generate random color and pass it to your shader.

You can also build a random function inside your shader to pick ramdom color dynamicaly like

const float UINT_MAX = 4294967295.0;<br />
<br />
// Generate a random uint<br />
uint randhash(uint seed)<br />
{<br />
uint i=(seed^12345391u)*2654435769u;<br />
i^=(i<>26u);<br />
i*=2654435769u;<br />
i+=(i<>12u);<br />
return i;<br />
}<br />
<br />
// Generate a random float [0.0, 1.0]<br />
float randhashf(uint seed)<br />
{<br />
return float(randhash(seed)) / UINT_MAX;<br />
```<br />
<br />
make sure that you study<b> GLSL</b> a little bit, the beginner section of the sdk is perfect for that , i havent seen better for now<br />
<br />
<br />
regards<br />

Do we need to create a handle for colour buffers similar to the VBO and IBO and call functions like :

glGenBuffers(nummeshes, m_ui32Cbo);

glBindBuffer(GL_ARRAY_BUFFER, m_ui32Cbo); //in a loop

glBufferData(GL_ARRAY_BUFFER, sizeof(colours_array),colours_array, GL_STATIC_DRAW); // in a loop

I can’t find a sample program which uses a colours array to set the colour of different objects.

yes there is a sample that does the job :

DrawSceneFlatColoured in the OGLES2DeferredShading sample

If you had followed the pfx sample in the beginner section then no problemo you are ready for it ( only the color bit :slight_smile: )

but mostly the principle is to set your uniform variable glUniform4f containing your Colors ( vec4 (r,g,b,1) )

If you may look at the sample you will see it , that help aloooooooooooot.

hope it help

kind regards


I agree with David. Generating a unique colour in your CPU code and passing it in to the shader would be the most efficient solution.

There is no need to pass an array to the shader as you can calculate and assign a single value to a vec4 uniform before rendering the object.



David, I can’t find any DeferredShading sample program in my sdk. And, I can’t find anything new in pfx sample too. I want to know how should I change my fragment shader code and how to pass the colours to the shader.

Nothing new in the PFX Introduction ??? that a good one :slight_smile:

about the sample it s in the directory : SDK_3.1/Examples/Advanced.

about a sample usage here it is :

glUniform4f(aUniforms[j].nLocation, pMaterial->pfMatDiffuse[0], pMaterial->pfMatDiffuse[1], pMaterial->pfMatDiffuse[2], 1.0f);<br />
```<br />
<br />
i am just giving advise that is all , make sure you understand uniform and  GLSL the sample for beginner is super rich of new knowledge that solve this issue.<br />
<br />
good luck<br />
david<br />
<br />

there is a doc that is golden for me it s in the directory ;

/SDK_3.1/Documentation/Architecture Guides

the name is PowerVR.Performance Recommendations.pdf

beyond the recommendation ,there is a lot of info specially section 6.4 might be interesting.

very good stuff

Hey, Got the colour done. It was easy. Was doing silly things. Finally, I changed the fragment shader as :

uniform mediump vec4 myColor;

void main (void)


gl_FragColor = myColor;


and called the below in a loop :

int i32LocationColor = glGetUniformLocation(m_uiProgramObject, “myColor”);

glUniform4f(i32LocationColor, colours[0], colours[1], colours[2], 1);