我在OpenGL项目中使用GLKit。一切都基于GLKView和GLKBaseEffect(无自定义着色器)。在我的项目中,我有几个GLKViews显示3D对象的视图,偶尔有几个视图可以一次“打开”(即处于模态视图堆栈中)。GLKBaseEffect不加载纹理(纹理在对象上出现黑色)
虽然到现在为止所有工作都很好,在创建新视图时我需要一个带纹理的矩形来模拟我的应用的3D世界的卷尺。由于某些未知的原因,仅在该视图中,纹理不会直接加载到opengl上下文中:纹理由GLKTextureLoader正确加载,但在绘制矩形为黑色并在调试中查看OpenGL框架时,可以看到一个空的纹理被加载(有一个对纹理的引用,但它全部归零或为空)。
形状我画的定义为:(它本来是一个三角形带,但我切换为三角形,以确保它不是问题)
static const GLfloat initTape[] = {
-TAPE_WIDTH/2.0f, 0, 0,
TAPE_WIDTH/2.0f, 0, 0,
-TAPE_WIDTH/2.0f, TAPE_INIT_LENGTH, 0,
TAPE_WIDTH/2.0f, 0, 0,
TAPE_WIDTH/2.0f, TAPE_INIT_LENGTH, 0,
-TAPE_WIDTH/2.0f, TAPE_INIT_LENGTH, 0,
};
static const GLfloat initTapeTex[] = {
0, 0,
1, 0,
0, 1.0,
1, 0,
1, 1,
0, 1,
};
我设置的影响变量:
effect.transform.modelviewMatrix = modelview;
effect.light0.enabled = GL_FALSE;
// Projection setup
GLfloat ratio = self.view.bounds.size.width/self.view.bounds.size.height;
GLKMatrix4 projection = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(self.fov), ratio, 0.1f, 1000.0f);
effect.transform.projectionMatrix = projection;
// Set the color of the wireframe.
if (tapeTex == nil) {
NSError* error;
tapeTex = [GLKTextureLoader textureWithContentsOfFile:[[[NSBundle mainBundle] URLForResource:@"ruler_texture" withExtension:@"png"] path] options:nil error:&error];
}
effect.texture2d0.enabled = GL_TRUE;
effect.texture2d0.target = GLKTextureTarget2D;
effect.texture2d0.envMode = GLKTextureEnvModeReplace;
effect.texture2d0.name = tapeTex.name;
而渲染循环是:
[effect prepareToDraw];
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribPosition, COORDS, GL_FLOAT, GL_FALSE, 0, tapeVerts);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, tapeTexCoord);
glDrawArrays(GL_TRIANGLES, 0, TAPE_VERTS);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
我还测试了本身的质感在肛与其他对象的视图,它工作正常,所以它不是纹理文件错误。
任何帮助将不胜感激,因为我在这个问题上停留了3天以上。
更新:另外,渲染循环中没有glErrors。
还有什么我可以添加到我的问题,以帮助人们回答它? –