![](/img/trans.png)
[英]Rendering a texture in another class' RenderBuffer/FrameBuffer in OpenGL ES 2.0 for iOS 5
[英]OpenGL ES 2.0 Rendering with a Texture
iPhone SDK 有一个使用 ES 2.0 和一组(顶点和片段)GLSL 着色器来渲染不同颜色的框的示例。 是否有关于如何使用此 API 渲染简单纹理的示例? 我基本上想拿一个四边形,然后在上面画一个纹理。
旧的 ES 1.1 API 根本不再起作用,所以我需要一些帮助才能开始。 大多数着色器参考主要讨论高级着色主题,但我真的不确定如何告诉着色器使用绑定纹理,以及如何引用 UV。
网站上有一个很好的教程,可以与OpenGL ES 2一书一起使用。书中的示例都在www.opengles-book.com 上。
第 9 章 Simple_Texture2D 正是您想要的。 它设置了一个着色器,对纹理进行采样、初始化并使用纹理对三角形进行着色。
着色器程序接近于:
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
gl_FragColor = texture2D(s_texture, v_texCoord);
}
你这样设置:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, userData->textureId);
// Set the sampler texture unit to 0
glUniform1i(userData->samplerLoc, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
但是从我上面给出的链接中查看实际代码,才能真正看到示例。
这是我可以使它真正起作用的最简单的版本:
设置(在您为顶点数组完成 glVertexAttribPointer 之后立即)
GLint program; // your shader-program, pre-filled
...
// AFTER you've created *and set* the EAGLContext
GLKTextureInfo* appleTexture = [GLKTextureLoader
textureWithContentsOfFile:... options:... error:...];
// NB: make sure that the returned texture is not nil!
// if it's nil, you'll get black objects, and need to check
// your path to your texture file
...
// INSIDE your VAO setup (usually "setupGL" in Apple's template),
// assuming you're using VAO,
// i.e. after "glBindVertexArrayOES"
GLint _textureBuffer; // an empty buffer that we'll create and fill
glEnableVertexAttribArray( glGetAttribLocation(program, "a_textureCoordinate") );
glGenBuffers(1, &_textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _textureBuffer);
glBufferData(GL_ARRAY_BUFFER,
self.currentScene.meshNumVertices * sizeof( (*self->sharedMeshTextureCoords) ),
self->sharedMeshTextureCoords, GL_DYNAMIC_DRAW);
glVertexAttribPointer( glGetAttribLocation(program, "a_textureCoordinate"),
2, GL_FLOAT, GL_FALSE, 0, 0);
glActiveTexture(GL_TEXTURE0);
渲染(调用 glDrawArrays 或类似之前的最后一件事)
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, [appleTexture name]);
glUniform1i( glGetUniformLocation( program, "s_texture"), 0); // No idea
纹理着色器:
attribute vec4 position;
attribute vec2 a_textureCoordinate;
varying vec2 v_textureCoordinate;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
void main()
{
v_textureCoordinate = a_textureCoordinate;
gl_Position = modelViewProjectionMatrix * position;
}
片段着色器:
uniform sampler2D s_texture;
varying mediump vec2 v_textureCoordinate;
void main(void)
{
gl_FragColor = texture2D( s_texture, v_textureCoordinate );
}
不幸的是,OpenGL ES 2.0 使用了 GLSL 1.4 的红头继子版本。 人们发布的大多数教程在此版本下不起作用。 所有的辅助变量,如 ftransform 和 gl_TexCoord[0] 都已被删除。 很难找到比纯粹基础知识更深入的特定 ES 2.0 教程。
OpenGL ES 2.0 是一个完全可编程的管道,它们取消了任何固定功能。 如果你想使用它,你必须提供你自己的矩阵来跟踪过去的模型视图和投影矩阵。
我知道您几个月前发布过,但如果有人仍在寻找信息,请在 opengl.org 上搜索与 OpenGL 3.0 相关的任何内容。 有许多很好的源版本是半适用的。 那里的论坛也是一个很好的信息来源。
我把一堆Nehe教程移植到OpenGLES2.0 - 可以在这里找到 。 在教程6中有一个纹理渲染的例子。
您是否尝试过像Lighthouse3D 的本教程或clockworkcoders 的本教程这样的“普通”OpenGL 教程? 这也适用于 OpenGL ES。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.