【发布时间】:2018-09-18 16:10:44
【问题描述】:
我可以在屏幕上获得距离点吗? 这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (!context || ![EAGLContext setCurrentContext:context]) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
glEnable(GL_DEPTH_TEST);
shader = [[BaseEffect alloc] initWithVertexShader:@"Shader.vsh"
fragmentShader:@"Shader.fsh"];
// setup projection and model matrix
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepthf(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[shader prepareToDraw];
// draw point
glBindVertexArray(vertexArray);
glDrawArrays(GL_POINTS, 0, pointsCount);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint tapLoc = [touch locationInView:self.view];
GLfloat depth = 0;
glReadPixels(tapLoc.x, tapLoc.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
NSLog(@"Depth %f", depth);
}
一切都画得很完美,但我无法确定画点的距离。
编辑:
我记录深度,也尝试颜色。仅使用此参数读取颜色:
glReadPixels(p.x, p.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);
使用 GL_FLOAT 时,无法读取 rgb。 我也尝试在绘制后获取值:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
// preparing and drawing
if (testDepthPoint.x != 0) {
[self testDepth:testDepthPoint];
testDepthPoint = CGPointZero;
}
}
- (void)testDepth:(CGPoint)p {
GLfloat depth = -99.0;
glReadPixels(p.x, p.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
NSLog(@"Depth %f", depth); // always log -99.0
}
不适合我
【问题讨论】: