当我尝试放置我的项目并在笔记本电脑上运行时,一切正常,但3D拾取混乱了。单击3D对象时,它会选择不同的对象。
我在另一台笔记本电脑上尝试过它会产生相同的效果。然后我尝试将台式电脑的显示器固定到我的笔记本电脑上,以查看天气是一个分辨率问题。但它仍然给出了相同的结果。但它在我的台式电脑上完全正常。
我的桌面上有一个Nvidia 760gtx gpu,笔记本电脑只有Intel HD 4500 gpu。是因为gpu吗?我。我输了。我尝试了一切。 这是我选择3D对象的代码。
public void select(int xx, int yy )
{
// The selection buffer
IntBuffer selBuffer = ByteBuffer.allocateDirect(1280).order(ByteOrder.nativeOrder()).asIntBuffer();
int buffer[] = new int[256];
IntBuffer vpBuffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer();
// The size of the viewport. [0] Is <x>, [1] Is <y>, [2] Is <width>, [3] Is <height>
int[] viewport = new int[4];
// The number of "hits" (objects within the pick area).
int hits;
// Get the viewport info
GL11.glGetInteger(GL11.GL_VIEWPORT, vpBuffer);
vpBuffer.get(viewport);
// Set the buffer that OpenGL uses for selection to our buffer
GL11.glSelectBuffer(selBuffer);
// Change to selection mode
GL11.glRenderMode(GL11.GL_SELECT);
// Initialize the name stack (used for identifying which object was selected)
GL11.glInitNames();
GL11.glPushName(0);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
/* create 5x5 pixel picking region near cursor location */
GLU.gluPickMatrix( (float) xx, (float) yy, 0.001f, 0.001f,IntBuffer.wrap(viewport));
GLU.gluPerspective(fov, screenResolutionx/screenResolutiony, near, far);
render();
GL11.glPopMatrix();
// Exit selection mode and return to render mode, returns number selected
hits = GL11.glRenderMode(GL11.GL_RENDER);
System.out.println("hits: " + hits);
selBuffer.get(buffer);
// Objects Were Drawn Where The Mouse Was
if (hits > 0) {
// If There Were More Than 0 Hits
choose = buffer[3]; // Make Our Selection The First Object
int depth = buffer[1]; // Store How Far Away It Is
for (int i = 1; i < hits; i++) {
// Loop Through All The Detected Hits
// If This Object Is Closer To Us Than The One We Have Selected
if (buffer[i * 4 + 1] < depth) {
choose = buffer[i * 4 + 3]; // Select The Closer Object
depth = buffer[i * 4 + 1]; // Store How Far Away It Is
}
}
System.out.println("Chosen: " + choose);
}
}
答案 0 :(得分:0)
最后我想通了。这是因为GPU。我在nvidia GPU(840m)笔记本电脑上尝试了这种方法,它确实运行良好。但是当我将gpu切换到Intel HD 5500时,选择无法正常工作。所以我的结论是使用GL11.glRenderMode(GL11.GL_SELECT)
方法进行3D对象选择仅适用于nvidia GPU。它可能适用于ATI GPU(未经测试),但不适用于英特尔。