15

iPhone SDK 有一个使用 ES 2.0 和一组(顶点和片段)GLSL 着色器来渲染不同颜色的框的示例。是否有关于如何使用此 API 呈现简单纹理的示例?我基本上想取一个四边形,并在其上绘制纹理。

旧的 ES 1.1 API 不再工作了,所以我需要一些帮助才能开始。大多数着色器参考主要讨论高级着色主题,但我真的不确定如何告诉着色器使用绑定纹理,以及如何引用 UV。

4

4 回答 4

15

网站上有一个很好的教程,与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);

但是从我上面给出的链接中查看实际代码,以真正看到示例。

于 2010-10-20T00:20:18.383 回答
9

这是我可以制作的最简单的版本:


设置(在您为顶点数组完成 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 );
}
于 2012-12-30T03:15:25.280 回答
5

不幸的是,OpenGL ES 2.0 使用 GLSL 的红头步进子版本 1.4。人们发布的大多数教程都不适用于此版本。所有辅助变量,例如 ftransform 和 gl_TexCoord[0] 已被删除。很难找到比纯基础知识更深入的特定 ES 2.0 教程。

OpenGL ES 2.0 是一个完全可编程的管道,它们取消了任何固定功能。如果你想使用它,你必须提供自己的矩阵来跟踪过去的模型视图和投影矩阵。

我知道您在几个月前发布了,但如果有人仍在寻找信息,请在 opengl.org 上搜索与 OpenGL 3.0 相关的任何内容。有一些很好的源代码版本是半适用的。论坛里也有很好的信息来源。

于 2010-08-31T18:06:12.130 回答
0

您是否尝试过像Lighthouse3D 的本教程或clockworkcoders的本教程这样的“普通”OpenGL 教程?这也应该适用于 OpenGL ES。

于 2010-05-08T16:31:10.750 回答