iOS-----Opengl--opengl ES iOS Entry 3---> Texture mapping (texture)

Source: Internet
Author: User

In this article, we'll learn how to use texture mapping in OpenGL.

Textures in Pengl can be divided into 1d,2d and 3D textures, and we need to specify the kinds of textures when binding texture objects. Since this article will take a picture as an example, we bind a gl_texture_2d texture to our texture object.

This article will be divided into two parts, part of how to get a 2D texture from a picture, and how to use a texture in the other part.

In the previous article, we introduced how to use the shader to draw a polygon, this article is based on the improvement of the previous article, we will continue to use the shader, for shader use of immature children's shoes can look at a piece.

First, let's take a look at how to get a 2D texture from a picture.

 + (Gluint) Createtexturewithimage: (UIImage *) image{
//Convert to Cgimage, get picture basic parameters Cgimageref Cgimageref = [Image Cgima GE]; Gluint width = (gluint) cgimagegetwidth (CGIMAGEREF); Gluint height = (gluint) cgimagegetheight (CGIMAGEREF); CGRect rect = CGRectMake (0, 0, width, height);
//Draw picture Cgcolorspaceref ColorSpace = Cgcolorspacecreatedevicergb (); void *imagedata = malloc (Width * height * 4); Cgcontextref context = Cgbitmapcontextcreate (imageData, width, height, 8, Width * 4, colorspace, kcgimagealphapremultipli Edlast | Kcgbitmap byteorder32big); CGCONTEXTTRANSLATECTM (context, 0, height); CGCONTEXTSCALECTM (context, 1.0f, -1.0f); Cgcolorspacerelease (ColorSpace); Cgcontextclearrect (context, rect); Cgcontextdrawimage (context, rect, cgimageref);
Texture some settings, dispensable    gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_edge);    Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_edge);    Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_linear);    Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear);
  
Creating textures
Glenable (gl_texture_2d);



Glteximage2d (gl_texture_2d, 0, Gl_rgba, width, height, 0, Gl_rgba, Gl_unsigned_byte, ImageData);
Bind texture Position glbindtexture (gl_texture_2d, 0);
Free Memory cgcontextrelease (context); Free (imageData); return Textureid;}

Once we get the textures, we're going to start using textures.

As with the previous process of drawing polygons, we will first pass the parameters into the shader. We only need to pass in the position of each vertex when we draw the polygon, but in order to use the texture, we need to pass the texture to shader,

Also, pass in the range of textures used (which is mapped using which part of the texture).

The Vertex shader code is as follows:

Attribute vec4 position;attribute vec2 texturecoords;varying vec2 texturecoordsout;void Main (void) {
The polygon vertex used to represent the texture gl_position = Position;
The vertex that represents the extent of the texture used, because it is a 2D texture, so use the VEC2 type texturecoordsout = texturecoords;}

The Fragment shader code is as follows:

Precision Mediump float;uniform sampler2d texture;varying vec2 texturecoordsout;void Main (void) {
Gets the texture of the pixel vec4 mask = texture2d (Texture, texturecoordsout);
Gl_fragcolor = VEC4 (Mask.rgb, 1.0);}

Attention:

The attribute property can only be passed in through vertex Shader and passed to fragment Shader, and uniform properties are passed directly to fragment Shader.

Similarly, as with the process of drawing polygons, we are compiling shader to generate a glprogram. The difference is that this time we're going to pass in a shader program with three parameters

Gluint Fragmentshader = [self compileshader:@ "mtshaderfragment"                                       Withtype:gl_fragment_shader];    _glprogram = Glcreateprogram ();    Glattachshader (_glprogram, vertexshader);    Glattachshader (_glprogram, fragmentshader);    Gllinkprogram (_glprogram);    Gluseprogram (_glprogram);        _positionslot = Glgetattriblocation (_glprogram, "Position");
The parameters of the uniform type are obtained in different ways _textureslot = Glgetuniformlocation (_glprogram, "Texture"); _texturecoordsslot = Glgetattriblocation (_glprogram, "texturecoords");

The next step is to "paste" the texture onto the polygon.

First the texture is passed in, activating the texture indexed to 1. Indicates that the next operation is for Textures 1

Glactivetexture (Gl_texture1);
Load Texture glbindtexture (gl_texture_2d, _textureid);
Specifies the value of the uniform variable for the current program object, and Parameter 1 represents the new value used (Gl_texture1) gluniform1i (_textureslot, 1);

Draws a texture in the specified area.

Note: The coordinate representation used to represent the texture range differs from the Uikit and OpenGL coordinates, ranging from (0,0) left down to (top) right.

Texture Usage Range vertex
Const Glfloat texcoords[] = { 0, 0,//lower left 1, 0,//lower right 0, 1,//left Upper 1, 1,//upper right }; Glvertexattribpointer (_texturecoordsslot, 2, Gl_float, Gl_false, 0, texcoords); Glenablevertexattribarray (_texturecoordsslot); Drawing area vertex const Glfloat vertices[] = { -1,-1, 0, //left lower 1, -1, 0, //Lower right -1, 1, 0,< c16/>//left 1, 1, 0};//upper Right glvertexattribpointer (_positionslot, 3, gl_float, Gl_false, 0, vertices ); Glenablevertexattribarray (_positionslot); Const Glubyte indices[] = { 0,1,2, N-only }; Gldrawelements (gl_triangles, sizeof (indices)/sizeof (Indices[0]), gl_unsigned_byte, indices); [_context Presentrenderbuffer:gl_renderbuffer];

Next we'll show you how to do some dynamic operations and simple filters to turn the area of your finger into gray.

iOS-----Opengl--opengl ES iOS Entry 3---> Texture mapping (texture)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.