package com.bn.Sample9_4;
import java.io.IOException;
import java.io.InputStream;
import android.opengl.GLSurfaceView;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.view.MotionEvent;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
class MySurfaceView extends GLSurfaceView
{
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
private SceneRenderer mRenderer;//场景渲染器
private boolean first=true;
private int w=0,h=0;
private float mPreviousY;//上次的触控位置Y坐标
private float mPreviousX;//上次的触控位置X坐标
int textureId1=0;//系统分配的纹理id
public MySurfaceView(Context context) {
super(context);
this.setEGLContextClientVersion(2); //设置使用OPENGL ES2.0
mRenderer = new SceneRenderer(); //创建场景渲染器
setRenderer(mRenderer); //设置渲染器
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
}
//触摸事件回调方法
@Override
public boolean onTouchEvent(MotionEvent e)
{
float y = e.getY();
float x = e.getX();
System.out.printf("the vertex you touch is \n", null);
System.out.printf("%f %f \n", x,y);
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dy = y - mPreviousY;//计算触控笔Y位移
float dx = x - mPreviousX;//计算触控笔X位移
mRenderer.xAngl += dy * TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
if(mRenderer.xAngl>90&&mRenderer.xAngl<270)
mRenderer.yAngle += dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角
else
mRenderer.yAngle -= dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角
if(mRenderer.yAngle>360)
mRenderer.yAngle-=360;
else if(mRenderer.yAngle<0)
mRenderer.yAngle+=360;
if(mRenderer.xAngl >360)
mRenderer.xAngl -=360;
else if(mRenderer.xAngl<0)
mRenderer.xAngl+=360;
requestRender();//重绘画面
}
mPreviousY = y;//记录触控笔位置
mPreviousX = x;//记录触控笔位置
return true;
}
private class SceneRenderer implements GLSurfaceView.Renderer
{
float yAngle=0;//绕Y轴旋转的角度
float xAngl=0;
//从指定的obj文件中加载对象
LoadedObjectVertexNormalTexture lovo;
NewVertexs nv;//拾取点的结构
public void onDrawFrame(GL10 gl)
{
//清除深度缓冲与颜色缓冲
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
double xAn = Math.toRadians(xAngl);
double yAn=Math.toRadians(yAngle);
float x=(float)(30*Math.cos(xAn)*Math.sin(yAn));
float y=(float)(30*Math.sin(xAn));
float z=(float)(30*Math.cos(xAn)*Math.cos(yAn));
//System.out.print("jkl");
// float yy=(float)(Math.sin((double)xAngl));30**30*Math.cos((double)xAngl
// System.out.printf("%f/n", xx);
// System.out.printf("%f %f %f %f \n", xAngl,yAngle,xx,yy);
//坐标系推远
if(xAngl>90&&xAngl<270)
MatrixState.setCamera(x, y, z, 0, 0, 0, 0, -1, 0);
else
MatrixState.setCamera(x, y, z, 0, 0, 0, 0, 1, 0);
MatrixState.pushMatrix();
//MatrixState.translate(0, 0, 0f); //ch.obj
//绕Y轴、Z轴旋转
// MatrixState.rotate(xAngl, 1, 0, 0);
// MatrixState.rotate(yAngle, 0, 1, 0);
//若加载的物体部位空则绘制物体
//System.out.printf("**%f %f %f %f \n",aa[12],aa[13],aa[14],aa[15]);
/* int h=Constant.size/3;
* */
// System.out.printf("the width and height is :\n", null);
// System.out.printf("%d %d \n ", w,h);
// System.out.printf("*****%f %d \n", Constant.vertex1[3*i-1]);
System.out.printf("****\n", null);
if(lovo!=null)
{
float []aa=new float[16];
float []bb=new float[4];
float []cc=new float[4];
aa=MatrixState.getFinalMatrix();
int i=0;
int h=Constant.size/3;
for(int j=0;j<h;j++)
{
bb[0]=aa[0]*Constant.vertex[3*j]+aa[1]*Constant.vertex[3*j+1]+aa[2]*Constant.vertex[3*j+2]+aa[3];
bb[1]=aa[4]*Constant.vertex[3*j]+aa[5]*Constant.vertex[3*j+1]+aa[6]*Constant.vertex[3*j+2]+aa[7];
bb[2]=aa[8]*Constant.vertex[3*j]+aa[9]*Constant.vertex[3*j+1]+aa[10]*Constant.vertex[3*j+2]+aa[11];
bb[3]=aa[12]*Constant.vertex[3*j]+aa[13]*Constant.vertex[3*j+1]+aa[14]*Constant.vertex[3*j+2]+aa[15];
// Matrix.multiplyMM(cc, 0, aa, 0, bb, 0);
//System.out.printf("**%f %f %f \n",Constant.vertex[j],Constant.vertex[j+1],Constant.vertex[j+2]);
// System.out.printf("**%f %f %f %f \n",bb[0],bb[1],bb[2],bb[3]);
if(bb[0]/bb[3]<0&&bb[1]/bb[3]<0)
{
Constant.vertex1[3*i]=Constant.vertex[3*j];
Constant.vertex1[3*i+1]=Constant.vertex[3*j+1];
Constant.vertex1[3*i+2]=Constant.vertex[3*j+2];
i++;
}
}
Constant.size1 = 3*i;
nv = new NewVertexs(MySurfaceView.this);
// System.out.printf("%f \n", Constant.vertex1[3*i-1]);
nv.drawSelf();
}
else
System.out.printf("%d \n", 3);
MatrixState.popMatrix();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
w=width;
h=height;
//设置视窗大小及位置
GLES20.glViewport(0, 0, width,height);
//System.out.printf("zhe jiu shi \n", null);
//System.out.printf("%d %d \n", width,height);
//计算GLSurfaceView的宽高比
float ratio = (float) width / height;
//调用此方法计算产生透视投影矩阵
MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);
//调用此方法产生摄像机9参数位置矩阵
MatrixState.setCamera(0,0,30,0f,0f,0f,0f,1.0f,0.0f);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
//设置屏幕背景色RGBA
GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
//打开深度检测
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
//打开背面剪裁
GLES20.glEnable(GLES20.GL_CULL_FACE);
//初始化变换矩阵
MatrixState.setInitStack();
//初始化光源位置''
// MatrixState.setLightLocation(40, 10, 20);
//加载要绘制的物体
lovo=LoadUtil.loadFromFile("ch_t.obj", MySurfaceView.this.getResources(),MySurfaceView.this);
// if(Constant.CURR_DRAW_MODE==3)
//加载纹理
// textureId=initTexture(R.drawable.texture3);
// else if(Constant.CURR_DRAW_MODE==4)
// textureId1=initTexture(R.drawable.texture1);
}
/*
public int initTexture(int drawableId)//textureId
{
//生成纹理ID
int[] textures = new int[1];
GLES20.glGenTextures
(
1, //产生的纹理id的数量
textures, //纹理id的数组
0 //偏移量
);
int textureId=textures[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
GLES20.glTexParameterf