質問編集履歴
10
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -533,3 +533,103 @@
|
|
533
533
|
|
534
534
|
|
535
535
|
「contextの持ち回し」や「クラス間で共有」などと調べても目的にあったものを見つけ出すことができませんでした。
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
#追記
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
```java
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
public void connect() {
|
550
|
+
|
551
|
+
new AsyncTask<Void, String, String>() {
|
552
|
+
|
553
|
+
@Override
|
554
|
+
|
555
|
+
protected String doInBackground(Void... voids) {
|
556
|
+
|
557
|
+
while (true) {
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
Socket socket = null;
|
562
|
+
|
563
|
+
BufferedReader reader = null;
|
564
|
+
|
565
|
+
try {
|
566
|
+
|
567
|
+
socket = new Socket("アドレス", 4000);
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
reader = new BufferedReader(
|
572
|
+
|
573
|
+
new InputStreamReader(socket.getInputStream()));
|
574
|
+
|
575
|
+
|
576
|
+
|
577
|
+
String temp = reader.readLine();
|
578
|
+
|
579
|
+
synchronized(sync) {
|
580
|
+
|
581
|
+
line = temp;
|
582
|
+
|
583
|
+
}
|
584
|
+
|
585
|
+
} catch(Exception e) {
|
586
|
+
|
587
|
+
e.printStackTrace();
|
588
|
+
|
589
|
+
} finally {
|
590
|
+
|
591
|
+
if(reader != null) {
|
592
|
+
|
593
|
+
try {
|
594
|
+
|
595
|
+
reader.close();
|
596
|
+
|
597
|
+
} catch(Exception e) {}
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
if(socket != null) {
|
602
|
+
|
603
|
+
try {
|
604
|
+
|
605
|
+
socket.close();
|
606
|
+
|
607
|
+
} catch(Exception e) {}
|
608
|
+
|
609
|
+
}
|
610
|
+
|
611
|
+
}
|
612
|
+
|
613
|
+
}
|
614
|
+
|
615
|
+
return null;
|
616
|
+
|
617
|
+
}
|
618
|
+
|
619
|
+
}.execute();
|
620
|
+
|
621
|
+
}
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
|
630
|
+
|
631
|
+
```
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
connect()修正したがreturn nullでエラーが出てしまう。
|
9
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,60 +30,322 @@
|
|
30
30
|
|
31
31
|
glView=new MyGLSurfaceView(this);
|
32
32
|
|
33
|
-
setContentView(glView);
|
33
|
+
setContentView(glView);
|
34
|
-
|
35
|
-
|
36
34
|
|
37
35
|
}
|
38
36
|
|
39
37
|
|
40
38
|
|
41
|
-
|
39
|
+
@Override
|
40
|
+
|
42
|
-
|
41
|
+
public void onResume() {
|
42
|
+
|
43
|
+
//中略
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
@Override
|
48
|
+
|
49
|
+
public void onPause() {
|
50
|
+
|
51
|
+
//中略
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
#MyGLSurfaceView.java
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
```java
|
66
|
+
|
67
|
+
public class MyGLSurfaceView extends GLSurfaceView {
|
68
|
+
|
69
|
+
private final Context mContext;
|
70
|
+
|
71
|
+
public GLRenderer renderer;
|
72
|
+
|
73
|
+
private static final String TAG = "MyGLSurfaceView";
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
public MyGLSurfaceView(Context context) {
|
80
|
+
|
81
|
+
super(context);
|
82
|
+
|
83
|
+
mContext=context;
|
84
|
+
|
85
|
+
setEGLContextClientVersion(2);
|
86
|
+
|
87
|
+
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
88
|
+
|
89
|
+
renderer = new GLRenderer(context);
|
90
|
+
|
91
|
+
setRenderer(renderer);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
#GLRenderer.java
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```java
|
106
|
+
|
107
|
+
import android.content.Context;
|
108
|
+
|
109
|
+
import android.opengl.GLES20;
|
110
|
+
|
111
|
+
import android.opengl.GLSurfaceView;
|
112
|
+
|
113
|
+
import android.opengl.Matrix;
|
114
|
+
|
115
|
+
import android.os.AsyncTask;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
import java.io.BufferedReader;
|
120
|
+
|
121
|
+
import java.io.IOException;
|
122
|
+
|
123
|
+
import java.io.InputStreamReader;
|
124
|
+
|
125
|
+
import java.net.Socket;
|
126
|
+
|
127
|
+
import java.nio.FloatBuffer;
|
128
|
+
|
129
|
+
import javax.microedition.khronos.egl.EGLConfig;
|
130
|
+
|
131
|
+
import javax.microedition.khronos.opengles.GL10;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
public class GLRenderer implements GLSurfaceView.Renderer {
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
private String line = "0";
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
private Object sync = new Object();
|
148
|
+
|
149
|
+
//システム
|
150
|
+
|
151
|
+
private final Context mContext;
|
152
|
+
|
153
|
+
private boolean validProgram=false; //シェーダプログラムが有効
|
154
|
+
|
155
|
+
private RotationSensor myRotationSensor=null; //加速度方位GPSセンサ
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
private float aspect;//アスペクト比
|
160
|
+
|
161
|
+
private float viewingangle = 45f; //視野角(画面長手方向に対応する視野角[deg])
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
//光源の座標 x,y,z
|
168
|
+
|
169
|
+
private float[] LightPos={-10f,1.5f,20f,1f};//x,y,z,1
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
//変換マトリックス
|
174
|
+
|
175
|
+
private float[] pMatrix=new float[16]; //プロジェクション変換マトリックス
|
176
|
+
|
177
|
+
private float[] mMatrix=new float[16]; //モデル変換マトリックス
|
178
|
+
|
179
|
+
private float[] cMatrix=new float[16]; //カメラビュー変換マトリックス
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
private Sphere MySphere=new Sphere(40,20);
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
//シェーダのattribute属性の変数に値を設定していないと暴走するのでそのための準備
|
192
|
+
|
193
|
+
private static float[] DummyFloat= new float[1];
|
194
|
+
|
195
|
+
private static final FloatBuffer DummyBuffer=BufferUtil.makeFloatBuffer(DummyFloat);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
GLRenderer(final Context context) {
|
200
|
+
|
201
|
+
mContext = context;
|
202
|
+
|
203
|
+
connect();
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//サーフェイス生成時に呼ばれる
|
210
|
+
|
211
|
+
@Override
|
212
|
+
|
213
|
+
public void onSurfaceCreated(GL10 gl10,EGLConfig eglConfig) {
|
214
|
+
|
215
|
+
//プログラムの生成
|
216
|
+
|
217
|
+
validProgram = GLES.makeProgram();
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
//頂点配列の有効化
|
222
|
+
|
223
|
+
GLES20.glEnableVertexAttribArray(GLES.positionHandle);
|
224
|
+
|
225
|
+
GLES20.glEnableVertexAttribArray(GLES.normalHandle);
|
226
|
+
|
227
|
+
GLES20.glEnableVertexAttribArray(GLES.texcoordHandle);
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
//デプスバッファの有効化
|
232
|
+
|
233
|
+
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
// カリングの有効化
|
238
|
+
|
239
|
+
GLES20.glEnable(GLES20.GL_CULL_FACE); //裏面を表示しないチェックを行う
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
// 裏面を描画しない
|
244
|
+
|
245
|
+
GLES20.glFrontFace(GLES20.GL_CCW); //表面のvertexのindex番号はCCWで登録
|
246
|
+
|
247
|
+
GLES20.glCullFace(GLES20.GL_BACK); //裏面は表示しない
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
//光源色の指定 (r, g, b,a)
|
252
|
+
|
253
|
+
GLES20.glUniform4f(GLES.lightAmbientHandle, 0.15f, 0.15f, 0.15f, 1.0f); //周辺光
|
254
|
+
|
255
|
+
GLES20.glUniform4f(GLES.lightDiffuseHandle, 0.5f, 0.5f, 0.5f, 1.0f); //乱反射光
|
256
|
+
|
257
|
+
GLES20.glUniform4f(GLES.lightSpecularHandle, 0.9f, 0.9f, 0.9f, 1.0f); //鏡面反射光
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
//背景色の設定
|
262
|
+
|
263
|
+
GLES20.glClearColor(0f, 0f, 0f, 1.0f);
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
//テクスチャの有効化
|
268
|
+
|
269
|
+
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
// 背景とのブレンド方法を設定します。
|
274
|
+
|
275
|
+
GLES20.glEnable(GLES20.GL_BLEND);
|
276
|
+
|
277
|
+
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); // 単純なアルファブレンド
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
public void setRotationSensor(RotationSensor s) {
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
myRotationSensor=s;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
43
|
-
public void connect() {
|
299
|
+
public void connect() {
|
44
300
|
|
45
301
|
new AsyncTask<Void, String, String>() {
|
46
302
|
|
47
|
-
|
48
|
-
|
49
303
|
@Override
|
50
304
|
|
51
305
|
protected String doInBackground(Void... voids) {
|
52
306
|
|
53
|
-
|
54
|
-
|
55
307
|
try {
|
56
308
|
|
57
309
|
while (true) {
|
58
310
|
|
59
311
|
Socket socket = new Socket("アドレス", 4000);
|
60
312
|
|
61
|
-
|
62
|
-
|
63
313
|
BufferedReader reader = new BufferedReader(
|
64
314
|
|
65
315
|
new InputStreamReader(socket.getInputStream()));
|
66
316
|
|
67
317
|
|
68
318
|
|
69
|
-
String line;
|
70
|
-
|
71
|
-
|
319
|
+
String temp = reader.readLine();
|
320
|
+
|
321
|
+
synchronized (sync) { // 別スレッドでlineを書き換えるので、synchronizedで同期を取る
|
322
|
+
|
323
|
+
line = temp;
|
324
|
+
|
325
|
+
}
|
72
326
|
|
73
327
|
|
74
328
|
|
75
329
|
reader.close();
|
76
330
|
|
77
|
-
socket.close();
|
331
|
+
socket.close();
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
//return temp;
|
78
336
|
|
79
337
|
}
|
80
338
|
|
81
|
-
}catch (IOException e) {
|
339
|
+
} catch (IOException e) {
|
82
340
|
|
83
341
|
e.printStackTrace();
|
84
342
|
|
85
343
|
}
|
86
344
|
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
87
349
|
return null;
|
88
350
|
|
89
351
|
}
|
@@ -92,19 +354,167 @@
|
|
92
354
|
|
93
355
|
}
|
94
356
|
|
357
|
+
|
358
|
+
|
359
|
+
//画面サイズ変更時に呼ばれる
|
360
|
+
|
95
|
-
@Override
|
361
|
+
@Override
|
96
|
-
|
362
|
+
|
97
|
-
public void on
|
363
|
+
public void onSurfaceChanged(GL10 gl10,int w,int h) {
|
98
|
-
|
364
|
+
|
99
|
-
//
|
365
|
+
//ビューポート変換
|
366
|
+
|
100
|
-
|
367
|
+
GLES20.glViewport(0,0,w,h);
|
368
|
+
|
369
|
+
aspect=(float)w/(float)h;
|
370
|
+
|
101
|
-
}
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
102
|
-
|
375
|
+
//毎フレーム描画時に呼ばれる
|
376
|
+
|
103
|
-
|
377
|
+
@Override
|
104
|
-
|
378
|
+
|
105
|
-
public void on
|
379
|
+
public void onDrawFrame(GL10 glUnused) {
|
380
|
+
|
106
|
-
|
381
|
+
if (!validProgram) return;
|
382
|
+
|
383
|
+
//シェーダのattribute属性の変数に値を設定していないと暴走するのでここでセットしておく。この位置でないといけない
|
384
|
+
|
385
|
+
GLES20.glVertexAttribPointer(GLES.positionHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
386
|
+
|
387
|
+
GLES20.glVertexAttribPointer(GLES.normalHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
388
|
+
|
389
|
+
GLES20.glVertexAttribPointer(GLES.texcoordHandle, 2, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
GLES.disableTexture(); //テクスチャ機能を無効にする。(デフォルト)
|
394
|
+
|
395
|
+
GLES.enableShading(); //シェーディング機能を有効にする。(デフォルト)
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
//画面のクリア
|
400
|
+
|
401
|
+
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
|
402
|
+
|
403
|
+
GLES20.GL_DEPTH_BUFFER_BIT);
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
//プロジェクション変換(射影変換)--------------------------------------
|
408
|
+
|
409
|
+
//透視変換(遠近感を作る)
|
410
|
+
|
411
|
+
//カメラは原点に有り,z軸の負の方向を向いていて,上方向はy軸+方向である。
|
412
|
+
|
413
|
+
GLES.gluPerspective(pMatrix,
|
414
|
+
|
415
|
+
viewingangle, //Y方向の画角
|
416
|
+
|
417
|
+
aspect, //アスペクト比
|
418
|
+
|
419
|
+
.1f, //ニアクリップ z=-.1から
|
420
|
+
|
421
|
+
20.0f);//ファークリップ Z=-10までの範囲を表示することになる
|
422
|
+
|
423
|
+
GLES.setPMatrix(pMatrix);
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
float[] c1Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
428
|
+
|
429
|
+
float[] c2Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
//カメラビュー変換(視野変換)-----------------------------------
|
436
|
+
|
437
|
+
//portraitで使うアプリを想定
|
438
|
+
|
439
|
+
//カメラ視点が原点になるような変換 カメラを下向き,長手方向奥側を2D画面の上にする
|
440
|
+
|
441
|
+
Matrix.setLookAtM(c1Matrix, 0,//アイポイント、ビューの中心、アップベクトルの観点からのビュー変換を定義します。
|
442
|
+
|
443
|
+
0f,0f,0f, //カメラの視点 x,y,z
|
444
|
+
|
445
|
+
0.0f, 0.0f, -1.0f, //カメラの焦点
|
446
|
+
|
447
|
+
0.0f, 1.0f, 0.0f);//カメラの上方向 画面長手方向前方
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
c2Matrix = myRotationSensor.getRotMatrix();
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
Matrix.translateM(c2Matrix,0,3f,0f,0f);
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
Matrix.multiplyMM(cMatrix, 0, c1Matrix, 0, c2Matrix, 0); //cMatrix = c1Matrix * c2Matrix
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
//カメラビュー変換はこれで終わり。
|
466
|
+
|
467
|
+
GLES.setCMatrix(cMatrix);
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
//cMatrixをセットしてから光源位置をセット
|
472
|
+
|
473
|
+
GLES.setLightPosition(LightPos);
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
int value = 0;
|
478
|
+
|
479
|
+
synchronized(sync) { // 別スレッドでlineが書き換わるかもしれないので、synchronizedで同期を取る
|
480
|
+
|
481
|
+
value = Integer.parseInt(line);
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
107
|
-
|
489
|
+
//ボール
|
490
|
+
|
491
|
+
Matrix.setIdentityM(mMatrix, 0);
|
492
|
+
|
493
|
+
Matrix.translateM(mMatrix, 0, value, 0f, 0f);
|
494
|
+
|
495
|
+
Matrix.scaleM(mMatrix, 0, 0.3f, 0.3f, 0.3f);
|
496
|
+
|
497
|
+
GLES.updateMatrix(mMatrix);//現在の変換行列をシェーダに指定
|
498
|
+
|
499
|
+
//MySphereの描画本体
|
500
|
+
|
501
|
+
// r, g, b, a, shininess(1以上の値 大きな値ほど鋭くなる)
|
502
|
+
|
503
|
+
MySphere.draw(1.5f, 1.5f, 0f, 1f, 5.f);
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
GLES.enableShading(); //シェーディング機能を使う設定に戻す
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
GLES.enableTexture();
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
|
108
518
|
|
109
519
|
}
|
110
520
|
|
@@ -112,476 +522,10 @@
|
|
112
522
|
|
113
523
|
}
|
114
524
|
|
525
|
+
|
526
|
+
|
115
527
|
```
|
116
528
|
|
117
|
-
#MyGLSurfaceView.java
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
```java
|
122
|
-
|
123
|
-
public class MyGLSurfaceView extends GLSurfaceView {
|
124
|
-
|
125
|
-
private final Context mContext;
|
126
|
-
|
127
|
-
public GLRenderer renderer;
|
128
|
-
|
129
|
-
private static final String TAG = "MyGLSurfaceView";
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
public MyGLSurfaceView(Context context) {
|
136
|
-
|
137
|
-
super(context);
|
138
|
-
|
139
|
-
mContext=context;
|
140
|
-
|
141
|
-
setEGLContextClientVersion(2);
|
142
|
-
|
143
|
-
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
144
|
-
|
145
|
-
renderer = new GLRenderer(context);
|
146
|
-
|
147
|
-
setRenderer(renderer);
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
#GLRenderer.java
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
```java
|
162
|
-
|
163
|
-
import android.content.Context;
|
164
|
-
|
165
|
-
import android.opengl.GLES20;
|
166
|
-
|
167
|
-
import android.opengl.GLSurfaceView;
|
168
|
-
|
169
|
-
import android.opengl.Matrix;
|
170
|
-
|
171
|
-
import android.os.AsyncTask;
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
import java.io.BufferedReader;
|
176
|
-
|
177
|
-
import java.io.IOException;
|
178
|
-
|
179
|
-
import java.io.InputStreamReader;
|
180
|
-
|
181
|
-
import java.net.Socket;
|
182
|
-
|
183
|
-
import java.nio.FloatBuffer;
|
184
|
-
|
185
|
-
import javax.microedition.khronos.egl.EGLConfig;
|
186
|
-
|
187
|
-
import javax.microedition.khronos.opengles.GL10;
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
public class GLRenderer implements GLSurfaceView.Renderer {
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
private String line = "0";
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
private Object sync = new Object();
|
204
|
-
|
205
|
-
//システム
|
206
|
-
|
207
|
-
private final Context mContext;
|
208
|
-
|
209
|
-
private boolean validProgram=false; //シェーダプログラムが有効
|
210
|
-
|
211
|
-
private RotationSensor myRotationSensor=null; //加速度方位GPSセンサ
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
private float aspect;//アスペクト比
|
216
|
-
|
217
|
-
private float viewingangle = 45f; //視野角(画面長手方向に対応する視野角[deg])
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
//光源の座標 x,y,z
|
224
|
-
|
225
|
-
private float[] LightPos={-10f,1.5f,20f,1f};//x,y,z,1
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
//変換マトリックス
|
230
|
-
|
231
|
-
private float[] pMatrix=new float[16]; //プロジェクション変換マトリックス
|
232
|
-
|
233
|
-
private float[] mMatrix=new float[16]; //モデル変換マトリックス
|
234
|
-
|
235
|
-
private float[] cMatrix=new float[16]; //カメラビュー変換マトリックス
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
private Sphere MySphere=new Sphere(40,20);
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
//シェーダのattribute属性の変数に値を設定していないと暴走するのでそのための準備
|
248
|
-
|
249
|
-
private static float[] DummyFloat= new float[1];
|
250
|
-
|
251
|
-
private static final FloatBuffer DummyBuffer=BufferUtil.makeFloatBuffer(DummyFloat);
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
GLRenderer(final Context context) {
|
256
|
-
|
257
|
-
mContext = context;
|
258
|
-
|
259
|
-
connect();
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
//サーフェイス生成時に呼ばれる
|
266
|
-
|
267
|
-
@Override
|
268
|
-
|
269
|
-
public void onSurfaceCreated(GL10 gl10,EGLConfig eglConfig) {
|
270
|
-
|
271
|
-
//プログラムの生成
|
272
|
-
|
273
|
-
validProgram = GLES.makeProgram();
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
//頂点配列の有効化
|
278
|
-
|
279
|
-
GLES20.glEnableVertexAttribArray(GLES.positionHandle);
|
280
|
-
|
281
|
-
GLES20.glEnableVertexAttribArray(GLES.normalHandle);
|
282
|
-
|
283
|
-
GLES20.glEnableVertexAttribArray(GLES.texcoordHandle);
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
//デプスバッファの有効化
|
288
|
-
|
289
|
-
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
// カリングの有効化
|
294
|
-
|
295
|
-
GLES20.glEnable(GLES20.GL_CULL_FACE); //裏面を表示しないチェックを行う
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
// 裏面を描画しない
|
300
|
-
|
301
|
-
GLES20.glFrontFace(GLES20.GL_CCW); //表面のvertexのindex番号はCCWで登録
|
302
|
-
|
303
|
-
GLES20.glCullFace(GLES20.GL_BACK); //裏面は表示しない
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
//光源色の指定 (r, g, b,a)
|
308
|
-
|
309
|
-
GLES20.glUniform4f(GLES.lightAmbientHandle, 0.15f, 0.15f, 0.15f, 1.0f); //周辺光
|
310
|
-
|
311
|
-
GLES20.glUniform4f(GLES.lightDiffuseHandle, 0.5f, 0.5f, 0.5f, 1.0f); //乱反射光
|
312
|
-
|
313
|
-
GLES20.glUniform4f(GLES.lightSpecularHandle, 0.9f, 0.9f, 0.9f, 1.0f); //鏡面反射光
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
//背景色の設定
|
318
|
-
|
319
|
-
GLES20.glClearColor(0f, 0f, 0f, 1.0f);
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
//テクスチャの有効化
|
324
|
-
|
325
|
-
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
// 背景とのブレンド方法を設定します。
|
330
|
-
|
331
|
-
GLES20.glEnable(GLES20.GL_BLEND);
|
332
|
-
|
333
|
-
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); // 単純なアルファブレンド
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
public void setRotationSensor(RotationSensor s) {
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
myRotationSensor=s;
|
350
|
-
|
351
|
-
}
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
public void connect() {
|
356
|
-
|
357
|
-
new AsyncTask<Void, String, String>() {
|
358
|
-
|
359
|
-
@Override
|
360
|
-
|
361
|
-
protected String doInBackground(Void... voids) {
|
362
|
-
|
363
|
-
try {
|
364
|
-
|
365
|
-
while (true) {
|
366
|
-
|
367
|
-
Socket socket = new Socket("アドレス", 4000);
|
368
|
-
|
369
|
-
BufferedReader reader = new BufferedReader(
|
370
|
-
|
371
|
-
new InputStreamReader(socket.getInputStream()));
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
String temp = reader.readLine();
|
376
|
-
|
377
|
-
synchronized (sync) { // 別スレッドでlineを書き換えるので、synchronizedで同期を取る
|
378
|
-
|
379
|
-
line = temp;
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
reader.close();
|
386
|
-
|
387
|
-
socket.close();
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
return temp;
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
} catch (IOException e) {
|
396
|
-
|
397
|
-
e.printStackTrace();
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
return null;
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
}.execute();
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
//画面サイズ変更時に呼ばれる
|
416
|
-
|
417
|
-
@Override
|
418
|
-
|
419
|
-
public void onSurfaceChanged(GL10 gl10,int w,int h) {
|
420
|
-
|
421
|
-
//ビューポート変換
|
422
|
-
|
423
|
-
GLES20.glViewport(0,0,w,h);
|
424
|
-
|
425
|
-
aspect=(float)w/(float)h;
|
426
|
-
|
427
|
-
}
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
//毎フレーム描画時に呼ばれる
|
432
|
-
|
433
|
-
@Override
|
434
|
-
|
435
|
-
public void onDrawFrame(GL10 glUnused) {
|
436
|
-
|
437
|
-
if (!validProgram) return;
|
438
|
-
|
439
|
-
//シェーダのattribute属性の変数に値を設定していないと暴走するのでここでセットしておく。この位置でないといけない
|
440
|
-
|
441
|
-
GLES20.glVertexAttribPointer(GLES.positionHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
442
|
-
|
443
|
-
GLES20.glVertexAttribPointer(GLES.normalHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
444
|
-
|
445
|
-
GLES20.glVertexAttribPointer(GLES.texcoordHandle, 2, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
GLES.disableTexture(); //テクスチャ機能を無効にする。(デフォルト)
|
450
|
-
|
451
|
-
GLES.enableShading(); //シェーディング機能を有効にする。(デフォルト)
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
//画面のクリア
|
456
|
-
|
457
|
-
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
|
458
|
-
|
459
|
-
GLES20.GL_DEPTH_BUFFER_BIT);
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
//プロジェクション変換(射影変換)--------------------------------------
|
464
|
-
|
465
|
-
//透視変換(遠近感を作る)
|
466
|
-
|
467
|
-
//カメラは原点に有り,z軸の負の方向を向いていて,上方向はy軸+方向である。
|
468
|
-
|
469
|
-
GLES.gluPerspective(pMatrix,
|
470
|
-
|
471
|
-
viewingangle, //Y方向の画角
|
472
|
-
|
473
|
-
aspect, //アスペクト比
|
474
|
-
|
475
|
-
.1f, //ニアクリップ z=-.1から
|
476
|
-
|
477
|
-
20.0f);//ファークリップ Z=-10までの範囲を表示することになる
|
478
|
-
|
479
|
-
GLES.setPMatrix(pMatrix);
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
float[] c1Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
484
|
-
|
485
|
-
float[] c2Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
//カメラビュー変換(視野変換)-----------------------------------
|
492
|
-
|
493
|
-
//portraitで使うアプリを想定
|
494
|
-
|
495
|
-
//カメラ視点が原点になるような変換 カメラを下向き,長手方向奥側を2D画面の上にする
|
496
|
-
|
497
|
-
Matrix.setLookAtM(c1Matrix, 0,//アイポイント、ビューの中心、アップベクトルの観点からのビュー変換を定義します。
|
498
|
-
|
499
|
-
0f,0f,0f, //カメラの視点 x,y,z
|
500
|
-
|
501
|
-
0.0f, 0.0f, -1.0f, //カメラの焦点
|
502
|
-
|
503
|
-
0.0f, 1.0f, 0.0f);//カメラの上方向 画面長手方向前方
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
c2Matrix = myRotationSensor.getRotMatrix();
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
Matrix.translateM(c2Matrix,0,3f,0f,0f);
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
Matrix.multiplyMM(cMatrix, 0, c1Matrix, 0, c2Matrix, 0); //cMatrix = c1Matrix * c2Matrix
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
//カメラビュー変換はこれで終わり。
|
522
|
-
|
523
|
-
GLES.setCMatrix(cMatrix);
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
//cMatrixをセットしてから光源位置をセット
|
528
|
-
|
529
|
-
GLES.setLightPosition(LightPos);
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
int value = 0;
|
534
|
-
|
535
|
-
synchronized(sync) { // 別スレッドでlineが書き換わるかもしれないので、synchronizedで同期を取る
|
536
|
-
|
537
|
-
value = Integer.parseInt(line);
|
538
|
-
|
539
|
-
}
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
//ボール
|
546
|
-
|
547
|
-
Matrix.setIdentityM(mMatrix, 0);
|
548
|
-
|
549
|
-
Matrix.translateM(mMatrix, 0, value, 0f, 0f);
|
550
|
-
|
551
|
-
Matrix.scaleM(mMatrix, 0, 0.3f, 0.3f, 0.3f);
|
552
|
-
|
553
|
-
GLES.updateMatrix(mMatrix);//現在の変換行列をシェーダに指定
|
554
|
-
|
555
|
-
//MySphereの描画本体
|
556
|
-
|
557
|
-
// r, g, b, a, shininess(1以上の値 大きな値ほど鋭くなる)
|
558
|
-
|
559
|
-
MySphere.draw(1.5f, 1.5f, 0f, 1f, 5.f);
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
GLES.enableShading(); //シェーディング機能を使う設定に戻す
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
GLES.enableTexture();
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
}
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
}
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
```
|
584
|
-
|
585
529
|
|
586
530
|
|
587
531
|
contextを用いずとも、このコードを新しく作ったクラスに書きインスタンスを渡すことで実現可能でしょうか?
|
8
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
setContentView(glView);
|
34
34
|
|
35
|
-
|
35
|
+
|
36
36
|
|
37
37
|
}
|
38
38
|
|
@@ -344,7 +344,7 @@
|
|
344
344
|
|
345
345
|
public void setRotationSensor(RotationSensor s) {
|
346
346
|
|
347
|
-
|
347
|
+
|
348
348
|
|
349
349
|
myRotationSensor=s;
|
350
350
|
|
7
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -256,6 +256,8 @@
|
|
256
256
|
|
257
257
|
mContext = context;
|
258
258
|
|
259
|
+
connect();
|
260
|
+
|
259
261
|
}
|
260
262
|
|
261
263
|
|
6
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -160,40 +160,424 @@
|
|
160
160
|
|
161
161
|
```java
|
162
162
|
|
163
|
+
import android.content.Context;
|
164
|
+
|
165
|
+
import android.opengl.GLES20;
|
166
|
+
|
167
|
+
import android.opengl.GLSurfaceView;
|
168
|
+
|
169
|
+
import android.opengl.Matrix;
|
170
|
+
|
171
|
+
import android.os.AsyncTask;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
import java.io.BufferedReader;
|
176
|
+
|
177
|
+
import java.io.IOException;
|
178
|
+
|
179
|
+
import java.io.InputStreamReader;
|
180
|
+
|
181
|
+
import java.net.Socket;
|
182
|
+
|
183
|
+
import java.nio.FloatBuffer;
|
184
|
+
|
185
|
+
import javax.microedition.khronos.egl.EGLConfig;
|
186
|
+
|
187
|
+
import javax.microedition.khronos.opengles.GL10;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
163
195
|
public class GLRenderer implements GLSurfaceView.Renderer {
|
164
196
|
|
165
197
|
|
166
198
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
p
|
178
|
-
|
179
|
-
//
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
199
|
+
private String line = "0";
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
private Object sync = new Object();
|
204
|
+
|
205
|
+
//システム
|
206
|
+
|
207
|
+
private final Context mContext;
|
208
|
+
|
209
|
+
private boolean validProgram=false; //シェーダプログラムが有効
|
210
|
+
|
211
|
+
private RotationSensor myRotationSensor=null; //加速度方位GPSセンサ
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
private float aspect;//アスペクト比
|
216
|
+
|
217
|
+
private float viewingangle = 45f; //視野角(画面長手方向に対応する視野角[deg])
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
//光源の座標 x,y,z
|
224
|
+
|
225
|
+
private float[] LightPos={-10f,1.5f,20f,1f};//x,y,z,1
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
//変換マトリックス
|
230
|
+
|
231
|
+
private float[] pMatrix=new float[16]; //プロジェクション変換マトリックス
|
232
|
+
|
233
|
+
private float[] mMatrix=new float[16]; //モデル変換マトリックス
|
234
|
+
|
235
|
+
private float[] cMatrix=new float[16]; //カメラビュー変換マトリックス
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
private Sphere MySphere=new Sphere(40,20);
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
//シェーダのattribute属性の変数に値を設定していないと暴走するのでそのための準備
|
248
|
+
|
249
|
+
private static float[] DummyFloat= new float[1];
|
250
|
+
|
251
|
+
private static final FloatBuffer DummyBuffer=BufferUtil.makeFloatBuffer(DummyFloat);
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
GLRenderer(final Context context) {
|
256
|
+
|
257
|
+
mContext = context;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
//サーフェイス生成時に呼ばれる
|
264
|
+
|
265
|
+
@Override
|
266
|
+
|
267
|
+
public void onSurfaceCreated(GL10 gl10,EGLConfig eglConfig) {
|
268
|
+
|
269
|
+
//プログラムの生成
|
270
|
+
|
271
|
+
validProgram = GLES.makeProgram();
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
//頂点配列の有効化
|
276
|
+
|
277
|
+
GLES20.glEnableVertexAttribArray(GLES.positionHandle);
|
278
|
+
|
279
|
+
GLES20.glEnableVertexAttribArray(GLES.normalHandle);
|
280
|
+
|
281
|
+
GLES20.glEnableVertexAttribArray(GLES.texcoordHandle);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
//デプスバッファの有効化
|
286
|
+
|
287
|
+
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
// カリングの有効化
|
292
|
+
|
293
|
+
GLES20.glEnable(GLES20.GL_CULL_FACE); //裏面を表示しないチェックを行う
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
// 裏面を描画しない
|
298
|
+
|
299
|
+
GLES20.glFrontFace(GLES20.GL_CCW); //表面のvertexのindex番号はCCWで登録
|
300
|
+
|
301
|
+
GLES20.glCullFace(GLES20.GL_BACK); //裏面は表示しない
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
//光源色の指定 (r, g, b,a)
|
306
|
+
|
307
|
+
GLES20.glUniform4f(GLES.lightAmbientHandle, 0.15f, 0.15f, 0.15f, 1.0f); //周辺光
|
308
|
+
|
309
|
+
GLES20.glUniform4f(GLES.lightDiffuseHandle, 0.5f, 0.5f, 0.5f, 1.0f); //乱反射光
|
310
|
+
|
311
|
+
GLES20.glUniform4f(GLES.lightSpecularHandle, 0.9f, 0.9f, 0.9f, 1.0f); //鏡面反射光
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
//背景色の設定
|
316
|
+
|
317
|
+
GLES20.glClearColor(0f, 0f, 0f, 1.0f);
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
//テクスチャの有効化
|
322
|
+
|
323
|
+
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
// 背景とのブレンド方法を設定します。
|
328
|
+
|
329
|
+
GLES20.glEnable(GLES20.GL_BLEND);
|
330
|
+
|
331
|
+
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); // 単純なアルファブレンド
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
public void setRotationSensor(RotationSensor s) {
|
344
|
+
|
345
|
+
connect();
|
346
|
+
|
347
|
+
myRotationSensor=s;
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
public void connect() {
|
354
|
+
|
355
|
+
new AsyncTask<Void, String, String>() {
|
356
|
+
|
357
|
+
@Override
|
358
|
+
|
359
|
+
protected String doInBackground(Void... voids) {
|
360
|
+
|
361
|
+
try {
|
362
|
+
|
363
|
+
while (true) {
|
364
|
+
|
365
|
+
Socket socket = new Socket("アドレス", 4000);
|
366
|
+
|
367
|
+
BufferedReader reader = new BufferedReader(
|
368
|
+
|
369
|
+
new InputStreamReader(socket.getInputStream()));
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
String temp = reader.readLine();
|
374
|
+
|
375
|
+
synchronized (sync) { // 別スレッドでlineを書き換えるので、synchronizedで同期を取る
|
376
|
+
|
377
|
+
line = temp;
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
reader.close();
|
384
|
+
|
385
|
+
socket.close();
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
return temp;
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
} catch (IOException e) {
|
394
|
+
|
395
|
+
e.printStackTrace();
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
return null;
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
}.execute();
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
//画面サイズ変更時に呼ばれる
|
414
|
+
|
415
|
+
@Override
|
416
|
+
|
417
|
+
public void onSurfaceChanged(GL10 gl10,int w,int h) {
|
418
|
+
|
419
|
+
//ビューポート変換
|
420
|
+
|
421
|
+
GLES20.glViewport(0,0,w,h);
|
422
|
+
|
423
|
+
aspect=(float)w/(float)h;
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
//毎フレーム描画時に呼ばれる
|
430
|
+
|
431
|
+
@Override
|
432
|
+
|
433
|
+
public void onDrawFrame(GL10 glUnused) {
|
434
|
+
|
435
|
+
if (!validProgram) return;
|
436
|
+
|
437
|
+
//シェーダのattribute属性の変数に値を設定していないと暴走するのでここでセットしておく。この位置でないといけない
|
438
|
+
|
439
|
+
GLES20.glVertexAttribPointer(GLES.positionHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
440
|
+
|
441
|
+
GLES20.glVertexAttribPointer(GLES.normalHandle, 3, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
442
|
+
|
443
|
+
GLES20.glVertexAttribPointer(GLES.texcoordHandle, 2, GLES20.GL_FLOAT, false, 0, DummyBuffer);
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
GLES.disableTexture(); //テクスチャ機能を無効にする。(デフォルト)
|
448
|
+
|
449
|
+
GLES.enableShading(); //シェーディング機能を有効にする。(デフォルト)
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
//画面のクリア
|
454
|
+
|
455
|
+
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
|
456
|
+
|
457
|
+
GLES20.GL_DEPTH_BUFFER_BIT);
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
//プロジェクション変換(射影変換)--------------------------------------
|
462
|
+
|
463
|
+
//透視変換(遠近感を作る)
|
464
|
+
|
465
|
+
//カメラは原点に有り,z軸の負の方向を向いていて,上方向はy軸+方向である。
|
466
|
+
|
467
|
+
GLES.gluPerspective(pMatrix,
|
468
|
+
|
469
|
+
viewingangle, //Y方向の画角
|
470
|
+
|
471
|
+
aspect, //アスペクト比
|
472
|
+
|
473
|
+
.1f, //ニアクリップ z=-.1から
|
474
|
+
|
475
|
+
20.0f);//ファークリップ Z=-10までの範囲を表示することになる
|
476
|
+
|
477
|
+
GLES.setPMatrix(pMatrix);
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
float[] c1Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
482
|
+
|
483
|
+
float[] c2Matrix=new float[16]; //カメラ視点変換マトリックス作成用
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
//カメラビュー変換(視野変換)-----------------------------------
|
490
|
+
|
491
|
+
//portraitで使うアプリを想定
|
492
|
+
|
493
|
+
//カメラ視点が原点になるような変換 カメラを下向き,長手方向奥側を2D画面の上にする
|
494
|
+
|
495
|
+
Matrix.setLookAtM(c1Matrix, 0,//アイポイント、ビューの中心、アップベクトルの観点からのビュー変換を定義します。
|
496
|
+
|
497
|
+
0f,0f,0f, //カメラの視点 x,y,z
|
498
|
+
|
499
|
+
0.0f, 0.0f, -1.0f, //カメラの焦点
|
500
|
+
|
501
|
+
0.0f, 1.0f, 0.0f);//カメラの上方向 画面長手方向前方
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
c2Matrix = myRotationSensor.getRotMatrix();
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
Matrix.translateM(c2Matrix,0,3f,0f,0f);
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
Matrix.multiplyMM(cMatrix, 0, c1Matrix, 0, c2Matrix, 0); //cMatrix = c1Matrix * c2Matrix
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
//カメラビュー変換はこれで終わり。
|
520
|
+
|
521
|
+
GLES.setCMatrix(cMatrix);
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
//cMatrixをセットしてから光源位置をセット
|
526
|
+
|
527
|
+
GLES.setLightPosition(LightPos);
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
int value = 0;
|
532
|
+
|
533
|
+
synchronized(sync) { // 別スレッドでlineが書き換わるかもしれないので、synchronizedで同期を取る
|
534
|
+
|
535
|
+
value = Integer.parseInt(line);
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
//ボール
|
544
|
+
|
545
|
+
Matrix.setIdentityM(mMatrix, 0);
|
546
|
+
|
547
|
+
Matrix.translateM(mMatrix, 0, value, 0f, 0f);
|
548
|
+
|
549
|
+
Matrix.scaleM(mMatrix, 0, 0.3f, 0.3f, 0.3f);
|
550
|
+
|
551
|
+
GLES.updateMatrix(mMatrix);//現在の変換行列をシェーダに指定
|
552
|
+
|
553
|
+
//MySphereの描画本体
|
554
|
+
|
555
|
+
// r, g, b, a, shininess(1以上の値 大きな値ほど鋭くなる)
|
556
|
+
|
557
|
+
MySphere.draw(1.5f, 1.5f, 0f, 1f, 5.f);
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
GLES.enableShading(); //シェーディング機能を使う設定に戻す
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
GLES.enableTexture();
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
}
|
192
574
|
|
193
575
|
|
194
576
|
|
195
577
|
}
|
196
578
|
|
579
|
+
|
580
|
+
|
197
581
|
```
|
198
582
|
|
199
583
|
|
5
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,15 +18,21 @@
|
|
18
18
|
|
19
19
|
public class MainActivity extends Activity {
|
20
20
|
|
21
|
-
|
21
|
+
private MyGLSurfaceView glView;
|
22
|
+
|
23
|
+
|
22
24
|
|
23
25
|
@Override
|
24
26
|
|
25
27
|
protected void onCreate(Bundle savedInstanceState) {
|
26
28
|
|
27
|
-
|
29
|
+
super.onCreate(savedInstanceState);
|
30
|
+
|
28
|
-
|
31
|
+
glView=new MyGLSurfaceView(this);
|
32
|
+
|
33
|
+
setContentView(glView);
|
34
|
+
|
29
|
-
|
35
|
+
connect();
|
30
36
|
|
31
37
|
}
|
32
38
|
|
@@ -108,6 +114,44 @@
|
|
108
114
|
|
109
115
|
```
|
110
116
|
|
117
|
+
#MyGLSurfaceView.java
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```java
|
122
|
+
|
123
|
+
public class MyGLSurfaceView extends GLSurfaceView {
|
124
|
+
|
125
|
+
private final Context mContext;
|
126
|
+
|
127
|
+
public GLRenderer renderer;
|
128
|
+
|
129
|
+
private static final String TAG = "MyGLSurfaceView";
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
public MyGLSurfaceView(Context context) {
|
136
|
+
|
137
|
+
super(context);
|
138
|
+
|
139
|
+
mContext=context;
|
140
|
+
|
141
|
+
setEGLContextClientVersion(2);
|
142
|
+
|
143
|
+
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
|
144
|
+
|
145
|
+
renderer = new GLRenderer(context);
|
146
|
+
|
147
|
+
setRenderer(renderer);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
```
|
154
|
+
|
111
155
|
|
112
156
|
|
113
157
|
#GLRenderer.java
|
4
情報追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,6 +7,10 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
ちなみにこのコードはMainActivityの@Override onCreateの中にあり,String line;を使いたいのは他クラスに書いている@Override onDrawFrameです。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#MainActivity.java
|
10
14
|
|
11
15
|
|
12
16
|
|
@@ -106,6 +110,10 @@
|
|
106
110
|
|
107
111
|
|
108
112
|
|
113
|
+
#GLRenderer.java
|
114
|
+
|
115
|
+
|
116
|
+
|
109
117
|
```java
|
110
118
|
|
111
119
|
public class GLRenderer implements GLSurfaceView.Renderer {
|
3
コードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,11 +12,27 @@
|
|
12
12
|
|
13
13
|
```java
|
14
14
|
|
15
|
+
public class MainActivity extends Activity {
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
@Override
|
20
|
+
|
21
|
+
protected void onCreate(Bundle savedInstanceState) {
|
22
|
+
|
23
|
+
super.onCreate(savedInstanceState);
|
24
|
+
|
25
|
+
connect();
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
15
|
-
public void connect() {
|
33
|
+
public void connect() {
|
16
34
|
|
17
35
|
new AsyncTask<Void, String, String>() {
|
18
|
-
|
19
|
-
|
20
36
|
|
21
37
|
|
22
38
|
|
@@ -48,11 +64,7 @@
|
|
48
64
|
|
49
65
|
reader.close();
|
50
66
|
|
51
|
-
socket.close();
|
67
|
+
socket.close();
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
68
|
|
57
69
|
}
|
58
70
|
|
@@ -66,11 +78,69 @@
|
|
66
78
|
|
67
79
|
}
|
68
80
|
|
69
|
-
|
70
|
-
|
71
81
|
}.execute();
|
72
82
|
|
73
83
|
}
|
84
|
+
|
85
|
+
@Override
|
86
|
+
|
87
|
+
public void onResume() {
|
88
|
+
|
89
|
+
//中略
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
@Override
|
94
|
+
|
95
|
+
public void onPause() {
|
96
|
+
|
97
|
+
//中略
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
```java
|
110
|
+
|
111
|
+
public class GLRenderer implements GLSurfaceView.Renderer {
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
@Override
|
116
|
+
|
117
|
+
public void onSurfaceCreated(GL10 gl10,EGLConfig eglConfig) {
|
118
|
+
|
119
|
+
//中略
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
@Override
|
124
|
+
|
125
|
+
public void onSurfaceChanged(GL10 gl10,int w,int h) {
|
126
|
+
|
127
|
+
//中略
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
//毎フレーム描画時に呼ばれる
|
132
|
+
|
133
|
+
@Override
|
134
|
+
|
135
|
+
public void onDrawFrame(GL10 glUnused) {
|
136
|
+
|
137
|
+
//ここでlineをintに変換して使いたいです。
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
}
|
74
144
|
|
75
145
|
```
|
76
146
|
|
2
情報の追加をしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
Macからandroidへソケット通信を用いて下のコードにより獲得することができますがそのコードにあるString line;をこの関数の外で(他クラスで)使えるようにするにはどうしたらいいのでしょうか?
|
6
6
|
|
7
7
|
|
8
|
+
|
9
|
+
ちなみにこのコードはMainActivityの@Override onCreateの中にあり,String line;を使いたいのは他クラスに書いている@Override onDrawFrameです。
|
8
10
|
|
9
11
|
|
10
12
|
|
@@ -28,7 +30,7 @@
|
|
28
30
|
|
29
31
|
while (true) {
|
30
32
|
|
31
|
-
Socket socket = new Socket("
|
33
|
+
Socket socket = new Socket("アドレス", 4000);
|
32
34
|
|
33
35
|
|
34
36
|
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
Macからandroidへソケット通信を用いて下のコードにより獲得することができますがそのコードにあるString line;をこの関数の外で使えるようにするにはどうしたらいいのでしょうか?
|
5
|
+
Macからandroidへソケット通信を用いて下のコードにより獲得することができますがそのコードにあるString line;をこの関数の外で(他クラスで)使えるようにするにはどうしたらいいのでしょうか?
|
6
6
|
|
7
7
|
|
8
8
|
|