Android 3D physical Engine

Source: Internet
Author: User
Tags gety

Android 3D physical Engine


There are many excellent 3D physical engines in Android, such as alien3d, gamine, and jpct. Today, we will introduce how to useJpct(This engine is selected because the demo and screenshots on its official website are good)



1.

In idea (such as Windows and Linux), the other is running on Android. We will download both of them and use them all.

The following are some of the official websites, which are good:










2.

My downloadedThe desktop version is jpctapi.zip, and the androidversion is jpct-ae.zip.

Decompress the package separately.



Now we are going to officially start.The first of the three experiments is how to compile the jpct demo, and the second is how to useJpct writes a helloworld, and the third is to use jpct to write a 3D program running on Android and download it to the real machine for running. The first two experiments use the desktop version jpct, and the last one usesJpct for Android. The following is the official start:



Lab 1:

Open the example of jpct. There are four example. Take car as an example:

Run the following commands respectively:

Unzip jpctapi.zip
CD jpct/examples/Car

Chmod 777 run_java.sh

./Run_java.sh

A window will pop up, as shown below:




At the same time, enter the following content in the console:

Adding Lightsource: 0Adding Lightsource: 1Adding Lightsource: 2Loading Texture...textures/other/numbers.jpgLoading Texture...textures/rocks.jpgLoading file models/terascene.3dsFile models/terascene.3ds loaded...146731 bytesProcessing new material Material!Processing object from 3DS-file: t3_wholeObject 't3_whole_jPCT0' created using 7200 polygons and 3721 vertices.Loading Texture...textures/spot.jpgCreated 1485 triangle-strips for t3_whole_jPCT0 in 1 pass(es)Loading Texture...textures/spot.jpgLoading Texture...textures/decal.jpgLoading Texture...textures/radar.jpgLoading Texture...textures/plant.jpgLoading Texture...textures/plant2.jpgLoading file models/plant.3dsFile models/plant.3ds loaded...1307 bytesProcessing new material Material!Processing object from 3DS-file: skinObject 'skin_jPCT129' created using 34 polygons and 29 vertices.Loading Texture...textures/skidmark.jpgBuilding octree for 7200 triangles!Octree constructed with 517 nodes / 423 leafs.Java version is: 1.6.0_26-> support for BufferedImageVersion helper for 1.5+ initialized!-> using BufferedImageSoftware renderer (OpenGL mode) initializedSoftware renderer disposedSoftware renderer (OpenGL mode) initialized339-361 -> using splitted buffer access!New WorldProcessor created using 1 thread(s) and granularity of 1!Creating new world processor buffer for thread mainSoftware renderer disposed

This is a car 3D game. Press the direction key and the car will move on the uneven hillside, as shown below:




The following is another example: FPS:




So far, we have learned how to compile the demo. Below is a small supplement: how to compile in Eclipse:


1.

Create a new one in eclipseJava project named demo



2.

Copy all the Java source files under car/src/to the demo/src file, and copy the car/models and car/textures to the demo file together., The result is as follows:



3.

Right-click demo and select Properties. Under libaries of Java build path, click "add external jars..." and add jpct. jar to the project.Such:




4.

ClickRun as Java applicationThe effect is the same as before.


So far, the first experiment has been completed!




Lab 2:

We need to use eclipse to write a 3D program. The main program isHelloworld on the official website, The Wiki on the official website has a detailed description of it:

Http://www.jpct.net/wiki/index.php/Hello_World



1.

CreateHelloworldJava project, the same as the first step,Add jpct. jar to libraries



2.

CreateHelloworld class, Enter the following content:

import javax.swing.JFrame;import com.threed.jpct.FrameBuffer;import com.threed.jpct.IRenderer;import com.threed.jpct.Object3D;import com.threed.jpct.Primitives;import com.threed.jpct.Texture;import com.threed.jpct.TextureManager;import com.threed.jpct.World;public class HelloWorld {private World world;private FrameBuffer buffer;private Object3D box;private JFrame frame;public static void main(String[] args) throws Exception {new HelloWorld().loop();}public HelloWorld() throws Exception {frame=new JFrame("Hello world");frame.setSize(800, 600);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);world = new World();world.setAmbientLight(0, 255, 0);TextureManager.getInstance().addTexture("box", new Texture("box.jpg"));box = Primitives.getBox(13f, 2f);box.setTexture("box");box.setEnvmapped(Object3D.ENVMAP_ENABLED);box.build();world.addObject(box);world.getCamera().setPosition(50, -50, -5);world.getCamera().lookAt(box.getTransformedCenter());}private void loop() throws Exception {buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);while (frame.isShowing()) {box.rotateY(0.01f);buffer.clear(java.awt.Color.BLUE);world.renderScene(buffer);world.draw(buffer);buffer.update();buffer.display(frame.getGraphics());Thread.sleep(10);}buffer.disableRenderer(IRenderer.RENDERER_OPENGL);buffer.dispose();frame.dispose();System.exit(0);}}

3.

SetBox.jpgCopy it to helloworld because it is used in our program.


4.

ClickRun as Java applicationYou can, for example:




This is a box that keeps rotating.



In fact, we can also directly run the demo helloworld:

CD jpct/examples/helloworld

Chmod 777 run_software.sh

./Run_software.sh



