Comprehensive example of Android 3D Game implementation (preliminary)

Source: Internet
Author: User

The effect is a little more harmonious than the code in March, but it still needs to be improved. Now I will send the code.

You can download the full project at http://simdanfeg.download.csdn.net /.

(1) Activity Class

Package SIM. feel; import Java. io. ioexception; import Java. io. inputstream; import android. app. activity; import android. content. res. assetmanager; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. openGL. glsurfaceview; import android. OS. bundle; import android. view. window; import android. view. windowmanager; public class advancedexample extends activity {// glsurfaceview private glsurfaceview glview; // instantiate myrenderer object private myrenderer mr = new myrenderer (); @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // you can specify the value of requestwindowfeature (window. feature_no_title); // sets full screen getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // init LoadFile new LoadFile (getresources (); glview = new glsurfaceview (this); glview. setrenderer (MR); setcontentview (glview) ;}// load the file class LoadFile {public static resources resource; Public LoadFile (Resources res) {resource = res ;} // file public static inputstream loadf (string filename) {assetmanager AM = LoadFile. resource. getassets (); try {return AM. open (filename) ;}catch (ioexception e) {return NULL ;}// image public static bitmap loadi (int id) {Bitmap bitmap = bitmapfactory. decoderesource (resource, ID); Return bitmap ;}}

 

(2) Renderer class

Package SIM. feel; import javax. microedition. khronos. EGL. eglconfig; import javax. microedition. khronos. opengles. gl10; import android. graphics. bitmap; import android. openGL. glsurfaceview. renderer; import COM. threed. jpct. camera; import COM. threed. jpct. config; import COM. threed. jpct. framebuffer; import COM. threed. jpct. genericvertexcontroller; import COM. threed. jpct. light; import COM. threed. jpct. loader; import COM. Threed. jpct. matrix; import COM. threed. jpct. mesh; import COM. threed. jpct. object3d; import COM. threed. jpct. primitives; import COM. threed. jpct. rgbcolor; import COM. threed. jpct. simplevector; import COM. threed. jpct. texture; import COM. threed. jpct. textureinfo; import COM. threed. jpct. texturemanager; import COM. threed. jpct. world; import COM. threed. jpct. util. memoryhelper;/*** a comprehensive example containing 3DS, md2 import and texture *** @ author SIMD Anfeg **/public class myrenderer implements Renderer {// circumference rate Private Static float Pi = (float) math. pi; // framebuffer private framebuffer Fb = NULL; // world object private world = NULL; // object3d object, which is a plane, character, rock, sky private object3d plane = NULL; private object3d Snork = NULL; private object3d rock = NULL; private object3d dome = NULL; // light object private light sun = NULL; // ind private float IND = 0; // FPS private int FPS = 0; // returns the current number of milliseconds private long time = system. currenttimemillis (); // bitmap object private bitmap igrass = NULL; private bitmap idisco = NULL; private bitmap irock = NULL; private bitmap inormals = NULL; private bitmap isky = NULL; // texturemanager object private texturemanager TM = NULL; // public myrenderer () is constructed by default {// config. maxpolysvisvisible = 1000; config. farplane = 15 00; config. gltransparencymul = 0.1f; config. gltransparencyoffset = 0.1f; config. usevbo = true; texture. defaulttomipmapping (true); texture. defaultto4bpp (true);} // Add the loaded image to the texturemanager public void loadtexture () {igrass = LoadFile. loadi (R. drawable. grasssample2); idisco = LoadFile. loadi (R. drawable. disco); irock = LoadFile. loadi (R. drawable. rock); inormals = LoadFile. loadi (R. drawable. normals); is Ky = LoadFile. loadi (R. drawable. sky); // texturemanager TM = texturemanager. getinstance (); TM. addtexture ("Grass", new texture (igrass); TM. addtexture ("Disco", new texture (idisco); TM. addtexture ("rock", new texture (irock); TM. addtexture ("normals", new texture (inormals); TM. addtexture ("sky", new texture (isky);} public void ondrawframe (gl10 GL) {simplevector offset = new simplevector (1, 0,-1 ). n Ormalize (); // animate the Snork and the dome animate (); offset. rotatey (0.007f); // render the scene FB. clear (); World. renderscene (FB); World. draw (FB); FB. display (); // print out the FPS to the console if (system. currenttimemillis ()-time> = 1000) {system. out. println (FPS); FPS = 0; time = system. currenttimemillis () ;}fps ++;} public void onsurfacechanged (gl10 GL, int width, int height) {If (FB! = NULL) {Fb = NULL;} Fb = new framebuffer (GL, width, height);} public void onsurfacecreated (gl10 GL, eglconfig height) {// enables hybrid GL. glable (gl10.gl _ blend); GL. glblendfunc (gl10.gl _ One, gl10.gl _ one_minus_src_alpha); // load textures loadtexture (); // world = New World (); // set the world environment to the world. setambientlight (120,120,120); // load/create and setup objects // flat (lawn) plane = primitives. getplane (20, 15); plane. rotatex (PI/2); plane. setspecularlighting (true); // set the texture plane. settexture ("Grass"); plane. setcollisionmode (object3d. collision_check_others); // plane. forcegeometryindices (true); // rock = loadmodel ("rock.3ds", 15f); rock. translate (0, 0,-90); rock. rotatex (-PI/2); // set the double texture textureinfo stonetex = new textureinfo (TM. gettextureid ("rock"); stonetex. add (TM. gettextureid ("normals"), textureinfo. mode_modulate); rock. settexture (stonetex); rock. setspecularlighting (true); // character Snork = loader. loadmd2 (LoadFile. loadf ("snork. md2 "), 0.8f); Snork. translate (0,-25,-50); Snork. rotatey (45); Snork. settexture ("Disco"); // sets the collision mode Snork. setcollisionmode (object3d. collision_check_self); // Sky Dome = loadmodel ("dome.3ds", 0.5f); dome. build (); dome. settexture ("sky"); dome. calctexturewrap (); // optimized, but this method can be used to show problems. // tiletexture (dome, 3); // calculate the dome vector between plane and dome. translate (plane. gettransformedcenter (). calcsub (dome. gettransformedcenter (); dome. setlighting (object3d. lighting_no_lights); // dome. forcegeometryindices (true); dome. setadditionalcolor (rgbcolor. white); // pass object3d to the world object. addobject (plane); World. addobject (rock); World. addobject (Snork); World. addobject (dome); // compile all objects in the world. buildallobjects (); // compile all objects for better performance plane. compile (); rock. compile (); dome. compile (); plane. strip (); rock. strip (); dome. strip (); // deformation plane, which is very interesting. I like // deform the plane mesh planemesh = plane. getmesh (); planemesh. setvertexcontroller (new Mod (), false); planemesh. applyvertexcontroller (); planemesh. removevertexcontroller (); // move camera cam = World. getcamera (); cam. movecamera (camera. camera_moveout, 150); cam. movecamera (camera. camera_moveup-to-date, 100); cam. lookat (plane. gettransformedcenter (); cam. setfov (1.5f); // setup dynamic light source sun = new light (World); Sun. setintensity (250,250,250); // reclaim memory memoryhelper. compact ();}/** animation */private void animate () {Ind + = 0.1f; If (IND> 1) {Ind-= 1;} Snork. animate (IND, 2); Snork. rotatey (-0.02f); Snork. translate (0,-50, 0); simplevector dir = Snork. getxaxis (); Dir = Snork. checkforcollisionellipsoid (Dir, new simplevector (5, 20, 5), 5); Snork. translate (DIR); Dir = Snork. checkforcollisionellipsoid (New simplevector (0,100, 0), new simplevector (5, 20, 5), 1); Snork. translate (DIR);} // load model private object3d loadmodel (string filename, float scale) {// Save the loaded 3DS file to the model array object3d [] model = loader. load3ds (LoadFile. loadf (filename), scale); // obtain the first 3DS file object3d o3d = new object3d (0); // Temporary Variable temp object3d temp = NULL; // traverse the model array for (INT I = 0; I <model. length; I ++) {// assign a temp = model [I] to the temp model array. // set the temp center to origin (, 0) temp. setcenter (simplevector. origin); // rotate the coordinate system along the X axis to the normal coordinate system (Y, X in the coordinate of jpct-AE is reversed) temp. rotatex (float )(-. 5 * Math. pi); // use the rotation matrix to specify the raw data temp for this object to rotate the grid. rotatemesh (); // a new matrix is used as the rotation matrix temp. setrotationmatrix (new matrix (); // combines o3d with temp o3d = object3d. mergeobjects (o3d, temp); // mainly for the migration from the desktop version jpct to the Android version (the desktop version is o3d here. build () o3d. compile () ;}// return o3d object return o3d;} // static class mod public static class mod extends genericvertexcontroller {Private Static final long serialversionuid = 1l; Public void apply () {// returns the plane mesh information simplevector [] S = getsourcemesh () before modification; // defines simplevector to get destinationmesh simplevector [] d = getdestinationmesh (); // main deformation operation for (INT I = 0; I <S. length; I ++) {d [I]. z = s [I]. z-(10f * (float) math. sin (s [I]. x/50f) + (float) math. cos (s [I]. y/50f); D [I]. X = s [I]. x; d [I]. y = s [I]. Y ;}}}}

 

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.