如何使用jogl选择对象?

时间:2014-05-28 09:13:42

标签: java opengl jogl

我遇到了一些关于jogl采摘的问题。我需要选择每一个点并处理它,但我总是得到0点击(没有被选中)。任何人都可以帮我这个吗?

display函数可以正确获取光标周围的2x2窗口。

public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    switch(cmd){
        case UPDATE:
            gl.glPushMatrix();
            gl.glMultMatrixf(rot_matrix, 0);
            buildPoints(gl);
            buildAxes(gl);
            gl.glPopMatrix();
            break;

        case SELECT:
            int buffsize = 512;
            double x = mouseX, y = mouseY;
            int[] viewPort = new int[4];
            IntBuffer selectBuffer = Buffers.newDirectIntBuffer(buffsize);
            int hits = 0;

            gl.glGetIntegerv(GL2.GL_VIEWPORT, viewPort, 0);
            gl.glSelectBuffer(buffsize, selectBuffer);
            gl.glRenderMode(GL2.GL_SELECT);

            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glPushMatrix();
            gl.glLoadIdentity();

            glu.gluPickMatrix(x, (double) viewPort[3] - y, 2.0d, 2.0d, viewPort, 0);

            //draw graph
            gl.glPushMatrix();
            gl.glMultMatrixf(rot_matrix, 0);
            buildPoints(gl);
            buildAxes(gl);
            gl.glPopMatrix();

            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glPopMatrix();
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glFlush();

            hits = gl.glRenderMode(GL2.GL_RENDER);
            processHits(hits, selectBuffer);
            cmd = UPDATE;
            break;
    }
}

所以我猜可能the drawing graph部分选择不正确。以下是buildPoints函数的代码。

public void buildPoints(GL2 gl) {
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glPointSize((float) radius / 2);

    int pointName = 0;
    gl.glBegin(GL.GL_POINTS);
    for (point p : pointsList) {
        if(cmd == SELECT) gl.glLoadName(pointName);
        gl.glPushMatrix();
        gl.glTranslatef(p.getX(), p.getY(), p.getZ());
        gl.glColor3f(0.95f, 0.207f, 0.031f);
        gl.glVertex3f((float) (p.getX() * scaleFactor),
                (float) (p.getY() * scaleFactor),
                (float) (p.getZ() * scaleFactor));
        gl.glPopMatrix();
        pointName++;
    }
    gl.glEnd();
}

1 个答案:

答案 0 :(得分:0)

我建议你看看this example of picking using JOGL 2。但是,不推荐使用OpenGL内置选择,不应使用它。我们在官方论坛上对此进行了很多讨论,特别是在this thread