[SOLVED] glReadPixels

Started by zik, March 29, 2010, 12:59:53

Previous topic - Next topic

zik

Hi,

In an event on a GLCanvas from SWT, I'm trying to find where the mouse look at.

code:

private Listener Event_Mouse_Hover = new Listener(){
  public void handleEvent(Event event) {
       Paint_Zone.setCurrent();
       try {GLContext.useContext(Paint_Zone);} catch(LWJGLException e) { e.printStackTrace(); }
       IntBuffer viewport = BufferUtils.createIntBuffer(16);    
       FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
       FloatBuffer projection = BufferUtils.createFloatBuffer(16);
       int winX, winY;
       FloatBuffer winZ = BufferUtils.createFloatBuffer(1);            
       FloatBuffer pos = BufferUtils.createFloatBuffer(3);//FloatBuffer.allocate(3);
       GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview);
       GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection);
       GL11.glGetInteger( GL11.GL_VIEWPORT, viewport);
       winX = event.x;
       winY = viewport.get(3) - event.y;
       GL11.glReadPixels( winX, winY, 1, 1, GL11.GL_DEPTH_COMPONENT,  GL11.GL_FLOAT, winZ);
       GLU.gluUnProject( winX, winY, winZ.get(0), modelview, projection, viewport, pos);
       console.setText("Coordinates : "
               + String.valueOf(pos.get(0))+ " "
               + String.valueOf(pos.get(1))+ " "
               + String.valueOf(pos.get(2))+ " "
               + String.valueOf(winZ.get(0)));   
  }
};

The problem is this function works perfectly on windows but on linux glReadPixels reports always 0.0 into winZ.

zik

By accident I found the problem.

On linux system, when you are creating the GLCanvas from SWT you have to force the use of a depth buffer.
To do that :

GLData gldata = new GLData();
gldata.doubleBuffer = true;
gldata.depthSize = 1; //Force to use a depth buffer (not needed with windows OS)
//gldata.sampleBuffers = 8;
GLCanvas Paint_Zone = new GLCanvas(parent, SWT.NO_BACKGROUND,gldata );

jacekcichy

Hi! I have similar problem..
I call glReadPixels in my game loop but it always return 1.0f.. I absolutely don't know what is wrong..

   static FloatBuffer modelMatrix = BufferUtils.createFloatBuffer(64);
    static FloatBuffer projMatrix = BufferUtils.createFloatBuffer(64);
    static IntBuffer viewport = BufferUtils.createIntBuffer(16);
    static FloatBuffer curPos = BufferUtils.createFloatBuffer(12);
    static float mouseX;
    static float mouseY;   
    static FloatBuffer mouseZ = BufferUtils.createFloatBuffer(1);



  mouseX = Mouse.getX();
   mouseY = Mouse.getY();
   mouseY = viewport.get(3) - mouseY;
			
			
   if(Keyboard.isKeyDown(Keyboard.KEY_T)){
       GL11.glReadPixels((int)mouseX, (int)mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, mouseZ);
       mouseZ.rewind();
       System.out.println(mouseZ.get(0));			
       mouseZ.clear();			
   }

jacekcichy

I solve my problem ;D I was making stupid mistake cause I was reading DepthBuffer before I was drawing anything on it and after that I was clearing it. So If anybody will have similiar problem You must first execute drawcalls() and after that glReadPixels()

greetz  ;)