首页 新闻 赞助 找找看

OpenGL 拾取问题 C# + OpenTK

0
悬赏园豆:50 [已关闭问题] 关闭于 2017-08-01 11:01

public override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);

if (e.Button == MouseButtons.Left)
{
//1、设置缓冲区
GL.SelectBuffer(512, selectBuf);
//2、进入选择模式
GL.RenderMode(RenderingMode.Select);

//2、初始化名字栈
GL.InitNames();
GL.PushName(1);
//设置变换的矩阵----切换到投影
GL.MatrixMode(MatrixMode.Projection);
//压入矩阵,保存之前的矩阵
GL.PushMatrix();
//设置单位矩阵(矩阵是初始状态)
GL.LoadIdentity();

//4、设置拾取矩阵
//得到当前视口
int[] viewPort = new int[4] { 0, 0, 644, 373 };
GL.GetInteger(GetPName.Viewport, viewPort);
//先确立拾取的位置,再移动摄像机到拾取的位置。

PickMatrix(e.X, 373 - e.Y, 3, 3, ref viewPort);

//相当于gluPerspective
//Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(1.0f, 644 / 373, 0.1f, 100.0f);
Matrix4 perspective = Matrix4.Perspective(45.0f, 644 / 373, 0.01f, 500.0f);
//这里是把拾取的矩阵进行透视投影。所以是乘以原来设置的拾取矩阵,不能用GL.LoadMatrix(ref perspective).
//LoadMatrix会造成重新设置一个矩阵
//GL.LoadMatrix(ref perspective);

//5、进行原来的矩阵变换
GL.MultMatrix(ref perspective);
//GL.Translate(0, 0, -8);

//5.5画图,进入选择模式是看不到的。
Draw();
//GL.MatrixMode(MatrixMode.Projection);

//返回选择模式所捕捉到的对象。
int hits = GL.RenderMode(RenderingMode.Render);

//7、恢复之前的矩阵
GL.PopMatrix();

if (hits != 0)
{
MessageBox.Show(ProcessIntersection(hits));
}
}
return;
}

private void Draw()
{
GL.PushMatrix();
GL.LoadName(3);

// 箭头相对于相机旋转
GL.Rotate(-60, new Vector3(0.0f, 1.0f, 0.0f));

// 相对坐标系
GL.Translate(new Vector3(0.2f, 0.3f, 0.4f));

// 箭头本身旋转
GL.Rotate(90, new Vector3d(1.0, 0.0, 0.0));

// 箭头方位旋转
GL.Rotate(10, new Vector3(0.0f, 0.0f, -1.0f));
GL.Scale(new Vector3(0.01f, 0.01f, 0.01f));

GL.Color3(1.0, 0.0, 0.0);
GL.Begin(BeginMode.Polygon);
GL.Vertex3(0.0f, 10.0f, 0.0f);
GL.Vertex3(10.0f, 0.0f, 0.0f);
GL.Vertex3(5.0f, 0.0f, 0.0f);
GL.Vertex3(5.0f, -10.0f, 0.0f);
GL.Vertex3(-5.0f, -10.0f, 0.0f);
GL.Vertex3(-5.0f, -0.0f, 0.0f);
GL.Vertex3(-10.0f, 0.0f, 0.0f);

GL.End();
GL.PopMatrix();
}

请问,我点图像时,没有拾取到,但是在某个位置能够拾取到,这是哪个地方变换时,出错了

 

求帮助啊~~

一叶知秋somthing的主页 一叶知秋somthing | 菜鸟二级 | 园豆:208
提问于:2017-07-28 11:34
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册