Android平台3D引擎研究2

在上一篇文章中,我们看到可以通过很少的代码,就可以在屏幕上显示出一个立方体,相当于min3d中的Hello World程序。但是,对于OpenGL ES编程来说,我们通常需要处理的数据是三角片,可惜使用min3d这样的引擎把这些细节给隐藏了。在这里我们自己手动生成一个放在原点上的立方体,给出顶点、三角片、法向量,然后由min3d引擎来显示,这样可以更深入理解OpenGL ES的工作原理。具体实现如下代码所示:

public MgnavObject(boolean useUvs, boolean useNormals, boolean useVertexColors) {
super(4*6, 2*6, useUvs, useNormals,useVertexColors);
float width = 1.0f / 2;
float height = 1.0f / 2;
float deepth = 1.0f / 2;
float textureU = 0.0f;
float textureV = 0.0f;
float normalX = 0.0f;
float normalY = 0.0f;
float normalZ = 0.0f;
short colorR = 0;
short colorG = 0;
short colorB = 0;
short colorA = 0;
// 显示正对的立方体:
// 面编号:从前面开始,面编号为1(前)、2(右)、3(后)、4(左)、5(上)、6(下)
// 顶点编号:前面从左上角点开始,顺时针1、2、3、4;后面与前面编号规则相同即4对5,2对6,3对7,4对8
// 如顶点1在前面时编号为:f1p1,对与上面时f5p1,参与左面时f4p1
// 1. front
normalX = 0f;
normalY = 0f;
normalZ = 1.0f;
// 1.1. 加入前面右上部三角片
short f1p1Idx = vertices().addVertex(-width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f1p3Idx = vertices().addVertex(width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f1p2Idx = vertices().addVertex(width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f1p1Idx, f1p3Idx, f1p2Idx);
// 1.2. 加入前面的左下部三角片
short f1p4Idx = vertices().addVertex(-width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f1p1Idx, f1p4Idx, f1p3Idx);
// 2. 加右面
normalX = 1.0f;
normalY = 0.0f;
normalZ = 0.0f;
// 2.1. 加三角片2,3,7
short f2p2Idx = vertices().addVertex(width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f2p3Idx = vertices().addVertex(width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f2p7Idx = vertices().addVertex(width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f2p2Idx, f2p3Idx, f2p7Idx);
// 2.2. 加三角片2,7,5
short f2p5Idx = vertices().addVertex(width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f2p2Idx, f2p7Idx, f2p5Idx);
// 3. 加后面
normalX = 0.0f;
normalY = 0.0f;
normalZ = -1.0f;
// 3.1. 加三角片6,7,8
short f3p6Idx = vertices().addVertex(width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f3p7Idx = vertices().addVertex(width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f3p8Idx = vertices().addVertex(-width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f3p6Idx, f3p7Idx, f3p8Idx);
// 3.2. 加三角片6,8,5
short f3p5Idx = vertices().addVertex(-width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f3p6Idx, f3p8Idx, f3p5Idx);
// 4. 加左面4
normalX = -1.0f;
normalY = 0.0f;
normalZ = 0.0f;
// 4.1. 加三角片1,5,8
short f4p1Idx = vertices().addVertex(-width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f4p5Idx = vertices().addVertex(-width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f4p8Idx = vertices().addVertex(-width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f4p1Idx, f4p5Idx, f4p8Idx);
// 4.2. 加三角片1,8,4
short f4p4Idx = vertices().addVertex(-width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f4p1Idx, f4p8Idx, f4p4Idx);
// 5. 上面
normalX = 0.0f;
normalY = 1.0f;
normalZ = 0.0f;
// 5.1. 添加三角片2,6,5
short f5p2Idx = vertices().addVertex(width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f5p6Idx = vertices().addVertex(width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f5p5Idx = vertices().addVertex(-width, height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f5p2Idx, f5p6Idx, f5p5Idx);
// 5.2. 添加三角片2,5,1
short f5p1Idx = vertices().addVertex(-width, height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f5p2Idx, f5p5Idx, f5p1Idx);
// 6. 下面
normalX = 0.0f;
normalY = -1.0f;
normalZ = 0.0f;
// 6.1. 添加三角片3,4,8
short f6p3Idx = vertices().addVertex(width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f6p4Idx = vertices().addVertex(-width, -height, deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
short f6p8Idx = vertices().addVertex(-width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f6p3Idx, f6p4Idx, f6p8Idx);
// 6.2. 添加三角片3,8,7
short f6p7Idx = vertices().addVertex(width, -height, -deepth,
textureU, textureV,
normalX, normalY, normalZ,
colorR, colorG, colorB, colorA);
faces().add(f6p3Idx, f6p8Idx, f6p7Idx);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值