NeHe OpenGL Lesson03 - Adding Color
This sample shows how to add color to your primitives. The main idea used here is that specify the vertex color for each vertex or the next all vertexes. And the vertex color was added by the function glColor3f. Well, sometimes most OpenGL beginners will be confused about it’s color shading pipeline, why it looks like this, how the final color should be and so on.
But the situation will become very easy to understand when we switch to use the GLSL instead of set so many render states like set the light sources, set mesh material properties(color material included here), diffuse textures(blend several textures together with texture stages will become more complicated), vertex color, and the action between vertex normal and the light sources and so on. A lots of objects mess together, so we could even figure out where we are. But we still need to continue ……
To specify vertex color for each vertex, just write some code like this:
glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.0f); glEnd();
The full source code could be found here. The whole program coded for Ubuntu OS, you need to install gcc, cmake, opengl on your system.
浙公网安备 33010602011771号