Activity类

package sim.feel;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;

public class my3d4 extends Activity {
/** Called when the activity is first created. */

private MyRenderer renderer = new MyRenderer();
private GLSurfaceView glSurfaceView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BitGL.init(getResources());
glSurfaceView = new GLSurfaceView(this);

glSurfaceView.setRenderer(renderer);
setContentView(glSurfaceView);
}

// 处理事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
renderer.onKeyDown(keyCode, event);
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
renderer.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
}

class BitGL {
public static Bitmap bitmap;

public static void init(Resources resources) {
bitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
}
}

Renderer类

package sim.feel;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.view.KeyEvent;

public class MyRenderer implements Renderer {
float step = 0.4f;
boolean key;
boolean light = true;

float xrot, yrot; // x,y轴旋转
float xspeed, yspeed;// 旋转的速度
private int one = 0x10000;
private int[] textures = new int[1];
/**
* @param context
* 顶点及纹理
*/
// verticesBuffer
private IntBuffer verticesBuffer;
// texcoordBuffer
private IntBuffer texcoordBuffer;
// 顶点数组vertices
private int[] vertices;
// 纹理数组texcoord
private int[] texcoord;

/**
* @param context
* 光线
*/
// lightAmbientBuffer
private FloatBuffer lightAmbientBuffer;
// 环境光lightAmbient
private float[] lightAmbient;
// lightDiffuseBuffer
private FloatBuffer lightDiffuseBuffer;
// 漫射光lightDiffuse
private float[] lightDiffuse;
// lightPositionBuffer
private FloatBuffer lightPositionBuffer;
// 光源的位置
private float[] lightPosition;

// 构造方法
public MyRenderer() {
// 在构造方法中初始化
initData();
initBuffer();
}

// 初始化各类值
public void initData() {
vertices = new int[] { -one, -one, one, one, -one, one, -one, one, one,
one, one, one, one, -one, one, one, -one, -one, one, one, one,
one, one, -one, one, -one, -one, -one, -one, -one, one, one,
-one, -one, one, -one, -one, -one, -one, -one, -one, one, -one,
one, -one, -one, one, one, -one, one, -one, one, one, -one,
-one, one, one, one, one, one, -one, -one, -one, -one, -one,
one, one, -one, -one, one, -one, one };

texcoord = new int[] { 0, 0, one, 0, 0, one, one, one };

lightAmbient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };

lightDiffuse = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };

lightPosition = new float[] { 0.0f, 0.0f, 2.0f, 1.0f };
}

public void initBuffer() {
// verticesbyteBuffer
ByteBuffer verticesbyteBuffer = ByteBuffer
.allocateDirect(vertices.length * 4);
verticesbyteBuffer.order(ByteOrder.nativeOrder());
verticesBuffer = verticesbyteBuffer.asIntBuffer();
verticesBuffer.put(vertices);
verticesBuffer.position(0);

// texcoordBuffer
ByteBuffer texcoordbyteBuffer = ByteBuffer
.allocateDirect(texcoord.length * 4 * 6);
texcoordbyteBuffer.order(ByteOrder.nativeOrder());
texcoordBuffer = texcoordbyteBuffer.asIntBuffer();
for (int i = 0; i < 6; i++) {
texcoordBuffer.put(texcoord);
}
texcoordBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightAmbientbyteBuffer = ByteBuffer
.allocateDirect(lightAmbient.length * 4 * 6);
lightAmbientbyteBuffer.order(ByteOrder.nativeOrder());
lightAmbientBuffer = lightAmbientbyteBuffer.asFloatBuffer();
lightAmbientBuffer.put(lightAmbient);
lightAmbientBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightPositionbyteBuffer = ByteBuffer
.allocateDirect(lightPosition.length * 4 * 6);
lightPositionbyteBuffer.order(ByteOrder.nativeOrder());
lightPositionBuffer = lightPositionbyteBuffer.asFloatBuffer();
lightPositionBuffer.put(lightPosition);
lightPositionBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightDiffusebyteBuffer = ByteBuffer
.allocateDirect(lightDiffuse.length * 4 * 6);
lightDiffusebyteBuffer.order(ByteOrder.nativeOrder());
lightDiffuseBuffer = lightDiffusebyteBuffer.asFloatBuffer();
lightDiffuseBuffer.put(lightDiffuse);
lightDiffuseBuffer.position(0);
}

