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)
但是从上面给出的链接中看到实际代码,真正看到了这个例子.
这是我可以制作的最简单的版本:
设置(在您为顶点数组完成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有关的任何内容.有一些很好的源版本是半适用的.那里的论坛也有很好的信息来源.
| 归档时间: |
|
| 查看次数: |
31754 次 |
| 最近记录: |