Home   Donators   Contact Us       

Next Tutorial >>

Tutorial 1 - Hello "OpenGL" world

Polygons are the building blocks of 3D models. A polygon is a collection of edges that forms a closed path. 3 or more edges can be used to create a polygon.

3-sided polygon         4-sided polygon         5-sided polygon        

The points where the edges come together are the vertices of the polygon. In the image below the red dots indicate the vertices found in a 3-sided polygon.

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. 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):
' clear the background 

OpenGL.glClearColor(0, 0, 0, 1)					

' draw the polygon

OpenGL.glBegin OpenGL.GL_POLYGON

OpenGL.glVertex3d 0, 1, 1
OpenGL.glVertex3d -1, 0, 1
OpenGL.glVertex3d 1, 0, 1

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

7. Save and run the project.

Code Analysis

The first glClearColor instruction is used to set the color that we want to use for the background. Four parameters named red, green, blue and alpha are passed to this function. These parameters can be any value between and including 0 and 1. All the values are mixed together to determine the final color that is displayed.

The call to the glClear function does the actual clearing of the background. We need to specify the GL_COLOR_BUFFER_BIT bit to indicate to OpenGL that we want to clear the background, using the color that we set with the glClearColor instruction.

glBegin starts the drawing routine. We indicated that we want to draw a polygon, by passing the GL_POLYGON bit to the function.

Next we tell OpenGL how to draw the polygon by passing the x, y and z coordinates of each vertex to the glVertex3d function. The values of these parameters can be any real number. The following diagram shows the coordinates for each vertex. It is very important to note that the vertices are given in an anti-clockwise direction. OpenGL uses the order of the vertices to determine if the polygon is facing towards you or away from you. Make sure to ALWAYS give polygon vertices in an anti-clockwise direction.

Finally we indicate to OpenGL that we have given all the vertices of the polygon by calling the glEnd function, which instructs OpenGL to draw the polygon.

When the operating system requests from our application to refresh itself, the OpenGLSurface1.Render method call instructs OpenGL to do draw our polygon to the screen.

And that is how easy it is to draw polygons with OpenGL in Real Studio. Try if for yourself and see if you can draw a 4-sided polygon.

Project Downloads

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.