Screen Picking help *CHANGED*

Started by white_dot1, February 19, 2007, 20:51:41

Previous topic - Next topic

white_dot1

I apologize if this problem was already answered, or is incredibly simple.  I'm new to lwjgl, but programmed opengl in C++ for a little while now.  I never did screen picking with c++ but I'm trying for the first time with java.  I'm using SWT GLCanvas for drawing, and have the rendering in a seperate thread.  The code supposedly executes, and I get the correct number of hits, but the values in the selection buffer are WAY off.  When I use 0 and -1 for the glLoadName() it works, I get those values back correctly, but everything else I get huge numbers, both positive and negative, and I have no clue why.  Any and all help would be apprecaited!

protected Point pickLocation(int x, int y) {
		y = this.getSize().y - y;
		int[] view = new int[4];
		int buffer[] = new int[256];
		IntBuffer selBuf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer();
	    selBuf.order();
	    GL11.glGetInteger(GL11.GL_VIEWPORT, selBuf);
        selBuf.get(view);
        selBuf = ByteBuffer.allocateDirect(1024).asIntBuffer();
        selBuf.clear();
		GL11.glSelectBuffer(selBuf);
		
        //Set render to select mode for picking
        GL11.glRenderMode(GL11.GL_SELECT);

        GL11.glInitNames();                                              // Initializes The Name Stack
        GL11.glPushName(0);
        
        GL11.glMatrixMode(GL11.GL_PROJECTION);                       // Selects The Projection Matrix
        GL11.glPushMatrix();     									// Push The Projection Matrix
        GL11.glLoadIdentity();
        
        GLU.gluPickMatrix(x, y, rng, rng, view);
        GLU.gluPerspective(45.0f, (float) (view[2] - view[0]) / (float) (view[3] - view[1]), 0.1f, 250.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);                                 // Select The Modelview Matrix
        //GL11.glLoadIdentity();
                        
        drawBoard(0,0);
        
        GL11.glMatrixMode(GL11.GL_PROJECTION);                                // Select The Projection Matrix
        GL11.glPopMatrix();                                              // Pop The Projection Matrix
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glFlush();
        
        //return to normal rendering
        int hits = GL11.glRenderMode(GL11.GL_RENDER); 
        selBuf.get(buffer);
		//selBuf.flip();
        
        for (int z = 0; z < hits * 4; z++) System.out.println(buffer[z]);
		//determine which were selected
		ArrayList<MLocation> temp = new ArrayList<MLocation>(hits);
		for (int z = 0; z < hits; z++) {
			int cur = buffer[z * 4 + 3];
			//int cur = selBuf.get(z * 4 + 3);
			temp.add(new MLocation((cur - (cur % 100)) / 100,cur % 100));
			System.out.println(temp.get(z));
		}
        //System.out.println(x  + ", " + y + " :: " + hits);
        //System.out.println(selBuf);
		return null;
	}


The x, and y passed in are the mouse location on the screen, drawBoard is a method that draws and names the polygons I'm trying to pick.  I pieced much of the code from other forums and the nehe tutorial, but I can't seem to get it to work.  Thanks for the help!