Using OpenGL in C# (Tao.Framework)


  • Install the Tao Framework, download it here.
  • In VisualStudio.net Toolbox, add a new Tab and rename it with a meaningful caption:
  • In the new Tab category, right click”Choose Items”:
    Tao_addto_toolbox0.png
  • Browse to the control “SimpleOpenGLControl”
  • Drag the control onto the Windows Form designer, this will automatically add two reference assemblies to the project:
  • Tao_addto_toolbox2.png
  • Using the object browser to have a closer look at the control, it shows that the class hierarchy is:
    public class SimpleOpenGlControl : System.Windows.Forms.UserControl , Member of Tao.Platform.Windows
    i.e. a user control. The class provides several virtual functions, which, you can inherit from it and override in order to perfom OpenGL renderings in your project.Tao_addto_toolbox3.png
  • Now, compile the program and there will be no error, however, if you run it, you will see a messagebox. And to trouble shoot this, you need to add a few code lines.
  • Tao4.png
  • Add the following code first
    using Tao.OpenGl;
    using Tao.Platform.Windows;
  • And then, in the Form.Load event handler add the code below and rerun it, now it works!
    glControl.InitializeContexts(); //Assuming that you have named the control “glControl”;
  • Now to render something, you can handle the OnPaint event of the control:
  • Type the demo code to see the results:

With this, all your previous C++ OpenGL codes can be immigrated to C# with ease.

30 thoughts on “Using OpenGL in C# (Tao.Framework)

  1. Dan

    I’ve followed this to the teeth and sadly it does not work, it only shows a white box where the “glControl” is located. I am using VS08, anything I’m missing?

  2. xinyustudio

    Dan,
    If you could offer more details of the errors, I can help troubleshoot this. Check below:
    +If you have coded some OpenGL actions;
    +What OS are you using? English or other international languages?
    +Can you successfully draw sth using C++?

  3. xinyustudio

    Update: the download link of Tao framework, previously no longer valid, is now corrected. You can directly download it using the link provided now.

  4. 10gler

    Hi,

    You ilustrate than OnPaint event should handled to call render method. I have a question, when OnPaint is invoked by the control? I would like perform same animation so the render should be invoked (scene refreshed) in any possible time (very fast).
    I added same thread to my program that call rener method. But somethimes I have collision (OnPain invoke render and my thread also).

    Best regards

  5. xinyustudio

    I suppose the OnPaint event of the control calls OpenGL funcions,instead of the opposite direction. Moreover, I am not very clear about your problem. You may refine your problem, and I will try to help.

  6. 10gler

    Ok, I try to explain main problem.
    In your example the triangle position is static. What sould be done rotate your triangle? (for example).
    I put in my rendeering class variable angle and call a glRotatef(angle++, 1, 0, 0) before draw triangle but nothing happen (possiotion of triangle is static). So I debuged program and noticed that onpaint event is called “from time to time” maybe even ones.

  7. 10gler

    In the and of onpaint event I call glControl.Draw(); method – now it works perfect and no extra rendering thred needed.

    Is it correct way?

  8. Pingback: 2010 in review « Xinyustudio

  9. Mahantesh

    I tried to add simpleopenglcontrol into my tool box but couldn’t find .so is there any mistake in installing it or any other problem?

  10. Mary Hawkins

    I am having this bug while trying to compile my project:
    Error 1 The name ‘winOG’ does not exist in the current context

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Tao.OpenGl;
    using Tao.FreeGlut;
    using Tao.Platform.Windows;

    namespace Lab1_OpenGL
    {
    public partial class Form1 : Form
    {
    PointF[] points;
    public Form1()
    {
    InitializeComponent();
    winOG.InitializeContexts();
    }
    private void loadObject()
    {
    points = new PointF[5];
    points[0].X = 5F; points[0].Y = 3.5F;
    points[1].X = 5F; points[1].Y = 5.5F;
    points[2].X = 4F; points[2].Y = 4.5F;
    points[3].X = 6F; points[3].Y = 4.5F;
    points[4].X = 5F; points[4].Y = 5.5F;
    }
    private void drawObject()
    {
    Gl.glBegin(Gl.GL_LINE_LOOP);
    for (int i = 0; i < points.Length; i++)
    Gl.glVertex2f(points[i].X, points[i].Y);
    Gl.glEnd();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    Glut.glutInit();
    Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE);
    Gl.glEnable(Gl.GL_DEPTH_TEST);

    Glu.gluOrtho2D(3, 6, 3, 6);
    Gl.glClearColor(150, 120, 235, 1);
    Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

    Gl.glColor3f(0, 255, 0);
    loadObject();
    drawObject();
    Gl.glFlush();
    winOG.Invalidate();

    }
    }
    }

    can anyone help with this issue?
    google knows nothing about winog… OMG…. ))

  11. Pingback: Recent hot posts | Xinyustudio

  12. I absolutely love your blog.. Very nice colors & theme. Did
    you create this amazing site yourself? Please reply back as
    I’m attempting to create my own blog and want to find out where you got this from or just what the theme is called.
    Many thanks!

  13. Pingback: How to: Using OpenGl with C#? | SevenNet

  14. Pingback: Using OpenGl with C#? [closed] | ASK AND ANSWER

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s