@Override
public void onDrawFrame(GL10 gl) {

// 清除深度缓存和颜色
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
// 重置观察模型
gl.glLoadIdentity();

// 如果不启用GL_LIGHTING光就什么都看不见
gl.glEnable(GL10.GL_LIGHTING);
// 启用vertex及texcoord
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

// 向左移动1.5f并向里移动6.0f
gl.glTranslatef(0.0f, 0.0f, -6.0f);

// 设置旋转
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

// 指定顶点映射
gl.glVertexPointer(3, GL10.GL_FIXED, 0, verticesBuffer);
// 指定纹理映射(每次都写错,烦)
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texcoordBuffer);

for (int i = 0; i < 6; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}

// 取消顶点及纹理映射
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

xrot += xspeed;
yrot += yspeed;

//混合开关
if (key)
{
gl.glEnable(GL10.GL_BLEND); // 打开混合
gl.glDisable(GL10.GL_DEPTH_TEST); // 关闭深度测试
}
else
{
gl.glDisable(GL10.GL_BLEND); // 关闭混合
gl.glEnable(GL10.GL_DEPTH_TEST); // 打开深度测试
}


if (!light) // 判断是否开始光源
{
gl.glDisable(GL10.GL_LIGHT1); // 禁用一号光源
} else // 否则
{
gl.glEnable(GL10.GL_LIGHT1); // 启用一号光源
}
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
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);
gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 告诉系统对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 设置背景色为黑色
gl.glClearColor(0, 0, 0, 0);
// 启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH);

// 设置深度缓存
gl.glClearDepthf(one);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所做深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);

// 启用纹理
gl.glEnable(GL10.GL_TEXTURE_2D); // 不启用没有效果的
// 创建纹理
gl.glGenTextures(1, textures, 0);
// 绑定纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// 生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmap, 0);

// 线性滤波
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);

// 设置环境光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbientBuffer);

// 设置漫射光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuseBuffer);

// 设置光源的位置
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPositionBuffer);

// 启用一号光源
gl.glEnable(GL10.GL_LIGHT1);

// 启用混合
gl.glEnable(GL10.GL_BLEND);

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
key = true;
xspeed = -step;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
key = true;
xspeed = step;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
key = true;
yspeed = -step;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
key = true;
yspeed = step;
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
light = !light;
break;
}
return false;
}

public boolean onKeyUp(int keyCode, KeyEvent event) {
key = false;
return false;
}

}

效果图

android opengl es 混合效果_第1张图片

更多相关文章

  1. ART深度探索开篇:从Method Hook谈起
  2. 深度探索Android应用程序的基本原理
  3. Android第二十七课 NDK 渲染色彩深度
  4. Android 实现系统深度休眠笔记
  5. Android 布局深度优化(减少布局层次)
  6. Android 3D 游戏学习笔记(4)-光源
  7. android纹理图片的加载与修改
  8. Android框架的深度分析1
  9. android 深度搜索笔记一

随机推荐

  1. Android年终盘点:阿里程序员必备技能知识
  2. Java中输入流的read()为阻塞式方法的相关
  3. Android开发之模板模式初探
  4. android emulator 运行缓慢的解决办法
  5. Android以当前Activity为基准进行截屏
  6. Android(安卓)应用的动画实践--View Anim
  7. 小米平板:天使与魔鬼的共同化身
  8. Android开发之如何使用通知提醒功能(Notif
  9. 那些Android上的性能优化
  10. Android开发之程序员必须要懂得Android的