简体   繁体   English

带有纹理的 OpenGL ES 2.0 渲染

[英]OpenGL ES 2.0 Rendering with a Texture

The iPhone SDK has an example of using ES 2.0 with a set of (Vertex & Fragment) GLSL shaders to render a varying colored box. iPhone SDK 有一个使用 ES 2.0 和一组(顶点和片段)GLSL 着色器来渲染不同颜色的框的示例。 Is there an example out there on how to render a simple texture using this API?是否有关于如何使用此 API 渲染简单纹理的示例? I basically want to take a quad, and draw a texture onto it.我基本上想拿一个四边形,然后在上面画一个纹理。

The old ES 1.1 API's don't work at all anymore, so I'm needing a bit of help getting started.旧的 ES 1.1 API 根本不再起作用,所以我需要一些帮助才能开始。 Most shader references talk mainly about advanced shading topics, but I'm really unsure about how to tell the shader to use the bound texture, and how to reference the UV's.大多数着色器参考主要讨论高级着色主题,但我真的不确定如何告诉着色器使用绑定纹理,以及如何引用 UV。

There's a nice tutorial on this in the web site to go with the book OpenGL ES 2 The examples from the book are all at www.opengles-book.com .网站上有一个很好的教程,可以与OpenGL ES 2一书一起使用。书中的示例都在www.opengles-book.com 上

Chapter 9, Simple_Texture2D does exactly what you want.第 9 章 Simple_Texture2D 正是您想要的。 It sets up a shader that samples a texture, initializes it, and shades the triangles using the texture.它设置了一个着色器,对纹理进行采样、初始化并使用纹理对三角形进行着色。

The shader program is close to:着色器程序接近于:

varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
  gl_FragColor = texture2D(s_texture, v_texCoord);
}

and you set it up thusly:你这样设置:

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);

But see the actual code, from the links I gave above, to really see the example.但是从我上面给出的链接中查看实际代码,才能真正看到示例。

Here's the simplest version I could make that actually worked:这是我可以使它真正起作用的最简单的版本:


Setup (immediately after you've done the glVertexAttribPointer for your vertex arrays)设置(在您为顶点数组完成 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);

Render (last thing before calling glDrawArrays or similar)渲染(调用 glDrawArrays 或类似之前的最后一件事)

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

Texture shader:纹理着色器:

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;
}

Fragment shader:片段着色器:

uniform sampler2D s_texture;
varying mediump vec2 v_textureCoordinate;

void main(void)
{
    gl_FragColor = texture2D( s_texture, v_textureCoordinate );
}

Unfortunately OpenGL ES 2.0 uses the red headed step child version of GLSL, 1.4.不幸的是,OpenGL ES 2.0 使用了 GLSL 1.4 的红头继子版本。 Most of the tutorials people post do not work under this version.人们发布的大多数教程在此版本下不起作用。 All of the helper variables such as ftransform and gl_TexCoord[0] have been removed.所有的辅助变量,如 ftransform 和 gl_TexCoord[0] 都已被删除。 Finding specific ES 2.0 tutorials that go further than just pure basics is difficult.很难找到比纯粹基础知识更深入的特定 ES 2.0 教程。

OpenGL ES 2.0 is a completly programmable pipeline, they have done away with anything fixed function. OpenGL ES 2.0 是一个完全可编程的管道,它们取消了任何固定功能。 If you want to use it you'll have to provide your own matrices to keep track of what used to be the model view and projection matrices.如果你想使用它,你必须提供你自己的矩阵来跟踪过去的模型视图和投影矩阵。

I know you posted a few months ago but if anyone is still looking for information do a search on opengl.org for anything relating to OpenGL 3.0.我知道您几个月前发布过,但如果有人仍在寻找信息,请在 opengl.org 上搜索与 OpenGL 3.0 相关的任何内容。 There were a number of good source releases that are semi applicable.有许多很好的源版本是半适用的。 The forums there are also a very good source of information.那里的论坛也是一个很好的信息来源。

I ported a bunch of the Nehe tutorials over to OpenGLES2.0 - available here . 我把一堆Nehe教程移植到OpenGLES2.0 - 可以在这里找到 There's an example of texture rendering in tutorial 6. 在教程6中有一个纹理渲染的例子。

Have you tried a "normal" OpenGL tutorial like this tutorial from Lighthouse3D or this tutorial from clockworkcoders ?您是否尝试过像Lighthouse3D 的本教程clockworkcoders 的本教程这样的“普通”OpenGL 教程? This should also work for OpenGL ES.这也适用于 OpenGL ES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM