Android应用开发揭秘的第29个程序12_5_Gfopengllw修改版和高仿版的源码注释

//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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值