对象在放大时消失-Opengl

问题描述 投票:1回答:1

我正在尝试使用OpenTK绘制2000万个点的二维图。我需要能够放大,缩小和平移。当我尝试在5万个点的图形上进行测试时,一切正常,但是当我尝试绘制2000万个图形时,图形对象在放大几次后似乎消失了。我绝对不知道为什么它消失了。我根据OnMouseWheel事件通过更改投影矩阵中的fovy值来放大和缩小。在测试放大时,我没有触摸平移。

    fov = 55.0f;
    public GraphWIndow (float [] Xpoints, float[][] YPoints ) : base(800, 600, default, "Data Analyzer", GameWindowFlags.Default, default,4,0, default)
            {
                Y1Vertices[10][2M] = Ypoints;
                X1Vertices[2M] = Xpoints
            }

         protected override void OnLoad(EventArgs e)
        {
            CursorVisible = true;
            shader = new Shader("shader.vert", "shader.frag");
            shader.Use();
            CameraPos = new Vector3(0.0f, 0.0f, 2.0f);
            CameraUp = new Vector3(0.0f, 1.0f, 0.0f);
            CameraFront = new Vector3 (0.0f, 0.0f, -1.0f);
            base.OnLoad(e);
        }

 private void CreateAndPlotData(float[] YVertices, int color)
        {
            VertexBufferObjectX = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjectX);
            GL.BufferData(BufferTarget.ArrayBuffer, XVertices.Count() * sizeof(float), XVertices, BufferUsageHint.StaticDraw);
            var vertexLocationX = shader.GetAttribLocation("aPositionX");
            VertexBufferObjectY = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjectY);
            GL.BufferData(BufferTarget.ArrayBuffer, YVertices.Count() * sizeof(float), YVertices, BufferUsageHint.StaticDraw);
            var vertexLocationY = shader.GetAttribLocation("aPositionY");
            VertexArrayObject = GL.GenVertexArray();
            GL.BindVertexArray(VertexArrayObject);
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjectX);
            GL.VertexAttribPointer(vertexLocationX, 1, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObjectY);
            GL.VertexAttribPointer(vertexLocationY, 1, VertexAttribPointerType.Float, false, 0, 0);
            GL.EnableVertexAttribArray(vertexLocationX);
            GL.EnableVertexAttribArray(vertexLocationY);
            GL.BindVertexArray(0);
            SetUpShaderAndView(color);
            GL.BindVertexArray(VertexArrayObject);
            GL.DrawArrays(PrimitiveType.LineStrip, 0, YVertices.Count());
            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            shader.Unbind();

        }

        private void SetUpShaderAndView(int color)
        {
            Matrix4 view, projection;
            projection = Matrix4.CreatePerspectiveFieldOfView(((float)fov * (float)Math.PI) /(float)180, (float)Width / (float)Height, 0.01f, 100f);
            view = Matrix4.LookAt(CameraPos,
            CameraPos + CameraFront, CameraUp);
            shader.Use();
            shader.SetMatrix4("model", Matrix4.Identity);
            shader.SetMatrix4("view", view);
            shader.SetMatrix4("projection", projection);
            SwapBuffers();
            base.OnRenderFrame(e);
        }



  protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.ClearColor(Color4.DarkBlue);
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        for (int j=0; j < 10; j++)
        {
            CreateAndPlotData(Y1Vertices[j], j);
        }
        SwapBuffers();
        base.OnRenderFrame(e);
    }

     protected override void OnMouseWheel(MouseWheelEventArgs e)
        {

           if (fov > 90.0f)
            {
               fov = 90.0f;
            }
            else if (fov <5.0f)
            {
                fov = 5.0f;
            }
            else 
            {
                fov -= e.DeltaPrecise;
            }

            base.OnMouseWheel(e);
        }

shader.vert

#version 400 core
in float aPositionX;
in float aPositionY;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = vec4(aPositionX, aPositionY, 0.0f, 1.0f)*model*view*projection;
}

Shader.cs

public void SetMatrix4(string name, Matrix4 data)
{
    GL.UseProgram(Handle);
    var uniform_loc = GL.GetUniformLocation(Handle, name);
    GL.UniformMatrix4(GL.GetUniformLocation(Handle,name), true, ref data);

}
c# opengl opentk
1个回答
1
投票

可能有几个原因。因为您没有提供可执行示例,所以我只能说出可能的原因。

  • 您是否设置了视口GL.ViewPort(0,0, WindowWidth, WindowHeight)
  • 您不解释确切消失的是什么。因此,您是否检查了“渲染和更新线程”仍在工作? (在您的情况下,正如我在您的代码示例中看到的那样,Update和Render-Thread是相同的。)
  • 您是否检查过错误?如我所见,您正在使用OpenTK3.1版本。注册错误处理程序时存在错误。因此,您应该尝试使用ApiTraceRenderDoc检查错误并检查管道的每一步。
  • 最后但并非最不重要的是,相机矩阵的近平面或远平面可能有问题。
  • 有很多原因导致东西消失。如果您可以提供更多信息,我相信我可以提供更深入的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.