You may not issue OpenGL commands from a thread that does not have an OpenGL context.
You have two options:
- use wglMakeCurrent to move the OpenGL context to your new thread. (In that case, the main thread may no longer issue OpenGL commands.)
- Hook the
Application.Idle
event and loop within that. Break from the loop whenever a new windows message arrives.
Tao SimpleOpenGlControl
does not offer a way to check for incoming windows messages. As one of the original maintainers of the Tao framework, I would suggest switching to OpenTK.GLControl instead. In that case, you would write:
// in Form.Load handler - separate thread
glControl.Context.MakeCurrent(null);
Thread thread = new Thread(() =>
{
glControl.MakeCurrent();
while (!exit)
{
GL.ClearColor(0f, 0f, 0f, 0f);
GL.Clear(ClearBufferMode.ColorBufferBit);
GL.Begin(PrimitiveType.Quads);
GL.Vertex(x + 0f, y + 0f);
GL.Vertex(x + 0f, y + 1f);
GL.Vertex(x + 1f, y + 1f);
GL.Vertex(x + 1f, y + 0f);
GL.End();
glControl.SwapBuffers();
if (x == 0 || x == 9) incX *= -1;
if (y == 0 || y == 9) incY *= -1;
x += incX;
y += incY;
}
};
or (if you do not wish to use a separate thread):
// in Form.Load handler - main thread
Application.Idle += (sender, e) =>
{
while (glControl.IsIdle)
{
GL.ClearColor(0f, 0f, 0f, 0f);
GL.Clear(ClearBufferMode.ColorBufferBit);
GL.Begin(PrimitiveType.Quads);
GL.Vertex(x + 0f, y + 0f);
GL.Vertex(x + 0f, y + 1f);
GL.Vertex(x + 1f, y + 1f);
GL.Vertex(x + 1f, y + 0f);
GL.End();
glControl.SwapBuffers();
if (x == 0 || x == 9) incX *= -1;
if (y == 0 || y == 9) incY *= -1;
x += incX;
y += incY;
}
};
Edit: in both cases, you need the following using directives.
using OpenTK;
using OpenTK.GLControl;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
You can ask Visual / Xamarin Studio to add the missing using directives by right-clicking the unknown identifier and selecting "Resolve".