This program draws a cube that rotates in X, Y and Z. This Windows application
was developed in C # and with the help of the OpenGL libraries.
This simple application demonstrates how OpenGL works and how easy it is to draw
a 3D figure and make it move in the direction we choose.
The first step is to add the library through Tao OpenGL
using
Tao.OpenGl;
Then declare global variables that will control the rotations in the different
axes. You declare the variables width and height to control the size of the
object where you draw the cube.
public
partial class
Form1 : Form
{
double xrot, yrot, zrot;
int width;
int height;
As a next step in the constructor of the form containing the control
initializers OpenGL, which draw the cube in shape. You have to initialize this
component to load all the pictures in it. It also contains the initialization of
variables as well as the port of vision, perspective and how the cube will be
displayed on the screen.
public
Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
width = simpleOpenGlControl1.Width;
height = simpleOpenGlControl1.Height;
Gl.glViewport(0, 0, width,
height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(45.0f, (double)width / (double)height,
0.01f, 5000.0f);
Gl.glEnable(Gl.GL_CULL_FACE);
Gl.glCullFace(Gl.GL_BACK);
}
You have to set the method to paint the control of OpenGL, which will contain
the methods, as well as the coordinates of the cube and how it is painted on the
screen. For example you can choose between lines or only points in addition to
filling the faces of the cube.
private
void simpleOpenGlControl1_Paint(object
sender, PaintEventArgs e)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT
| Gl.GL_DEPTH_BUFFER_BIT);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glTranslated(0, 0, -5);
Gl.glRotated(xrot += 0.5, 1, 0,
0);
Gl.glRotated(yrot += 0.3, 0, 1,
0);
Gl.glRotated(zrot += 0.2, 0, 0,
1);
Gl.glPointSize(3);
Gl.glPolygonMode(Gl.GL_FRONT,
Gl.GL_LINES);
Gl.glPolygonMode(Gl.GL_BACK,
Gl.GL_LINES);
Gl.glBegin(Gl.GL_QUADS);
This method is the most important program because it contains everything that
will make the figure on screen, the rotation factors, such as can be seen in the
X axis will be greater the rotation because he has been assigned a greater
increase the other axes. It can be seen in the call to PolygonMode method () as
draw cube style, in this case was selected lines.
It includes the project in Microsoft C #, in addition to the executable and the
libraries necessary to run this program.