您的位置:首页 > 运维架构

openGL 选择和反馈(csGL)

2013-08-12 17:23 309 查看
画图形,定义模式为GL_SELECT,并为图形创建名字堆栈:

/// <summary>
/// Draws the rectangles.
/// </summary>
/// <param name="mode">The selection mode.</param>
private static void DrawRects(uint mode)
{
// The three rectangles are drawn.  In selection mode, each rectangle is given
// the same name.  Note that each rectangle is drawn with a different z value.
if (mode == GL_SELECT)
{
glLoadName(1);
}
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3i(2, 0, 0);
glVertex3i(2, 6, 0);
glVertex3i(6, 6, 0);
glVertex3i(6, 0, 0);
glEnd();

if (mode == GL_SELECT)
{
glLoadName(2);
}
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3i(3, 2, -1);
glVertex3i(3, 8, -1);
glVertex3i(8, 8, -1);
glVertex3i(8, 2, -1);
glEnd();

if (mode == GL_SELECT)
{
glLoadName(3);
}
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3i(0, 2, -2);
glVertex3i(0, 7, -2);
glVertex3i(5, 7, -2);
glVertex3i(5, 2, -2);
glEnd();
}


鼠标左键点击选中目标hit select:

/// <summary>
/// Updates pick selection.
/// </summary>
private static void PickRects()
{
private const int BUFSIZE = 512; // sets up selection mode, name stack,  and projection matrix for picking.  Then the objects are drawn
uint[] selectBuf = new uint[BUFSIZE];
int hits;
int[] viewport = new int[4];

glGetIntegerv(GL_VIEWPORT, viewport);

glSelectBuffer(BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// create 5x5 pixel picking region near cursor location
gluPickMatrix((double)Model.Mouse.X, (double)(viewport[3] - Model.Mouse.Y), 5.0, 5.0, viewport);
glOrtho(0.0f, 8.0f, 0.0f, 8.0f, -0.5f, 2.5f);
DrawRects(GL_SELECT);
glPopMatrix();
glFlush();

hits = glRenderMode(GL_RENDER);
ProcessHits(hits, selectBuf);
}
/// <summary>
/// Displays hit data.
/// </summary>
/// <param name="hits">Number of hits.</param>
/// <param name="buffer">The selection buffer.</param>
private static void ProcessHits(int hits, uint[] buffer)
{
uint i, j;
uint names;
uint[] ptr;

Console.WriteLine("hits = {0}", hits);
ptr = buffer;
for (i = 0; i < hits; i++)
{                                                    // For Each Hit
names = ptr[i];
Console.WriteLine(" number of names for hit = {0}", names);
i++;
Console.WriteLine(" z1 is {0}", (float)ptr[i] / 0x7fffffff);
i++;
Console.WriteLine(" z2 is {0}", (float)ptr[i] / 0x7fffffff);
i++;
Console.Write(" the name is ");
for (j = 0; j < names; j++)
{                                            // For Each Name
Console.Write("{0} ", ptr[i]);
i++;
}
Console.Write("\n");
}
Console.Write("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: