Android应用开发揭秘的第29个程序12_5_Gfopengllw修改版和高仿版的源码注释
2012-12-06 17:10
561 查看
//opengl纹理贴图
//一个纹理对象有其自己的一套坐标系,左下角是 (0,0) 右上角是 (1,1):.....
//我们要将一个图像的一部分绘制到屏幕上,称之为纹理映射, 就是将图像根据上述坐标系计算出要绘制的部分的各个点的纹理坐标,然后一一对应到屏幕上的坐标中去(下图//中的坐标系是左上角为原点的):
//GfopenglwlActivity.java
package pak.Gfopenglwl;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class GfopenglwlActivity extends Activity
{
Renderer render = new GLRender();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GLImage.load(this.getResources());
GLSurfaceView glView = new GLSurfaceView(this);
glView.setRenderer(render);
setContentView(glView);
}
}
class GLImage
{
public static Bitmap mBitmap;
public static void load(Resources resources)
{
mBitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
}
}
//GLRender.java
package pak.Gfopenglwl;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class GLRender implements Renderer
{
float xrot, yrot, zrot;
int texture = -1;
int one = 0x10000;
IntBuffer vertices = IntBuffer.wrap(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 texCoords = IntBuffer.wrap(new int[]{
//one,0,0,0,0,one,one,one,
0,one,one,one,one,0,0,0,
0,0,0,one,one,one,one,0,
one,one,one,0,0,0,0,one,
0,one,one,one,one,0,0,0,
0,0,0,one,one,one,one,0,
one,0,0,0,0,one,one,one,
});
ByteBuffer indices = ByteBuffer.wrap(new byte[]{
0,1,3,2,
4,5,7,6,
8,9,11,10,
12,13,15,14,
16,17,19,18,
20,21,23,22,
});
@Override
public void onDrawFrame(GL10 gl)
{
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 重置当前的模型观察矩阵
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
//设置3个方向的旋转
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
// 绑定纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
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, texCoords);
//绘制
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
xrot+=0.5f;
yrot+=0.6f;
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.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);
IntBuffer intBuffer = IntBuffer.allocate(1);
// 创建纹理
gl.glGenTextures(1, intBuffer);
texture = intBuffer.get();
// 设置要使用的纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
//生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 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);
}
}
//一个纹理对象有其自己的一套坐标系,左下角是 (0,0) 右上角是 (1,1):.....
//我们要将一个图像的一部分绘制到屏幕上,称之为纹理映射, 就是将图像根据上述坐标系计算出要绘制的部分的各个点的纹理坐标,然后一一对应到屏幕上的坐标中去(下图//中的坐标系是左上角为原点的):
//GfopenglwlActivity.java
package pak.Gfopenglwl;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class GfopenglwlActivity extends Activity
{
Renderer render = new GLRender();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GLImage.load(this.getResources());
GLSurfaceView glView = new GLSurfaceView(this);
glView.setRenderer(render);
setContentView(glView);
}
}
class GLImage
{
public static Bitmap mBitmap;
public static void load(Resources resources)
{
mBitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
}
}
//GLRender.java
package pak.Gfopenglwl;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class GLRender implements Renderer
{
float xrot, yrot, zrot;
int texture = -1;
int one = 0x10000;
IntBuffer vertices = IntBuffer.wrap(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 texCoords = IntBuffer.wrap(new int[]{
//one,0,0,0,0,one,one,one,
0,one,one,one,one,0,0,0,
0,0,0,one,one,one,one,0,
one,one,one,0,0,0,0,one,
0,one,one,one,one,0,0,0,
0,0,0,one,one,one,one,0,
one,0,0,0,0,one,one,one,
});
ByteBuffer indices = ByteBuffer.wrap(new byte[]{
0,1,3,2,
4,5,7,6,
8,9,11,10,
12,13,15,14,
16,17,19,18,
20,21,23,22,
});
@Override
public void onDrawFrame(GL10 gl)
{
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 重置当前的模型观察矩阵
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
//设置3个方向的旋转
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
// 绑定纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
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, texCoords);
//绘制
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
xrot+=0.5f;
yrot+=0.6f;
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.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);
IntBuffer intBuffer = IntBuffer.allocate(1);
// 创建纹理
gl.glGenTextures(1, intBuffer);
texture = intBuffer.get();
// 设置要使用的纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
//生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 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);
}
}
相关文章推荐
- Android应用开发揭秘的第27个程序12_3_GfopenglRotate修改版和高仿版的源码注释
- Android应用开发揭秘的第28个程序12_4_Gfopengl3D修改版和高仿版的源码注释
- Android应用开发揭秘的第26个程序12_2_openglcolor修改版和高仿版的源码注释
- Android应用开发揭秘的第17个程序5_12_DBuffImg修改版和高仿版的源码注释
- Android应用开发揭秘的第25个程序12_1_opengl修改版和高仿版的源码注释
- Android应用开发揭秘的第14个程序5_9_MatrixScaleImg修改版和高仿版的源码注释
- Android应用开发揭秘的第7个程序5_2_SurfaceViewDrawCircle修改版和高仿版的源码注释
- Android应用开发揭秘的第9个程序5_4_canvas修改版和高仿版的源码注释
- Android应用开发揭秘的第一个程序HelloAndroid修改版和高仿版的源码注释
- Android应用开发揭秘的第二个程序3_1_Android_Intent修改版和高仿版的源码注释
- Android应用开发揭秘的第13个程序5_8_MatrixRotateImg修改版和高仿版的源码注释
- Android应用开发揭秘的第5个程序3_4_cycle修改版和高仿版的源码注释
- Android应用开发揭秘的第8个程序5_3_PaintDraw修改版和高仿版的源码注释
- Android应用开发揭秘的第24个程序5_19_gif修改版和高仿版的源码注释
- Android应用开发揭秘的第3个程序3_2_Content_Provider修改版和高仿版的源码注释
- Android应用开发揭秘的第15个程序5_10_WavePixelImg修改版和高仿版的源码注释
- Android应用开发揭秘的第11个程序5_6_DrawText修改版和高仿版的源码注释
- Android应用开发揭秘的第12个程序5_7_drawImage修改版和高仿版的源码注释
- Android应用开发揭秘的第22,23个程序5_17_frame,5_18_framexml修改版和高仿版的源码注释
- Android应用开发揭秘的第10个程序5_5_DrawAllShape修改版和高仿版的源码注释