pikob's picture

No display

I have Mobile Intel(R) 945 Express Chipset Family (Microsoft Corporation - Prerelease WDDM 1.0)
and Intel Core 2 T7200@2.00GHz CPU with Windows 7 (b.7100) and .NET 3.5 SP1.

Graphic driver was installed from original Fujitsu-Siemens CD.

Before changing program to OpenTK on Windows Forms from stand-alone OpenTK window everything was working ok.

Now, I can't see anything drawed in glControl1.

Please help.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Audio;
using OpenTK.Math;
using OpenTK.Input;
using OpenTK.Platform;
 
namespace Aplikacja
{
    public partial class Form1 : Form
    {
        // variables here
        bool loaded = false;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            loaded = true;
 
            // first 2D texture
            Bitmap bmp = new Bitmap("drewno3.bmp");
            GL.Enable(EnableCap.Texture2D);
            GL.GenTextures(1, out tekstura1);
            GL.BindTexture(TextureTarget.Texture2D, tekstura1);
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            bmp.UnlockBits(data);
            bmp.Dispose();
 
            // second 2D texture
            bmp = new Bitmap("drewno2.bmp");
            GL.GenTextures(1, out tekstura2);
            GL.BindTexture(TextureTarget.Texture2D, tekstura2);
            data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0,
                OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            bmp.UnlockBits(data);
            bmp.Dispose();
 
            GL.Enable(EnableCap.DepthTest);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.ClearColor(Color.SteelBlue);
            int w = glControl1.Width;
            int h = glControl1.Height;
            GL.MatrixMode(MatrixMode.Modelview);
            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            glControl1.SwapBuffers();
 
            glControl1.SwapBuffers();
        }
 
        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (!loaded)
                return;
            //GL.Viewport(0, 0, Width, Height);
            //GL.MatrixMode(MatrixMode.Projection);
            //GL.LoadIdentity();
            Glu.Perspective(45.0, Width / (double)Height, 1.0, 64.0);
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded)
                return;
 
            //this.glControl1.MakeCurrent();
            //GL.Translate(x, 0, 0);
            GL.Clear(ClearBufferMask.ColorBufferBit |
                    ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            // this doesn't appear in the window
            GL.Begin(BeginMode.Quads);
            GL.Color3(Color.Red); GL.Vertex3(-1.0f, -1.0f, 4.0f);
            GL.Color3(Color.Red); GL.Vertex3(-1.0f, 1.0f, 4.0f);
            GL.Color3(Color.Red); GL.Vertex3(1.0f, -1.0f, 4.0f);
            GL.Color3(Color.Red); GL.Vertex3(1.0f, 1.0f, 4.0f);
            GL.End();
            GL.Begin(BeginMode.Quads);
            GL.Color3(Color.Red); GL.Vertex3(-1.0f, -1.0f, -4.0f);
            GL.Color3(Color.Red); GL.Vertex3(-1.0f, 1.0f, -4.0f);
            GL.Color3(Color.Red); GL.Vertex3(1.0f, -1.0f, -4.0f);
            GL.Color3(Color.Red); GL.Vertex3(1.0f, 1.0f, -4.0f);
            GL.End();
 
 
            glControl1.SwapBuffers();
            System.Threading.Thread.Sleep(5000);
 
// here appears the main code (objects and drawing)
        }
 
        private void Wyjscie(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Comments

Comment viewing options

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

I'm using 0.9.8 version of OpenTK.

objarni's picture

You have commented out these lines:

//GL.Viewport(0, 0, Width, Height);
//GL.MatrixMode(MatrixMode.Projection);
//GL.LoadIdentity();

.. which setup the perspective projection. That means that in your Paint event handler, the projection is undefined (or identity).

Google for modelview / projection matrix plus OpenGL to understand what I mean.

the Fiddler's picture

There is no guarantee that an OpenGL context exists during the Form1_Load event. Use glControl1_Load instead.

The debug version of OpenTK will catch errors like this and throw an exception ("No GraphicsContext is current on the calling thread.")

pikob's picture

I have changed the code and it works now:

            GL.Viewport(0, 0, w, h); // ile widzimy
            GL.MatrixMode(MatrixMode.Projection); // wybierz macierz projekcji
            GL.LoadIdentity(); // resetuj macierz
            Glu.Perspective(45, w / (double)h, 0.1, 100); // aspect ratio okna
            GL.MatrixMode(MatrixMode.Modelview); // wybierz macierz widoku modeli
            GL.LoadIdentity(); // resetuj macierz
            Glu.LookAt( // punkt patrzenia

I tried to mess around with changing the load events but given up. As it started working, I didn't need to.

Now the application is done.

I was writing commercial application to show a cavity wardrobe with configurable shelves and partitions and textures.

the Fiddler's picture

Just a word of warning: calling OpenGL functions without an OpenGL context is not allowed. Windows typically ignore this error, but some Windows versions and drivers have been known to crash.