标签:
package com.cumt.opengeschange;
import com.cumt.render.MyRender;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
public class MySurfaceView extends GLSurfaceView {
private MyRender myRender;
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRender = new MyRender(context);
this.setEGLContextClientVersion(2);
this.setRenderer(myRender);
// 设置渲染模式为主动渲染
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
}
package com.cumt.opengeschange;
import com.cumt.render.MyRender;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.View;
public class MySurfaceView extends GLSurfaceView {
private MyRender myRender;
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRender = new MyRender(context);
this.setEGLContextClientVersion(2);
this.setRenderer(myRender);
// 设置渲染模式为主动渲染
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
}package com.cumt.opengeschange;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private MySurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置为全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 设置为横屏模式
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
glSurfaceView = new MySurfaceView(this);
setContentView(glSurfaceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
glSurfaceView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
glSurfaceView.onResume();
}
}
package com.cumt.utils;
import android.opengl.Matrix;
//存储系统矩阵状态的类
public class MatrixState {
private static float[] mProjMatrix = new float[16];// 4x4矩阵 存储投影矩阵
private static float[] mVMatrix = new float[16];// 摄像机位置朝向9参数矩阵
/*
* 第一步 :新建平移变换矩阵
*/
private static float[] mtMatrix = new float[16];// 平移变换矩阵
/*
* 第二步: 初始化为单位矩阵
*/
static{
//初始化为单位矩阵
Matrix.setIdentityM(mtMatrix, 0);
}
/*
* 第三步 : 平移变换方法共外部使用
*/
public static void translate(float x,float y,float z)//设置沿xyz轴移动
{
Matrix.translateM(mtMatrix, 0, x, y, z);
}
// 设置摄像机
public static void setCamera(float cx, // 摄像机位置x
float cy, // 摄像机位置y
float cz, // 摄像机位置z
float tx, // 摄像机目标点x
float ty, // 摄像机目标点y
float tz, // 摄像机目标点z
float upx, // 摄像机UP向量X分量
float upy, // 摄像机UP向量Y分量
float upz // 摄像机UP向量Z分量
) {
Matrix.setLookAtM(mVMatrix, 0, cx, cy, cz, tx, ty, tz, upx, upy, upz);
}
// 设置透视投影参数
public static void setProjectFrustum(float left, // near面的left
float right, // near面的right
float bottom, // near面的bottom
float top, // near面的top
float near, // near面距离
float far // far面距离
) {
Matrix.frustumM(mProjMatrix, 0, left, right, bottom, top, near, far);
}
// 获取具体物体的总变换矩阵
static float[] mMVPMatrix = new float[16];
public static float[] getFinalMatrix() {
/*
* 第四步 : 乘以平移变换矩阵
*/
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mtMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
return mMVPMatrix;
}
}
package com.cumt.opengeschange;
import com.cumt.render.MyRender;
import com.cumt.utils.MatrixState;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.View;
public class MySurfaceView extends GLSurfaceView {
private MyRender myRender;
private float mPreviousX;//上次的触控位置X坐标
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRender = new MyRender(context);
this.setEGLContextClientVersion(2);
this.setRenderer(myRender);
// 设置渲染模式为主动渲染
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float x = event.getX();//当前的触控位置X坐标
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE://检测到移动事件时
float dx = x - mPreviousX;
if(dx > 0){
MatrixState.translate(0.1f, 0, 0);
}else{
MatrixState.translate(-0.1f, 0, 0);
}
}
mPreviousX=x;
return true;
}
});
}
}//旋转变换
public static void rotate(float angle, float x, float y, float z) {// 设置绕xyz轴移动
Matrix.rotateM(mtMatrix, 0, angle, x, y, z);
}package com.cumt.opengeschange;
import com.cumt.render.MyRender;
import com.cumt.utils.MatrixState;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.View;
public class MySurfaceView extends GLSurfaceView {
private MyRender myRender;
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRender = new MyRender(context);
this.setEGLContextClientVersion(2);
this.setRenderer(myRender);
// 设置渲染模式为主动渲染
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://检测到点击事件时
MatrixState.rotate(30, 0, 1, 0);
}
return true;
}
});
}
}
//缩放变换
public static void scale(float x,float y,float z)
{
Matrix.scaleM(mtMatrix,0, x, y, z);
}
package com.cumt.opengeschange;
import com.cumt.render.MyRender;
import com.cumt.utils.MatrixState;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.View;
public class MySurfaceView extends GLSurfaceView {
private MyRender myRender;
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRender = new MyRender(context);
this.setEGLContextClientVersion(2);
this.setRenderer(myRender);
// 设置渲染模式为主动渲染
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://检测到点击事件时
MatrixState.scale(0.4f, 1.5f, 0.6f);//xyz三个方向按各自的缩放因子进行缩放
}
return true;
}
});
}
}OpenglES2.0 for Android:各种变换来一波
标签:
原文地址:http://blog.csdn.net/cassiepython/article/details/51606205