OpenGL ES 2.0渲染纹理

Kyl*_*yle 14 iphone shader opengl-es glsl

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

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

谢谢!

Dav*_*ter 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);
}
Run Code Online (Sandbox Code Playgroud)

然后你就这样设置了:

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);
Run Code Online (Sandbox Code Playgroud)

但是从上面给出的链接中看到实际代码,真正看到了这个例子.

  • "glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);" 任何使用硬编码的书而不是将它们存储在变量中的书(由ogl分配)都是一本书,我会远离它.谁知道他们想要教什么样的其他坏东西. (2认同)

Ada*_*dam 7

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


设置(在您为顶点数组完成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);
Run Code Online (Sandbox Code Playgroud)

渲染(在调用glDrawArrays或类似之前的最后一件事)

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, [appleTexture name]);
glUniform1i( glGetUniformLocation( program, "s_texture"), 0); // No idea
Run Code Online (Sandbox Code Playgroud)

纹理着色器:

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;
}
Run Code Online (Sandbox Code Playgroud)

片段着色器:

uniform sampler2D s_texture;
varying mediump vec2 v_textureCoordinate;

void main(void)
{
    gl_FragColor = texture2D( s_texture, v_textureCoordinate );
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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

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