openGL es2.0 创建方向方体

package com.gzdxid.utils;

public class DrawCubeTextureLight {

	private DrawRectTextureLight frontRect = null;
	private DrawRectTextureLight leftRect = null;
	private DrawRectTextureLight bottomRect = null;
	private int flag;
	private float halfWidth;
	private float halfHeight;
	private float halfLength;
	final float tzz = 0.1f;
	final float tzs=0.0f;
	// flag==0:法线朝里;flag==1:法线朝外;
	public DrawCubeTextureLight(float width,float height,float length,int mProgram, int flag) {
		frontRect = new DrawRectTextureLight(width, height,mProgram);
		leftRect=new DrawRectTextureLight(height, length, mProgram);
		bottomRect=new DrawRectTextureLight(width, length, mProgram);
		this.flag = flag;
		this.halfWidth=width/2;
		this.halfHeight=height/2;
		this.halfLength=length/2;
	}

	// textureId:up-down-left-right-front-back
	public void drawSelf(int[] textureId) {
		switch (flag) {
		case 0:
			drawCube0(textureId);
			break;
		case 1:
			drawCube1(textureId);
			break;
		}
	}

	private void drawCube0(int[] textureId) {
		// TODO Auto-generated method stub
		// up
		MatrixState.pushMatrix();
		MatrixState.translate(0, halfHeight - tzz, 0);
		MatrixState.rotate(90, 1, 0, 0);
		bottomRect.drawSelf(textureId[0]);
		MatrixState.popMatrix();

		// down
		MatrixState.pushMatrix();
		MatrixState.translate(0, -halfHeight + tzz, 0);
		MatrixState.rotate(-90, 1, 0, 0);
		bottomRect.drawSelf(textureId[1]);
		MatrixState.popMatrix();

		// left
		MatrixState.pushMatrix();
		MatrixState.translate(-halfWidth + tzz, 0, 0);
		MatrixState.rotate(90, 0, 1, 0);
		leftRect.drawSelf(textureId[2]);
		MatrixState.popMatrix();

		// right
		MatrixState.pushMatrix();
		MatrixState.translate(halfWidth - tzz, 0, 0);
		MatrixState.rotate(-90, 0, 1, 0);
		leftRect.drawSelf(textureId[3]);
		MatrixState.popMatrix();

		// front
		MatrixState.pushMatrix();
		MatrixState.translate(0, 0, halfLength - tzz);
		MatrixState.rotate(180, 0, 1, 0);
		frontRect.drawSelf(textureId[4]);
		MatrixState.popMatrix();

		// back
		MatrixState.pushMatrix();
		MatrixState.translate(0, 0, -halfLength + tzz);
		frontRect.drawSelf(textureId[5]);
		MatrixState.popMatrix();

	}

	private void drawCube1(int[] textureId) {

		// up
		MatrixState.pushMatrix();
		MatrixState.translate(0, halfHeight - tzs, 0);
		MatrixState.rotate(-90, 1, 0, 0);
		bottomRect.drawSelf(textureId[0]);
		MatrixState.popMatrix();

		// down
		MatrixState.pushMatrix();
		MatrixState.translate(0, -halfHeight + tzs, 0);
		MatrixState.rotate(90, 1, 0, 0);
		bottomRect.drawSelf(textureId[1]);
		MatrixState.popMatrix();

		// left
		MatrixState.pushMatrix();
		MatrixState.translate(-halfWidth + tzs, 0, 0);
		MatrixState.rotate(-90, 0, 1, 0);
		leftRect.drawSelf(textureId[3]);
		MatrixState.popMatrix();

		// right
		MatrixState.pushMatrix();
		MatrixState.translate(halfWidth - tzs, 0, 0);
		MatrixState.rotate(90, 0, 1, 0);
		leftRect.drawSelf(textureId[2]);
		MatrixState.popMatrix();

		// front
		MatrixState.pushMatrix();
		MatrixState.translate(0, 0, halfLength - tzs);
		frontRect.drawSelf(textureId[4]);
		MatrixState.popMatrix();

		// back
		MatrixState.pushMatrix();
		MatrixState.translate(0, 0, -halfLength + tzs);
		MatrixState.rotate(180, 0, 1, 0);
		frontRect.drawSelf(textureId[5]);
		MatrixState.popMatrix();

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值