Short intro -> Im a CS student, knowledge of basic OpenGL and java... and in need of some lovely help...im a novice with android so sorry if im making stupid mistakes..
im currently making a free game for one of my projects. For test purposes im outputting aload of 3d triangles. i need to select certain objects and move them in a certain direction. After researching i found it can be quite tricky on android devices to implement a picking technique which selects objects based on where you touch the screen and read the framebuffer.
All i need to know is to know which object(square/triangle etc) im touching. To do this i render all the objects, when a touch event occurs it clears all past rendering and re renders the scene with each objects own special 'picking' method which draws the objects with a unique colour. i use glReadPixels() to find out where im touching and it reads the colour been displayed at that point. To make sure im not reading incorrect colours i have disabled all effects such as textures/fog etc.
The problem im encountering is that glReadPixels always reads 0 0 0 0 which is black???... even when selecting something rendered... im not entirely sure on how to fix this problem and would like some help if possible or perhaps a hint in the right direction thanks
The following code examples are a cut down version of what im currently using and some DDM output:
Using java Syntax Highlighting
- public boolean onTouchEvent(MotionEvent event)
- {
- float x = event.getX();
- float y = event.getY();
- if(event.getAction() == MotionEvent.ACTION_MOVE)
- {
- float dx = x - oldX;
- float dy = y - oldY;
- //Define camera change areas (8% up down left right)
- //Leaves a middle area which can be used to select components
- int upperArea = this.getHeight() / 8;
- int lowerArea = this.getHeight() - upperArea;
- int leftArea = this.getWidth() / 8;
- int rightArea = this.getWidth() - leftArea;
- /***************************/
- /** CAMERA CHANGING AREA **/
- /***************************/
- if(x < leftArea || x > rightArea)
- {
- xrot += dy * TOUCH_SCALE;//changes camera view up or down
- }
- if(y > lowerArea || y < upperArea)
- {
- yrot += dx * TOUCH_SCALE;//changes camera view left or right
- }
- /**
- * Where the magic is meant to happen, touch screen, read colour, check
- */
- CurrentGL.glClearColor(0.0f,0.0f,0.0f,0.0f);
- CurrentGL.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- CurrentGL.glLoadIdentity();
- //This and lighting disabled to only allow rendering of solid colours
- CurrentGL.glDisable(GL10.GL_DITHER);
- CurrentGL.glDisable(GL10.GL_LIGHTING);
- CurrentGL.glDisable(GL10.GL_TEXTURE_2D);
- CurrentGL.glDisable(GL10.GL_FOG);
- CurrentGL.glEnable(GL10.GL_COLOR_MATERIAL);
- CurrentGL.glShadeModel(GL10.GL_FLAT);
- try
- {
- for(int i = 0; i < TriangleObjects.size();i++)
- {
- TriangleObjects.get(i).picking(CurrentGL);//renders the triangles with their unique colours
- }
- }
- catch(Exception e)
- {
- System.out.println("End of list or no Triangle objects in list");
- }
- ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
- PixelBuffer.order(ByteOrder.nativeOrder());
- CurrentGL.glReadPixels((int)x, (int) y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer);
- System.out.println(PixelBuffer.get(0) + " "+ PixelBuffer.get(1)+ " " + PixelBuffer.get(2) + " " + PixelBuffer.get(3));//prints out the RGBA values for testing so far only 0 0 0 0
- }
- }
- oldX = x;
- oldY = y;
- return true;
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- public void onDrawFrame(GL10 gl)
- {
- CurrentGL = gl;//updates global im not sure if you're meant to do this...but allows rendering in touch method
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity();
- gl.glShadeModel(GL10.GL_SMOOTH);
- gl.glTranslatef(0.0f, 0.0f, z);
- gl.glPushMatrix();
- gl.glTranslatef(0.0f, 0.0f, 0.0f);
- gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
- gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
- try
- {
- for(int i = 0; i < TriangleObjects.size();i++)
- {
- TriangleObjects.get(i).draw(gl);
- }
- }
- catch(Exception e)
- {
- System.out.println("End of list or no Triangle objects in list");
- }
- gl.glPopMatrix();
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Inside the Triangle object class:
Using java Syntax Highlighting
- public void picking(GL10 gl)
- {
- gl.glFrontFace(GL10.GL_CW);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glColor4f(uniqueColour[0], uniqueColour[1], uniqueColour[2], uniqueColour[3]);//unique colouring
- gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- 02-27 01:41:48.959: INFO/System.out(1215): 0 0 0 0
- 02-27 01:41:48.989: INFO/System.out(1215): 0 0 0 0
- 02-27 01:41:49.029: INFO/System.out(1215): 0 0 0 0
- 02-27 01:41:49.059: INFO/System.out(1215): 0 0 0 0
- 02-27 01:41:49.099: INFO/System.out(1215): 0 0 0 0
- 02-27 01:41:49.119: INFO/System.out(1215): 0 0 0 0
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
any help would be greatly appreciated! sorry of the code looks choppy..


