zx32b's picture

Using Vertex3 with Windows.Forms

Hello,

I'm having trouble using Vertex3 inside a Windows.Form. I'm following the example on this website and am playing around with it. I was trying to follow NeHe's second lesson, but none of the Vertex/Translations showed up when I translated them to GL.Vertex3 and GL.Translate. The following is my Render() function, modified from the example:

        private void Render()
        {
            if (!loaded)
            {
                return;
            }
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            DrawTest();
 
            glControl1.SwapBuffers();
        }
 
        private void DrawTest()
        {
            GL.Color3(Color.Black);
 
            GL.Translate(-1.5f, 0.0f, -6.0f);
            GL.Begin(BeginMode.Triangles);
            GL.Vertex3(0.0f,1.0f,0.0f);
            GL.Vertex3(-1.0f,-1.0f,0.0f);
            GL.Vertex3(1.0f,-1.0f,0.0f);
            GL.End();
 
            GL.Translate(3.0f, 0.0f, 0.0f);
            GL.Begin(BeginMode.Quads);            // Draw A Quad
            GL.Vertex3(-1.0f, 1.0f, 0.0f);                // Top Left
            GL.Vertex3(1.0f, 1.0f, 0.0f);                // Top Right
            GL.Vertex3(1.0f, -1.0f, 0.0f);                // Bottom Right
            GL.Vertex3(-1.0f, -1.0f, 0.0f);                // Bottom Left
            GL.End();                            // Done Drawing The Quad
        }

This code displays absolutely nothing. Just a white rectangle. I'm pretty new to this, so I was wondering if I was doing something wrong. I do not believe it is my OS/Video Card/.NET version/etc. because the C# version of NeHe's example (given on his website) works fine. Further, the Vertex2 example from this website works fine, and also when I draw shapes using Vertex2. Vertex3 only works when I translate it directly from Vertex2 and pass a 0 as the Z argument.

Below are my hardware and software specs:

ATI Radeon X1300PRO 256MB
Windows XP 32-bit
Visual Studio 2008 .NET 3.5
OpenTK 0.9.9.4

Thanks for any and all help in advance.


Comments

Comment viewing options

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

Vertex3 only works when I translate it directly from Vertex2 and pass a 0 as the Z argument.

This generally means that your projection matrix is not setup correctly. You need something like this:

Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(45, Width / (float)Height, 1, 128);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);

in your Render method.

zx32b's picture
the Fiddler wrote:

This generally means that your projection matrix is not setup correctly. You need something like this:

This worked great when I changed the call to CreatePerspectiveFieldOfView to take the first argument in radians rather than degrees like so:

Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(
                (float)Math.PI / 4.0f, 
                glControl1.Width / (float)glControl1.Height, 1, 128);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);

Thanks for your help!

the Fiddler's picture

Oops, you are right!

Edit: you should use glControl1.ClientSize.Width and glControl1.ClientSize.Height to get the correct aspect ratio. The plain Width/Height properties include the border size of the control and may throw your calculations a few pixels off. The same holds true for your GL.Viewport call (use glControl1.ClientRectangle).