samara's picture

Windows form not displaying graphics

Hi,

I trying to get the simple example "Adding the GLControl to your Windows.Form" to work but I couldn't get any graphics. I can't get the black screen. it seems it's displaying what on the screen behind the application

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 OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
 
 
namespace GLAPP
{
    public partial class Form1 : Form
    {
        bool loaded = false;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // Play nice
                return;
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            glControl1.SwapBuffers();
        }
    }
}

Comments

Comment viewing options

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

According to the code you are showing, the variable loaded is initialized as false but it never changes; therefore the code in glControl_Paint is never executed.

samara's picture

Thank you so much for your help. I tried to set the flag loaded to true but I am still getting no graphics
Your help is very much appreciated

public partial class Form1 : Form
    {
        bool loaded = true;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
 
 
        private void glControl1_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ClearColor(Color.SkyBlue); // Yey! .NET Colors can be used directly!
 
 
        }
 
 
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
 
 
            if (!loaded)
              return;
 
 
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
 
            glControl1.SwapBuffers();
        }
 
 
 
    }
haplokuon's picture

You still need to define the viewport, try placing this code in glControl1_Paint.
GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height);
To be precise the viewport only needs to be changed when the size of the control also changes, usually this code is only needed in the glControl1_Resize event.

Also, you can just initialize loaded as false and then change it to true once the form is loaded.

I would recommend to check the examples in the OpenTK SDK, the example GLControlSimple shows the basic squeleton on how to use the GLControl.

natinusala's picture

I had got the same issue and I found how to fix this !

  1. Erase glControl1_Load and glControl1_Paint methods
  2. Go on the designer, select your GLControl and go to the "event" part of the properties panel
  3. Double clic on the right of "Paint" and "Load"
  4. Copy your code on the new methods created by Visual Studio

Simply copying the example on the documentation will not work, Visual Studio have to know that glControl1_Load is glControl1_Load :)

Enjoy ;)

And sorry for the bad english, I'm french.

Ligety's picture

Big THX natinusala !!!!!!!!

Got the same problem and almost went creazy with it.

Why nobody fixed the example documentation ??