我需要做的是在我的 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 数学 - 将屏幕空间投影到世界空间坐标
和其他人,但没有工作,当我点击屏幕时,对象被绘制在奇怪的地方。我什至不确定我在这里寻找什么,我不知道我要实现的是 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;
}