/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ex;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;
/**
* Wrapper activity demonstrating the use of {@link GLSurfaceView}, a view
* that uses OpenGL drawing into a dedicated surface.
*
* Shows:
* + How to redraw in response to user input.
*/
public class Main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create our Preview view and set it as the content of our
// Activity
mGLSurfaceView = new TouchSurfaceView(this);
setContentView(mGLSurfaceView);
mGLSurfaceView.requestFocus();
mGLSurfaceView.setFocusableInTouchMode(true);
}
@Override
protected void onResume() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onResume();
mGLSurfaceView.onResume();
}
@Override
protected void onPause() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onPause();
mGLSurfaceView.onPause();
}
private GLSurfaceView mGLSurfaceView;
}
/**
* Implement a simple rotation control.
*
*/
class TouchSurfaceView extends GLSurfaceView {
private boolean axesX = true;
public void setAxesX(boolean b){
axesX = b;
}
public TouchSurfaceView(Context context) {
super(context);
mRenderer = new CubeRenderer(context);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override public boolean onTrackballEvent(MotionEvent e) {
float dx = Math.abs(e.getX() * TRACKBALL_SCALE_FACTOR);
float dy = Math.abs(e.getY() * TRACKBALL_SCALE_FACTOR);
if(dx>dy && dx>2.5){
mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR;
mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR;
this.setAxesX(true);
}else if(dx<dy && dy>2.5){
mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR;
mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR;
this.setAxesX(false);
}
requestRender();
return true;
}
@Override public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
if(Math.abs(dx)>Math.abs(dy) && Math.abs(dx)>2.5){
mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
this.setAxesX(true);
}else if(Math.abs(dx)<Math.abs(dy) && Math.abs(dy)>2.5){
mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
this.setAxesX(false);
}
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
/**
* Render a cube.
*/
private class CubeRenderer implements GLSurfaceView.Renderer {
public CubeRenderer(Context context) {
mContext = context;
mCube = new Cube();
}
public void onDrawFrame(GL10 gl) {
Log.i("=============","this.mAngleX=" + this.mAngleX + ",this.mAngleY=" + this.mAngleY);
gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
MyUtil.deBug("drawFrame");
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
//gl.glTranslatef(0, 0.0f, -1.0f);
if(axesX){
gl.glRotatef(mAngleX, 0, 1, 0);
}else{
gl.glRotatef(mAngleY, 1, 0, 0);
}
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
mCube.draw(gl);
}
//当屏幕 改变时候,比如手机横着拿,变成竖着拿,先onSurfaceCreated 在两次onSurfaceChanged 。
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, 320, 320);
/**
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
**/
//Toast.makeText(mContext, "width=" + width, Toast.LENGTH_SHORT).show();
}
//当屏幕 改变时候,比如手机横着拿,变成竖着拿,先onSurfaceCreated 在两次onSurfaceChanged 。
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//载入纹理 自己写的方法
loadTexture(gl,1,R.drawable.cube_front);
loadTexture(gl,2,R.drawable.cube_back);
loadTexture(gl,3,R.drawable.cube_left);
loadTexture(gl,4,R.drawable.cube_right);
loadTexture(gl,5,R.drawable.cube_top);
loadTexture(gl,6,R.drawable.cube_bottom);
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(1,1,1,1);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
//默认旋转
gl.glRotatef(10.0f, 1.0f, 1.0f, 0);
}
public void loadTexture(GL10 gl,int no,int drawableId){
//定义纹理
int[] textures = new int[1];
switch(no){
//绑定生成绑定纹理
case 1:
gl.glGenTextures(1, textures, 0);
Cube.mTextureID_front = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_front);
break;
case 2:
gl.glGenTextures(1, textures, 0);
Cube.mTextureID__back = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID__back);
break;
case 3:
gl.glGenTextures(1, textures, 0);
Cube.mTextureID_left = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_left);
break;
case 4:
gl.glGenTextures(1, textures, 0);
Cube.mTextureID_right = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_right);
break;
case 5:
gl.glGenTextures(1, textur
- 1
- 2
- 3
- 4
前往页