質問編集履歴

1

追加

2017/02/01 01:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,321 @@
21
21
  ```
22
22
 
23
23
  と入れればよいのでしょうか?
24
+
25
+ ```
26
+
27
+ class Cube {
28
+
29
+ //各バッファの接点
30
+
31
+ private FloatBuffer mVertexBuffer;
32
+
33
+ private FloatBuffer mNormalBuffer;
34
+
35
+ private ShortBuffer mIndexBuffer;
36
+
37
+ private FloatBuffer mTextureBuffer;
38
+
39
+ // 接続する頂点の配列数
40
+
41
+ private int numberOfindex = 36;
42
+
43
+ //テクスチャの管理id
44
+
45
+ private int textureId;
46
+
47
+
48
+
49
+ //角度
50
+
51
+ private float angle = 0f;
52
+
53
+
54
+
55
+ public Cube(GL10 gl, Resources r) {
56
+
57
+ float x = 2f;
58
+
59
+ float y = 2f;
60
+
61
+ float z = 0.1f;
62
+
63
+
64
+
65
+ //頂点
66
+
67
+ float[] vertices = {
68
+
69
+ x, y, z, -x, y, z, -x,-y, z, x,-y, z, // v0-v1-v2-v3 front
70
+
71
+ x, y, z, x,-y, z, x,-y,-z, x, y,-z, // v0-v3-v4-v5 right
72
+
73
+ x, y, z, x, y,-z, -x, y,-z, -x, y, z, // v0-v5-v6-v1 top
74
+
75
+ -x, y, z, -x, y,-z, -x,-y,-z, -x,-y, z, // v1-v6-v7-v2 left
76
+
77
+ -x,-y,-z, x,-y,-z, x,-y, z, -x,-y, z, // v7-v4-v3-v2 bottom
78
+
79
+ x,-y,-z, -x,-y,-z, -x, y,-z, x, y,-z // v4-v7-v6-v5 back
80
+
81
+ };
82
+
83
+ //テクスチャUV座標
84
+
85
+ float[] textures = {
86
+
87
+ 1, 0, 0, 0, 0, 1, 1, 1, // v0-v1-v2-v3 front
88
+
89
+ 0, 0, 0, 1, 1, 1, 1, 0, // v0-v3-v4-v5 right
90
+
91
+ 0, 1, 1, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top
92
+
93
+ 1, 0, 0, 0, 0, 1, 1, 1, // v1-v6-v7-v2 left
94
+
95
+ 0, 1, 1, 1, 1, 0, 0, 0, // v7-v4-v3-v2 bottom
96
+
97
+ 0, 1, 1, 1, 1, 0, 0, 0 // v4-v7-v6-v5 back
98
+
99
+ };
100
+
101
+
102
+
103
+ //法線ベクトル 表面と裏面どちらを向いているか
104
+
105
+ float[] normals = {
106
+
107
+ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2-v3 front
108
+
109
+ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
110
+
111
+ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top
112
+
113
+ -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7-v2 left
114
+
115
+ 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3-v2 bottom
116
+
117
+ 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 // v4-v7-v6-v5 back
118
+
119
+ };
120
+
121
+
122
+
123
+ short indices[] = {
124
+
125
+ 0, 1, 2, 0, 2, 3, // front
126
+
127
+ 4, 5, 6, 4, 6, 7, // right
128
+
129
+ 8, 9,10, 8,10,11, // top
130
+
131
+ 12,13,14, 12,14,15, // left
132
+
133
+ 16,17,18, 16,18,19, // bottom
134
+
135
+ 20,21,22, 20,22,23 // back
136
+
137
+ };
138
+
139
+
140
+
141
+
142
+
143
+ // Vertex
144
+
145
+ mVertexBuffer = makeFloatBuffer(vertices);
146
+
147
+
148
+
149
+ //Normal
150
+
151
+ mNormalBuffer = makeFloatBuffer(normals);
152
+
153
+
154
+
155
+ // index
156
+
157
+ mIndexBuffer = makeShortBuffer(indices);
158
+
159
+
160
+
161
+ // texture
162
+
163
+ mTextureBuffer = makeFloatBuffer(textures);
164
+
165
+
166
+
167
+ textureId = loadTexture(gl, r, R.drawable.one);
168
+
169
+ }
170
+
171
+
172
+
173
+
174
+
175
+ //システム上のメモリ領域を確保するためのメソッド
176
+
177
+ // Float用
178
+
179
+ public static final FloatBuffer makeFloatBuffer(float[] arr) {
180
+
181
+ ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
182
+
183
+ bb.order(ByteOrder.nativeOrder());
184
+
185
+ FloatBuffer fb = bb.asFloatBuffer();
186
+
187
+ fb.put(arr);
188
+
189
+ fb.position(0);
190
+
191
+ return fb;
192
+
193
+ }
194
+
195
+
196
+
197
+ //システム上のメモリ領域を確保するためのメソッド
198
+
199
+ //Short用
200
+
201
+ public static final ShortBuffer makeShortBuffer(short[] arr) {
202
+
203
+ ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
204
+
205
+ bb.order(ByteOrder.nativeOrder());
206
+
207
+ ShortBuffer fb = bb.asShortBuffer();
208
+
209
+ fb.put(arr);
210
+
211
+ fb.position(0);
212
+
213
+ return fb;
214
+
215
+ }
216
+
217
+
218
+
219
+ //描画
220
+
221
+ public void draw(GL10 gl) {
222
+
223
+ //ここではz軸方向に -3.0だけ移動させます.
224
+
225
+ gl.glTranslatef(0, 0, -30.0f);
226
+
227
+
228
+
229
+ //マトリクスの回転
230
+
231
+ gl.glRotatef(angle++, 0, 1, 0);
232
+
233
+
234
+
235
+ //テクスチャ機能の有効化
236
+
237
+ gl.glEnable(GL10.GL_TEXTURE_2D);
238
+
239
+ //テクスチャオブジェクトの指定
240
+
241
+ gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
242
+
243
+
244
+
245
+ // 頂点のポインタを設定
246
+
247
+ gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
248
+
249
+
250
+
251
+ //法線ベクトルの指定
252
+
253
+ gl.glNormalPointer(GL10.GL_FLOAT,0,mNormalBuffer);
254
+
255
+ gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
256
+
257
+
258
+
259
+ //テクスチャのポインタを設定
260
+
261
+ gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
262
+
263
+
264
+
265
+ // 頂点の描画
266
+
267
+ // gl.glDrawElements( プリミティブ, 頂点の数, タイプ, インデックス)
268
+
269
+ gl.glDrawElements(GL10.GL_TRIANGLES, numberOfindex, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
270
+
271
+
272
+
273
+ }
274
+
275
+ /**
276
+
277
+ * テクスチャを読み込む.
278
+
279
+ * 読み込む画像のサイズは2の階乗の値でなければならない.
280
+
281
+ */
282
+
283
+ public static final int loadTexture(GL10 gl, Resources r, int resId) {
284
+
285
+ int[] textures = new int[1];
286
+
287
+
288
+
289
+ //読み込むBitmapのオプションの設定
290
+
291
+ BitmapFactory.Options options = new BitmapFactory.Options();
292
+
293
+ //リソースの自動リサイズをしない
294
+
295
+ options.inScaled = false;
296
+
297
+ // 32bit画像として読み込む
298
+
299
+ options.inPreferredConfig = Config.ARGB_8888;
300
+
301
+ //Bitmapの作成
302
+
303
+ Bitmap bmp = BitmapFactory.decodeResource(r, resId, options);
304
+
305
+ if (bmp == null) {
306
+
307
+ return 0;
308
+
309
+ }
310
+
311
+
312
+
313
+ // OpenGL用のテクスチャを生成する
314
+
315
+ gl.glGenTextures(1, textures, 0);
316
+
317
+ gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
318
+
319
+ GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
320
+
321
+ gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
322
+
323
+ gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
324
+
325
+ gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
326
+
327
+
328
+
329
+ //OpenGLへの転送が完了したので、VMメモリ上に作成したBitmapを破棄する
330
+
331
+ bmp.recycle();
332
+
333
+
334
+
335
+ return textures[0];
336
+
337
+ }
338
+
339
+ }
340
+
341
+ ```