【发布时间】:2010-08-14 08:25:20
【问题描述】:
我正在使用 Xcode 3.2.4 开发一个 iPhone 项目,我想使用 OpenGL ES 2.0 显示纹理。我以前没有太多使用 OpenGL 的经验,但到目前为止它似乎并不太糟糕。到目前为止,我主要参考的是The OpenGL ES 2.0 Programming Guide(金书)。
现在我有一个基于 Xcode 的默认 OpenGL 模板的项目,将 drawFrame 函数替换为我的纹理显示代码,该代码又基于金书上的 Simple_Texture2D 项目。但是,我应该在其上绘制纹理的正方形不显示它;它是黑色的,我不确定为什么会这样。
这里是drawFrame的内容:
[(EAGLView *)self.view setFramebuffer];
GLuint textureID;
static const GLfloat squareVertices[] =
{
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f
};
// Generate a 2 x 2 rgb image at 3 bytes per pixel.
GLubyte image[4 * 3] =
{
255, 0, 0,
0, 255, 0,
0, 0, 255,
255, 255, 0
};
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLushort indices[] = {0, 1, 2, 3, 2, 1};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
[(EAGLView *)self.view presentFramebuffer];
也许对于有经验的 OpenGL 用户来说,我做错了什么会很明显。否则,任何建议、猜测或任何形式的建设性 cmet 也都很棒。
提前致谢!
【问题讨论】:
标签: iphone objective-c xcode opengl-es textures