opentk俯仰旋转变形的形状 [英] opentk pitch rotation deforms the shape

查看:1148
本文介绍了opentk俯仰旋转变形的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用opentk在C#来渲染3D曲面和旋转。 偏航工作正常,但在球场旋转(倾斜朝向相机的对象)使表面变形。

I'm using opentk in c# to render a 3d surface and rotate it. The yaw works fine, but the pitch rotation (tilting the object towards the camera) causes the surface to deform.

左侧的图像是什么林呈现其变形,并且一一右边是正确的。 注意,当间距是零,它看起来完全正常。 这里是我的code中的要点:

The image on the left is what I'm rendering which is deformed and the one one the right is correct. Note that when the pitch is zero, it looks perfectly fine. Here is the gist of my code:

private void glControl1_Paint(object sender, PaintEventArgs e)
{
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(1.0f, aspect_ratio, 1.0f, 10000.0f); //Setup Perspective [fovy, aspect, zNear, zFar]
    Matrix4 lookat = Matrix4.LookAt(eye_pos.X, eye_pos.Y, eye_pos.Z, 0, 0, 0, 0, 0, 1); //Setup camera   [eyeX, eyeY, eyeZ, targetX, targetY, targetZ, upX, upY, upZ)]

    GL.MatrixMode(MatrixMode.Projection); //Load Perspective
    GL.LoadIdentity();
    GL.Viewport(0, 0, this.ClientSize.Width, this.ClientSize.Height);
    GL.LoadMatrix(ref perspective);
    GL.MatrixMode(MatrixMode.Modelview); //Load Camera
    GL.LoadIdentity();
    GL.LoadMatrix(ref lookat);
    GL.Viewport(0, 0, glControl1.Width, glControl1.Height); //Size of window
    GL.Enable(EnableCap.DepthTest); //Enable correct Z Drawings
    GL.DepthFunc(DepthFunction.Less); //Enable correct Z Drawings

    //Rotating
    Vector2 xy_view_vector = new Vector2(eye_pos.X, eye_pos.Y);
    xy_view_vector.Normalize();
    Vector2 fold_vec = xy_view_vector.PerpendicularLeft; // this is the line over which you'd tilt the view forward towards the camera
    GL.Rotate((float)numericUpDown2.Value, fold_vec.X, fold_vec.Y, 0.0f);// pitch (tilt forward)
    GL.Rotate((float)numericUpDown1.Value, 0.0f, 0.0f, 1.0f);// yaw rotation - about z


    // axes
    GL.Begin(PrimitiveType.Lines);
    GL.Color3(Color.White);
    GL.Vertex3(-10, 0, 0);
    GL.Vertex3(10, 0, 0);
    GL.Vertex3(0, -10, 0);
    GL.Vertex3(0, 10, 0);
    GL.Vertex3(0, 0, -10);
    GL.Vertex3(0, 0, 10);
    GL.End();

    int i2, j2;
    float z1, z2, z3, z4;
    GL.Begin(PrimitiveType.Quads);
    for (int i = 0; i < data.x.Count - 2; i++)
    {
        for (int j = 0; j < data.y.Count - 2; j++)
        {
            i2 = i + 1;
            j2 = j + 1;
            z1 = data.z[i.ToString() + "," + j.ToString()];
            z2 = data.z[i2.ToString() + "," + j.ToString()];
            z3 = data.z[i2.ToString() + "," + j2.ToString()];
            z4 = data.z[i.ToString() + "," + j2.ToString()];
            GL.Color4(color_map.GetColor((z1 + z3) / 2, data.minz, data.maxz, opacity));

            GL.Vertex3(new Vector3(data.x[i], data.y[j], z1));
            GL.Vertex3(new Vector3(data.x[i2], data.y[j], z2));
            GL.Vertex3(new Vector3(data.x[i2], data.y[j2], z3));
            GL.Vertex3(new Vector3(data.x[i], data.y[j2], z4));
        }
    }
    GL.End();

    GraphicsContext.CurrentContext.VSync = true; //Caps frame rate as to not over run GPU
    glControl1.SwapBuffers(); //Takes from the 'GL' and puts into control
}

我想知道如果任何人有一个线索,为什么发生这种情况。 有一个简单的俯仰/偏航样本将帮助 - 所以,如果你知道一个opentk样本,可以帮助,我倒是AP preciate联系起来,

I was wondering if anyone has a clue as to why this is happening. Having a simple pitch/yaw rotation sample would help - so if you know an opentk sample that can help, I'd appreciate linking it.

推荐答案

我不能告诉你到底你在做什么错,但我可以贴code,我用它来在旋转的摄像头周围的物体产地:

I can not tell you exactly what you are doing wrong, but I can paste the code which I use to rotate the camera around an object at the origin:

void SetupPerspective()
{
    var aspectRatio = Width / (float)Height;
    Projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.1f, 10);
    ModelView = Matrix4.Identity;
    // apply camera transform
    Camera.ApplyCamera(ref ModelView);
}

void ApplyCamera(ref Matrix4 modelView)
{
    modelView = Matrix4.CreateRotationY(Yaw)
        * Matrix4.CreateRotationX(Pitch)
        * Matrix4.CreateRotationZ(Roll)
        * Matrix4.CreateTranslation(-Position)
        * modelView;
}

我不使用固定功能管线,但你可以设置所产生的投影和模型视图矩阵,就像你已经在做了。

I don't use the fixed-function pipeline but you could set the resulting Projection and ModelView matrices just like you're already doing it.

这篇关于opentk俯仰旋转变形的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