[英]GLKit, Opengl ES 3.0, get depth with glReadPixels
我可以在屏幕上獲得距離點嗎? 這是代碼:
- (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);
}
一切都完美地繪制,但我無法獲得繪制點的距離。
我記錄深度,也嘗試顏色。 Color 僅讀取此參數:
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
}
不適合我
除了您應該查看depth
而不是pixelColor
的事實pixelColor
,您還需要注意時間,它應該在幀完成繪制之后和下一幀提交之前。
不支持 OpenGL ES 2.0 從緩沖區讀取GL_DEPTH_COMPONENT
。 更多信息在這里:
https://stackoverflow.com/a/18805281/820795
它適用於 Opengl ES 3.0
在 ES 3.0 及更高版本中,支持深度紋理。 使用它,您可以使用以下步驟獲得深度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.