如何在鼠标单击时显示opengl对象?

时间:2012-06-15 21:32:10

标签: opengl graphics mouse-picking

我需要做的是点击我的OpenGL视口上的某处,并在该位置渲染一个对象。我已经知道基本的东西,即创建窗口,检测鼠标点击,获取点击坐标和绘制对象。我缺少的链接是让坐标正确。我需要确定我点击屏幕上的(X,Y)点是什么意思是openGL世界坐标。我知道一个名为gluUnProject的GLUT函数,但由于技术原因我无法使用GLUT。我已经检查了很多算法来手工创建我自己的unproject函数:

http://schabby.de/picking-opengl-ray-tracing/ http://collagefactory.blogspot.mx/2010/03/gluunproject-source-code.html OpenGL Math - Projecting Screen space to World space coords

和其他人一样,但没有人工作,当我点击屏幕时,对象被绘制在奇怪的地方。我甚至不确定我在这里寻找什么,我不知道我想要实现的是Picking,或Raycasting或Raypicking还是其他什么。有谁知道我正在寻找的算法?

编辑: 我正在添加一个屏幕截图,我标记了我点击的位置,正如您所看到的,球体在不同的位置绘制。 http://img23.imageshack.us/img23/4738/proofxd.jpg 我还要添加我的opengl源代码:

    /**OPENGL**/
    private void GLC_display_Load(object sender, EventArgs e)
    {
        //CONTEXT
        GL.ClearColor(Color.LightGray);
        GL.Enable(EnableCap.Texture2D);
        GL.Enable(EnableCap.Blend);
        GL.Enable(EnableCap.DepthTest);
        GL.Enable(EnableCap.CullFace);
        GL.CullFace(CullFaceMode.Back);
        GL.Enable(EnableCap.Blend);
        GL.DepthFunc(DepthFunction.Always);     
        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); 
        SetupViewport();
    }


    private void SetupViewport()
    {
        GL.Clear(ClearBufferMask.ColorBufferBit);
        GL.ShadeModel(ShadingModel.Smooth);
        float aspectRatio = (float)GLC_display.Width / (float)GLC_display.Height;
        GL.Viewport(0, 0, GLC_display.Width, GLC_display.Height);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView((float)(System.Math.PI / 4f), aspectRatio, 0.1f, 2000f);
        GL.MultMatrix(ref perspective);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();
    }

    private void GLC_display_Resize(object sender, EventArgs e)
    {
        SetupViewport();
        GLC_display.Invalidate();
    }

    private void GLC_display_Paint(object sender, EventArgs e)
    {

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        Matrix4 lookat = Matrix4.LookAt(posVector, eyeVector, Vector3.UnitY);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref lookat);
        GL.Color3(Color.Red);
        //GL.PushMatrix();
        //GL.Translate(new Vector3(10f,3f,5f));
        //Geometry.drawSphere(5, 20);
        //GL.PopMatrix();

        foreach (Vector3 coord in clicks)
        {
            Console.WriteLine("List: "+coord.ToString());
            //GL.Color3(0f, 1f, 0f);
            GL.PushMatrix();
            GL.Translate(coord);
            Geometry.drawSphere(5, 20);
            GL.PopMatrix();
        }
        GLC_display.SwapBuffers();
    }

    public Vector3 unProjectInput(Vector2 mouse)
    {
        Vector4 result;
        int[] view = new int[4];
        GL.GetInteger(GetPName.Viewport, view);

        mouse.Y = view[3] - mouse.Y;
        result.X = (2f*((float)(mouse.X - view[0])/view[2])) - 1f;
        result.Y = (2f*((float)(mouse.Y - view[1]) / view[3])) - 1f;
        result.Z = 0f;
        result.W = 1f;

        double[] projection = new double[16];
        GL.GetDouble(GetPName.ProjectionMatrix, projection);
        Matrix4 Projection = new Matrix4((float)projection[0], (float)projection[1], (float)projection[2], (float)projection[3],
                                                   (float)projection[4], (float)projection[5], (float)projection[6], (float)projection[7],
                                                   (float)projection[8], (float)projection[9], (float)projection[10], (float)projection[11],
                                                   (float)projection[12], (float)projection[13], (float)projection[14], (float)projection[15]);
        double[] modelViewMatrix = new double[16];
        GL.GetDouble(GetPName.ModelviewMatrix, modelViewMatrix);
        Matrix4 MV = new Matrix4((float)modelViewMatrix[0], (float)modelViewMatrix[1], (float)modelViewMatrix[2], (float)modelViewMatrix[3],
                                                   (float)modelViewMatrix[4], (float)modelViewMatrix[5], (float)modelViewMatrix[6], (float)modelViewMatrix[7],
                                                   (float)modelViewMatrix[8], (float)modelViewMatrix[9], (float)modelViewMatrix[10], (float)modelViewMatrix[11],
                                                   (float)modelViewMatrix[12], (float)modelViewMatrix[13], (float)modelViewMatrix[14], (float)modelViewMatrix[15]);

        Matrix4 iMVP = Matrix4.Invert(Matrix4.Mult(MV, Projection));
        result = Vector4.Transform(result, iMVP);

        if (result.W > float.Epsilon || result.W < float.Epsilon)
        {
            result.X /= result.W;
            result.Y /= result.W;
            result.Z = 0f;
            //result.Z /= result.W;
        }

        return result.Xyz;
    }

2 个答案:

答案 0 :(得分:1)

GLU和GLUT不是一回事。任何合理的现代OpenGL安装都应该有GLU功能。

对于它的价值,gluUnProject的实现实际上是在its man page中详细说明的。

答案 1 :(得分:0)

vec3f cameraPos=camera->position();
vec3f objectPos=vec3f(
    (float)(mousePos.x)/(videoProperties.w)*2-1,
    1,
    (float)(((videoProperties.h)-mousePos.y))/(videoProperties.h)*2-1);
objectPos.x/=(videoProperties.h)/(videoProperties.w);
objectPos*=distance;
objectPos+=cameraPos;
objectPos*=camera->transformation();//may need to inverse this depending on your camera system

这是我在引擎中使用的