我开发了一个小的c#示例,通过使用OpenTK和winforms来选择点
有一种方法可以画正方形,还有一种方法可以选择三角形。不知怎么的,我没有任何点击。Readpixel方法有什么问题吗?因为我刚刚用正确的方法将Tao采摘样本转换为OpenTK ?你能告诉我我的方法有什么问题吗?
private static int[,] board = new int[3, 3]; //Definition
private const int BUFSIZE = 512;
private void glControl1_MouseDown(object sender, MouseEventArgs e)
{
int[] selectBuffer = new int[BUFSIZE]; //This has to be redifined
int hits;
int[] viewport = new int[4];
if (e.Button == MouseButtons.Left)
{
GL.GetInteger(GetPName.Viewport, viewport);
GL.SelectBuffer(BUFSIZE, selectBuffer);
GL.RenderMode(RenderingMode.Select);
GL.InitNames();
GL.PushName(0);
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
Byte4 Pixel = new Byte4();
GL.ReadPixels(e.X, viewport[3] - e.Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel);
uint SelectedTriangle=SelectedTriangle = Pixel.ToUInt32();
GL.Ortho(0, 3 ,0, 3, 1,-1); // Bottom-left corner pixel has coordinate (0, 0)
DrawSquares(GL.RenderMode(RenderingMode.Select));
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.Flush();
hits = GL.RenderMode(RenderingMode.Render);
ProcessHits(hits, selectBuffer);
glControl1.SwapBuffers();
}
}
private static void DrawSquares(int mode)
{
int i, j;
for (i = 0; i < 3; i++)
{
if (mode == GL.RenderMode(RenderingMode.Select))
GL.LoadName(i);
for (j = 0; j < 3; j++)
{
if (mode == GL.RenderMode(RenderingMode.Select))
GL.PushName(j);
GL.Color3((float)i / 3.0f, (float)j / 3.0f, (float)board[i, j] / 3.0f);
GL.Rect(i, j, (i + 1), (j + 1));
if (mode == GL.RenderMode(RenderingMode.Select))
GL.PopName();
}
}
}
我在代码中犯了一些问题。它制造了这个问题。像这样更改实例。然后它将工作:)。一个小错误引起了一个大问题:
//(mode == GL.RenderMode(RenderingMode.Select))
(mode == RenderingMode.Select) // Removed GL.RenderMode