3D 拾取在另一台计算机上不起作用 - lwjgl
3D picking doesnt work on another computer - lwjgl
当我尝试将我的项目 运行 放在笔记本电脑上时,一切正常,但 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);
}
}
我终于明白了。这是因为GPU。我在 nvidia GPU (840m) 笔记本电脑上尝试过这种方法,效果很好。但是当我将 gpu 切换到 Intel HD 5500 时,选择无法正常工作。所以我的结论是使用 GL11.glRenderMode(GL11.GL_SELECT)
方法进行 3D 对象拾取仅适用于 nvidia GPU。它也可能适用于 ATI GPU(未测试),但不适用于 Intel。
当我尝试将我的项目 运行 放在笔记本电脑上时,一切正常,但 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);
}
}
我终于明白了。这是因为GPU。我在 nvidia GPU (840m) 笔记本电脑上尝试过这种方法,效果很好。但是当我将 gpu 切换到 Intel HD 5500 时,选择无法正常工作。所以我的结论是使用 GL11.glRenderMode(GL11.GL_SELECT)
方法进行 3D 对象拾取仅适用于 nvidia GPU。它也可能适用于 ATI GPU(未测试),但不适用于 Intel。