mzdmr's picture

Embedding the openGL console into windows form applicaion

I am using tao.framework in C# and I've done what I want in OpenGL using external window , but I want to integrate this window in windows form application. I created black window in form by using simpleOpenGlControl1 in toolbox, but I dont know how can I integrate my code in this. I think there is a problem at Glut.glutMainLoop(); but ı dont know how can ı solve it. I've attached my main method below. Any help will be much appreciated. Thanks

        [STAThread]
        static void Main()
        {
 
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB | Glut.GLUT_DEPTH);
            Glut.glutGetWindow();
            Glut.glutInitWindowSize(500, 500);
            Glut.glutInitWindowPosition(700, 100);
            Glut.glutCreateWindow("Lang Yuzer Robot Arm");
 
            Gl.glEnable(Gl.GL_COLOR_MATERIAL);
            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_LIGHT0);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_NORMALIZE);
            Gl.glEnable(Gl.GL_CULL_FACE);
 
            Glut.glutDisplayFunc(Form1.myDisplay);
            Glut.glutReshapeFunc(Form1.myReshape);
            Glut.glutIdleFunc(Form1.myIdle);
 
 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
 
 
       Glut.glutMainLoop();
 
                }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You cannot combine GLUT with Windows.Forms like this. Since you are using SimpleOpenGlControl, you can simply remove GLUT.

The GLUT functions are straightforward to replace:
glutDisplayFunc -> glControl.Paint event
glutReshapeFunc -> glControl.Resize event
glutIdleFunc -> Application.Idle event

Note that SimpleOpenGlControl hasn't been maintained for close to a decade now. It has been replaced by OpenTK.GLControl which has identical functionality but is better maintained and cross-platform.

mzdmr's picture

OK I replaced SimpleOpenGlControl by OpenTK.GLControl and now there is a method in form1.cs

           private void glControl1_Load(object sender, EventArgs e)
            {
 
            }

after that can I replaced functions below in glControl1_Load function?

glutDisplayFunc -> glControl.Paint event
glutReshapeFunc -> glControl.Resize event
glutIdleFunc -> Application.Idle event

the Fiddler's picture

Paint, Resize, Idle are .Net events. You can add an event handler in one of two ways:

  1. Using the Windows.Forms designer.
  2. Using code, i.e. glControl.Paint += HandlePaintEvent; where HandlePaintEvent is a function with the correct signature.

Check out this tutorial (with pictures): Building a Windows.Forms + GLControl based application

Additional GLControl example code here. The closest one to GLUT is probably GLControlGameLoop.cs.

mzdmr's picture

I have windows form and external OpenGL console window. I tried to embed it by using OpenK.GLControl but I couldn't succeed. I have glut functions so I don't know how can I embed this.

This is myreshape , myDisplay and myIdle functions

               public static void myIdle()
            {
                Glut.glutPostRedisplay();
            }
 
 
             static void DrawGroundPlane2_1(int numSegs)
            {
                Gl.glColor3f(0.7f, 0.1f, 0.1f);
                Gl.glBegin(Gl.GL_POLYGON);
                Gl.glNormal3f(0f, 0f, 1f);
                // right side
 
                Gl.glVertex3f(-0.8f, (float)(z7 + zz7 + 180) / 600, 0f);
                Gl.glVertex3f(-0.8f, 0f, 0f);
                Gl.glVertex3f((float)(-x8) / 600, 0f, 0f);
                Gl.glVertex3f((float)(-x8) / 600, (float)(z8 + zz8 + 180) / 600, 0f);
                Gl.glVertex3f((float)(-x7) / 600, (float)(z7 + zz7 + 180) / 600, 0f);
 
                Gl.glEnd();
            }
 
 
 
            public static void myDisplay()
            {
                Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
 
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glLoadIdentity();
                Glu.gluLookAt(0f, 1f, 4.25f, 0f, 1f, 0f, 0f, 0.5f,0f);
                // Glu.gluLookAt(ekrn sla dn, Dik krşıdn,kçldkçe zoom ypıyr,ekrn sğa dn,ekrn ykrı,ekrn aşağı,grk yk,grk yk,grk yk)
 
               // DrawGroundPlane(16);
                DrawGroundPlane3(16);
                DrawGroundPlane1(16);
                DrawGroundPlane2(16);
                DrawGroundPlane0(16);
                DrawGroundPlane1_1(16);
                DrawGroundPlane2_1(16);
 
                DrawRobotArm(16);
 
                Glut.glutSwapBuffers();
            }
 
 
 
            public static void myReshape(int w, int h)
            {
                Gl.glViewport(0, 0, w, h);
 
                Gl.glMatrixMode(Gl.GL_PROJECTION);
                Gl.glLoadIdentity();
                Glu.gluPerspective(30, w / h, 0.1, 10);
 
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glLoadIdentity();
                Gl.glTranslatef(3.0f, 0.5f, -7.0f);
 
            }

And these are glControl1_Load and glControl1_Paint functions;

           private void glControl1_Load(object sender, EventArgs e)
            {
 
            }
 
            private void glControl1_Paint(object sender, PaintEventArgs e)
            {
 
            }

If you can give my your mail address I can send you my .sln file. My address " demir.muaz@gmail.com "