3d opentk俯仰旋转使形状变形

3d opentk俯仰旋转使形状变形,3d,opentk,pitch,3d,Opentk,Pitch,我正在使用c#中的opentk渲染3d曲面并旋转它。 偏航工作正常,但俯仰旋转(将对象向摄影机倾斜)会导致曲面变形。 左边的图像是我正在渲染的变形图像,右边的图像是正确的。 请注意,当节距为零时,它看起来非常好。 以下是我的代码要点: private void glControl1_Paint(object sender, PaintEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.Dept

我正在使用c#中的opentk渲染3d曲面并旋转它。 偏航工作正常,但俯仰旋转(将对象向摄影机倾斜)会导致曲面变形。

左边的图像是我正在渲染的变形图像,右边的图像是正确的。 请注意,当节距为零时,它看起来非常好。 以下是我的代码要点:

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
}
private void glControl1_Paint(对象发送方,PaintEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 perspective=Matrix4.CreatePerspectiveFieldOfView(1.0f,纵横比,1.0f,10000.0f);//设置透视图[fovy,纵横比,zNear,zFar]
Matrix4 lookat=Matrix4.lookat(眼睛位置X,眼睛位置Y,眼睛位置Z,0,0,0,0,0,0,1);//设置相机[眼睛,眼睛,眼睛,眼睛,眼睛,眼睛Z,目标X,目标Y,目标Z,upX,upY,upZ]
GL.MatrixMode(MatrixMode.Projection);//加载透视图
GL.LoadIdentity();
GL.Viewport(0,0,this.ClientSize.Width,this.ClientSize.Height);
总帐负荷矩阵(参考透视图);
GL.MatrixMode(MatrixMode.Modelview);//加载摄影机
GL.LoadIdentity();
总帐负荷矩阵(参考观察);
GL.Viewport(0,0,glControl1.Width,glControl1.Height);//窗口大小
GL.Enable(EnableCap.DepthTest);//启用正确的Z图形
GL.DepthFunc(DepthFunction.Less);//启用正确的Z图形
//旋转
向量2 xy_视图_向量=新向量2(眼睛位置X,眼睛位置Y);
xy_视图_向量。规格化();
Vector2 fold_vec=xy_view_vector.per垂直度left;//这是一条线,您可以将视图向前倾斜到相机上
GL.Rotate((浮动)NUMERIC UPDOWN 2.Value,fold_vec.X,fold_vec.Y,0.0f);//俯仰(向前倾斜)
GL.Rotate((float)numericUpDown1.Value,0.0f,0.0f,1.0f);//偏航旋转-关于z
//斧头
总账开始(基本类型行);
GL.Color3(颜色为白色);
顶点3(-10,0,0);
顶点3(10,0,0);
顶点3(0,-10,0);
顶点3(0,10,0);
顶点3(0,0,-10);
顶点3(0,0,10);
GL.End();
inti2,j2;
浮子z1、z2、z3、z4;
总帐开始(原语类型四元组);
对于(int i=0;i
我想知道是否有人知道为什么会发生这种情况。
拥有一个简单的俯仰/偏航旋转示例会有所帮助-因此,如果您知道一个opentk示例可以提供帮助,我将非常感谢将其链接起来。

我不能确切地告诉您您做错了什么,但我可以粘贴用于围绕原点的对象旋转相机的代码:

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

我不使用固定函数管道,但您可以像您已经在做的那样设置生成的投影和模型视图矩阵。

我不能确切地告诉您您做错了什么,但我可以粘贴用于围绕原点的对象旋转相机的代码:

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

我不使用固定函数管道,但您可以像您已经在做的那样设置生成的投影和模型视图矩阵。

我不能确切地告诉您您做错了什么,但我可以粘贴用于围绕原点的对象旋转相机的代码:

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

我不使用固定函数管道,但您可以像您已经在做的那样设置生成的投影和模型视图矩阵。

我不能确切地告诉您您做错了什么,但我可以粘贴用于围绕原点的对象旋转相机的代码:

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

我不使用固定函数管道,但您可以像您已经在做的那样设置生成的投影和模型视图矩阵。

设置纵横比=1改进了很多。设置纵横比=1改进了很多。设置纵横比=1改进了很多。设置纵横比=1改进了很多。设置纵横比=1改进了很多。感谢您发布本文。我正在使用GL.MatrixMode(MatrixMode.Projection);有人知道这和GL.MatrixMode(MatrixMode.Modelview)有什么区别吗?这可能会有所帮助:MatrixMode()仅用于告诉固定函数管道在使用LoadMatrix()、LoadIdentity()等时您试图访问的矩阵。谢谢。读了你的评论,我开始明白了。谢谢你发这个帖子。我正在使用GL.MatrixMode(MatrixMode.Projection);有人知道这和GL.MatrixMode(MatrixMode.Modelview)有什么区别吗?这可能会有所帮助:MatrixMode()仅用于告诉固定函数管道在使用LoadMatrix()、LoadIdentity()等时您试图访问的矩阵。谢谢。读了你的评论,我开始明白了。谢谢你发这个帖子。我正在使用GL.MatrixMode(MatrixMode.Projection);有人知道这和GL.MatrixMode(MatrixMode.Modelview)有什么区别吗?这可能会有所帮助:MatrixMode()仅用于告诉固定函数管道在使用LoadMatrix()、LoadIdentity()等时您试图访问的矩阵。谢谢。读了你的评论,我开始明白了。谢谢你发这个帖子。我正在使用GL.MatrixMode(MatrixMode.Projection);有人知道这和GL.MatrixMode(MatrixMode.Modelview)有什么区别吗?今年五月