oafak's picture

Help with Tutorial

I am new to OpenTK, OpenGL but experienced with C++ and C#. (I downloaded opentk-0.9.8-2). The first exercise with QuickStart worked perfectly in my VisualStudio 2008. Trying to go step-by-step to create a GLControl in a Windows Form was NOT successful:
1. I did not see any "Garbage" as was expected to when I added the GLContol to the form
2. I did not get a black page on the form when I first ran it as expected. Instead, I got a form with a beige background.
3. Nothing else showed up on the control afterwards. The expected yellow triangle was not shown.
I will be grateful for any assistance I can get on these please. In addition, I would be pleased to know if this can work with WPF controls.


Comments

Comment viewing options

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

1. The Form should not show any garbage when you add the GLControl. Instead, it should display the color specified by the BackgroundColor (or BackColor) property.

2 and 3. Make sure the GLControl is actually visible. Use the "Bring To Front" menu option to bring it to the front of the z-stack and make sure its size is larger than 0 and that it fits inside the form Bounds (alternatively, you can set Dock = DockStyle.Fill; for testing purposes).

If it still doesn't work as expected, consider attaching your project here (source only, no need for OpenTK.dll) and someone will take a look. This will also help us improve documentation to avoid similar issues in the future.

oafak's picture

Thanks for your patience. I have tried your suggestions and still I have not succeeded. I deliberately allowed the form window to be slightly larger than the GLControl. Requested that they both be painted Skyblue and I am sure that the GLControl (glControl1 in my code) is in front. To confirm this, I deliberately added a Windows button intersecting the Form and the GLControl so that the order is clearly shown. The first code is the Form.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics.OpenGL.Enums;
using OpenTK.Graphics;
 
namespace WinGLTest01
{
    public partial class Form1 : Form
    {
        bool loaded = false;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void glControl1_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Fill; 
            loaded = true;
            GL.ClearColor(Color.SkyBlue);
 
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (loaded) GL.ClearColor(Color.SkyBlue);
            Form1.ActiveForm.BackColor = Color.SkyBlue;
        }
 
 
    }
}

This is followed by the generated code by the designer:

namespace WinGLTest01
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.glControl1 = new OpenTK.GLControl();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // glControl1
            // 
            this.glControl1.BackColor = System.Drawing.Color.Red;
            this.glControl1.Location = new System.Drawing.Point(12, 7);
            this.glControl1.Name = "glControl1";
            this.glControl1.Size = new System.Drawing.Size(261, 243);
            this.glControl1.TabIndex = 0;
            this.glControl1.VSync = true;
            this.glControl1.Load += new System.EventHandler(this.glControl1_Load);
            this.glControl1.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl1_Paint);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(226, 232);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(57, 28);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.glControl1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private OpenTK.GLControl glControl1;
        private System.Windows.Forms.Button button1;
    }
}

And lastly, the program source:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace WinGLTest01
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
the Fiddler's picture

You are missing a call to glControl1.SwapBuffers() in your glControl1_Paint event handler.

Moreover, you are likely hitting issue #1071: Not firing Load event for GLControl.

As a temporary workaround, you can add the following code to your Form1 class:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    glControl1.CreateControl();
    glControl1_Load(glControl1, EventArgs.Empty);
}