tuennes's picture

glControl Question

Hello, I am very excited about the glControl and its capabilities. I followed the tutorial but unfortunately my glControl still only shows junk, even though I cleaned the view port.

This is my program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenTK.OpenGL;
using OpenTK.OpenGL.Enums;
 
 
namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
 
        bool loaded = false;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ClearColor(Color.SkyBlue); // Yey! .NET Colors can be used directly!
            SetupViewport();
        }
 
        private void glControl1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (!loaded)
                return;
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // Play nice
                return;
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.Color3(Color.Yellow);
            GL.Begin(BeginMode.Triangles);
            GL.Vertex2(10, 20);
            GL.Vertex2(100, 20);
            GL.Vertex2(100, 50);
            GL.End();
 
            glControl1.SwapBuffers();
        }
 
        private void SetupViewport()
        {
            int w = glControl1.Width;
            int h = glControl1.Height;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
            GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
        }
    }
}

Can someone give me an advice why I still only see junk ?
Thanks in advance

[moderator edit: code tags :)]


Comments

Comment viewing options

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

Ok, first things first: what operating system are you using and which video card? Are the drivers for the video card up to date?

The code looks correct, but double-check that glControl1.Visible is true, and that it is not partially or fully obscured/overlapped by some other control.

Can you post a screenshot of the problem? (you can attach it at the opening post, preferrably as a jpeg or gif)

Also, do the Windows.Forms examples work on your computer? (check Examples.exe under the Binaries directory)

tuennes's picture

I am using Windows XP Pro 2002 SP 2 along with Visual Studio 2005 Pro. The graphics card is a NVidia GeForce Go 7400. Drivers are up to date. I double checked that glControl1.Visible is true, and it is not overlapped or obscured by any other control.
The Windows.Forms example work fine.

Screenshot is here.

Thanks

objarni's picture

Does minimizing/unminimizing or moving the form out of the desktop area and into it again have any effect..? If it does, it might be that the Paint event isn't fired often enough.

For example, try adding a MouseDown event handler for glControl, something like this:

public void glControl_MouseDown(object sender, MouseDownEventArgs args) {
   glControl.Invalidate();
}

.. and see if glControl is repainted when clicked.

tuennes's picture

No, it has no effect whatsoever. :-(

objarni's picture

Have you tried copying the whole example from the tutorial..? It's in the end of the tutorial. Does it work..?

tuennes's picture

I copied and pasted the entire code from the tutorial. Same result.
I think I am gonna try the whole thing on another machine.

the Fiddler's picture

Ok, can you start from scratch?

Create a new project (Windows Application), add OpenTK.dll, drag & drop the GLControl from the toolbox to the main Form. Then hook GLControl.Paint and clear / swap buffers.

This should work! If it doesn't, it might be a good idea to build a debug version of OpenTK, to check what's going wrong. This is easy enough: execute "build vs" in the Build/ directory, and open the resulting solution with Visual Studio.

haymo's picture

Seems, the paint event is not connected ? This part should be in the Designer file (which was not uploaded). Try to set a breakpoint at the first line of

private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // Play nice

, and see, if it pops up? If not, look into the Form1.Designer.cs for a line saying something like

glControl.Paint += new PaintEventHandler(glControl1_Paint)

.
If that fails, I would suggest to follow Fiddler and start from scratch.

tuennes's picture

This is crazy. I set the breakpoint in glControl1_Paint and it did NOT pop up. So I looked for the PaintEventHandler in Form1.Designer.cs and it wasn't there. Please bare with me because I am coming from C/C++ and I am not yet that fluent w. C#.

objarni's picture

Glad that you found a solution!

The tutorial assumes some familiarity with Windows.Forms programming under Visual Studio 2005. The most popular way of implementing a Paint event handler is double-clicking the Paint event in the Properties window of the IDE (second tab; Events IIRC). Doing that adds the

glControl.Paint +=...;

.. row in the Form1.Designer.cs file -- ie. creates the "hook" to the glControl_Paint() event handler method of the Form1.cs file.

This was good feedback; maybe I should add a picture of the Properties window of Visual Studio to make this more clear in the tutorial.