Different colors are created on a computer by mixing the primary red, green and blue colors. This color scheme is more commonly known as RGB. By mixing these three colors you can create any imaginable color.
To create a bright red, we mix in 100% red, and 0% of green and blue. In OpenGL this color is represented as (1,0,0), with the first parameter being red, the second green and the third blue (R,G,B). For a dark green we could choose to mix in 50% green, and 0% of red and blue, described by (0,0.5,0).
Purple is created by mixing red and blue together. For a light purple we could choose to mix in 70% red and 70% blue, resulting in the color (0.7,0,0.7).
White is obtained when 100% of red, green and blue is used (1,1,1), whereas black is obtained when none of the colors are added to the mix (0,0,0).
OpenGL.glPushMatrix ' save matrix ' clear the background OpenGL.glClearColor(0, 0, 0, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT) ' move back a bit so that we can see the object OpenGL.glTranslatef 0.0, 0.0, -5.0 ' draw a red polygon OpenGL.glBegin OpenGL.GL_POLYGON OpenGL.glColor3d 1, 0, 0 OpenGL.glVertex3d -1, 1, 1 OpenGL.glVertex3d -2, 0, 1 OpenGL.glVertex3d 0, 0, 1 OpenGL.glEnd ' draw a colored polygon OpenGL.glBegin OpenGL.GL_POLYGON OpenGL.glColor3d 1, 0, 0 OpenGL.glVertex3d 1.5, 1, 1 OpenGL.glColor3d 0, 1, 0 OpenGL.glVertex3d 0.5, 0, 1 OpenGL.glColor3d 0, 0, 1 OpenGL.glVertex3d 2.5, 0, 1 OpenGL.glEnd OpenGL.glPopMatrix ' restore matrix7. Add the following code to the Resized event of OpenGLSurface1 (You can access the Resized event by double clicking on OpenGLSurface1 and then selecting the event from the list):
' set the viewport rectangle OpenGL.glViewport 0, 0, OpenGLSurface1.Width, OpenGLSurface1.Height ' set up the perspective projection settings OpenGL.glMatrixMode OpenGL.GL_PROJECTION OpenGL.glLoadIdentity OpenGL.gluPerspective 60.0, OpenGLSurface1.Width / OpenGLSurface1.Height, 1, 100.0 ' select and reset the modelview matrix OpenGL.glMatrixMode OpenGL.GL_MODELVIEW OpenGL.glLoadIdentity8. Add the following code to the Paint event of Window1 (You can access the Paint event by double clicking on the titlebar of Window1 and then selecting the event from the list):
' refresh the OpenGL surface OpenGLSurface1.Render9. Save and run the project.
We can change the color of the OpenGL "paintbrush" with the glColor3d instruction. In the OpenGLSurface1.Render event we make a call to glColor3d with the color settings (1, 0, 0) to set the "paintbrush" color to a bright red (1,0,0). The parameters passed to the glColor3d are the respective red, green and blue values. Each vertex added with the glVertex3d instruction, after calling glColor3d, will now be painted in a bright red color. The whole polygon is therefore red.
When drawing the second polygon we take this one step further. Each vertex of the polygon is painted with a different color, namely red (1,0,0), green (0,1,0) and blue (0,0,1). A powerful feature of OpenGL is that it will automatically fill the polygon with the correct mix of colors. This gives the smooth color effect between the different vertices of the polygon.
Play around with different color combinations. See if you can change the first polygon to a blue color.