Ошибка смещения объекта после курсора - opengl - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь реализовать строку, следующую за курсором. Центр линии должен находиться точно под курсором без какого-либо смещения или отставания при движении курсора. А пока я рисую линию, используя начальное положение мыши. Затем я попытался перевести строку, обнаружив изменение положения курсора, но смещение по сторонам экрана. Это для 2D-приложения, поэтому я не думаю, что мне нужно изменять z-позицию?

      protected override void OnUpdateFrame(FrameEventArgs e)
    {
        MouseState mstate = Mouse.GetCursorState();
        currenMouse= this.PointToClient(new Point(mstate.X, mstate.Y));
        if(draw){

         oldPos = currentMouse;
                }

    }

      protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.ClearColor(Color4.Black);
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        DrawCursor1Y(currentMouse.X, currentMouse.Y);
    }

    private void DrawCursor1Y(float currentMouseX, float currentMouseY)
    {
        float[] cursors = new float[] {  oldposX , (float)0.005 * this.Height, oldposX, (float)0.995 * this.Height};
        ////binding arrays////
        GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindVertexArray(0);
        shader3.Use();
        Matrix4 Translate = Matrix4.CreateTranslation(oldposX - currentMouseX, 0, 0);
        Matrix4 model = Matrix4.Identity;
        Matrix4 projectionM = Matrix4.CreateScale(new Vector3(1f / this.Width, 1f / this.Height, 1.0f));
        projectionM = Matrix4.CreateOrthographicOffCenter(0.0f, this.Width, this.Height, 0.0f, -1.0f, 1.0f);
        projectionM = projectionM * Translate;
        GL.UniformMatrix4(0, false, ref model);
        GL.UniformMatrix4(1, false, ref projectionM);
        shader3.SetFloat("color", new Vector4(1.0f, 1.0f, 1.0f, 1));
        GL.BindVertexArray(_vao);
        GL.DrawArrays(PrimitiveType.Lines, 0, 2);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindVertexArray(0);
        shader3.Unbind();
    }

Кажется, что он работает, когда мышь почти в центре, но смещается с обоих концов .

enter image description here enter image description here

...