|3-sided polygon||4-sided polygon||5-sided polygon|
' clear the background OpenGL.glClearColor(0, 0, 0, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT) ' draw the polygon OpenGL.glBegin OpenGL.GL_POLYGON OpenGL.glVertex3d 0, 1, 1 OpenGL.glVertex3d -1, 0, 1 OpenGL.glVertex3d 1, 0, 1 OpenGL.glEnd6. 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.Render7. Save and run the project.
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.