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();


}


}
posted @ 2015-03-03 23:32  Anzhongliu  阅读(127)  评论(0编辑  收藏  举报