【问题标题】:opengl es 2.0 object picking on ios using color coding使用颜色编码在 ios 上拾取 opengl es 2.0 对象
【发布时间】:2014-05-10 13:41:09
【问题描述】:

我已经阅读了很多关于在 iOS 上使用颜色编码实现 3D 对象拾取的教程。但我不知道该怎么做。谁能给我一个由 Objective-C 编写的演示。

相关问题是这样的:

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

非常感谢。 罗

【问题讨论】:

  • 您必须更具体...您不确定哪一部分?理论很简单:使用唯一的每个对象常量输出颜色绘制对象,跟踪从每种颜色到绘制对象的映射(在 GLES 2 中,这将是一个微不足道的片段着色器)。使用 glReadPixels 得到结果;使用您的映射将颜色转换回对象。那么你有什么不清楚的地方?
  • @heinrichj 也许我需要示例代码来了解如何在代码级别实现此功能
  • @luo 你能分享一些代码示例吗?

标签: opengl-es color-coding


【解决方案1】:

我已经使用快照在 OpenGL ES 场景中拾取对象。这是关键代码:

-(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;
}

然后,在您的 tapGesture 方法中,您只需添加以下内容:

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.......
}

当然,你应该添加这个:

@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;
}

在 OpenGL ES 场景中实现对象拾取很有用。

【讨论】:

    【解决方案2】:

    我已经实现了使用颜色编码的 3D 对象拾取,如果有人想要演示,请给我打电话并告诉我你的电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多