Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Is there an example out there on how to render a simple texture using this 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. 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.

like image 223
Kalen Avatar asked May 05 '10 07:05

Kalen


People also ask

Is OpenGL ES better than OpenGL?

OpenGL runs on desktops/laptops etc. OpenGLES runs on embedded systems (duh, ES stands for embedded systems). So.. if you have a very good GPU on your embedded system (like a phone) and a very old GPU on your desktop, then OpenGL ES would run faster, as it would be running on a faster GPU.

What is OpenGL es2?

What is OpenGL ES 2.0? Edit. OpenGL for Embedded Systems (OpenGL ES) is a subset of the OpenGL 3D graphics API. It is designed for embedded devices such as mobile phones, PDAs, and video game consoles. Notable platforms supporting OpenGL ES 2.0 include the iPhone 3GS and later, Android 2.2 and later, and WebGL.

Is OpenGL the same as OpenGL ES?

OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware. OpenGL ES is a subset of the OpenGL specification for embedded devices. To be Android compatible, devices need to provide drivers for EGL, OpenGL ES 1.

How do textures work in OpenGL?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.


2 Answers

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.

Chapter 9, Simple_Texture2D does exactly what you want. 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.

like image 116
DavidPhillipOster Avatar answered Sep 28 '22 10:09

DavidPhillipOster


Here's the simplest version I could make that actually worked:


Setup (immediately after you've done the glVertexAttribPointer for your vertex arrays)

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)

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 );
}
like image 23
Adam Avatar answered Sep 28 '22 10:09

Adam