Home   Donators   Contact Us       

<< Previous Tutorial     Next Tutorial >>

Tutorial 3 - A splash of color


Theory

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

Tutorial Steps
Tutorial created with Real Studio 2011 Release 4.3.
1. Open Real Studio.
2. Choose the "Desktop" project template.
3. Add an OpenGLSurface control to Window1.
4. Resize and position OpenGLSurface1 to fill the whole form.
5. Tick the LockRight and LockBottom properties of OpenGLSurface1. This is required to raise the Resized event and set up the perspective projection settings.
6. Add the following code to the Render event of OpenGLSurface1 (You can access the Render event by double clicking on OpenGLSurface1 and then selecting the event from the list):
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 matrix
7. 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.glLoadIdentity
8. 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.Render
9. Save and run the project.

Code Analysis

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.

Project Downloads

<< Previous Tutorial     Next Tutorial >>


 
All the content on Real 3D Tutorials, with the exception of the SyntaxHighlighter which is licensed under the MIT License, is provided to the public domain and everyone is free to use, modify, republish, sell or give away this work without prior consent from anybody. Content is provided without warranty of any kind. Under no circumstances shall the author(s) or contributor(s) be liable for damages resulting directly or indirectly from the use or non-use of the content.
Should you find the content useful and would like to make a contribution, you can show your support by making a donation.