SIGN UP MEMBER LOGIN:    
ARTICLE

Rotation of a Cube with OpenGL in C#

Posted by Eduardo de la Garza Articles | GDI+ & Graphics December 14, 2010
This article 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.
Reader Level:
Download Files:
 

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.

rotation.gif

It includes the project in Microsoft C #, in addition to the executable and the libraries necessary to run this program.

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

In the Zip Folder therez no Tao library,unable to execute the project.Need help asap.

Posted by pooja dixit Mar 18, 2011

Gran Aporte. Gracias

Posted by Emanuel Acosta Feb 09, 2011

me encanto el documental mereces un 100 con oswaldo

Posted by Eduardo de la Garza Dec 15, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor