admin管理员组

文章数量:1129019

I'm writing a C# program to render three 3D objects: a sphere, a prism, and a hexagonal pyramid, using OpenGL with OpenTK.

The problem is that it doesn't display any errors or exceptions; it just displays an empty window with a gray background (the color I specified).

Here is the full code:

public class Game : GameWindow
{
    public Game() : base(GameWindowSettings.Default, NativeWindowSettings.Default) 
    {
        this.CenterWindow(new Vector2i(800, 600));
    }

    protected override void OnLoad()
    {
        GL.ClearColor(new Color4(0.1f, 0.1f, 0.1f, 1.0f));
        GL.Enable(EnableCap.DepthTest);
        base.OnLoad();
    }

    protected override void OnResize(ResizeEventArgs e)
    {
        GL.Viewport(0, 0, e.Width, e.Height);
        Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Size.X / Size.Y, 0.1f, 100.0f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref projection);
        base.OnResize(e);
    }

    protected override void OnUpdateFrame(FrameEventArgs args)
    {
        base.OnUpdateFrame(args);
    }

    protected override void OnRenderFrame(FrameEventArgs args)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        Matrix4 modelview = Matrix4.LookAt(new Vector3(5, 5, 5), Vector3.Zero, Vector3.UnitY);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref modelview);
        GL.Color3(1.0, 0.0, 0.0);
        this.DrawSphere(1.0, 20, 20);
        GL.Color3(0.0, 1.0, 0.0);
        GL.PushMatrix();
        GL.Translate(3.0, 0.0, 0.0);
        this.DrawTriangularPrism();
        GL.PopMatrix();
        GL.Color3(0.0, 0.0, 1.0);
        GL.PushMatrix();
        GL.Translate(-3.0, 0.0, 0.0);
        this.DrawHexagonalPyramid();
        GL.PopMatrix();
        GL.Flush();
        this.Context.SwapBuffers();
        base.OnRenderFrame(args);
    }

    private void DrawSphere(double radius, int lats, int longs)
    {
        for (int i = 0; i <= lats; i++)
        {
            double lat0 = Math.PI * (-0.5 + (double)(i - 1) / lats);
            double z0 = Math.Sin(lat0) * radius;
            double zr0 = Math.Cos(lat0) * radius;
            double lat1 = Math.PI * (-0.5 + (double)i / lats);
            double z1 = Math.Sin(lat1) * radius;
            double zr1 = Math.Cos(lat1) * radius;
            GL.Begin(PrimitiveType.QuadStrip);
            for (int j = 0; j <= longs; j++)
            {
                double lng = 2 * Math.PI * (double)(j - 1) / longs;
                double x = Math.Cos(lng);
                double y = Math.Sin(lng);
                GL.Normal3(x * zr0, y * zr0, z0);
                GL.Vertex3(x * zr0, y * zr0, z0);
                GL.Normal3(x * zr1, y * zr1, z1);
                GL.Vertex3(x * zr1, y * zr1, z1);
            }
            GL.End();
        }
    }

    private void DrawTriangularPrism()
    {
        GL.Begin(PrimitiveType.Triangles);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(-1, -1, 1);
        GL.Vertex3(1, -1, 1);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(1, -1, -1);
        GL.Vertex3(-1, -1, -1);
        GL.End();
        GL.Begin(PrimitiveType.Quads);
        GL.Vertex3(-1, -1, 1);
        GL.Vertex3(-1, -1, -1);
        GL.Vertex3(1, -1, -1);
        GL.Vertex3(1, -1, 1);
        GL.End();
    }

    private void DrawHexagonalPyramid()
    {
        GL.Begin(PrimitiveType.Triangles);
        Vector3 apex = new Vector3(0, 1, 0);
        float radius = 1.0f;
        Vector3[] hexagonVertices = new Vector3[6];
        for (int i = 0; i < 6; i++)
        {
            float angle = MathHelper.TwoPi * i / 6;
            hexagonVertices[i] = new Vector3(
                radius * (float)Math.Cos(angle),
                -1.0f,
                radius * (float)Math.Sin(angle)
            );
        }
        for (int i = 0; i < 6; i++)
        {
            GL.Vertex3(apex);
            GL.Vertex3(hexagonVertices[i]);
            GL.Vertex3(hexagonVertices[(i + 1) % 6]);
        }
        GL.End();
        GL.Begin(PrimitiveType.Polygon);
        for (int i = 0; i < 6; i++)
        {
            GL.Vertex3(hexagonVertices[i]);
        }
        GL.End();
    }
}

I tried to call the DrawSphere function to draw only the sphere in the 3D scene to check it, but it still does not display anything. I also tried changing the camera position, but that did not help.

本文标签: openglWhy does the program written in C using OpenTK display only an empty gray windowStack Overflow