像Autocad一样的Ortho Camera with OpenGL

时间:2016-01-26 18:44:53

标签: c# opengl 3d camera graphic

我要写一个程序来查看dwg文件。我正在使用SharpGL库。 我为3D编写了一个鼠标控制摄像头。 这是一个代码:

private void Device_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
    {
        OpenGL gl = args.OpenGL;

        gl.ClearColor((float)s3.Value, (float)s2.Value, (float)s1.Value, 0f);

        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
        gl.LoadIdentity();

        gl.MatrixMode(OpenGL.GL_PROJECTION);
        gl.LoadIdentity();

        gl.Ortho(-wheelx, wheelx, -wheely, wheely, 0.1f, 1000000000.0f);

        gl.MatrixMode(OpenGL.GL_MODELVIEW);

        gl.Rotate(xrot, 1.0, 0.0, 0.0);
        gl.Rotate(yrot, 0.0, 1.0, 0.0);
        gl.Translate(-xpos, -ypos, -zpos);

        DrawScene();   
        gl.Flush();
    }

private void Device_MouseMove(object sender, MouseEventArgs e)
    {
        System.Windows.Point position = Mouse.GetPosition(Device);
        x = (int)Math.Ceiling(position.X);
        y = -(int)Math.Ceiling(position.Y);

        if (e.MiddleButton == MouseButtonState.Pressed)
        {
            Cursor = Cursors.Hand;
            diffx = x - lastx; //check the difference between the 
            diffy = y - lasty; //check the difference between the 


            xrot += (float)diffy * 0.05f * (float)Math.Min(wheelx, wheely); ; //set the xrot to xrot with the addition
            yrot += (float)diffx * 0.05f * (float)Math.Min(wheelx, wheely); ;    //set the xrot to yrot with the addition

            if (xrot < 0)
            {
                xrot = 360;
            }
            if (xrot > 360)
            {
                xrot = 0;
            }

            if (yrot < 0)
            {
                yrot = 360;
            }
            if (yrot > 360)
            {
                yrot = 0;
            }

        }

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            diffx = x - lastx; //check the difference between the 
            diffy = y - lasty; //check the difference between the 

            float xrotrad, yrotrad;
            yrotrad = (yrot / 180 * PI);
            xrotrad = (xrot / 180 * PI);

            float xs = (float)(Math.Tan(xrotrad));
            float xc = (float)(Math.Cos(xrotrad));
            float yc = (float)(Math.Cos(yrotrad));
            float ys = (float)(Math.Sin(yrotrad));

            ypos -= (xc * (float)diffy)* 0.003f * (float)Math.Min(wheelx, wheely);     
            zpos -= (-yc * (float)diffy + ys * (float)diffx) * 0.003f * (float)Math.Min(wheelx, wheely);
            xpos += (-ys * (float)diffy - yc * (float)diffx) * 0.003f * (float)Math.Min(wheelx, wheely);

            l5.Content = xc;
        }

        lastx = x; //set lastx to the current x position
        lasty = y; //set lasty to the current y position
    }

对于2D相机效果很好。在3D上有问题:

当X角在0-180度之间时,垂直移动效果良好,但X角在180-360之间(约315度),垂直移动不能正常工作。

我做错了什么?

0 个答案:

没有答案