在OpenGL中选择一个对象

在OpenGL中选择一个对象

问题描述:

我画一个立方体和一个球体.我加载立方体的名称,但不加载球体的名称.当我通过光标选择球体时,程序将显示多维数据集的名称.像这样的代码:

I draw one cube and one sphere. I load the cube''s name but don''t load the sphere''s name. When I select the sphere by cursor, The program shows the cube''s name. The code like this:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Translate the whole scene out and into view
glTranslatef(-80.0f, 0.0f, -300.0f);
// Initialize the names stack
glInitNames();
glPushName(0);
// Set material color, Yellow
// Cube
glRGB(255, 255, 0);
glLoadName(CUBE);
glutSolidCube(75.0f);

// Draw Sphere
glRGB(128,0,0);
glTranslatef(120.0f, 0.0f, 0.0f);
glutSolidSphere(50.0f, 15, 15);

glRGB(128,0,0);
glTranslatef(0.0f, -80.0f, 0.0f);
glutSolidSphere(20.0f, 15, 15);
// Restore the matrix state
glPopMatrix();  // Modelview matrix

您能不能这样尝试?

在这里,CUBE名称被赋予glutSolidCube,而SPHERES名称被赋予球体绘图.
现在,您将在屏幕上拾取球体对象时得到SPHERES.
Could you please try like this.

Here CUBE name is given to glutSolidCube and SPHERES name is given to sphere drawing.
Now you will get SPHERES on picking sphere objects in screen.
// Set material color, Yellow
// Cube
glRGB(255, 255, 0);
// Set Drawing object name to CUBE
glLoadName(CUBE);
glutSolidCube(75.0f);

// Set Drawing object name to SPHERES
glLoadName(SPHERES);
// Draw Sphere
glRGB(128,0,0);
glTranslatef(120.0f, 0.0f, 0.0f);
glutSolidSphere(50.0f, 15, 15);

// Other drawing..........