SIGN UP MEMBER LOGIN:    
ARTICLE

Basic Drawings with OpenGL using C#

Posted by Uziel gc Articles | GDI+ & Graphics December 15, 2010
The purpose of this paper is to show the functions and elements used to plot basic shapes and implement their transformations.
Reader Level:
Download Files:
 

The purpose of this paper is to show the functions and elements used to plot basic shapes and implement their transformations.

The line drawing in OpenGL is based on the use of vertices, i.e. if you want to draw a line you must define two vertices (start and end), the line will be drawn according to the connection mode you choose, taking into account each of the vertices defined (GL_LINESGL_POLYGON, etc.) then if we define four vertices and selects as the connection mode "GL_LINES" this may result in 2-line stroke, but if you select "GL_QUADS" will result a square.

OpenGl1.gif

Well, having said this we will proceed with our application. The first thing we do is add our control SimpleOpenGlControl which make our lives easier by not having to follow a series of complicated steps to initialize your work area.

El SimpleOpenGlControl belongs to Tao Framework and if we want to use it we must add a reference to Tao.Platform.Windows.

OpenGl2.gif

SimpleOpenGlControl

Step 1:

public Form1()
        {
            InitializeComponent();

            int height = simpleOpenGlControl1.Height;
            int width = simpleOpenGlControl1.Width;

            simpleOpenGlControl1.InitializeContexts();
            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);
       
}

double xrot, yrot, zrot = 0;


Initialize our workspace and variables that will control the rotation of our figure
simpleOpenGlControl1.InitializeContexts() Initialize out control context or work area.

Gl.glViewport().-Our work area
Gl.glMatrixMode().-  Specify which matrix is the current matrix
Gl.GL_PROJECTION .- Defines the properties of the camera that views the objects 
Gl.GL_MODELVIEW.- matrix defines how your objects are transformed
Glu.gluPerspective.-  set up a perspective projection matrix

Step 2:

Now we are ready to start drawing.

For this we will use the "Paint" event of the control.

private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e) // OnPaint event
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
//clear buffers to preset values

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();                
// load the identity matrix

            Gl.glTranslated(0, 0, -4);          //moves our figure (x,y,z)
            Gl.glRotated(xrot += 0.5, 1, 0, 0); //rotate on x
            Gl.glRotated(yrot += 0.3, 0, 1, 0); //rotate on y
            Gl.glRotated(zrot += 0.2, 0, 0, 1);
//rotate on z

            //face 1
            Gl.glBegin(Gl.GL_LINE_LOOP);    //start drawing GL_LINE_LOOP is the connection mode
           
Gl.glColor3ub(255, 0, 255);
            Gl.glVertex3d(1, 1, -1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(-1, 1, -1);
            Gl.glEnd();

            //face 2
            Gl.glBegin(Gl.GL_LINE_LOOP);
           
Gl.glColor3ub(0, 255, 255);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(1, -1, 1);
            Gl.glVertex3d(-1, -1, 1);
            Gl.glEnd();

            //face 3
            Gl.glBegin(Gl.GL_LINE_LOOP);
           
Gl.glColor3ub(255, 255, 0);
            Gl.glVertex3d(-1, 1, -1);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(-1, -1, 1);
            Gl.glVertex3d(-1, 1, 1);
            Gl.glEnd();

            //face 4
            Gl.glBegin(Gl.GL_LINE_LOOP);
           
Gl.glColor3ub(0, 0, 255);
            Gl.glVertex3d(1, 1, 1);
            Gl.glVertex3d(1, -1, 1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(1, 1, -1);
            Gl.glEnd();

            //face 5
            Gl.glBegin(Gl.GL_LINE_LOOP);
           
Gl.glColor3ub(0, 255, 0);
            Gl.glVertex3d(-1, 1, -1);
            Gl.glVertex3d(-1, 1, 1);
            Gl.glVertex3d(1, 1, 1);
            Gl.glVertex3d(1, 1, -1);
            Gl.glEnd();

            //face 6
            Gl.glBegin(Gl.GL_LINE_LOOP);
           
Gl.glColor4d(255, 0, 0, 100);
            Gl.glVertex3d(-1, 1, 1);
            Gl.glVertex3d(-1, -1, 1);
            Gl.glVertex3d(1, -1, 1);
           
Gl.glVertex3d(1, 1, 1);
            Gl.glEnd();

        }

OpenGl3.gif

Gl.glTranslated(0, 0, -4);          //moves our figure (x,y,z)
Gl.glRotated(xrot += 0.5, 1, 0, 0); //rotate on x
Gl.glRotated(yrot += 0.3, 0, 1, 0); //rotate on y
Gl.glRotated(zrot += 0.2, 0, 0, 1); //rotate on z

Applies rotation and translation matrix to our figure.


Don't you know what TAO is?
Get in here 
http://tinyurl.com/2d5napd 

You need it to start working with openGL and C#
____________
Thanks to Sam ... Great advice

References:

http://www.fastgraph.com/makegames/3drotation/
http://www.opengl.org/

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

Thx gj

Posted by can can Mar 07, 2012

Thanks for your contribution..very useful!

Posted by Mayshol mg Dec 15, 2010

Thank you Great advice

Posted by Uziel gc Dec 15, 2010

Thank you, Uziel. That works. Now your sample works for me. It might help to add a note for your other two articles. I don't know if it should be a feedback comment or an article extension or a revision of the article. At least people looking at this article know where to go to get TAO.

Posted by Sam Hobbs Dec 15, 2010

Tao Framework is the opengl library for .NET you can get it here http://tinyurl.com/2d5napd

Posted by Uziel gc Dec 15, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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
Team Foundation Server Hosting
Become a Sponsor