混色,半透明的设定,以及我们视角即屏幕处在-1层,-1层的物体是看不见的
本文是生成一个半透明的纹理贴图画像,demo如下:
package com.example.zp.a3dword;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by lenovo on 2016/9/7.
*/
public class GLRenderer implements GLSurfaceView.Renderer{
float xrot, yrot, zrot;
int textureSize = 2;
int texture[] = new int [textureSize];
int one = 0x10000;
private Bitmap mBitmap[];
//正方体的6个面,4个点确定一个面
IntBuffer vertices = BufferUtil.intToBuffer(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
});
IntBuffer texCoords2 = BufferUtil.intToBuffer(new int[]{
0, one, one, one, 0, 0, one, 0,
0, 0, 0, one, one, 0, one, one,
one, one, one, 0, 0, one, 0, 0,
0, one, one, one, 0, 0, one, 0,
0, 0, 0, one, one, 0, one, one,
one, 0, 0, 0, one, one, 0, one,
});
public GLRenderer(Bitmap mBitmap[])
{
this.mBitmap = mBitmap;
}
@Override
public void onDrawFrame(GL10 gl)
{
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 重置当前的模型观察矩阵
gl.glEnable(GL10.GL_LIGHTING);
gl.glLoadIdentity();
// 通过修改z的值,发现屏幕是处在-1层,因此若想物体显示出来必须在屏幕内的-1层以下
gl.glTranslatef(0.0f, 0.0f, -4f);
//设置3个方向的旋转
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, -1.0f, 0.0f, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//启用纹理映射
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords2);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
for(int i = 0 ; i < 6 ; i ++)
{
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP , i*4 , 4);
gl.glFinish();
}
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
xrot+=0.5f;
yrot+=0.2f;
zrot+=0.3f;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
float ratio = (float) width / height;
//设置OpenGL场景的大小
gl.glViewport(0, 0, 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.glClearColor(0, 0, 0, 0);
// 这里千万别加这条,它让纹理只贴在图形表面,而正方体内部是黑色的,因此如果启用了,我们就看不到透明的立方体了
// gl.glEnable(GL10.GL_CULL_FACE);
// 启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// 启用混合模式
gl.glEnable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_DEPTH_TEST); // 关闭深度测试
//设置光线,,1.0f为全光线,a=50%
gl.glColor4f(1.0f,1.0f,1.0f,0.5f);
// 基于源象素alpha通道值的半透明混合函数
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE);
//启用纹理映射
gl.glClearDepthf(1.0f);
//深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);
//精细的透视修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//允许2D贴图,纹理
gl.glEnable(GL10.GL_TEXTURE_2D);
getTextures(gl);
//设置光颜色
FloatBuffer lightAmbient = BufferUtil.floatToBuffer(new float[]{
1f, 1f, 1f, 1f
} );
FloatBuffer lightDiffuse = BufferUtil.floatToBuffer(new float[]{
1f,1f,1f,1f
} );
//定义光源位置
FloatBuffer lightPosition = BufferUtil.floatToBuffer(new float[]{
-2f,0f,-4f,1f
} );
// //设置环境光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbient);
//设置漫射光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse);
//设置光源位置
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosition);
//开启一号光源
gl.glEnable(GL10.GL_LIGHT1);
}
private void getTextures(GL10 gl)
{
IntBuffer intBuffer = IntBuffer.allocate(2);//申请2个纹理存储空间
// 创建纹理
gl.glGenTextures(2 , intBuffer); //创建2个纹理,绑定intuffer
texture[0] = intBuffer.get(); // 获取第一个纹理的存储指针,即纹理存储位置,位置+1
texture[1] = intBuffer.get(); //获取下一个纹理存储的位置
// 设置要使用的纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
//生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0 , mBitmap[0] , 0);//利用图mBitmap[0]生成纹理,存储在texture[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.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0 , mBitmap[1] , 0);//利用图mBitmap[0]生成纹理,存储在texture[1]
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);
}
}