How to pick objects using jogl?

瘦欲@ 提交于 2019-12-12 02:01:30

问题


I met some problems about jogl picking. I need to pick each single point and process it, but I always get 0 hit (nothing is picked). Can anyone help me with this?

the display function can correctly get the 2x2 window around the cursor.

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;
    }
}

so I guess maybe the drawing graph part for picking is not correct. Here is the code of buildPoints function.

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:


I advise you to look at this example of picking using JOGL 2. However, OpenGL build-in picking is deprecated and shouldn't be used. We discussed a lot about it on our official forum, especially in this thread.



来源:https://stackoverflow.com/questions/23907413/how-to-pick-objects-using-jogl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!