Android OpenGL ES 简明开发教程七:材质渲染

http://www.imobilebbs.com/wordpress/?p=1571

 

新建一个SimplePlane类

package com.lau.gl03;

public class SimplePlane extends Mesh {

	public SimplePlane() {
		this(1, 1);
	}
	
	public SimplePlane(float width, float height) {
		float textureCoordinates[] = {
			0.0f, 2.0f,
			2.0f, 2.0f,
			0.0f, 0.0f,
			2.0f, 0.0f,
		};
		
		short[] indices = new short[] {0, 1, 2, 1, 3, 2};
		
		float[] vertices = new float[] {
			-0.5f, -0.5f, 0.0f,
			0.5f, -0.5f, 0.0f,
			-0.5f, 0.5f, 0.0f,
			0.5f, 0.5f, 0.0f,
		};
		
		setIndices(indices);
		setVertices(vertices);
		setTextureCoordinates(textureCoordinates);
	}
}


修改mesh类

	
        ...
        private int mTextureId = -1;
	private Bitmap mBitmap;
	//UV texture buffer
	private FloatBuffer mTextureBuffer;
	private boolean mShouldLoadTexture = false;
	
	protected void setTextureCoordinates(float[] textureCoords) {
		ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
		tbb.order(ByteOrder.nativeOrder());
		mTextureBuffer = tbb.asFloatBuffer();
		mTextureBuffer.put(textureCoords);
		mTextureBuffer.position(0);
	}
	
	public void loadBitmap(Bitmap bitmap) {
		this.mBitmap = bitmap;
		this.mShouldLoadTexture = true;
	}
	
	public void loadGLTexture(GL10 gl) {
		int[] textures = new int[1];
		gl.glGenTextures(1, textures, 0);
		mTextureId = textures[0];
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
		
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_LINEAR);
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
	}
        ...


 

修改Mesh的draw方法如下:

		if(mShouldLoadTexture) {
			loadGLTexture(gl);
			mShouldLoadTexture = false;
		}
		
		if(mTextureId != -1 && mTextureBuffer != null) {
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
		}
                ...
		if(mTextureId != -1 && mTextureBuffer != null) {
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		}
                ...

 

最后如下调用


    	        SimplePlane plane = null;
    	
    	        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
    		plane = new SimplePlane();
    		plane.loadBitmap(mBitmap);


 

 

public abstract   void  glTexParameterf (int target, int pname, float param)函数参数的含义:

target —— 目标纹理,必须为GL_TEXTURE_1D或GL_TEXTURE_2D;

pname —— 用来设置纹理映射过程中像素映射的问题等,取值可以为:GL_TEXTURE_MIN_FILTER、GL_TEXTURE_MAG_FILTER、GL_TEXTURE_WRAP_S、GL_TEXTURE_WRAP_T,详细含义可以查看MSDN;

param —— 实际上就是pname的值,可以参考MSDN。
pname:
GL_TEXTURE_MIN_FILTER 设置最小过滤,第三个参数决定用什么过滤;
GL_TEXTURE_MAG_FILTER设置最大过滤,也是第三个参数决定;
GL_TEXTURE_WRAP_S;纹理坐标一般用str表示,分别对应xyz,2d纹理用st表示
GL_TEXTURE_WRAP_T   接上面,纹理和你画的几何体可能不是完全一样大的,在边界的时候如何处理呢?就是这两个参数决定的,wrap表示环绕,可以理解成让纹理重复使用,直到全部填充完成;
param;与第二个参数配合使用,一般取GL_LINEAR和GL_NEAREST,过滤形式

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值