首先抱歉我的英语不好,我不是本地人,应该在学校工作更多:S
那些日子我正试图在iOS上使用OpengL ES构建AR系统。
我使用AVCaptureSession检索相机帧,然后将其发送到跟踪算法和我的OpenGL ES显示类(等待跟踪算法的旋转和平移)。与此同时,我正在阅读一个视频,显示在一个OpenGL平面上,放置在与真实平面相同的位置是场景(我不知道我是否清楚)。
我的目标是只使用一个VBO和一个着色器来渲染整个事物(从背景中的相机拍摄的图像,并使用我的跟踪算法给出的旋转和平移来定位视频纹理)。
我不知道为什么但没有显示任何内容,这里是渲染代码(称为每帧):
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
[self setProjectionMatrix];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
//Wait for rotation and translation
@synchronized(self.trackeriOS){
transfo = [trackeriOS getMat];
}
[self dumpMatrix:transfo];
[self fillModelViewMatrix:transfo];
glUniformMatrix4fv(_modelViewUniform, 1, 0, finalModelView);
videoTexture = [self setupTextureFromBuffer:self.buffer];
if (videoTexture == -1) {
NSLog(@"swap");
}
if (cameraVideoTexture == -1) {
NSLog(@"swap");
}
else {
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, videoTexture);
glUniform1i(_textureUniform, 0);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
glUniformMatrix4fv(_projectionUniform, 1, 0, modelViewIdentity);
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelViewIdentity);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, cameraVideoTexture);
glUniform1i(_cameraVideotextureUniform, 1);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
CVBufferRelease(self.buffer);
glDeleteTextures(1, &cameraVideoTexture);
glDeleteTextures(1,&videoTexture);
}
非常感谢你的帮助。
编辑2:这是我在读取摄像机视频数据时从AVFoundation委托函数给出的CVImageBufferRef创建OpenGL纹理的方法:
- (void)processNewCameraFrame:(CVImageBufferRef)cameraFrame;
{
dispatch_async(backgroundImageDisplay, ^(void) {
self.cameraVideoBuffer = cameraFrame;
CVPixelBufferLockBaseAddress(cameraFrame, 0);
int bufferHeight = CVPixelBufferGetHeight(cameraFrame);
int bufferWidth = CVPixelBufferGetWidth(cameraFrame);
glGenTextures(1, &cameraVideoTexture);
glBindTexture(GL_TEXTURE_2D, cameraVideoTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));
CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
CMTime outputItemTime = [videoOutput itemTimeForHostTime:CACurrentMediaTime()];
if ([[self videoOutput] hasNewPixelBufferForItemTime:outputItemTime2]) {
self.buffer = [[self videoOutput] copyPixelBufferForItemTime:outputItemTime2 itemTimeForDisplay:NULL];
//The above render function
[self render];
}
});
}
编辑:我忘了说我像这样设置我的VBO:
- (void)setupVBOs {
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
NSLog(@"OpenGL View, set up VBO 1");
}
我设置了这样的着色器的入口点:
-(void)setTextureProgramShader {
_positionSlot = glGetAttribLocation(programHandle1, "Position");
glEnableVertexAttribArray(_positionSlot);
_projectionUniform = glGetUniformLocation(programHandle1, "Projection");
_modelViewUniform = glGetUniformLocation(programHandle1, "Modelview");
_texCoordSlot = glGetAttribLocation(programHandle1, "TexCoordIn");
glEnableVertexAttribArray(_texCoordSlot);
_textureUniform = glGetUniformLocation(programHandle1, "Texture");
}
最后,顶点着色器是:
attribute vec4 Position; // 1
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
void main(void) { // 4
gl_Position = Projection * Modelview * Position; // 6
TexCoordOut = TexCoordIn;
}