After watching 3D for a few days, I can understand some pictures. Now I am very annoyed with this texture, but I am not familiar with it. This example is downloaded from the website. It feels good. Let's take a look at it first, I will discuss with you later.
Package WYF. SJ; import android. app. activity; import android. OS. bundle; import android. widget. compoundbutton; import android. widget. linearlayout; import android. widget. togglebutton; import android. widget. compoundbutton. oncheckedchangelistener; public class sample6_2 extends activity {/** called when the activity is first created. */mysurfaceview mglsurfaceview; @ override public void oncreate (bundle saved Instancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mglsurfaceview = new mysurfaceview (this); mglsurfaceview. requestfocus (); // obtain the focus mglsurfaceview. setfocusableintouchmode (true); // set to touch linearlayout LL = (linearlayout) findviewbyid (R. id. main_liner); LL. addview (mglsurfaceview); togglebutton TB = (togglebutton) This. findviewbyid (R. id. togglebutton01); TB. setoncheckedchangelis Tener (New oncheckedchangelistener () {@ override public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {mglsurfaceview. setsmoothflag (! Mglsurfaceview. issmoothflag ());Package WYF. SJ; import Java. io. ioexception; import Java. io. inputstream; import javax. microedition. khronos. EGL. eglconfig; import javax. microedition. khronos. opengles. gl10; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. openGL. glsurfaceview; import android. openGL. glutils; import android. view. motionevent; public class mysurfaceview extends glsurfaceview {private final float touch_scale_factor = 180.0f/320; // Private scenerenderer mrenderer; // scene Renderer private float mpreviousy; // last touch position y coordinate private float mpreviousx; // last touch position y coordinate private Boolean smoothflag = true; // whether to perform smooth coloring private int lightanglegreen = 0; // current angle of the green light private int lightanglered = 90; // current angle of the red light int textureid; // texture name idpublic mysurfaceview (context) {super (context ); mrenderer = new scenerenderer (); // creates the scenario Renderer setrenderer (mrenderer); // sets the Renderer setrendermode (glsurfaceview. rendermode_continuously); // sets the rendering mode to active rendering} @ override public Boolean ontouchevent (motionevent e) {float y = E. gety (); float x = E. getx (); Switch (E. getaction () {Case motionevent. action_move: Float DY = Y-mpreviousy; // calculate the touch pen y displacement float dx = x-mpreviousx; // calculate the touch pen y displacement mrenderer. ball. manglex + = Dy * touch_scale_factor; // sets the Rotation Angle of mrenderer along the X axis. ball. manglez + = DX * touch_scale_factor; // set the Rotation Angle Along the Z axis requestrender (); // re-painting surface} mpreviousy = y; // record the position of the touch pen mpreviousx = X; // record the touch pen position return true;} public void setsmoothflag (Boolean smoothflag) {This. smoothflag = smoothflag;} public Boolean issmoothflag () {return smoothflag;} private class scenerenderer implements glsurfaceview. renderer {ball; Public scenerenderer () {// enable a thread to automatically rotate the sphere new thread () {public void run () {try {thread. sleep (1000); // rest for MS and draw again} catch (exception e) {e. printstacktrace () ;}while (true) {lightanglegreen + = 5; // turn the green light lightanglered + = 5; // turn the red light requestrender (); // try {thread. sleep (50); // 10 ms rest before re-painting} catch (exception e) {e. printstacktrace ();}}}}. start ();} public void ondrawframe (gl10 GL) {If (smoothflag) {// smooth coloring GL. glshademodel (gl10.gl _ smooth);} else {// do not perform smooth GL coloring. glshademodel (gl10.gl _ flat);} // set the position float lxgreen = (float) (7 * Math. cos (math. toradians (lightanglegreen); float lzgreen = (float) (7 * Math. sin (math. toradians (lightanglegreen); float [] positionparamsgreen = {lxgreen, 0, lzgreen, 1}; // the last 1 indicates using the located light GL. gllightfv (gl10.gl _ light0, gl10.gl _ Position, positionparamsgreen, 0); // set the position float lyred = (float) (7 * Math. cos (math. toradians (lightanglered); float lzred = (float) (7 * Math. sin (math. toradians (lightanglered); float [] positionparamsred = {0, lyred, lzred, 1}; // the last 1 indicates using the located light GL. gllightfv (gl10.gl _ light1, gl10.gl _ Position, positionparamsred, 0); // clear the color cache GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // set the current matrix to the mode matrix GL. glmatrixmode (gl10.gl _ modelview); // sets the current matrix as the unit matrix GL. glloadidentity (); GL. gltranslatef (0, 0f,-1.8f); GL. glpushmatrix (); // protection transformation matrix field ball. drawself (GL); // draw the ball GL. glpopmatrix (); // restore transformation matrix field} public void onsurfacechanged (gl10 GL, int width, int height) {// set the window size and position GL. glviewport (0, 0, width, height); // sets the current matrix as the projection matrix GL. glmatrixmode (gl10.gl _ projection); // sets the current matrix as the unit matrix GL. glloadidentity (); // calculate the ratio of Perspective Projection float ratio = (float) width/height; // call this method to calculate and generate a Perspective Projection Matrix GL. glfrustumf (-ratio, ratio,-1, 1, 1, 10);} public void onsurfacecreated (gl10 GL, eglconfig config) {// disable anti-jitter GL. gldisable (gl10.gl _ dither); // sets the mode of a specific hint project. Here, it is set to use the Quick Mode GL. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest); // sets the screen background color to black rgba GL. glclearcolor (,); // set the color model to smooth color GL. glshademodel (gl10.gl _ smooth); // gl10.gl _ smooth gl10.gl _ flat // enable the deep test GL. glenable (gl10.gl _ depth_test); GL. glable (gl10.gl _ lighting); // enable illumination initgreenlight (GL); // initialize the green light initredlight (GL); // initialize the red light initmaterial (GL ); // initialize the material textureid = inittexture (GL, R. drawable. duke); // initialize texture ball = new ball (4, textureid) ;}}// initialize green light private void initgreenlight (gl10 GL) {GL. glable (gl10.gl _ light0); // turn on lamp 0 // set float [] ambientparams = {0.1f, 0.1f, 0.1f, 1.0f}; // The parameter rgba GL. gllightfv (gl10.gl _ light0, gl10.gl _ ambient, ambientparams, 0); // set float [] diffuseparams = {0f, 1f, 0f, 1.0f} for scattered light }; // The optical parameter rgba GL. gllightfv (gl10.gl _ light0, gl10.gl _ diffuse, diffuseparams, 0); // set float [] specularparams = {1f, 1f, 1f, 1.0f }; // The optical parameter rgba GL. gllightfv (gl10.gl _ light0, gl10.gl _ specular, specularparams, 0);} // initialize the private void initredlight (gl10 GL) {GL. glable (gl10.gl _ light1); // enable Lamp 1 // set float [] ambientparams = {0.2f, 0.2f, 0.2f, 1.0f}; // specify the rgba GL parameter. gllightfv (gl10.gl _ light1, gl10.gl _ ambient, ambientparams, 0); // scatter light setting float [] diffuseparams = {1f, 0f, 0f, 1.0f }; // The optical parameter rgba GL. gllightfv (gl10.gl _ light1, gl10.gl _ diffuse, diffuseparams, 0); // set float [] specularparams = {1f, 1f, 1f, 1.0f }; // The optical parameter rgba GL. gllightfv (gl10.gl _ light1, gl10.gl _ specular, specularparams, 0);} // initialize the material private void initmaterial (gl10 GL) {// when the material is white, what color of light will be displayed on the top? // the ambient light is white float ambientmaterial [] = {1.0f, 1.0f, 1.0f, 1.0f }; GL. glmaterialfv (gl10.gl _ front_and_back, gl10.gl _ ambient, ambientmaterial, 0); // float diffusematerial [] = {1.0f, 1.0f, 1.0f, 1.0f}; GL. glmaterialfv (gl10.gl _ front_and_back, gl10.gl _ diffuse, diffusematerial, 0); // The highlight material is white float specularmaterial [] = {1f, 1f, 1f, 1.0f}; GL. glmaterialfv (gl10.gl _ front_and_back, gl10.gl _ specular, specularmaterial, 0); GL. glmaterialf (gl10.gl _ front_and_back, gl10.gl _ shininess, 1000000f);} public int inittexture (gl10 GL, int textureid) // textureid {int [] textures = new int [1]; GL. glgentextures (1, textures, 0); int currtextureid = textures [0]; GL. glbindtexture (gl10.gl _ texture_2d, currtextureid); GL. gltexparameterf (gl10.gl _ texture_2d, gl10.gl _ texture_min_filter, gl10.gl _ nearest); GL. gltexparameterf (gl10.gl _ texture_2d, gl10.gl _ texture_mag_filter, gl10.gl _ linear); GL. gltexparameterf (gl10.gl _ texture_2d, gl10.gl _ texture_wrap_s, gl10.gl _ repeat); GL. gltexparameterf (gl10.gl _ texture_2d, gl10.gl _ texture_wrap_t, gl10.gl _ repeat); inputstream is = This. getresources (). openrawresource (textureid); bitmap bitmaptmp; try {bitmaptmp = bitmapfactory. decodestream (is);} finally {try {is. close ();} catch (ioexception e) {e. printstacktrace () ;}} glutils. teximage2d (gl10.gl _ texture_2d, 0, bitmaptmp, 0); bitmaptmp. recycle (); Return currtextureid ;}}
Package WYF. SJ; import Java. NIO. bytebuffer; import Java. NIO. byteorder; import Java. NIO. floatbuffer; import Java. NIO. intbuffer; import Java. util. arraylist; import javax. microedition. khronos. opengles. gl10; public class ball {private intbuffer mvertexbuffer; // The Private intbuffer mnormalbuffer for the vertex coordinate data buffer; // The Private floatbuffer mtexturebuffer for the vertex method vector data buffer; // vertex texture data buffer public float manglex; // along the X axis rotation angle public float mangley; // along the Y axis rotation angle public float manglez; // int vcount = 0 along the Z axis rotation angle; // Number of vertices int textureid; // texture ID public ball (INT scale, int textureid) {This. textureid = textureid; Final int unit_size = 10000; // initialize the actual vertex coordinate data ========================== begin ================== ============== arraylist <integer> alvertix = new arraylist <integer> (); // arraylist final int anglespan = 18 for storing vertex coordinates; // angle from which the ball is split in units for (INT vangle =-90; vangle <= 90; vangle = vangle + anglespan) // anglespan degree in the vertical direction {for (INT hangle = 0; hangle <360; hangle = hangle + anglespan) // anglespan degree in the horizontal direction {// calculate the Coordinate Double xozlength = scale * unit_size * math on the spherical surface after the anglespan degree in the vertical direction ranges to an angle. cos (math. toradians (vangle); int x = (INT) (xozlength * Math. cos (math. toradians (hangle); int z = (INT) (xozlength * Math. sin (math. toradians (hangle); int y = (INT) (scale * unit_size * Math. sin (math. toradians (vangle); // Add the calculated XYZ coordinates to the arraylist alvertix. add (x); alvertix. add (y); alvertix. add (z) ;}} vcount = alvertix. size ()/3; // The number of vertices is 1/3 of the number of coordinate values, because a vertex has three coordinates // transfers the coordinate values in alvertix to an int array, int vertices [] = new int [vcount * 3]; for (INT I = 0; I <alvertix. size (); I ++) {vertices [I] = alvertix. get (I );} // initialize the actual vertex coordinate data ============================ end ================== ==================/// triangle construction vertex, texture, method Vector Data initialization ============= begin ==== ================================== alvertix. clear (); arraylist <float> altexture = new arraylist <float> (); // texture int ROW = (180/anglespan) + 1; // number of rows to be split on the Sphere int Col = 360/anglespan; // number of rows to be split on the sphere for (INT I = 0; I <row; I ++) // loop {if (I> 0 & I <row-1) {// The middle row for (Int J =-1; j <Col; j ++) {// The two adjacent points in the middle row and the corresponding points in the next row constitute the triangle int K = I * Col + J; // The alvertix OF THE 1st triangle vertices. add (vertices [(k + col) * 3]); alvertix. add (vertices [(k + col) * 3 + 1]); alvertix. add (vertices [(k + col) * 3 + 2]); altexture. add (0.0f); altexture. add (0.0f); // 2nd triangle vertices alvertix. add (vertices [(k + 1) * 3]); alvertix. add (vertices [(k + 1) * 3 + 1]); alvertix. add (vertices [(k + 1) * 3 + 2]); altexture. add (1.0f); altexture. add (1.0f); // 3rd triangle vertices alvertix. add (vertices [K * 3]); alvertix. add (vertices [K * 3 + 1]); alvertix. add (vertices [K * 3 + 2]); altexture. add (1.0f); altexture. add (0.0f) ;}for (Int J = 0; j <Col + 1; j ++) {// The two adjacent points of the middle row and the corresponding points of the previous row constitute a triangle int K = I * Col + J; // 1st triangle vertices alvertix. add (vertices [(k-col) * 3]); alvertix. add (vertices [(k-col) * 3 + 1]); alvertix. add (vertices [(k-col) * 3 + 2]); altexture. add (1f); altexture. add (1f); // 2nd triangle vertices alvertix. add (vertices [(k-1) * 3]); alvertix. add (vertices [(k-1) * 3 + 1]); alvertix. add (vertices [(k-1) * 3 + 2]); altexture. add (0.0f); altexture. add (0.0f); // 3rd triangle vertices alvertix. add (vertices [K * 3]); alvertix. add (vertices [K * 3 + 1]); alvertix. add (vertices [K * 3 + 2]); altexture. add (0f); altexture. add (1f) ;}} vcount = alvertix. size ()/3; // The number of vertices is 1/3 of the number of coordinate values, because a vertex has three coordinates // transfers the coordinate values in alvertix to an int array vertices = new int [vcount * 3]; for (INT I = 0; I <alvertix. size (); I ++) {vertices [I] = alvertix. get (I) ;}// create a data buffer for drawing vertex data bytebuffer vBB = bytebuffer. allocatedirect (vertices. length * 4); vBB. order (byteorder. nativeorder (); // sets the byte sequence mvertexbuffer = vBB. asintbuffer (); // converts it to an int-Type Buffer mvertexbuffer. put (vertices); // puts the vertex coordinate data in the buffer zone mvertexbuffer. position (0); // set the starting position of the buffer // create the vertex method vector data buffer bytebuffer NBB = bytebuffer. allocatedirect (vertices. length * 4); NBB. order (byteorder. nativeorder (); // sets the byte sequence mnormalbuffer = vBB. asintbuffer (); // converts it to an int buffer mnormalbuffer. put (vertices); // Add the vertex coordinate data mnormalbuffer to the buffer. position (0); // set the starting position of the buffer // create a texture coordinate buffer float texturecoors [] = new float [altexture. size ()]; // vertex texture value array for (INT I = 0; I <altexture. size (); I ++) {texturecoors [I] = altexture. get (I);} bytebuffer CBB = bytebuffer. allocatedirect (texturecoors. length * 4); CBB. order (byteorder. nativeorder (); // sets the byte sequence mtexturebuffer = CBB. asfloatbuffer (); // converts it to an int-Type Buffer mtexturebuffer. put (texturecoors); // Add Vertex coloring data to the buffer zone mtexturebuffer. position (0 ); // set the starting position of the buffer. // triangle construction vertex, texture, and normal vector data initialization ============ end ====== ===========================} public void drawself (gl10 GL) {GL. glrotatef (manglez, 0, 0, 1); // rotate GL along the Z axis. glrotatef (manglex, 1, 0, 0); // rotate GL along the X axis. glrotatef (mangley, 0, 1, 0); // rotate along the Y axis // The vertex array GL is allowed. glableclientstate (gl10.gl _ vertex_array); // specify the vertex coordinate data GL for the paint brush. glvertexpointer (3, // The number of coordinates for each vertex is 3 XYZ gl10.gl _ fixed, // The type of the vertex coordinate value is gl_fixed 0, // The interval between consecutive vertex coordinate data (mvertexbuffer // vertex coordinate data); // The array GL of normal vectors is allowed. glableclientstate (gl10.gl _ normal_array); // specify the vertex algorithm Vector Data GL for the paint brush. glnormalpointer (gl10.gl _ fixed, 0, mnormalbuffer); // enable the texture GL. glable (gl10.gl _ texture_2d); // The texture st coordinate can be used to buffer GL. glableclientstate (gl10.gl _ texture_coord_array); // specify the st coordinate buffer GL for the texture of the paint brush. gltexcoordpointer (2, gl10.gl _ float, 0, mtexturebuffer); // bind the current texture GL. glbindtexture (gl10.gl _ texture_2d, textureid); // draw the image GL. gldrawarrays (gl10.gl _ triangles, // fill in 0 as a triangle, // start point number vcount // Number of vertices );}}
}) ;}@ Override protected void onresume () {super. onresume (); mglsurfaceview. onresume () ;}@ override protected void onpause () {super. onpause (); mglsurfaceview. onpause ();}}