TheJahooli's picture

GlControl Not Working

When I try to use a GLControl in my project it only displays a white box where it should be on the form. This is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics.OpenGL.Enums;
 
namespace FormPractise
{
    public partial class frmMain : Form
    {
        bool glLoaded = false;
 
        public frmMain()
        {
            InitializeComponent();
        }
 
        private void glControl_Load(object sender, EventArgs e)
        {
            glLoaded = true;
            GL.ClearColor(Color.SkyBlue);
            glControl_Resize(null, EventArgs.Empty);
        }
 
        private void glControl_Paint(object sender, PaintEventArgs e)
        {
            if (!glLoaded) return;
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            glControl.SwapBuffers();
        }
 
        public static Rectangle viewPort
        {
            get
            {
                int[] tab = new int[4];
 
                GL.GetInteger(GetPName.Viewport, tab);
 
                return new Rectangle(tab[0], tab[1], tab[2], tab[3]);
            }
            set
            {
                if (value.Size.IsEmpty)
                    return;
 
                Rectangle rect = value;
                if (rect.Width == 0)
                    rect.Width = 1;
 
                GL.Viewport(0, 0, rect.Width, rect.Height);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();
                GL.Ortho(rect.Left, rect.Width, rect.Height, rect.Top, -1, 1);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
            }
        }
 
        private void glControl_Resize(object sender, EventArgs e)
        {
            glControl.MakeCurrent();
            frmMain.viewPort = new Rectangle(new Point(), glControl.Size);
        }
    }
}

Edit: iliak's code added to my previous code
Edit: glControl_Resize called during glControl_Load method


Comments

Comment viewing options

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

You have to setup view matrix according to control size.
Here's the code for a GLControl named "GLMoveAwayTile". Bind it to the Resize event

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GLMoveAwayTile_Resize(object sender, EventArgs e)
		{
			GLMoveAwayTile.MakeCurrent();
			Display.ViewPort = new Rectangle(new Point(), GLMoveAwayTile.Size);
		}

Display.Viewport :

		/// <summary>
		/// Gets/sets the viewport
		/// </summary>
		public static Rectangle ViewPort
		{
			get
			{
				int[] tab = new int[4];
 
				GL.GetInteger(GetPName.Viewport, tab);
 
				return new Rectangle(tab[0], tab[1], tab[2], tab[3]);
			}
			set
			{
				if (value.Size.IsEmpty)
					return;
 
				Rectangle rect = value;
				if (rect.Width == 0)
					rect.Width = 1;
 
 
				GL.Viewport(0, 0, rect.Width, rect.Height);
				GL.MatrixMode(MatrixMode.Projection);
				GL.LoadIdentity();
				GL.Ortho(rect.Left, rect.Width, rect.Height, rect.Top, -1, 1);
				GL.MatrixMode(MatrixMode.Modelview);
				GL.LoadIdentity();
			}
		}

This code is for 2D mode not for 3D mode.

iliak's picture

By the way, if you plan to have several GLControl in your form, add this :

       private void glControl_Paint(object sender, PaintEventArgs e)
        {
            if (!glLoaded) return;
 
 
 
           // Make the control the good OpenGL context
           glControl.MakeCurrent();
 
 
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            glControl.SwapBuffers();
        }
TheJahooli's picture

I added the code to it but nothing has changed.
I assume that the 'Display' is the form and that is where the viewport rectangle belongs.

the Fiddler's picture

OpenTK comes with a few GLControl examples (e.g. run Examples.exe -> OpenTK -> Simple GLControl). Do these work correctly?

If those don't work either, this is probably a bug in OpenTK: what is your OS and video card / drivers?

If those *do* work, make sure you that glControl_Resize is actually called at least once (try calling it from your Load event to make sure).

TheJahooli's picture

Again there is no change and the examples do work so there is no problem with my hardware.
I have the feeling that this is because of not adding a dll or something that the documentation doesn't mention so could someone say what you have to do before using OpenTK so I can check that I have done everything.

the Fiddler's picture

Try compiling and running against a debug version of OpenTK (it is included into the OpenTK zip). This might be caused by some OpenGL error going unnoticed - the debug version will catch such errors and convert them into exceptions.

Edit: to use OpenTK, simply reference OpenTK.dll. If you plan to support Linux / Mac OS, add OpenTK.dll.config to your project and enable 'copy to output directory'.