Android Color Picking Not Getting Correct Colors
I implemented a simple color picker for my application, that is working correctly except the objects are being drawn with not there exact color id. So color IDs of 22,0,0, 23,0,0, 24,0,0 might be picked up by the glReadPixles as 22,0,0. I also tried disable dithering, but not sure if there is another gl setting I have to disable or enable to get the objects to draw as there exact color id.
if(picked)
{
GLES10.glClear(GLES10.GL_COLOR_BUFFER_BIT | GLES10.GL_DEPTH_BUFFER_BIT);
GLES10.glDisab开发者_如何学编程le(GLES10.GL_TEXTURE_2D);
GLES10.glDisable(GLES10.GL_LIGHTING);
GLES10.glDisable(GLES10.GL_FOG);
GLES10.glPushMatrix();
Camera.Draw(gl);
for(Actor actor : ActorManager.actors)
{
actor.Picking();
}
ByteBuffer pixels = ByteBuffer.allocate(4);
GLES10.glReadPixels(x, (int)_height - y, 1, 1, GLES10.GL_RGBA, GLES10.GL_UNSIGNED_BYTE, pixels);
for(Actor actor : ActorManager.actors)
{
if(actor._colorID[0] == (pixels.get(0) & 0xff) && actor._colorID[1] == (pixels.get(1) & 0xff) && actor._colorID[2] == (pixels.get(2) & 0xff))
{
actor._location.y += -1;
}
}
GLES10.glPopMatrix();
picked = false;
}
public void Picking()
{
GLES10.glPushMatrix();
GLES10.glTranslatef(_location.x, _location.y, _location.z);
GLES10.glVertexPointer(3, GLES10.GL_FLOAT, 0, _vertexBuffer);
GLES10.glColor4f((_colorID[0] & 0xff)/255.0f,( _colorID[1] & 0xff)/255.0f, (_colorID[2] & 0xff)/255.0f, 1.0f);
GLES10.glDrawElements(GLES10.GL_TRIANGLES, _numIndicies,
GLES10.GL_UNSIGNED_SHORT, _indexBuffer);
GLES10.glPopMatrix();
}
Looks like android doesn't default to 8888 color mode so this was causing the problem of the colors not drawing correctly for me. By setting up the correct color mode in the activity, I solved the problem for now as long as the device supports it. I will probably have to find a way later to draw to another buffer or texture later if I want my program to support more devices, but this will work for now.
_graphicsView.getHolder().setFormat(PixelFormat.RGBA_8888);
精彩评论