0

I have read a lot of tutorials about using color coding to achieve 3D Object picking on iOS. But I'm not sure how to do it. Anyone who can get me a demo written by Objective-C .

The related issues just like this:

OpenGL ES 2.0 Object Picking on iOS (Using Color Coding)

many thanks. luo

Community
  • 1
  • 1
luo
  • 1
  • 1
  • You'll have to be more specific... What part aren't you sure about? The theory is simple: draw your objects with a unique per-object constant output color, keeping track of the mapping from each color to the object drawn (in GLES 2 this would be a trivial fragment shader). Use glReadPixels to get the result; convert the color back to the object using your mapping. So what isn't clear to you? – heinrichj Mar 31 '14 at 20:44
  • @heinrichj maybe I need the sample code to know how to achieve this function at code-level – luo Apr 01 '14 at 05:53
  • possible duplicate of [OpenGL ES 2.0 Object Picking on iOS (Using Color Coding)](http://stackoverflow.com/questions/10312607/opengl-es-2-0-object-picking-on-ios-using-color-coding) – user1095108 Apr 02 '14 at 09:14
  • @luo can u share some code sample? – hbk Jul 15 '16 at 06:34

2 Answers2

1

I have achieved picking object in OpenGL ES scene using snapshot. Here is the key code:

-(GLKVector4)pickAtX:(GLuint)x Y:(GLuint)y {
    GLKView *glkView = (GLKView*)[self view];
    UIImage *snapshot = [glkView snapshot];
    GLKVector4 objColor = [snapshot pickPixelAtX:x Y:y];
    return objColor;
}

And, then in your tapGesture method, you just need to add this:

const CGPoint loc = [recognizer locationInView:[self view]];
GLKVector4 objColor = [self pickAtX:loc.x Y:loc.y];

if (GLKVector4AllEqualToVector4(objColor, GLKVector4Make(1.0f, 0.0f, 0.0f, 1.0f)))
{
   //do something.......
}

of course, you should add this :

@implementation UIImage (NDBExtensions)

- (GLKVector4)pickPixelAtX:(NSUInteger)x Y:(NSUInteger)y {

    CGImageRef cgImage = [self CGImage];
    size_t width = CGImageGetWidth(cgImage);
    size_t height = CGImageGetHeight(cgImage);

    if ((x > width) || (y > height))
    {
        GLKVector4 baseColor = GLKVector4Make(0.0f, 0.0f, 0.0f, 1.0f);
        return baseColor;
    }

    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
    CFDataRef bitmapData = CGDataProviderCopyData(provider);
    const UInt8* data = CFDataGetBytePtr(bitmapData);
    size_t offset = ((width * y) + x) * 4;
    //UInt8 b = data[offset+0];
    float b = data[offset+0];
    float g = data[offset+1];
    float r = data[offset+2];
    float a = data[offset+3];
    CFRelease(bitmapData);
    NSLog(@"R:%f G:%f B:%f A:%f", r, g, b, a);
    GLKVector4 objColor = GLKVector4Make(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
    return objColor;
}

it is useful to achieve object picking in OpenGL ES scene.

Pang
  • 9,564
  • 146
  • 81
  • 122
罗大柚
  • 260
  • 3
  • 10
-3

I had been achieve 3D Object picking use color coding, if anybody want the demo, please call me and tell me your e-mail.

luo
  • 1
  • 1