Drawing Colored Cube

I have recently started coding on opengl ES 2.0, and very new to this field. Actually I want to draw colored cube on android such that each face will have different color. However I am only able to do single color shading using following fragment shader
precision mediump float;
void main() {
gl_FragColor =vec4(1.0,0.0,0.0,0.0) ;
Can anybody suggest how to make multicolor shader?
Also if anybody could provide sample code(Android) for reference, it would be really helpful.

You would need to add a per-vertex attribute to your mesh data to specify the color at each vertex. This also means that you can’t share vertices between faces as you may be doing right now.

Once you’ve done that, you would change the shader so that the vertex shader copies the per-vertex color into a ‘varying’ parameter - which the fragment shader would then copy into the gl_FragColor.

If what I’m saying is too complicated for you to understand, then you’ll need to go and read some OpenGL tutorials - or possibly buy a copy of “OpenGL® ES 2.0 Programming Guide” to study from. Detailed descriptions at this fundamental level are really beyond the scope of what we can teach you in a forum post - it takes an entire book to explain it all - and I don’t have enough time at my disposal to type that much! We could give you example code that you could just mindlessly copy - but then you’d get stuck again at the very next thing you tried to do.

So I strongly recommend getting a book to learn from. When you get stuck on really complicated things that books don’t cover - then by all means, come back here and ask.

– Steve

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.