The previous article tells the basics of getting started with OpenGL (i), this time main learning OpenGL Texture Basic Learning Summary
If you do a complex OpenGL application, you will definitely use texture technology. The texture is plainly a picture or video image drawn into OpenGL space.
So textures also have coordinate systems, called St coordinates, or UV
Above is the texture coordinate space, but there is no fixed direction
The following demo loads an image as a texture map.
public class Textureutils {public static int createtexture (InputStream ins) {int[] textures = new INT[1]; Gles20.glgentextures (1, textures, 0);//generate a texture int textureid = textures[0]; Gles20.glbindtexture (gles20.gl_texture_2d, Textureid); Gles20.gltexparameterf (gles20.gl_texture_2d, gles20.gl_texture_min_filter,gles20.gl_nearest); Gles20.gltexparameterf (gles20.gl_texture_2d,gles20.gl_texture_mag_filter,gles20.gl_linear); Gles20.gltexparameterf (gles20.gl_texture_2d, Gles20.gl_texture_wrap_s,gles20.gl_clamp_to_edge); Gles20.gltexparameterf (gles20.gl_texture_2d, Gles20.gl_texture_wrap_t,gles20.gl_clamp_to_edge);//above is the sampling method of texture map, Including stretching method, taking the adjacent value and linear value bitmap bitmap = bitmapfactory.decodestream (INS); Glutils.teximage2d (gles20.gl_texture_2d, 0, bitmap, 0);//allow images and textures to be associated and loaded into OpenGL space Log.d ("OpenGL", "Bitmap:" + bitmap); Bitmap.recycle ();//No need, can release return Textureid;}}
public class Myrenderer implements Renderer {public static float[] Projmatrix = new float[16];//project public static float[] V Iewmatrix = new float[16];//camera public static float[] mviewpjmatrix;//Total transformation matrix public static float[] Matrixs = new FLOAT[16];p ublic static int textureid =-1; Context context; Mydrawmodel Drawmodel;public Myrenderer (context context) {This.context = context;} @Overridepublic void Ondrawframe (GL10 arg0) {gles20.glclear (Gles20.gl_depth_buffer_bit | Gles20.gl_color_buffer_bit); LOG.E ("", "Textureid:" + Textureid);d rawmodel.drawframe (Textureid);} @Overridepublic void onsurfacechanged (GL10 arg0, int w, int h) {gles20.glviewport (0, 0, W, h); float ratio = (float) w/h; Matrix.frustumm (Projmatrix, 0,-ratio, ratio,-1, 1, 1, 10);//projection Matrix settings Matrix.setlookatm (viewmatrix, 0, 0, 0, 3, 0, 0, 0, 0.0 F, 1.0f, 0.0f);//Camera coordinate settings} @Overridepublic void Onsurfacecreated (GL10 g, EGLConfig EGLConfig) {Gles20.glclearcolor (0.5f, 0.5f,0.5f, 1.0f); Gles20.glenable (gles20.gl_depth_test); InputStream ins = nulL;drawmodel = new Mydrawmodel ();d rawmodel.init (), try {ins = Context.getassets (). Open ("house.jpg"); Textureid = Textureutils.createtexture (INS); LOG.E ("", "Textureid:" + Textureid);} catch (IOException e) {e.printstacktrace ();} finally {try {ins.close ();} catch (IOException e) {e.printstacktrace ()}} Gles20.gldisable (Gles20.gl_cull_face);}}
public class Mydrawmodel {private int programid;private int mvpmatrixhandle;//Total transformation matrix reference idprivate int positionhandle;//Vertex Position Idprivate int texcoorhandle; Vertex texture coordinates idprivate floatbuffer vertexbuffer;private floatbuffer texcoorbuffer;public Mydrawmodel () {}public void init () {initdata (); int vertexsharder = Glhelper.compilescript (GLES20.GL_VERTEX_SHADER,GLSCRIPT.VERTEX2); int Fragmentsharder = Glhelper.compilescript (gles20.gl_fragment_shader,glscript.fragment2);p Rogramid = Glhelper.linkattach (Vertexsharder, Fragmentsharder); Boolean IsOK = Glhelper.checkprogram (programid);p Ositionhandle = Gles20.glgetattriblocation (ProgramID, "aposition"); texcoorhandle = Gles20.glgetattriblocation (ProgramId, " Atexcoor "); mvpmatrixhandle = Gles20.glgetuniformlocation (ProgramID," Umvpmatrix "); LOG.D ("OPENGL", "Positionhandle:" + Positionhandle + "; Texcoorhandle:" + Texcoorhandle + "; Mvpmatrixhandle:" + Mvpmatrixhandle + ";" + IsOK);} private void InitData () {//x,y,z, vertex of painting float vertices[] = new float[] {0, 0, 0, -1.8f, -1f, 0, 1.8f, -1f, 0, 1.8f, 1f, 0, -1.8f, 1f, 0, -1.8f, -1f, 0}; Bytebuffer vb = Bytebuffer.allocatedirect (Vertices.length * 4); Vb.order (Byteorder.nativeorder ()); VertexBuffer = Vb.asfloatbuffer (); vertexbuffer.put (vertices); vertexbuffer.position (0);//Texture Space coordinate s,tfloat texcoor[] = new float[] { 0.5f, 0.5f,0f, 1f,1f, 1f,1f, 0f,0f, 0f,0f, 1f}; Bytebuffer cb = Bytebuffer.allocatedirect (Texcoor.length * 4); Cb.order (Byteorder.nativeorder ()); TexCoorBuffer = Cb.asfloatbuffer (); Texcoorbuffer.put (Texcoor); texcoorbuffer.position (0);} public void Drawframe (int textureid) {gles20.gluseprogram (ProgramID); Initialize matrix Matrix.setrotatem (myrenderer.matrixs, 0, 0, 1, 0, 0); Matrix.translatem (myrenderer.matrixs, 0, 0, 0, 1); Matrix conversion, projection matrix, camera matrix, model matrix Myrenderer.mviewpjmatrix = new FLOAT[16]; Matrix.multiplymm (Myrenderer.mviewpjmatrix, 0, myrenderer.viewmatrix,0, Myrenderer.matrixs, 0); Matrix.multiplymm (Myrenderer.mviewpjmatrix, 0, myrenderer.projmatrix,0, Myrenderer.mviewpjmatrix, 0); GLES20.GLUNIFORMMATRIX4FV (Mvpmatrixhandle, 1, False, Myrenderer.mviewpjmatrix, 0); Gles20.glvertexattribpointer (Positionhandle, 3, Gles20.gl_float, FALSE, 3 * 4, vertexbuffer); Gles20.glvertexattribpointer (Texcoorhandle, 2, Gles20.gl_float, FALSE, 2 * 4, Texcoorbuffer); Gles20.glenablevertexattribarray (Positionhandle); Gles20.glenablevertexattribarray (Texcoorhandle); Gles20.glactivetexture (GLES20.GL_TEXTURE0); Gles20.glbindtexture (gles20.gl_texture_2d, Textureid); Gles20.gldrawarrays (Gles20.gl_triangle_fan, 0, 6);//Six-point, Draw triangle}}
OpenGL needs to have the coordinates of the device normalized to [ -1,-1] space, so here is the theory of matrix multiplication, including world coordinates, object coordinates, camera coordinates conversion, which will be described in detail later.
public class Glscript {public Glscript () {}public static final String vertex1 = "attribute vec4 mposition;\n" + "Void main () \ n" + "{\ n" + "gl_position=mposition;\n" + "}\n"; public static final String fragment1 = "Precision mediump float;\n" + "Uniform vec4 mcolor;\n" + "Void Main () {gl_fragcolor=mcolor;\n}"; public static final String vertex2 = "Uniform mat4 umvpmatrix;\n" + "attribute vec3 aposition;\n" + "attribute Vec 2 atexcoor;\n "+" varying vec2 vtexturecoord;\n "+" void Main () {\ n "+" gl_position = Umvpmatrix * VEC4 (APos ition,1) \ n "+" Vtexturecoord = atexcoor;\n "+"}\n "; public static final String Fragment2 = "Precision mediump float;\n" + "varying vec2 vtexturecoord;\n" + "Uniform sam pler2d stexture;\n "+" void Main () {\ n "+" vec2 coord = vtexturecoord;\n "+" Coord.s = Coord.s * 0.5;\n "//actually is To the half of the image, the vector shrinks + "Gl_fragcoLor = Texture2d (stexture, coord); \ n "+"}\n ";}
Coord.s = Coord.s * 0.5;
This is half the texture image, which is displayed on the interface, which is the first half of the picture.
Other tool classes are the same as in the previous article.
Content display
Original:
Android OpenGL ES Application (ii) Textures