package com.ceno.gdlt;
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.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
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.example.R;
import com.threed.jpct.util.MemoryHelper;
/**
* A simple demo. This shows more how to use jPCT-AE than it shows how to write
* a proper application for Android. It includes basic activity management to
* handle pause and resume...
*
* @author EgonOlsen
*
*/
@SuppressLint("NewApi")
public class HelloWorld extends Activity {
// Used to handle pause and resume...
private static HelloWorld master = null;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
// 当JPCT渲染背景时FrameBuffer类提供了一个缓冲,它的结果本质上是一个能显示或者修改甚至能进行更多后处理的图片。
private FrameBuffer fb = null;
// World类是JPCT时最重要的一个类,它好像胶水一样把事物"粘"起来。它包含的对象和光线定义了JPCT的场景
private World world = null;
// 类似java.awt.*中的Color类
private RGBColor back = new RGBColor(0, 0, 0);
private View bottom;
private float touchTurn = 0.0f;
private float touchTurnUp = 0.0f;
// private float touchDown = 0;
//
// private float baseValue = 0;
// private float baseCameraValue = 100;
// private float scale = 0;
private float xpos = -1;
private float ypos = -1;
private Object3D cube = null;// 3d模型对象
// 每秒帧数
private int fps = 0;
// 光照类
private Light sun = null;
private Light moon = null;
private ProgressDialog progressDialog;
protected void onCreate(Bundle savedInstanceState) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("加载中,请稍候...");
progressDialog.show();
// Logger类中 jPCT中一个普通的用于打印和存储消息,错误和警告的日志类。
// 每一个JPCT生成的消息将被加入到这个类的队列中
Logger.log("onCreate");
// 如果本类对象不为NULL,将从Object中所有属性装入该类
if (master != null) {
copy(master);
}
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
// 使用自己实现的 EGLConfigChooser,该实现必须在setRenderer(renderer)之前
// 如果没有setEGLConfigChooser方法被调用,则默认情况下,视图将选择一个与当前android.view.Surface兼容至少16位深度缓冲深度EGLConfig。
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);
// 在模型下面添加底部view
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT));
RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(
R.layout.mobile_bottom, null);
bottom = view.findViewById(R.id.parent);
frameLayout.addView(mGLView);
frameLayout.addView(bottom);
setContentView(frameLayout);
}
/**
* 监听3D手机底部的返回、放大、缩小、左旋转、有旋转、复位控件
*/
public void onBtnClick(View v) {
switch (v.getId()) {
case R.id.goback:
// cube.clearAnimation();
// cube.clearAdditionalColor();
// cube.clearShader();
// cube.clearObject();
//world.removeAll();
finish();
break;
case R.id.big:
cube.scale(1.1f);
break;
case R.id.small:
cube.scale(0.9f);
break;
case R.id.left:
touchTurn = touchTurn + 0.08641969f;
break;
case R.id.right:
touchTurn = touchTurn - 0.08641969f;
break;
case R.id.reset:
cube.clearRotation();
break;
default:
break;
}
}
@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
private void copy(Object src) {
try {
// 打印日志
Logger.log("Copying data from master Activity!");
// 返回一个数组,其中包含目前这个类的的所有字段的Filed对象
Field[] fs = src.getClass().getDeclaredFields();
// 遍历fs数组
for (Field f : fs) {
// 尝试设置无障碍标志的值。标志设置为false将使访问检查,设置为true,将其禁用。
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) {
// 保存按下的初始x,y位置于xpos,ypos中
xpos = me.getX();
ypos = me.getY();
// System.out.println("ACTION_DOWN===============" + xpos);
// System.out.println("ACTION_DOWN===============" + ypos);
// touchDown = 1;
// baseCameraValue = world.getCamera().getSideVector().z;
return true;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
// 设置x,y及旋转角度为初始值
// System.out.println("ACTION_UP===============" + xpos);
// System.out.println("ACTION_UP===============" + ypos);
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
// touchDown = 0;
return true;
}
if (me.getAction() == MotionEvent.ACTION_MOVE) {
// if (me.getPointerCount() == 2) {
// float x = me.getX(0) - me.getX(1);
// float y = me.getY(0) - me.getY(1);
// float value = (float) Math.sqrt(x * x + y * y);// 计算两点的距离
//
// if (baseValue == 0) {
// baseValue = value;
// }
// else {
// if (value - baseValue >= 10 || value - baseValue <= -10) {
// scale = value / baseValue;// 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。
// Log.d("scale", "" + scale);
// }
// }
// return super.onTouchEvent(me);
// }
// 计算x,y偏移位置及x,y轴上的旋转角度
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;
xpos = me.getX();
ypos = me.getY();
// 以x轴为例,鼠标从左向右拉为正,从右向左拉为负
touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
// System.out.println("ACTION_MOVE touchTurnUp===============" +
// touchTurnUp);
return true;
}
try {
// 每Move一下休眠15毫秒
Thread.sleep(15);
} catch (Exception e) {
}
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) {
// 如果FrameBuffer不为NULL,释放fb所占资源
if (fb != null) {
fb.dispose();
}
// 在使用前创建一个宽w,高h,内容为支持opengl
fb = new FrameBuffer(gl, w, h);
if (master == null) {
System.out.println("onSurfaceChanged");
world = new World();
// 设置了环境光源强度。设置此值是负的整个场景会变暗,而为正将照亮了一切。
world.setAmbientLight(20, 20, 20);
// 在World中创建一个新的光源
sun = new Light(world);
// 设置�
- 1
- 2
- 3
- 4
前往页