/
SMMesh.java
144 lines (112 loc) · 4.6 KB
/
SMMesh.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.supernovamobile.smashout;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Vector;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;
public class SMMesh extends SMGroup {
private FloatBuffer mVerticesBuffer = null;
private FloatBuffer mColorBuffer = null;
private FloatBuffer mTextureBuffer = null;
private FloatBuffer mVNormalsBuffer = null;
private ShortBuffer mIndicesBuffer = null;
private int mNumIndices = -1;
protected int[] mTextures = new int[1];
private float[] mRGBA = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
protected String mTextureString = null;
@Override
public void draw(GL10 gl) throws IOException {
super.draw(gl);
if (mVerticesBuffer != null) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);
gl.glNormalPointer(3, GL10.GL_FLOAT, mVNormalsBuffer);
if (mTextureBuffer != null && mTextureString != null) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
}
gl.glColor4f(mRGBA[0], mRGBA[1], mRGBA[2], mRGBA[3]);
if (mColorBuffer != null) {
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
}
gl.glPushMatrix();
gl.glTranslatef(mX, mY, mZ);
gl.glRotatef(mRx, 1, 0, 0);
gl.glRotatef(mRy, 0, 1, 0);
gl.glRotatef(mRz, 0, 0, 1);
gl.glDrawElements(GL10.GL_TRIANGLES, mNumIndices, GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
if (mTextureString != null) {
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
gl.glDisable(GL10.GL_CULL_FACE);
}
}
public void setVertices(float[] vertices) {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVerticesBuffer = vbb.asFloatBuffer();
mVerticesBuffer.put(vertices);
mVerticesBuffer.position(0);
setNormals();
}
public void setNormals() {
}
public void setVNormals(float[] vnormals) {
ByteBuffer vnbb = ByteBuffer.allocateDirect(vnormals.length * 4);
vnbb.order(ByteOrder.nativeOrder());
mVNormalsBuffer = vnbb.asFloatBuffer();
mVNormalsBuffer.put(vnormals);
mVNormalsBuffer.position(0);
}
public void setIndices(short[] indices) {
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
mIndicesBuffer = ibb.asShortBuffer();
mIndicesBuffer.put(indices);
mIndicesBuffer.position(0);
mNumIndices = indices.length;
}
public void setTextureCoords(float[] coords) {
ByteBuffer tbb = ByteBuffer.allocateDirect(coords.length * 4);
tbb.order(ByteOrder.nativeOrder());
mTextureBuffer = tbb.asFloatBuffer();
mTextureBuffer.put(coords);
mTextureBuffer.position(0);
}
protected void setColor(float red, float green, float blue, float alpha) {
mRGBA[0] = red;
mRGBA[0] = green;
mRGBA[0] = blue;
mRGBA[0] = alpha;
}
protected void setColors(float[] colors) {
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
}
public void setTextureStr(String texture) {
mTextureString = texture;
}
public String getTextureStr() {
return mTextureString;
}
}