GL11.glRenderMode(GL11.GL_SELECT) problem :s !!

I do not know why, but when I try to use GL11.glRenderMode (GL11.GL_SELECT) to manage a viewfinder on the objects of my 3D scenes it gets me this error:

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
	at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
	at org.lwjgl.opengl.GL11.glRenderMode(GL11.java:2573)
	at Renderer.test(Renderer.java:117)
	at Renderer.start(Renderer.java:77)
	at Main.main(Main.java:19)

Line 117 of file Renderer.java corresponds to the line where I call to GL11.glRenderMode (GL11.GL_SELECT)!

Here is the complete code of the function:

public void test(float xcam, float zcam, float ycam)
    {
        GL11.glRenderMode(GL11.GL_SELECT);
        IntBuffer viewport = BufferUtils.createIntBuffer(16);
        FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
        FloatBuffer projection = BufferUtils.createFloatBuffer(16);
        FloatBuffer fDepth = BufferUtils.createFloatBuffer(1);
        FloatBuffer position = BufferUtils.createFloatBuffer(3);
        
        GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
        GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
        GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
        
        GL11.glReadPixels( SCREEN_WIDTH/2, viewport.get(3) - (SCREEN_HEIGHT/2), 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, fDepth);
        GLU.gluUnProject((float)(SCREEN_WIDTH/2), viewport.get(3) - (float)(SCREEN_HEIGHT/2), fDepth.get(), modelview, projection, viewport, position);
    
        float distMaxPick = (float)Math.sqrt(Math.pow(xcam - position.get(0), 2)+Math.pow(ycam - position.get(1), 2)+Math.pow(zcam - position.get(2), 2));
        if (distMaxPick <= 2.5*CUBESIZE)
            tabCube[(int)(position.get(0)/CUBESIZE)][(int)(position.get(2)/CUBESIZE)][(int)(position.get(1)/CUBESIZE)].aimed(basicTexPack, CUBESIZE);
   
        /*
        System.out.println("World coords at z=" + fDepth.get(0) + "are (" +
           (int)(position.get(0)/CUBESIZE) + ", " +
           (int)(position.get(1)/CUBESIZE) + ", " +
           (int)(position.get(2)/CUBESIZE)+
                                            ")");
        */
        GL11.glRenderMode(GL11.GL_RENDER);
    }

I found it :


        IntBuffer selectBuffer = BufferUtils.createIntBuffer(512);
        GL11.glSelectBuffer(selectBuffer);
        GL11.glRenderMode(GL11.GL_SELECT);

But strangely, now in 3D target point through the center of the screen is always in the same manner, 0 to X, 0 to Y and -0 .**** to Z.
(0, 0, -0 .***)
Why?

Problem solved! :wink:

I was calling this function after calling my function to display the HUD, the coup matrices were taking the wrong values​​.

You should know that it is recommended to not use Select mode these days. Generally it is not such a good way to select, rather do it yourself with something like ray casting.

Can you tell me more about this method ? (with the code)

Not really. You can Google. The details (aka source) depends quite strongly on the data structures used. For example i am writing a RTS, and i use a quad tree to speed up raycasting since it is quasi 2d. While if you are doing a cad program you may want to use a kd tree or perhaps a oct-tree.

Raycasting is really ray tracing. I find the ray from the “eye” through the mouse pointer and then find the closest piece of geometry that intersects it.