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
| package com.example.gl_lines_test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;
public class MyRenderer implements Renderer{
private Context mContext;
private FloatBuffer mVertexBuffer = null;
private ShortBuffer mTriangleBorderIndicesBuffer = null;
private int mNumOfTriangleBorderIndices = 0;
public float mAngleX = 0.0f;
public float mAngleY = 0.0f;
public float mAngleZ = 0.0f;
private float mPreviousX;
private float mPreviousY;
private final float TOUCH_SCALE_FACTOR = 0.6f;
private final float AXIS_SCALE_FACTOR = 1.5f;
private final int AXIS_WIDTH = 10;
private final float POINT_WIDTH = 10f;
public MyRenderer(Context context) {
mContext = context;
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -3.0f);
gl.glRotatef(mAngleX, 1, 0, 0);
gl.glRotatef(mAngleY, 0, 1, 0);
gl.glRotatef(mAngleZ, 0, 0, 1);
gl.glScalef(AXIS_SCALE_FACTOR, AXIS_SCALE_FACTOR, AXIS_SCALE_FACTOR);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glLineWidth(AXIS_WIDTH);
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINES, 0, 2);
gl.glDrawArrays(GL10.GL_LINES, 9, 2);
gl.glDrawArrays(GL10.GL_LINES, 11, 2);
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINES, 2, 2);
gl.glDrawArrays(GL10.GL_LINES, 13, 2);
gl.glDrawArrays(GL10.GL_LINES, 15, 2);
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINES, 4, 2);
gl.glDrawArrays(GL10.GL_LINES, 17, 2);
gl.glDrawArrays(GL10.GL_LINES, 19, 2);
gl.glPointSize(POINT_WIDTH);
gl.glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 5, 3);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
setAllBuffers();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float aspect = (float)width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-aspect, aspect, -1.0f, 1.0f, 1.0f, 10.0f);
}
private void setAllBuffers(){
float vertexlist[] = {
-1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f,-1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f,-1.0f,
-0.5f , -0.5f, 0.0f ,
0.5f, -0.5f, 0.0f ,
0.0f , 0.5f, 0.0f ,
0.9f, 0.1f, 0.0f,
1.0f, 0.0f, 0.0f,
0.9f,-0.1f, 0.0f,
1.0f, 0.0f, 0.0f,
-0.1f, 0.9f, 0.0f,
0.0f, 1.0f, 0.0f,
0.1f, 0.9f, 0.0f,
0.0f, 1.0f, 0.0f,
0.1f, 0.0f, 0.9f,
0.0f, 0.0f, 1.0f,
-0.1f,0.0f, 0.9f,
0.0f, 0.0f, 1.0f,
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertexlist);
mVertexBuffer.position(0);
}
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
mAngleY = (mAngleY + (int)(dx * TOUCH_SCALE_FACTOR) + 360) % 360;
mAngleX = (mAngleX + (int)(dy * TOUCH_SCALE_FACTOR) + 360) % 360;
break;
}
mPreviousX = x;
mPreviousY = y;
return true;
}
}
|