简体   繁体   English

使用glReadPixels读取Tap Point的值? OpenGL 2.0 iOS

[英]Using glReadPixels to read value at Tap Point ? OpenGL 2.0 iOS

Say I have a square which is defined as follows : 假设我有一个定义如下的正方形:

typedef struct {
float Position[3];
float Color[4];
} Vertex;

const Vertex Vertices[] = {
{{2, 0, 0}, {1, 0, 0, 1}},
{{4, 0, 0}, {1, 0, 0, 1}},
{{4, 2, 0}, {1, 0, 0, 1}},
{{2, 2, 0}, {1, 0, 0, 1}}
};

const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0
};

And I am applying the following projection and modelview matrices : 我正在应用以下投影和模型视图矩阵:

- (void)update {

//Projection matrix.
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(_projectionAngle), aspect, 4.0f, 10.f);
self.effect.transform.projectionMatrix = projectionMatrix;

//Modelview matrix.
modelViewMatrix = GLKMatrix4MakeTranslation(_xTranslation, _yTranslation, -7.0);
self.effect.transform.modelviewMatrix = modelViewMatrix;

}

I now want to read the pixel colour of the object where a user taps on the screen. 我现在想要读取用户在屏幕上点击的对象的像素颜色。 I am trying glkMathUnproject in combination with glReadPixels as follows, however glReadPixels is returning incorrect colour values for the tap point : 我正在尝试将glkMathUnproject与glReadPixels结合使用,但是glReadPixels为点击点返回了错误的颜色值:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint tapLoc = [touch locationInView:self.view];

bool testResult;

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

GLKVector3 nearPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-1024)*-1, 0.0), modelViewMatrix, projectionMatrix, &viewport[0] , &testResult);

GLKVector3 farPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-1024)*-1, 1.0), modelViewMatrix, projectionMatrix, &viewport[0] , &testResult);

farPt = GLKVector3Subtract(farPt, nearPt);

GLubyte pixelColor[4];
glReadPixels(farPt.v[0], farPt.v[1], 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColor[0]);
NSLog(@"pixelColor %u %u %u %u", pixelColor[0],pixelColor[1],pixelColor[2], pixelColor[3]);

}

Can anyone advise how I can get the pixel colour accurately ? 任何人都可以建议我如何准确地获得像素颜色?

Check out cocos2d and see how they're doing the following inspection. 查看cocos2d并查看他们如何进行以下检查。

Here's some code that I'm using to get the tapped pixel colour. 这是我用来获取抽头像素颜色的一些代码。 Hope it helps. 希望能帮助到你。

I've actually changed the code a bit to run on a whole scene, but you can do the same on a specific node. 我实际上已将代码更改为在整个场景上运行,但您可以在特定节点上执行相同操作。 If you do, make sure to translate it to {0,0} with the anchor-point at {0,0} and the identity transform so that it displays properly, then reset it when you're done. 如果这样做,请确保将其转换为{0,0},锚点位于{0,0}并使用标识转换以使其正确显示,然后在完成后重置它。

    CCScene *runningScene = [CCDirector sharedDirector].runningScene;
    CGRect boundingBox = runningScene.boundingBox;
    CCRenderTexture *renderTexture = [[CCRenderTexture alloc] initWithWidth:(int)CGRectGetWidth(boundingBox)
                                                                     height:(int)CGRectGetHeight(boundingBox)
                                                                pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
    [renderTexture begin];

    [runningScene visit];

    // Get the colour of the pixel at the touched point
    CGPoint location = ccp((point.x - CGRectGetMinX(boundingBox)) * CC_CONTENT_SCALE_FACTOR(),
                           (point.y - CGRectGetMinY(boundingBox)) * CC_CONTENT_SCALE_FACTOR());
    UInt8 data[4];
    glReadPixels((GLint)location.x,(GLint)location.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);

    [renderTexture end];

    // data[0] = R
    // data[1] = G
    // data[2] = B
    // data[3] = A

Guy Cogus' code looks like the right approach, but you should know why what you posted isn't working. Guy Cogus的代码看起来是正确的方法,但你应该知道为什么你发布的代码不起作用。 The point of unprojecting the vector using GLKUnproject is to get it out of screen space and into object space (the coordinate system of your geometry before it is transformed by the projection or modelview matrices), not into screen space, which is what glReadPixels is in (by definition, since you are reading pixels). 使用GLKUnproject取消投影向量的要点是将它屏幕空间中移出并进入对象空间(几何体的坐标系在被投影或模型视图矩阵变换之前),而不是进入屏幕空间,这是glReadPixels所在的(根据定义,因为你正在读取像素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM