Android 真正的3D图形(一)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android 真正的3D图形(一)
Design设计
在使用OpenGL 框架时一个好的设计原则是使用“Composite Pattern”,本篇采用如下设计:
Mesh
首先定义一个基类Mesh,所有空间形体最基本的构成元素为Mesh(三角形网格),其基本定义如下:
Java代码:
Java代码
1publicclassMesh{
2//Ourvertexbuffer.
3privateFloatBufferverticesBuffer=null;
4
5//Ourindexbuffer.
6privateShortBufferindicesBuffer=null;
7
8//Thenumberofindices.
9privateintnumOfIndices=-1;
10
11//FlatColor
12privatefloat[]rgba
13=newfloat[]{1.0f,1.0f,1.0f,1.0f};
14
15//SmoothColors
16privateFloatBuffercolorBuffer=null;
17
18//Translateparams.
19publicfloatx=0;
20
21publicfloaty=0;
22
23publicfloatz=0;
24
25//Rotateparams.
26publicfloatrx=0;
27
28publicfloatry=0;
29
30publicfloatrz=0;
31
32publicvoiddraw(GL10gl){
33//Counter-clockwisewinding.
34gl.glFrontFace(GL10.GL_CCW);
35//Enablefaceculling.
36gl.glEnable(GL10.GL_CULL_FACE);
37//Whatfacestoremovewiththefaceculling.
38gl.glCullFace(GL10.GL_BACK);
39//Enabledtheverticesbufferforwritingand
40//tobeusedduring
41//rendering.
42gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
43//Specifiesthelocationanddataformat
44//ofanarrayofvertex
45//coordinatestousewhenrendering.
46gl.glV ertexPointer(3,GL10.GL_FLOA T,0,verticesBuffer); 47//Setflatcolor
48gl.glColor4f(rgba[0],rgba[1],rgba[2],rgba[3]);
49//Smoothcolor
50if(colorBuffer!=null){
51//Enablethecolorarraybuffertobe
52//usedduringrendering.
53gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
54gl.glColorPointer(4,GL10.GL_FLOA T,0,colorBuffer); 55}
56
57gl.glTranslatef(x,y,z);
58gl.glRotatef(rx,1,0,0);
59gl.glRotatef(ry,0,1,0);
60gl.glRotatef(rz,0,0,1);
61
62//Pointoutthewherethecolorbufferis.
63gl.glDrawElements(GL10.GL_TRIANGLES,numOfIndices, 64GL10.GL_UNSIGNED_SHORT,indicesBuffer);
65//Disabletheverticesbuffer.
66gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
67//Disablefaceculling.
68gl.glDisable(GL10.GL_CULL_FACE);
69}
70
71protectedvoidsetV ertices(float[]vertices){
72//afloatis4bytes,therefore
73//wemultiplythenumberif
74//verticeswith4.
75ByteBuffervbb
76=ByteBuffer.allocateDirect(vertices.length*4);
77vbb.order(ByteOrder.nativeOrder());
78verticesBuffer=vbb.asFloatBuffer();
79verticesBuffer.put(vertices);
80verticesBuffer.position(0);
81}
82
83protectedvoidsetIndices(short[]indices){
84//shortis2bytes,thereforewemultiply
85//thenumberif
86//verticeswith2.
87ByteBufferibb
88=ByteBuffer.allocateDirect(indices.length*2);
89ibb.order(ByteOrder.nativeOrder());
90indicesBuffer=ibb.asShortBuffer();
91indicesBuffer.put(indices);
92indicesBuffer.position(0);
93numOfIndices=indices.length;
94}
95
96protectedvoidsetColor(floatred,floatgreen,
97floatblue,floatalpha){
98//Settingtheflatcolor.
99rgba[0]=red;
100rgba[1]=green;
101rgba[2]=blue;
102rgba[3]=alpha;
103}
104
105protectedvoidsetColors(float[]colors){
106//floathas4bytes.
107ByteBuffercbb
108=ByteBuffer.allocateDirect(colors.length*4);
109cbb.order(ByteOrder.nativeOrder());
110colorBuffer=cbb.asFloatBuffer();
111colorBuffer.put(colors);
112colorBuffer.position(0);
113}
114}
* setV ertices 允许子类重新定义顶点坐标。
* setIndices 允许子类重新定义顶点的顺序。