Note: There are three different versions of helloworld: OpenGL, awtgl, and software-simulated OpenGL, our example uses software-simulated OpenGL (my machine cannot run the OpenGL version, which is strange !), You can also try other versions with the same effect



Now, the second experiment is completed. We wrote a helloworld with jpct.



Third Experiment

Let's write a 3D program running under Android: There is a cube in the center of the screen. If you drag it to a different place by hand, it will rotate towards that place.See the demo on the official website:Http://www.jpct.net/wiki/index.php/Hello_World_for_Android



1.

CreateAndroid project of demo, As follows:




2.

Add jpct-ae.jar to project (note, not jpct. Jar)



3.

Modify demoactivity. Java as follows:

package com.test.demo;import java.lang.reflect.Field;import javax.microedition.khronos.egl.EGL10;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.egl.EGLDisplay;import javax.microedition.khronos.opengles.GL10;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;import android.view.MotionEvent;import com.threed.jpct.Camera;import com.threed.jpct.FrameBuffer;import com.threed.jpct.Light;import com.threed.jpct.Logger;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.TextureManager;import com.threed.jpct.World;import com.threed.jpct.util.BitmapHelper;import com.threed.jpct.util.MemoryHelper;public class DemoActivity extends Activity {// Used to handle pause and resume...private static DemoActivity master = null;private GLSurfaceView mGLView;private MyRenderer renderer = null;private FrameBuffer fb = null;private World world = null;private RGBColor back = new RGBColor(50, 50, 100);private float touchTurn = 0;private float touchTurnUp = 0;private float xpos = -1;private float ypos = -1;private Object3D cube = null;private int fps = 0;private Light sun = null;protected void onCreate(Bundle savedInstanceState) {Logger.log("onCreate");if (master != null) {copy(master);}super.onCreate(savedInstanceState);mGLView = new GLSurfaceView(getApplication());mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall// back to Pixelflinger on some device (read: Samsung I7500)int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };EGLConfig[] configs = new EGLConfig[1];int[] result = new int[1];egl.eglChooseConfig(display, attributes, configs, 1, result);return configs[0];}});renderer = new MyRenderer();mGLView.setRenderer(renderer);setContentView(mGLView);}@Overrideprotected void onPause() {super.onPause();mGLView.onPause();}@Overrideprotected void onResume() {super.onResume();mGLView.onResume();}@Overrideprotected void onStop() {super.onStop();}private void copy(Object src) {try {Logger.log("Copying data from master Activity!");Field[] fs = src.getClass().getDeclaredFields();for (Field f : fs) {f.setAccessible(true);f.set(this, f.get(src));}} catch (Exception e) {throw new RuntimeException(e);}}public boolean onTouchEvent(MotionEvent me) {if (me.getAction() == MotionEvent.ACTION_DOWN) {xpos = me.getX();ypos = me.getY();return true;}if (me.getAction() == MotionEvent.ACTION_UP) {xpos = -1;ypos = -1;touchTurn = 0;touchTurnUp = 0;return true;}if (me.getAction() == MotionEvent.ACTION_MOVE) {float xd = me.getX() - xpos;float yd = me.getY() - ypos;xpos = me.getX();ypos = me.getY();touchTurn = xd / -100f;touchTurnUp = yd / -100f;return true;}try {Thread.sleep(15);} catch (Exception e) {// No need for this...}return super.onTouchEvent(me);}protected boolean isFullscreenOpaque() {return true;}class MyRenderer implements GLSurfaceView.Renderer {private long time = System.currentTimeMillis();public MyRenderer() {}public void onSurfaceChanged(GL10 gl, int w, int h) {if (fb != null) {fb.dispose();}fb = new FrameBuffer(gl, w, h);if (master == null) {world = new World();world.setAmbientLight(20, 20, 20);sun = new Light(world);sun.setIntensity(250, 250, 250);// Create a texture out of the icon...:-)Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.ic_launcher)), 64, 64));TextureManager.getInstance().addTexture("texture", texture);cube = Primitives.getCube(10);cube.calcTextureWrapSpherical();cube.setTexture("texture");cube.strip();cube.build();world.addObject(cube);Camera cam = world.getCamera();cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);cam.lookAt(cube.getTransformedCenter());SimpleVector sv = new SimpleVector();sv.set(cube.getTransformedCenter());sv.y -= 100;sv.z -= 100;sun.setPosition(sv);MemoryHelper.compact();if (master == null) {Logger.log("Saving master Activity!");master = DemoActivity.this;}}}public void onSurfaceCreated(GL10 gl, EGLConfig config) {}public void onDrawFrame(GL10 gl) {if (touchTurn != 0) {cube.rotateY(touchTurn);touchTurn = 0;}if (touchTurnUp != 0) {cube.rotateX(touchTurnUp);touchTurnUp = 0;}fb.clear(back);world.renderScene(fb);world.draw(fb);fb.display();if (System.currentTimeMillis() - time >= 1000) {Logger.log(fps + "fps");fps = 0;time = System.currentTimeMillis();}fps++;}}}


4.

ClickRun as Android ApplicationOpen the simulator. The effect is as follows:




Drag the cube in the center to rotate in different directions.



So far, the third experiment has been completed. Please download it to your mobile phone for fun ~~



These three experiments are just the use of jpct, more complex things needRead the Wiki and related documents on the official website..




Done!

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.