0

首先抱歉我的英语不好,我不是本地人,应该在学校工作更多: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;
}
4

0 回答 0