android opengl es 立方體

來源:互聯網
上載者:User

繪製立方體的原理很簡單了,就是通過繪製三角形而來的,當然還可以通過繪製線來完成。

     

下面是核心代碼:

  

public class CubeRender implements Renderer {

Cube cb;
float ratio;
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -5);

cb.drawSelf(gl);
//tgl.drawSelf(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL10.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glShadeModel(GL10.GL_SMOOTH);
  ratio=(float)width/height;
  gl.glFrustumf(-ratio,ratio, -1, 1, 1,10);
  cb=new Cube(0,0,0,2,ratio);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  gl.glClearColor(0, 0, 0, 0);
  gl.glEnable(GL10.GL_DEPTH_TEST);
}

}

 

大家可以看到Cube的建構函式中,最後一個參數為 螢幕的 寬高比,

這是因為 我使用的是平板,寬高並不是1:1,畫出來的立方體 高度有點短,為了對此進行修複,

將寬高比傳入進去,在計算座標的時候  只需要將高度乘以ratio即可。

下面是Cube(立方體的代碼)

public class Cube {
private int one=50000;
private int cone=65535;
private int icont;
private IntBuffer pointBuffer;
private IntBuffer colorBuffer;
private ByteBuffer indexBuffer;
private int rotateX;
private int rotateY;
private int rotateZ;
public Cube(int centerX,int centerY,int centerZ,int sideLen,float ratio)
{
int leftX=centerX-sideLen/2;
int rightX=centerX+sideLen/2;
int bottomY=centerY-(int)(sideLen/2*ratio);//*ratio 對高度 進行修複
int topY=centerY+(int)(sideLen/2*ratio);//*ratio 對高度 進行修複
int outZ=centerZ+sideLen/2;
int inZ=centerZ-sideLen/2;

int[] points=new int[]{

leftX*one,bottomY*one,outZ*one,
rightX*one,bottomY*one,outZ*one,
rightX*one,topY*one,outZ*one,
leftX*one,topY*one,outZ*one,

leftX*one,bottomY*one,inZ*one,
leftX*one,topY*one,inZ*one,

rightX*one,bottomY*one,inZ*one,
rightX*one,topY*one,inZ*one,
};

byte[] index=new byte[]{
0,1,2,2,3,0,
0,4,3,3,5,4,
1,6,2,2,7,6,
4,5,6,5,7,6,
3,2,5,5,7,2,
0,1,4,4,6,1
};
icont=index.length;

int[] color=new int[]{
cone,0,0,0,
0,cone,0,0,
0,0,cone,0,
0,0,0,cone,
cone,0,0,cone,
cone,0,cone,0,
cone,cone,0,0,
cone,cone,0,cone,
};

ByteBuffer pBuf=ByteBuffer.allocateDirect(points.length*4);
pBuf.order(ByteOrder.nativeOrder());
pointBuffer=pBuf.asIntBuffer();
pointBuffer.put(points);
pointBuffer.flip();
pointBuffer.position(0);

ByteBuffer iBuf=ByteBuffer.allocateDirect(index.length);
iBuf.order(ByteOrder.nativeOrder());
indexBuffer=iBuf;
indexBuffer.put(index);
indexBuffer.flip();
indexBuffer.position(0);

ByteBuffer cBuf=ByteBuffer.allocateDirect(color.length*4);
cBuf.order(ByteOrder.nativeOrder());
colorBuffer=cBuf.asIntBuffer();
colorBuffer.put(color);
colorBuffer.flip();
colorBuffer.position(0);
}
public void drawSelf(GL10 gl)
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FIXED,0,pointBuffer);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4,GL10.GL_FIXED,0,colorBuffer);

gl.glRotatef(rotateX,1,0,0);
gl.glRotatef(rotateY,0,1,0);

gl.glDrawElements(GL10.GL_TRIANGLES,icont,GL10.GL_UNSIGNED_BYTE,indexBuffer);
}
/**
* @param rotateX the rotateX to set
*/
public void setRotateX(int rotateX) {
this.rotateX += rotateX;
}
/**
* @param rotateY the rotateY to set
*/
public void setRotateY(int rotateY) {
this.rotateY += rotateY;
}
/**
* @param rotateZ the rotateZ to set
*/
public void setRotateZ(int rotateZ) {
this.rotateZ += rotateZ;
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.