在有正方形顶部的翻译立方体有录影饲料作为纹理

时间:2014-08-21 11:22:04

标签: ios iphone ipad opengl-es augmented-reality

我正在尝试开发一个POC,它有助于在相机Feed上显示3D对象。我拥有的那种3D对象很容易使用this项目进行渲染。我指的是Apple的Camera Ripple代码,用于显示相机输入。这两个都是相同上下文中的单独对象。其中每个都使用自己的着色器程序。我很困惑如何从一个程序切换到另一个程序。

我的glkview:drawInRect:方法看起来像这样

    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(_program);
if (_ripple)
{
    glDrawElements(GL_TRIANGLE_STRIP, [_ripple getIndexCount], GL_UNSIGNED_SHORT, 0);
}

glUseProgram(_program1);

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set View Matrices
[self updateViewMatrices];
glUniformMatrix4fv(_uniforms.uProjectionMatrix, 1, 0, _projectionMatrix1.m);
glUniformMatrix4fv(_uniforms.uModelViewMatrix, 1, 0, _modelViewMatrix1.m);
glUniformMatrix3fv(_uniforms.uNormalMatrix, 1, 0, _normalMatrix1.m);

// Attach Texture
glUniform1i(_uniforms.uTexture, 0);

// Set View Mode
glUniform1i(_uniforms.uMode, self.viewMode.selectedSegmentIndex);

// Enable Attributes
glEnableVertexAttribArray(_attributes.aVertex);
glEnableVertexAttribArray(_attributes.aNormal);
glEnableVertexAttribArray(_attributes.aTexture);

// Load OBJ Data
glVertexAttribPointer(_attributes.aVertex, 3, GL_FLOAT, GL_FALSE, 0, cubeOBJVerts);
glVertexAttribPointer(_attributes.aNormal, 3, GL_FLOAT, GL_FALSE, 0, cubeOBJNormals);
glVertexAttribPointer(_attributes.aTexture, 2, GL_FLOAT, GL_FALSE, 0, cubeOBJTexCoords);

// Load MTL Data
for(int i=0; i<cubeMTLNumMaterials; i++)
{
    glUniform3f(_uniforms.uAmbient, cubeMTLAmbient[i][0], cubeMTLAmbient[i][1], cubeMTLAmbient[i][2]);
    glUniform3f(_uniforms.uDiffuse, cubeMTLDiffuse[i][0], cubeMTLDiffuse[i][1], cubeMTLDiffuse[i][2]);
    glUniform3f(_uniforms.uSpecular, cubeMTLSpecular[i][0], cubeMTLSpecular[i][1], cubeMTLSpecular[i][2]);
    glUniform1f(_uniforms.uExponent, cubeMTLExponent[i]);

    // Draw scene by material group
    glDrawArrays(GL_TRIANGLES, cubeMTLFirst[i], cubeMTLCount[i]);
}

// Disable Attributes
glDisableVertexAttribArray(_attributes.aVertex);
glDisableVertexAttribArray(_attributes.aNormal);
glDisableVertexAttribArray(_attributes.aTexture);
}

这会导致此错误gpus_ReturnGuiltyForHardwareRestart

导致崩溃

1 个答案:

答案 0 :(得分:0)

我发现我的问题的解决方案是重置使用这两个程序之间的所有内容。现在我的glkview:drawInRect:如下所示,

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(_program);
if (_ripple)
{
    glDrawElements(GL_TRIANGLE_STRIP, [_ripple getIndexCount], GL_UNSIGNED_SHORT, 0);
    [self resetProgrameOne];
}


glUseProgram(_program1);
glClear(GL_DEPTH_BUFFER_BIT);

// Set View Matrices
[self updateViewMatrices];
glUniformMatrix4fv(_uniforms.uProjectionMatrix, 1, 0, _projectionMatrix1.m);
glUniformMatrix4fv(_uniforms.uModelViewMatrix, 1, 0, _modelViewMatrix1.m);
glUniformMatrix3fv(_uniforms.uNormalMatrix, 1, 0, _normalMatrix1.m);


// Attach Texture
glUniform1i(_uniforms.uTexture, 0);

// Set View Mode
glUniform1i(_uniforms.uMode, 1);

// Enable Attributes
glEnableVertexAttribArray(_attributes.aVertex);
glEnableVertexAttribArray(_attributes.aNormal);
glEnableVertexAttribArray(_attributes.aTexture);

// Load OBJ Data
glVertexAttribPointer(_attributes.aVertex, 3, GL_FLOAT, GL_FALSE, 0, table1OBJVerts);
glVertexAttribPointer(_attributes.aNormal, 3, GL_FLOAT, GL_FALSE, 0, table1OBJNormals);
glVertexAttribPointer(_attributes.aTexture, 2, GL_FLOAT, GL_FALSE, 0, table1OBJTexCoords);

// Load MTL Data
for(int i=0; i<table1MTLNumMaterials; i++)
{
    glUniform3f(_uniforms.uAmbient, table1MTLAmbient[i][0], table1MTLAmbient[i][1], table1MTLAmbient[i][2]);
    glUniform3f(_uniforms.uDiffuse, table1MTLDiffuse[i][0], table1MTLDiffuse[i][1], table1MTLDiffuse[i][2]);
    glUniform3f(_uniforms.uSpecular, table1MTLSpecular[i][0], table1MTLSpecular[i][1], table1MTLSpecular[i][2]);
    glUniform1f(_uniforms.uExponent, table1MTLExponent[i]);

    // Draw scene by material group
    glDrawArrays(GL_TRIANGLES, table1MTLFirst[i], table1MTLCount[i]);
}

// Disable Attributes
glDisableVertexAttribArray(_attributes.aVertex);
glDisableVertexAttribArray(_attributes.aNormal);
glDisableVertexAttribArray(_attributes.aTexture);
}

resetProgrameOne方法通过删除缓冲区并禁用glDisableVertexAttribArrays来重置所有缓冲区和必要的东西。

相关问题