Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top