1

GL_SELECT用来执行采摘。一切正常,对象被正确拾取。但是,拾取对象的 z 坐标存在问题。出于某种原因,它们都等于-2147483648。因此,当拾取几个对象时,我无法确定哪个更接近。

我有以下代码:

private int[] pickBuffer = new int[PICK_BUFFER_SIZE];

private IntBuffer pickGlBuffer = java.nio.IntBuffer.wrap(pickBuffer);

public void init(GLAutoDrawable drawable) {
    glu = new GLU();
    GL2 g2 = drawable.getGL().getGL2();

    { // options
        g2.glEnable(GL.GL_DEPTH_TEST);
        g2.glDepthFunc(GL.GL_LEQUAL);
        g2.glShadeModel(GLLightingFunc.GL_SMOOTH);
        g2.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
        g2.glClearColor(0f, 0f, 0f, 1f);
    }

}

public void display(final GLAutoDrawable drwabale) {
    GL2 g2 = drwabale.getGL().getGL2();
    g2.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    Arrays.fill(pickBuffer, 0); // just for debug
    g2.glSelectBuffer(PICK_BUFFER_SIZE, pickGlBuffer);

    { // selection
        g2.glRenderMode(GL2.GL_SELECT);
        g2.glInitNames();
        g2.glPushName(-1);

        setProjections(g2, GL2.GL_SELECT);

        doDraw(g2);
    }

    int hits = g2.glRenderMode(GL2.GL_RENDER);
    processPicking(hits, pickBuffer);

    { // actual drawing
        setProjections(g2, GL2.GL_RENDER);

        doDraw(g2);
    }
}

private void setProjections(final GL2 g2, final int renderMode) {
    g2.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    g2.glLoadIdentity();
    if (renderMode == GL2.GL_SELECT) {
        glu.gluPickMatrix((double) currentMouseX, (double) (viewPort[3] - currentMouseY), 3, 3, viewPort, 0);
    }
    double width = glCanvas.getWidth(), height = glCanvas.getHeight();
    glu.gluPerspective(45, width / height, 1, 1000);

    {
        double yawRad = getYawAngle() / 180 * Math.PI;
        double pitchRad = getPitchAngle() / 180 * Math.PI;
        double vd = getViewerDistance();
        double vx = Math.sin(pitchRad) * Math.sin(yawRad) * vd;
        double vy = Math.cos(pitchRad) * vd;
        double vz = Math.sin(pitchRad) * Math.cos(yawRad) * vd;
        glu.gluLookAt(
                vx, vy, vz,
                0, 0, 0,
                0, 1, 0);
    }

    g2.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    g2.glLoadIdentity();
}

private void doDraw(final GL2 g2) {
    if (isAxesPainted()) {
        drawAxes(g2);
    }

    drawPlanes(g2);
}

private void drawPlanes(final GL2 g2) {
    g2.glColor3d(1, 1, 1);
    g2.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE);
    { // xy plane
        g2.glLoadName(XY_PLANE_NAME);
        g2.glBegin(GL2.GL_QUADS);
            g2.glTexCoord2d(0, 0);
            g2.glVertex3d(-xBound / 2, -yBound / 2, xyPosition);
            g2.glTexCoord2d(1, 0);
            g2.glVertex3d(xBound / 2, -yBound / 2, xyPosition);
            g2.glTexCoord2d(1, 1);
            g2.glVertex3d(xBound / 2, yBound / 2, xyPosition);
            g2.glTexCoord2d(0, 1);
            g2.glVertex3d(-xBound / 2, yBound / 2, xyPosition);
        g2.glEnd();
    }
    { // xz plane
        g2.glLoadName(XZ_PLANE_NAME);
        g2.glBegin(GL2.GL_QUADS);
            g2.glTexCoord2d(0, 0);
            g2.glVertex3d(-xBound / 2, xzPosition, -zBound / 2);
            g2.glTexCoord2d(1, 0);
            g2.glVertex3d(xBound / 2, xzPosition, -zBound / 2);
            g2.glTexCoord2d(1, 1);
            g2.glVertex3d(xBound / 2, xzPosition, zBound / 2);
            g2.glTexCoord2d(0, 1);
            g2.glVertex3d(-xBound / 2, xzPosition, yBound / 2);
        g2.glEnd();
    }
    { // yz plane
        g2.glLoadName(YZ_PLANE_NAME);
        g2.glBegin(GL2.GL_QUADS);
            g2.glTexCoord2d(0, 0);
            g2.glVertex3d(yzPosition, yBound / 2, zBound / 2);
            g2.glTexCoord2d(1, 0);
            g2.glVertex3d(yzPosition, yBound / 2, -zBound / 2);
            g2.glTexCoord2d(1, 1);
            g2.glVertex3d(yzPosition, -yBound / 2, -zBound / 2);
            g2.glTexCoord2d(0, 1);
            g2.glVertex3d(yzPosition, -yBound / 2, zBound / 2);
        g2.glEnd();
    }
}

UPD

问题似乎不在于应用程序逻辑,而在于 jogl 或 opengl 驱动程序,因为我试图在另一台计算机(win 7 x64)上运行此代码并且一切正常。有属性 z 坐标存储在选择缓冲区中,我可以按它们排序以获得最近的对象。

当前问题在 win xp x86 上可重现。

4

0 回答 0