OpenGL-floating flag (Java)

Source: Internet
Author: User

Original content. For more information, see EOE forum. Statement: I am studying the book "Secrets of Android Application Development" by Yang rich. Therefore, the tutorial code and most of the content are from the Code published by instructor Yang on the Internet. C/C ++ part can see the nehe OpenGL Tutorial: http://yarin.blog.51cto.com/1130898/380974

 

Slice

 

1. Local vertices: We cut the image from X and Y axes into 44 parts in average, that is, there are 45 Split points. The points that constitute a small square are local vertices.

2. Local Texture: Because vertices are local, that is to say, texture (texture) each small square of a local vertex ). The unit of the texture is also an integer "1". Therefore, each small square indicates the part 1/44 of the texture.

3. trigonometric function: Because the trigonometric function "Sine" or "cosine" fluctuates on the coordinate axis, we lay the picture on the plane of X and Y axes, the Z axis represents the fluctuation axis of "Sine" or "cosine. Then we will see the fluctuating effect.
Main Code:

 

Package COM. geolo. android; <br/>/* <br/> * Most of the code comments are from Yang Rich's Android Application Development secrets <br/> **/<br/> Import Java. NIO. floatbuffer; <br/> Import Java. NIO. intbuffer; <br/> Import javax. microedition. khronos. EGL. eglconfig; <br/> Import javax. microedition. khronos. opengles. gl10; <br/> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. openGL. Glsurfaceview. renderer; <br/> Import android. openGL. glu; <br/> Import android. openGL. glutils; <br/> public class myrenderer implements Renderer {</P> <p> private float vertex [] [] [] [] = new float [45] [45] [3]; // local vertex array (45, 45, 3) <br/> private float hold; // temporary variable of local vertex data <br/> private float anglex, angley = 0; <br/> public float getangley () {<br/> return angley; <br/>}< br/> Public void setangley (float angley) {<Br/> This. angley = angley; <br/>}< br/> private bitmap mbitmap = NULL; <br/> private int textrueid = 0; <br/> private floatbuffer texcoord = floatbuffer. allocate (8); <br/> private floatbuffer points = floatbuffer. allocate (12); <br/> int wigle_count = 0; // specify the speed of the flat wave <br/> Public myrenderer (context) {// constructor, image used to initialize texture <br/> mbitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. IMG); <br/>}< br/> @ override <br/> Public void onsurfacecreated (gl10 GL, eglconfig config) {<br/> GL. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ nicest); // indicates that the system needs to correct the perspective. <br/> GL. glclearcolor (0, 0, 0, 1); // sets the color of the cleanup screen. <br/> GL. glable (gl10.gl _ depth_test); // enable deep cache <br/> GL. glcleardepthf (10.0f); // sets the deep test <br/> GL. gldepthfunc (gl10.gl _ lequal); // type of the deep test (We will render it when it is less than or equal to) <br/> setuptext Ure (GL); // load the texture <br/> initdata (); // initialize local vertex data <br/>}< br/> @ override <br/> Public void onsurfacechanged (gl10 GL, int width, int height) {<br/> float radio = (float) width/height; <br/> GL. glviewport (0, 0, width, height); // you can specify the size of the image in OpenGL. <br/> GL. glmatrixmode (gl10.gl _ projection); // sets the projection matrix to Perspective Projection. <br/> GL. glloadidentity (); // reset the projection matrix (unit matrix) <br/> GL. glfrustumf (-Radio, Radio,-1, 1, 1, 30); // create a pivot Projection Matrix (Set the preview size) <br/>}< br/> @ override <br/> Public void ondrawframe (gl10 GL) {<br/> // first clear the screen <br/> GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); <br/> // sets the Model View matrix <br/> GL. glmatrixmode (gl10.gl _ modelview); <br/> // reset the matrix <br/> GL. glloadidentity (); <br/> // Viewpoint Change <br/> Glu. glulookat (GL, 0, 0, 3, 0, 0, 0, 0, 1, 0); <br/> // draw the flag <br/> drawflag (GL ); <br/>}< br/>/** load texture */<br/> private Vo Id setuptexture (gl10 GL) {<br/> GL. glable (gl10.gl _ texture_2d); // enable the 2D texture map <br/> intbuffer = intbuffer. allocate (1); // create a buffer in int [] units to store the name ID of the texture <br/> GL. glgentextures (1, intbuffer); // creates a texture and writes the texture id value to the buffer zone. -- I don't know what I understand, right .... <Br/> textrueid = intbuffer. get (); // Save the ID name <br/> // bind the Texture Based on the texture id <br/> GL. glbindtexture (gl10.gl _ texture_2d, textrueid); <br/> // generate a texture or load a texture <br/> glutils. teximage2d (gl10.gl _ texture_2d, 0, mbitmap, 0); <br/> // you can set the image filter. <br/> GL. gltexparameterx (gl10.gl _ texture_2d, gl10.gl _ texture_min_filter, gl10.gl _ linear_mipmap_nearest); <br/> GL. gltexparameterx (gl10.gl _ texture_2d, gl10.gl _ texture_mag_filter, Gl10.gl _ linear); <br/> // bitmap can be cleared because the texture has been loaded. <Br/> mbitmap. recycle (); <br/> mbitmap = NULL; <br/>}< br/>/** initialize vertex data */<br/> private void initdata () {<br/> // loops along the X plane <br/> for (INT x = 0; x <45; X ++) {<br/> // loops along the Y plane <br/> for (INT y = 0; y <45; y ++) {<br/> // Add a wave effect to the surface <br/> vertex [x] [y] [0] = (float) x/5.0f)-4.5f; // x <br/> vertex [x] [y] [1] = (float) y/5.0f)-4.5f ); // y <br/> vertex [x] [y] [2] = (float) (math. sin (float) x/5.0f) * 40366f)/360.0f) * 3.141592654 * 2.0f )); // z <br/>}< br/> private void drawflag (gl10 GL) {<br/> int X, Y; // cyclic variable <br/> float float_x, float_y, float_xb, float_yb; // This variable is used to split the flat wave into a very small quadrilateral <br/> GL. gltranslatef (0.0f, 0.0f,-12.0f); // translation operation <br/> // rotation operation <br/> GL. glrotatef (anglex, 1.0f, 0.0f, 0.0f); <br/> GL. glrotatef (angley, 0.0f, 1.0f, 0.0f); <br/> // sets the vertex array and texture coordinate array. <br/> GL. glableclientstate (gl10.gl _ vertex_array); <br/> GL. glableclientstate (gl10.gl _ texture_coord_array); <br/> // sets the vertex array and texture coordinate array <br/> GL. glvertexpointer (3, gl10.gl _ float, 0, points); <br/> GL. gltexcoordpointer (2, gl10.gl _ float, 0, texcoord); <br/> GL. glbindtexture (gl10.gl _ texture_2d, textrueid); // bind the texture <br/> for (x = 0; x <44; X ++) {<br/> for (y = 0; y <44; y ++) {<br/> float_x = (float) (X)/44.0f; // generate X floating point value <br/> float_y = (float) (y)/44.0f; // generate y floating point value <br/> float_xb = (float) (x + 1)/44.0f; // X floating point value + 0.0227f <br/> float_yb = (float) (Y + 1)/44.0f; // y floating point value + 0.0227f <br/> // Save the texture coordinate array <br/> texcoord. clear (); <br/> texcoord. put (float_x); <br/> texcoord. put (float_y); <br/> texcoord. put (float_x); <br/> texcoord. put (float_yb); <br/> texcoord. put (float_xb); <br/> texcoord. put (float_yb); <br/> texcoord. put (float_xb); <br/> texcoord. put (float_y); <br/> // Save the vertex array <br/> points. clear (); <br/> points. put (vertex [x] [y] [0]); <br/> points. put (vertex [x] [y] [1]); <br/> points. put (vertex [x] [y] [2]); <br/> points. put (vertex [x] [Y + 1] [0]); <br/> points. put (vertex [x] [Y + 1] [1]); <br/> points. put (vertex [x] [Y + 1] [2]); <br/> points. put (vertex [x + 1] [Y + 1] [0]); <br/> points. put (vertex [x + 1] [Y + 1] [1]); <br/> points. put (vertex [x + 1] [Y + 1] [2]); <br/> points. put (vertex [x + 1] [y] [0]); <br/> points. put (vertex [x + 1] [y] [1]); <br/> points. put (vertex [x + 1] [y] [2]); <br/> // draw <br/> GL. gldrawarrays (gl10.gl _ triangle_fan, 0, 4 ); <br/>}< br/> // disable vertex array and texture coordinate array <br/> GL. gldisableclientstate (gl10.gl _ texture_coord_array); <br/> GL. gldisableclientstate (gl10.gl _ vertex_array); </P> <p> // used to reduce the wave speed (once every two frames) <br/> If (wigle_count = 2) {<br/> // loops along the Y plane <br/> for (y = 0; y <45; y ++) {<br/> // store the current left-side wave value <br/> hold = vertex [0] [y] [2]; <br/> // loops along the X plane <br/> for (x = 0; x <44; X ++) {<br/> // The current wave value is equal to the wave value on the right <br/> vertex [x] [y] [2] = vertex [x + 1] [Y] [2]; <br/>}< br/> // The current value is the leftmost wave value. <br/> vertex [44] [y] [2] = hold; <br/>}< br/> // the counter is cleared. <br/> wigggle_count = 0; <br/>}< br/> wigggle_count ++; <br/>}< br/> public float getanglex () {<br/> return anglex; <br/>}< br/> Public void setanglex (float anglex) {<br/> This. anglex = anglex; <br/>}< br/>} 

 

--> Analyze the main parts of the Code:

 

For (INT x = 0; x <45; X ++) {<br/> for (INT y = 0; y <45; y ++) {<br/> // Add a wave effect to the surface <br/> points [x] [y] [0] = float (x/5.0f)-4.5f ); <br/> points [x] [y] [1] = float (y/5.0f)-4.5f ); <br/> points [x] [y] [2] = float (sin (x/5.0f) * 40366f)/360.0f) * 3.141592654 * 2.0f )); <br/>}< br/>} 

 

The above two loops initialize the points on the grid. The integer loop can be used to eliminate the appearance of pulse sawtooth caused by floating point operation rounding. Divide the X and Y variables by 5, and then subtract 4.5. In this way, our waves can be "centered" (TRANSLATOR: in this way, the calculation result falls between [-4.5, 4.5 ). (Nehe document translation)
Click [x] [y] [2]. The final value is the result of an sine function compute. The sin () function requires a radian variable. Multiply float_x by 40366f to obtain the angle value. Divide it by 360.0f, multiply it by PI, and multiply it by 2 to convert it to radian. (Nehe document translation)

 

For (y = 0; y <45; y ++) {<br/> // store the current left-side wave value <br/> hold = vertex [0] [y] [2]; <br/> // loops along the X plane <br/> for (x = 0; x <44; X ++) {<br/> // The current wave value is equal to the wave value on the right <br/> vertex [x] [y] [2] = vertex [x + 1] [Y] [2]; <br/>}< br/> // The current value is the leftmost wave value. <br/> vertex [44] [y] [2] = hold; <br/>} 

 

Each time we save the local coordinates of the current small square, we will pass the coordinates to the next small square-the effect of particle fluctuations in physics. For example, rope skipping.
If you have any questions, I will add some content.

 

 

:

 

Source code: http://www.eoeandroid.com/thread-66326-1-1.html

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.