Ext.: http://blog.csdn.net/junzia/article/details/52842816
In the previous blogs, we used OpenGL ES 2.0 to draw various forms in Android, and specifically spoke the GLSL language in the previous blog post. But the applications and games we see based on OpenGL are not just the regular form and some simple colors, but the irregular forms of people and things that make up the real world or the cartoon world, and they are all wearing beautiful "clothes" outside. This blog is to explain the basis of these "clothes". These clothes are texture maps.
What is a texture map
Generally, a texture is a two-dimensional graphic, also called a texture map (texture), that represents the surface of an object. When the texture is mapped to the surface of the object in a specific way, it makes the object appear more realistic. In the current popular graphics system, texture drawing has become an indispensable rendering method. When you understand texture mapping, you can think of a texture as a pixel color applied to the surface of an object. In the real world, textures represent the color, pattern, and tactile characteristics of an object. The texture represents only the color pattern of the object's surface, and it cannot change the geometry of the object. Further, it's just a high-intensity computational behavior. --Baidu Encyclopedia
For example, when we use OpenGL to play the game, loaded a character model came in, this character model is no color. We need to paint it the color we need. But where do these colors come from? It is not possible for us to create the desired color from the vertex, as we did with the sphere before, which is too much work for us to paint the character model. This time we need to use texture mapping technology-a texture (for 2D mapping, can be easily understood as a picture), in the desired way to display the surface of a number of triangles composed of objects.
Principle of texture mapping
When texture mapping is enabled, if you want to map a texture to the corresponding geometry, you must tell the GPU how to do texture mapping, which is to specify the appropriate texture coordinates for the elements ' vertices. Texture coordinates are represented by floating-point numbers, ranging from 0.0 to 1.0, upper-left coordinates (0.0,0.0), upper-right coordinates (1.0,0.0), lower-left coordinates (0.0,1.0), and lower-right coordinates (1.0,1.0), as shown in:
The left image is the texture and texture coordinates, and the right image is the vertex and vertex coordinates.
Mapping textures to the two triangles on the right (that is, a rectangle), you need to assign texture coordinates to the correct vertices to make the texture display correctly, otherwise the displayed texture will not display, or the appearance of rotation, flipping, dislocation and so on.
When the right vertex is passed in as v2v1v4v3 and drawn in triangular strips, the texture coordinates should be passed in as V2v1v4v3. If you follow v3v4v1v2, you get a texture that rotates 180 degrees. If you follow v4v3v2v1, you get a texture that flips around.
Show pictures
Based on the principle of texture mapping, combined with previous experience in drawing squares, we can use OpenGL ES to display an image based on the following steps:
The first step is to modify the shader
First, we need to modify our shader to modify the vertex shader to:
attribute vec4 vPosition;attribute vec2 vCoordinate;uniform mat4 vMatrix;varying vec2 aCoordinate;void main(){ gl_Position=vMatrix*vPosition; aCoordinate=vCoordinate;}
As you can see, a VEC2 variable is added to the vertex shader, and the variable is passed to the slice shader, which is the texture coordinates. Then we modify the slice shader as:
precision mediump float;uniform sampler2D vTexture;varying vec2 aCoordinate;void main(){ gl_FragColor=texture2D(vTexture,aCoordinate);}
The element shader adds a sampler2d variable, sampler2d we mentioned in the previous blog GLSL Language Foundation, is a sampler of one of the variable types of GLSL. Texture2d also mentions that it is a built-in function of GLSL for 2D texture sampling, based on texture samplers and texture coordinates, to get the pixel color of the current texture sample.
The second step, set vertex coordinates and texture coordinates
Based on the description of the texture mapping principle, we set the vertex coordinates to:
private final float[] sPos={ -1.0f,1.0f, //左上角 -1.0f,-1.0f, //左下角 1.0f,1.0f, //右上角 1.0f,-1.0f //右下角 };
corresponding to the vertex coordinates, we can set the texture coordinates to:
private final float[] sCoord={ 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f, };
The third step is to calculate the transformation matrix
The vertex coordinates and texture coordinates are set in the previous step, and in most cases we get a picture that is pulled up or compressed. To make the picture complete, and not to be stretched and compressed, we need to calculate an appropriate transformation matrix for the isosceles right triangle, passing in the vertex shader with the following code:
@OverridePublicvoidOnsurfacechanged (GL10 GL,int width,int height) {Gles20.glviewport (0,0,width,height);int W=mbitmap.getwidth ();int H=mbitmap.getheight ();Float swh=w/(float) H;Float swidthheight=width/(float) Height;if (width>height) {if (swh>swidthheight) {Matrix.orthom (Mprojectmatrix,0,-SWIDTHHEIGHT*SWH,SWIDTHHEIGHT*SWH,-1,1,3,7); }else{Matrix.orthom (Mprojectmatrix,0,-SWIDTHHEIGHT/SWH,SWIDTHHEIGHT/SWH,-1,1,3,7); } }else{if (swh>swidthheight) {Matrix.orthom (Mprojectmatrix,0,-1,1,-1/SWIDTHHEIGHT*SWH,1/SWIDTHHEIGHT*SWH,3,7); }else{Matrix.orthom (Mprojectmatrix, 0,- 1, 1,-swh/swidthheight, Swh/swidthheight, 3, 7);} } //set camera position MATRIX.SETLOOKATM (Mviewmatrix, 0, 0, 0, 7.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f); //Compute transformation matrix matrix.multiplymm (Mmvpmatrix,0,mProjectMatrix,< Span class= "Hljs-number" >0,mviewmatrix,0);
Mmvpmatrix is the transformation matrix that we need.
Fourth step, show picture
And then what we need to do is just as easy as before to draw a square. Unlike before, we also need to pass in texture and texture coordinates to the shader before drawing:
@OverridePublicvoidOndrawframe (GL10 gl) {gles20.glclear (gles20.gl_color_buffer_bit| Gles20.gl_depth_buffer_bit); Gles20.gluseprogram (Mprogram); Ondrawset (); GLES20.GLUNIFORMMATRIX4FV (Glhmatrix,1,False,mmvpmatrix,0); Gles20.glenablevertexattribarray (glhposition); Gles20.glenablevertexattribarray (glhcoordinate); Gles20.gluniform1i (Glhtexture,0); Textureid=createtexture ();Incoming vertex coordinate gles20.glvertexattribpointer (glhposition,2,gles20.gl_float,False0,bpos);Incoming texture coordinate gles20.glvertexattribpointer (glhcoordinate,2,gles20.gl_float,False0,bcoord); Gles20.gldrawarrays (Gles20.gl_triangle_strip,0,4);}PublicAbstractvoidOndrawset ();PublicAbstractvoidOndrawcreatedset (int mprogram);PrivateIntCreateTexture () {Int[] Texture=Newint[1];if (mbitmap!=Null&&!mbitmap.isrecycled ()) {Generate Texture Gles20.glgentextures (1,texture,0);Generate Texture Gles20.glbindtexture (gles20.gl_texture_2d,texture[0]);Set shrink filter to use the color of the pixel closest to the coordinates in the texture as the pixel color to draw gles20.gltexparameterf (gles20.gl_texture_2d, Gles20.gl_texture_min_filter , gles20.gl_nearest);//Set magnification filter to use a number of colors closest to the coordinates in the texture, get the pixel color to be drawn by the weighted average algorithm gles20.gltexparameterf (gles20.gl_texture_2d,gles20.gl_texture _mag_filter,gles20.gl_linear); //Set the wrapping direction s, and intercept the texture coordinates to [1/2n,1-1/2n]. will lead to never fusion with border Gles20.gltexparameterf (gles20.gl_texture_2d, Gles20.gl_texture_wrap_s,gles20.gl_clamp_to_edge); //Set the surround direction t to intercept the texture coordinates to [1/2n,1-1/2n]. will lead to never fusion with border Gles20.gltexparameterf (gles20.gl_texture_2d, Gles20.gl_texture_wrap_t,gles20.gl_clamp_to_edge); generate a 2D texture glutils.teximage2d (gles20.gl_texture_2d, 0, Mbitmap, 0) based on the parameters specified above ; return texture[0];} return 0;}
So that we can show the picture we need to display, and make sure it's completely centered and not deformed, such as:
Source
All the code is in one project, hosted on GitHub.--android opengles 2.0 Series Blog Demo
Welcome reprint, Reproduced please keep the source of the article. Huguang Wang's Blog [http://blog.csdn.net/junzia/article/details/52842816]
(GO) Use OpenGL display image (vii) display picture of Android opengles2.0--texture map