luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.
MIT License
578 stars 110 forks source link

Possible issue with Gl.Enable(EnableCap.DepthTest); #33

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hello again,

screenshot_1

I've been trying to use z-buffers, but it seems that the following calls have no effect whatsoever. I mean, the result is always the same, no matter whether the following code is commented, or whether the values are different:

Gl.ClearDepth(1.0f);
Gl.Enable(EnableCap.DepthTest);
Gl.DepthFunc(DepthFunction.Less);

Here is the code that executes from glControl_ContextCreated:

        Gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gl.ClearDepth(1.0f);
        Gl.Enable(EnableCap.DepthTest);
        Gl.DepthFunc(DepthFunction.Less);
        Gl.ShadeModel(ShadingModel.Smooth);
        Gl.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

        float aspect = (float)this.size.Height / (float)this.size.Width;
        Gl.Viewport(0, 0, this.size.Width, this.size.Height);

                    Gl.MatrixMode(MatrixMode.Projection);
                    Gl.LoadIdentity();
        //http://stackoverflow.com/questions/2417697/gluperspective-was-removed-in-opengl-3-1-any-replacements
        // The following code is a fancy bit of math that is eqivilant to calling:
        // gluPerspective( fieldOfView/2.0f, width/height , 0.1f, 255.0f )
        // We do it this way simply to avoid requiring glu.h
        float fieldOfView = 90;
        float zNear = 0.1f;
        float zFar = 100.0f;
        float fH = (float)Math.Tan(fieldOfView * 3.14159f / 360.0f) * zNear;
        float fW = fH * aspect;
        Gl.Frustum(-fW, fW, -fH, fH, zNear, zFar);

        Gl.MatrixMode(MatrixMode.Modelview);
        Gl.LoadIdentity();
        Gl.Translate(0f, 0f, -1f);
        Gl.Scale(0.02f, 0.02f, 0.02f);
        Gl.Rotate(45f, 1f, 1f, 1f);

        List<Vertex3f> vertexList = new List<Vertex3f>();
        List<ColorRGBA32> colorList = new List<ColorRGBA32>();

        for(int x = -20; x <= 20; x += 8)
            for (int y = -20; y <= 20; y += 8)
                for (int z = -20; z <= 20; z += 8)
                {
                    vertexList.Add(new Vertex3f( 0 + x,  1 + y,  0 + z)); colorList.Add(new ColorRGBA32(Color.Red.R, Color.Red.G, Color.Red.B, Color.Red.A));
                    vertexList.Add(new Vertex3f(-1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Green.R, Color.Green.G, Color.Green.B, Color.Green.A));
                    vertexList.Add(new Vertex3f( 1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Blue.R, Color.Blue.G, Color.Blue.B, Color.Blue.A));

                    vertexList.Add(new Vertex3f( 0 + x,  1 + y,  0 + z)); colorList.Add(new ColorRGBA32(Color.Red.R, Color.Red.G, Color.Red.B, Color.Red.A));
                    vertexList.Add(new Vertex3f( 0 + x, -1 + y,  1 + z)); colorList.Add(new ColorRGBA32(Color.Magenta.R, Color.Magenta.G, Color.Magenta.B, Color.Magenta.A));
                    vertexList.Add(new Vertex3f(-1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Green.R, Color.Green.G, Color.Green.B, Color.Green.A));

                    vertexList.Add(new Vertex3f( 0 + x,  1 + y,  0 + z)); colorList.Add(new ColorRGBA32(Color.Red.R, Color.Red.G, Color.Red.B, Color.Red.A));
                    vertexList.Add(new Vertex3f( 1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Blue.R, Color.Blue.G, Color.Blue.B, Color.Blue.A));
                    vertexList.Add(new Vertex3f( 0 + x, -1 + y,  1 + z)); colorList.Add(new ColorRGBA32(Color.Magenta.R, Color.Magenta.G, Color.Magenta.B, Color.Magenta.A));

                    vertexList.Add(new Vertex3f( 0 + x, -1 + y,  1 + z)); colorList.Add(new ColorRGBA32(Color.Magenta.R, Color.Magenta.G, Color.Magenta.B, Color.Magenta.A));
                    vertexList.Add(new Vertex3f(-1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Green.R, Color.Green.G, Color.Green.B, Color.Green.A));
                    vertexList.Add(new Vertex3f( 1 + x, -1 + y, -1 + z)); colorList.Add(new ColorRGBA32(Color.Blue.R, Color.Blue.G, Color.Blue.B, Color.Blue.A));
                }

        var vertexArray = vertexList.ToArray();
        var colorArray = colorList.ToArray();
        using (MemoryLock vertexArrayLock = new MemoryLock(vertexArray))
        using (MemoryLock vertexColorLock = new MemoryLock(colorArray))
        {
            Gl.VertexPointer(3, VertexPointerType.Float, 0, vertexArrayLock.Address);
            Gl.EnableClientState(EnableCap.VertexArray);

            Gl.ColorPointer(4, ColorPointerType.UnsignedByte, 0, vertexColorLock.Address);
            Gl.EnableClientState(EnableCap.ColorArray);

            Gl.DrawArrays(PrimitiveType.Triangles, 0, vertexArray.Length);
        }

Thank you, Daniel

luca-piccioni commented 7 years ago

https://github.com/luca-piccioni/OpenGL.Net/issues/31#issuecomment-270756302

ghost commented 7 years ago

yep, that was it, thank you :) you should probably consider setting a different default value for this field? and i find it a bit strange to be able to control such a low-level setting from the ui.

luca-piccioni commented 7 years ago

I thought the same, but I changed my mind when I realized that neophytes will learn what is a pixel format and the importance of it.

Designer properties are really necessary since the pixel format must be set before creating any GL context.

Il lun 6 mar 2017, 08:43 igium notifications@github.com ha scritto:

yep, that was it, thank you :) you should probably consider setting a different default value for this field? and i find it a bit strange to be able to control such a low-level setting from the ui.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/luca-piccioni/OpenGL.Net/issues/33#issuecomment-284323945, or mute the thread https://github.com/notifications/unsubscribe-auth/AEe6CazAT85Exk1i6ywQmCySME9Wb48Kks5ri7kigaJpZM4MTi_g .

ghost commented 7 years ago

yes, that makes sense. obviously adapting opengl to winforms is quite a challenge :)