当前位置: 首页 > 工具软件 > jPCT > 使用案例 >

jpct基本使用框架

陆涵畅
2023-12-01

package com.example.gatest41;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

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;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class MyRenderer implements GLSurfaceView.Renderer 
{
	private long time = System.currentTimeMillis();
	private Camera cam;
	private FrameBuffer fb = null;
	private World world = null;
	private Light sun = null;
	private Object3D cube = null;
	private RGBColor back = new RGBColor(50, 50, 100);
	private Context context;
	
	private MyGLView mGLView;
	public static MainActivity master = null;
	

	public MyRenderer(Context context) { 
		this.context = context;
	}

	@Override
	public void onDrawFrame(GL10 gl) 
	{ 
		fb.clear(back);
		world.renderScene(fb);
		world.draw(fb);
		fb.display();
		
		
	}

	@Override
	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);
			
			Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(
					context.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);
			world.buildAllObjects();
			
			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.x -= 100;
			sun.setPosition(sv);
			
			MemoryHelper.compact();
			
			if(master == null){
				Logger.log("Saving master Activity!");
				//master = new MainActivity();
				master = mGLView.ma;
			}			
			
		}
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		
	}

}


package com.example.gatest41;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder.Callback;

public class MyGLView extends GLSurfaceView implements Callback,Runnable {

	public static MainActivity ma;
	
	public MyGLView(Context context,MainActivity ma) {
		super(context);
		this.ma = ma;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(event);
	}

	public MyGLView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

}


package com.example.gatest41;




import java.lang.reflect.Field;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;

import com.threed.jpct.Logger;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends Activity 
{

	private static MainActivity master = null;
	private MyGLView mGLView;
	private MyRenderer renderer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Logger.log("onCreate");
		
		if(master!=null){
			copy(master);
		}
		super.onCreate(savedInstanceState);
		/*这里可能错误*/
		mGLView = new MyGLView(getApplication(),this);
		
		mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
			
			@Override
			public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
			
				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(this);
		mGLView.setRenderer(renderer);
		
		setContentView(mGLView);
	}

	@Override
	protected void onPause() 
	{ 
		super.onPause();
		mGLView.onPause();
	}

	@Override
	protected void onResume() 
	{ 
		super.onResume();
		mGLView.onResume();
	}

	@Override
	protected void onStop() 
	{ 
		super.onStop();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(event);
	}

	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();
		}  
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}




 类似资料: