Android 3d series texture

Source: Internet
Author: User

By ho minggui (http://blog.csdn.net/hmg25)
Reprinted please indicate the source

The previous examples are solid 3D models. Today, we began to attach the gorgeous textures to these models to make them look closer to the real objects we remember.

Function startup

To use textures, we need to turn on some OpenGL switches to start some of the features we need:

    gl.glEnable(GL10.GL_TEXTURE_2D);

. This call is indispensable. If you do not enable this function, you cannot map the image to a polygon. It can be opened and closed as needed, usually during initialization.

Create texture

Generate texture

The texture in OpenGL is referenced by a unique number and implemented by the glBindTexture () function. You can specify this unique number by yourself, or call the glGenTextures () function to generate a unique number.

Int [] tmp_tex = new int [1]; // although there is only one texture, an array of one element is used.
// GlGenTextures (Application count, storage array, offset value)
Gl. glGenTextures (1, tmp_tex, 0); // apply to the system for available, used to indicate the texture ID
Int texture = tmp_tex [0];

Texture binding

After a name is generated for the texture, we mustBindTexture. Binding makes the specified texture active. Only one texture can be activated at a time. The active or "Bound" texture is the texture used when the polygon is drawn. It is also the texture on which the new texture data will be loaded. Therefore, the texture must be bound before providing the image data.

Gl. glBindTexture (GL10.GL _ TEXTURE_2D, texture );

Bind texture data and pass in the specified Image
GLUtils. texImage2D (GL10.GL _ TEXTURE_2D, 0, bmp, 0 );

Texture restrictions

The width and height of the image used for texture must be the Multiplication Side, such as 2, 4, 8, 16, 32, 64,128,256,512, or 1024. The sample image may be 64x128 or 512x512.

Texture coordinates

When texture ing is started, you must provide other data for OpenGL ES, that is,Texture coordinates. Texture coordinates define which part of the image will be mapped to a polygon. It works in a strange way. It is inconsistent with the vertex direction. assume that you have a square or rectangular texture, and the lower left corner of the texture is the origin of a two-dimensional plane. The unit of height and width is one. Like this:

 

This is our "texture coordinate system", not usedXAndYTo represent two-dimensional space, we useSAndTAs the texture coordinate axis, but in principle it is the same.

BesidesSAndTOutside the axis, the mapped texture has two axes in the polygon, which are calledUAndVAxis. This is derived from many 3D image programs.UV ing.

 

Well, we understand the texture coordinate system. Now we will discuss how to use these texture coordinates. When we specify the vertex in the vertex array, we need to provide texture coordinates in another array, which is calledTexture coordinate Array. For each vertex, we use float to specify the position of the vertex in the coordinate system shown in. Let's take a look at the simplest example, which maps the entire image to a square composed of triangle bars. First, we create a vertex array composed of four vertices:

 

Now the two blocks are stacked together, and the values of the used coordinate array become obvious:

Convert it to a coordinate array:

    float texCoords[] = new float[]{        0.0f, 1.0f,        1.0f, 1.0f,        0.0f, 0.0f,        1.0f, 0.0f    };
Texture coordinates correspond to the position and direction of the texture image on the object. We also need to pass the texture coordinates to the system gl. glTexCoordPointer (2, GL10.GL _ FLOAT, 0, texBuff); gl. glableclientstate (GL10.GL _ TEXTURE_COORD_ARRAY); // enable the texture coordinate array. The effect of our instance today: code: 
Public class CubeRenderer implements Renderer {Bitmap bmp; float box [] = new float [] {// FRONT-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // BACK-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f,-0.5f,-0.5f, 0.5f, 0.5f, 0.5f, -0.5f, // LEFT-0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f,-0.5f, // RIGHT 0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // TOP-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f,-0.5f, // BOTTOM-0.5f, -0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f ,}; float lightAmbient [] = new float [] {0.5f, 0.5f, 0.6f, 1.0f}; // environmental light float lightDiffuse [] = new float [] {0.6f, 0.6f, 0.6f, 1.0f}; // float [] lightPos = new float [] {0, 0, 3, 1}; // Light Source Position/** for illumination, you must inform the System of the model you have defined. The direction of each plane is used to calculate the light and shade. The direction description is the */float norms [] = new float [] {// method Vector Array described by vector points, describes the direction of each vertex. // FRONT0f, 0f, 1f, // The direction is (0, 0) to (0, 0, 1) that is, the Z axis is in the positive direction of 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, // BACK0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, // LEFT-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f, // RIGHT1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, // TOP0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, // BOTTOM0f ,- 1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f }; float texCoords [] = new float [] {// array of texture coordinates // FRONT 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // BACK 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // LEFT 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // RIGHT 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // TOP 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // BOTTOM 1.0f, 0.0f, 1.0f, 1.0f, 0. 0f, 0.0f, 0.0f, 1.0f}; FloatBuffer cubeBuff; FloatBuffer normBuff; FloatBuffer texBuff; float xrot = 0.0f; float yrot = 0.0f; /*** convert the float array and store it in the byte buffer array * @ param arr * @ return */public FloatBuffer makeFloatBuffer (float [] arr) {ByteBuffer bb = ByteBuffer. allocateDirect (arr. length * 4); // allocate the buffer space. A float occupies 4 byte bb. order (ByteOrder. nativeOrder (); // you can specify ByteOrder. nativeOrder () is to obtain the local byte order FloatBuffer fb = bb. as FloatBuffer (); // convert to float type fb. put (arr); // Add data to fb. position (0); // set the initial position of the array return fb;} public CubeRenderer (Context c) {// TODO Auto-generated constructor stubcubeBuff = makeFloatBuffer (box ); // convert the float array normBuff = makeFloatBuffer (norms); texBuff = makeFloatBuffer (texCoords); bmp = BitmapFactory. decodeResource (c. getResources (), R. drawable. face);} protected void init (GL10 gl) {gl. glClearColor (0.0f, 0.0f, 0.0) F, 1.0f); // set the background color, R, G, B, And Agl. glable (GL10.GL _ LIGHTING); // enables illumination gl. glable (GL10.GL _ LIGHT0); // enable the light source 0 // set the lighting parameters. You can also use the default one without setting gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ AMBIENT, lightAmbient, 0); gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ DIFFUSE, lightDiffuse, 0); gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ POSITION, lightPos, 0); gl. glNormalPointer (GL10.GL _ FLOAT, 0, normBuff); gl. glableclientstate (GL10.GL _ NORMAL_ARRAY );/ /Use the texture: // 1. enable texture gl. glable (GL10.GL _ TEXTURE_2D); // 2. generate texture IDint [] tmp_tex = new int [1]; // although there is only one texture, use an array of one element // glGenTextures (quantity applied, storage array, offset value) gl. glGenTextures (1, tmp_tex, 0); // apply to the system for available IDint texture, used to mark the texture = tmp_tex [0]; // 3. binds the texture to make the specified texture active. Only one texture gl can be activated at a time. glBindTexture (GL10.GL _ TEXTURE_2D, texture); // 4. bind the texture data and input the specified image GLUtils. texImage2D (GL10.GL _ TEXTURE_2D, 0, bmp, 0); // 5. transmits the texture coordinates of each vertex in gl. glTexCoordPointer (2, GL10.GL _ FLOAT, 0, texBuff); gl. glableclientstate (GL10.GL _ TEXTURE_COORD_ARRAY); // enable the texture coordinate array // 6. set the texture parameters (optional)/* the following two lines of parameters tell OpenGL that when displaying an image, it is larger than the original enlarged texture * (GL_TEXTURE_MAG_FILTER) or it is smaller than the original texture (GL_TEXTURE_MIN_FILTER) *. OpenGL uses the filtering method. I usually use GL_LINEAR in both cases. This allows the texture to be smoothly displayed from very far * to very close to the screen. Using GL_LINEAR requires more operations on the CPU and video card. If your machine is slow, * you may need to use GL_NEAREST. When the filtered texture is enlarged, it looks like mosaic. You can also combine these two filtering methods. Use GL_LINEAR at the nearest point, and GL_NEAREST at the distance. **/Gl. glTexParameterx (GL10.GL _ TEXTURE_2D, GL10.GL _ TEXTURE_MAG_FILTER, GL10.GL _ LINEAR); gl. glTexParameterx (GL10.GL _ TEXTURE_2D, GL10.GL _ TEXTURE_MIN_FILTER, GL10.GL _ LINEAR); gl. glable (GL10.GL _ DEPTH_TEST); // enable the deep cache gl. glable (GL10.GL _ CULL_FACE); // enable back-side cropping gl. glClearDepthf (1.0f); // sets the deep cache value gl. glDepthFunc (GL10.GL _ LEQUAL); // sets the depth cache comparison function. GL_LEQUAL indicates that the depth cache value of the new Pixel is smaller than or equal to the depth cache value of the current pixel (through gl. glClearDepthf (1.0f) is used to test gl in depth. glShadeModel (GL10.GL _ SMOOTH); // sets the shadow mode GL_SMOOTH} @ Overridepublic void onSurfaceCreated (GL10 gl, EGLConfig config) {// TODO Auto-generated method stubinit (gl );} @ Overridepublic void onSurfaceChanged (GL10 gl, int w, int h) {// TODO Auto-generated method stubgl. glViewport (0, 0, w, h); // set the Windows gl. glMatrixMode (GL10.GL _ PROJECTION); // sets the PROJECTION matrix gl. glLoadIdentity (); // sets the matrix as the unit matrix, which is equivalent to Resetting the matrix. gluPerspective (gl, 45.0f, (float) w)/h, 0.1f, 10f); // sets the perspective range} @ Overridepublic void onDrawFrame (GL10 gl) {// TODO Auto-generated method stubgl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT); // clear the screen and depth cache gl. glMatrixMode (GL10.GL _ MODELVIEW); // switch to the model observation matrix gl. glLoadIdentity (); // reset the current model observation matrix (GLU. gluLookAt (gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); // set the viewpoint and model center position gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, cubeBuff); // sets the vertex data gl. glableclientstate (GL10.GL _ VERTEX_ARRAY); gl. glRotatef (xrot, 1, 0, 0); // rotate gl around (0, 0) and (1, 0, 0) on the X axis. glRotatef (yrot, 0, 1, 0); gl. glColor4f (1.0f, 0, 0, 1.0f); // set the color and red gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 0, 4); // draw the positive FRONT gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 4, 4); gl. glColor4f (0, 1.0f, 0, 1.0f); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 8, 4); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 12, 4); gl. glColor4f (0, 0, 1.0f, 1.0f); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 16, 4); gl. glDrawArrays (GL10.GL _ TRIANGLE_STRIP, 20, 4); xrot + = 0.5f; yrot + = 0.5f ;}}
Source Code address: http://download.csdn.net/source/3567174

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.