Api Demo - .graphics(24)>>Cube

package com.example.android.apis.graphics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

/**
 * A vertex shaded cube.
 */
class Cube
{
    public Cube()
    {
        int one = 0x10000;
      /*
       * 0x10000是出于OPENGL前期内存节约的考虑, 以INT型模拟FLOAT型来表示, 0x 0001 0000 前面4位表示小数点前,后4位表示小数点后,
       *所以0x10000表示浮点数的1。如果你用的是FloatBuffer,就可以知道此处应该写1.0
       */
        int vertices[] = {//顶点数组
                -one, -one, -one,
                one, -one, -one,
                one,  one, -one,
                -one,  one, -one,
                -one, -one,  one,
                one, -one,  one,
                one,  one,  one,
                -one,  one,  one,
        };

        int colors[] = {//颜色数组
                0,    0,    0,  one,
                one,    0,    0,  one,
                one,  one,    0,  one,
                0,  one,    0,  one,
                0,    0,  one,  one,
                one,    0,  one,  one,
                one,  one,  one,  one,
                0,  one,  one,  one,
        };

        byte indices[] = {//索引数组,即指定哪3个点构成一个面(三角形)
                0, 4, 5,    0, 5, 1,
                1, 5, 6,    1, 6, 2,
                2, 6, 7,    2, 7, 3,
                3, 7, 4,    3, 4, 0,
                4, 7, 6,    4, 6, 5,
                3, 0, 1,    3, 1, 2
        };


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);//建立顶点缓冲 int类型占8个字节,Byte占2个所以*4;
        vbb.order(ByteOrder.nativeOrder());//设置为本地平台字节顺序。
        mVertexBuffer = vbb.asIntBuffer();//将Byte缓冲转为int缓冲。
        mVertexBuffer.put(vertices);//放入顶点数组
        mVertexBuffer.position(0);//置0;

        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);//颜色缓冲
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asIntBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);//索引缓冲。
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glFrontFace(gl.GL_CW);//确定正面。
        gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);//为画笔指定顶点坐标
        gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);//为画笔指定顶点颜色
        gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);//索引法画图。
    }

    private IntBuffer   mVertexBuffer;
    private IntBuffer   mColorBuffer;
    private ByteBuffer  mIndexBuffer;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值