FATzz
FATzz

Reputation: 83

Object disappears on zooming in- Opengl

I am trying to plot a 2d graph of 20 million points using OpenTK. I need to be able to zoom in, zoom-out and pan. Everything worked when I tried to test it on 50K points graph but when I tried to plot the 20 million, the graph object seems to disappear after zooming in few times. I am absolutely clueless why it is disappearing. I zoom in and out by changing the fovy value in projection matrix depending on the OnMouseWheel event. I didn't touch the panning while I was testing for zooming in.

    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);

}

Upvotes: 1

Views: 555

Answers (1)

Arakis
Arakis

Reputation: 889

There could be several reasons. Because you do not provide an executable sample, I can tell only the possible reasons.

  • Did you set the viewport GL.ViewPort(0,0, WindowWidth, WindowHeight)?
  • You do not explain what is disappearing exactly. So, did you checked the Render and Update Thread is still working? (In you case, Update and Render-Thread are the same, as I see in your code sample).
  • Did you checked for error? As I see, you're using the OpenTK3.1 Version. There's a bug to register an error handler. So you should try to use ApiTrace or RenderDoc to check for errors and check every step of your pipeline.
  • Last but not least there could be a problem with the Near plane or far plane of the camera matrix.

There are many reasons why stuff could disappear. If you could provide more information, I'm sure I can help more in-depth.

Upvotes: 1

Related Questions