Technolithic's picture

3D window inside of a subform

I'm new to OpenGL, not to mention OpenTK, as well as C#. I checked out the OpenGL RedBook to try and gain a better understanding of how it works, but it seems when I make a small change in the z-depth of a point, nothing appears on the screen. I grabbed this stuff out of the tutorial, which works great for a 2D graphics window, but I'm having problems with getting 3D to work. The window draws nothing outside of a z-depth of 0-1, inclusive.

I'm betting that the problem is obvious to anybody with some experience. I was working with Tao before this, but decided to give OpenTK a shot, because it claims to have easy implementation and seems it should work amazingly with what I'd like to accomplish with subforms. With the easy-to-use part, it seems that more people would flock toward OpenTK. Can someone please help the noob??! Thanks. Oh yeah, for the posting requirements: I'm using OpenTK ver. 0.9.0

As per the OOP design of this project, all code for the glcontrol needs to be contained in the Viewport class.

    public class Viewport : Form
    {
 
        // PROPERTIES:
 
        private OpenTK.GLControl glControl1;
        private bool loaded = false;
 
 
        // METHODS:
 
        private void Viewport_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ClearColor(Color.Black);
            SetupViewport();
        }
 
        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
            GL.Enable(EnableCap.DepthTest);
        }
 
        private void glControl1_Resize(object sender, EventArgs e)
        {
            int myWidth = Width;
 
            if (!loaded)
                return;
 
            if (myWidth == 0)
                myWidth = 1;
 
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            Glu.Perspective(45, 1.0, 0.1, 1000);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
        }
 
        // render the graphics in the viewport
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // don't execute if not already loaded!
                return;
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.Color3(Color.Yellow);            
            GL.PointSize(2.0f);
 
            GL.Begin(BeginMode.Points);
 
                GL.Vertex3(50.0, 50.0, 0.0);
                GL.Vertex3(60.0, 60.0, 0.5);
                GL.Vertex3(70.0, 70.0, 1.0);
                GL.Vertex3(80.0, 80.0, 1.1);
 
            GL.End();
 
            glControl1.SwapBuffers();
        }

Comments

Comment viewing options

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

The call to GL.Ortho sets an orthographic projection with z values from -1 to 1:

GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)

Try replacing it with a call to Glu.Perspective:

Glu.Perspective(60.0, 4.0/3.0, 1.0, 100.0);
Inertia's picture

You are also missing a Glu.LookAt() call for your modelview matrix, the folder \Source\Examples\Tutorial\ in the OpenTK distribution contains several working examples.

Technolithic's picture

I tried your suggestion and it still gave me a solid black graphics window until I plotted points in a n^3 nested for loop... 8 million points and it filled the screen with yellow, cool!!!

Now I just need to go back and work the formulas in the RedBook to find out what values I need for Glu.Perspective and Glu.LookAt.

Thanks for the help and the hella quick response. This forum rocks and so does OpenTK!!!