假设我有一个定义如下的正方形:
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
};
我正在应用以下投影和Modelview矩阵:
- (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;
}
现在,我想读取用户在屏幕上点击的对象的像素颜色。我正在尝试将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]);
}
谁能建议我如何准确获得像素颜色?
最佳答案
盖伊·古格斯(Guy Cogus)的代码看起来像是正确的方法,但是您应该知道为什么发布的内容行不通。使用GLKUnproject对向量进行非投影的目的是使其脱离屏幕空间并进入对象空间(在通过投影或模型视图矩阵对其进行转换之前,几何的坐标系),而不是屏幕空间,glReadPixels就是其中的对象。 (根据定义,因为您正在读取像素)。
关于ios - 使用glReadPixels读取Tap Point的值? OpenGL 2.0 iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19491085/