-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
256 lines (219 loc) · 7.44 KB
/
MainActivity.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
*
* @author Jacob Payne
* @author Shannon Jones
*
* @date Winter 2013
*
* Porting CIS 367 projects 1 and 2 from OpenGL to OpenGLES for Android:
* The Portal 2 Companion Cube traveling in an ellipse around
* a cone while rotating
*
*/
package edu.gvsu.payne.proj3;
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.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;
public class MainActivity extends Activity {
static boolean pause = false;
private GLSurfaceView glView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* remove the title bar at the top of screen */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/* remove the status bar, make it full screen */
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(new renderView());
//setContentView(glView);
setContentView(R.layout.activity_glview);
glView.requestRender();
/* we are going to replace the dummy view with our own GLView */
View dummy = (View) findViewById(R.id.graphics_view);
/* identify dummy's parent */
ViewGroup top = (ViewGroup) dummy.getParent();
/* copy over all layout parameters from dummy to your GLView */
glView.setLayoutParams(dummy.getLayoutParams());
/* replace by removing and adding */
int idx = top.indexOfChild(dummy);
top.removeViewAt(idx);
top.addView (glView, idx);
}
public void onToggleClicked(View v) {
if (pause) {
pause = false;
} else {
pause = true;
}
}
@Override
protected void onPause() {
super.onPause();
glView.onPause();
}
@Override
protected void onResume() {
super.onResume();
glView.onResume();
}
}
/**
* GLSurfaceView render implementation
*/
class renderView implements GLSurfaceView.Renderer {
ByteBuffer buff;
FloatBuffer faceVertexBuff;
FloatBuffer faceColorBuff;
ShortBuffer faceOrderBuff;
FloatBuffer light0_posBuff;
FloatBuffer light0_colorBuff;
FloatBuffer light1_posBuff;
FloatBuffer light1_colorBuff;
FloatBuffer light1_dirBuff;
// light 1
float light0_pos[] = {25.0f, 45.0f, -46.0f, 1.0f}; // position
float light0_color[] = {1.0f, 1.0f, 1.0f, 1.0f}; // color
//light 2
float light1_pos[] = {0, -20, -46, 1}; // position
float light1_color[] = {0.5f, 1.0f, 0.06f, 1.0f}; // color
float light1_dir[] = {0, 1, 0}; // direction (spotlight)
float x_coord = 0;
float y_coord = 0;
int z_rot= 0;
int animTime = 0;
public renderView () {
float[] faceVertexData = {-1f, 1f, 1f, // 0
1f, 1f, 1f, // 1
1f, -1f, 1f, // 2
-1f, -1f, 1f, // 3
-1f, 1f, -1f, // 4
1f, 1f, -1f, // 5
1f, -1f, -1f, // 6
-1f, -1f, -1f /* 7 */};
short[] faceOrderData = {0,2,1, // front t1
3,2,0, // front t2
4,7,3, // left t1
0,4,3, // left t2
1,2,5, // right t1
5,2,6, // right t2
4,0,5, // top t1
5,0,1, // top t2
5,6,4, // back t2
4,6,7, // back t2
2,3,7, // bottom t1
2,7,6 /* bottom t2 */};
float[] faceColorData = {
1, 0, 0, 1, // 0
1, 1, 0, 1, // 1
0, 0, 1, 1, // 2
0, 1, 1, 1, // 3
0, 0, 1, 1, // 4
1, 0, 1, 1, // 5
0, 1, 0, 1, // 6
1, 0, 1, 1 /* 7*/};
faceVertexBuff = ByteBuffer.allocateDirect(faceVertexData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
faceVertexBuff.put(faceVertexData);
faceVertexBuff.position(0);
faceColorBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
faceColorBuff.put(faceColorData);
faceColorBuff.position(0);
faceOrderBuff = ByteBuffer.allocateDirect(faceOrderData.length*4).order(ByteOrder.nativeOrder()).asShortBuffer();
faceOrderBuff.put(faceOrderData);
faceOrderBuff.position(0);
light0_posBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
light0_posBuff.put(light0_pos);
light0_posBuff.position(0);
light0_colorBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
light0_colorBuff.put(light0_pos);
light0_colorBuff.position(0);
light1_posBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
light1_posBuff.put(light0_pos);
light1_posBuff.position(0);
light1_colorBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
light1_colorBuff.put(light0_pos);
light1_colorBuff.position(0);
light1_dirBuff = ByteBuffer.allocateDirect(faceColorData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
light1_dirBuff.put(light0_pos);
light1_dirBuff.position(0);
}
/**
* Display callback
*/
@Override
public void onDrawFrame(GL10 gl) {
/* Clear the buffer, clear the matrix */
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, faceVertexBuff);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, faceColorBuff);
//gl.glNormalPointer(GL10.GL_FLOAT, 0, faceOrderBuff);
GLU.gluLookAt(gl, 0, -5, -8, 0, -4, 0, 0, 1, 0);
gl.glPushMatrix();
gl.glTranslatef(x_coord, y_coord, 0);
gl.glRotatef(z_rot, 1, 0, 0);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_SHORT, faceOrderBuff);
gl.glPopMatrix();
if (MainActivity.pause == false) {
z_rot++;
if (animTime>359) animTime=0;
//for(j)
float theta = (float) (animTime * (Math.PI/180.0f));
x_coord = (float) (2-(Math.sin(theta)+1)*2);
y_coord = (float) (.2-(Math.cos(theta)+2)*2);
animTime++;
}
}
/**
* Resized callback
*/
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45f, (float) width / (float) height, 0.1f, 100f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
/**
* Location for init() code
*/
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor (0.0f, 0.0f, 0.0f, 1.0f); /* black background */
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glCullFace(GL10.GL_BACK);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_DEPTH_TEST);
/* Enable shading */
gl.glEnable (GL10.GL_LIGHTING);
// gl.glEnable (GL10.GL_NORMALIZE); /* Tell OpenGL to renormalize normal vector
// after transformation */
// gl.glEnable(GL10.GL_COLOR_MATERIAL);
// gl.glColorMaterial(GL10.GL_FRONT, GL10.GL_AMBIENT_AND_DIFFUSE);
/* initialize two light sources */
// gl.glEnable (GL10.GL_LIGHT0);
gl.glLightfv (GL10.GL_LIGHT0, GL10.GL_AMBIENT, light0_colorBuff);
gl.glLightfv (GL10.GL_LIGHT0, GL10.GL_DIFFUSE, light0_colorBuff);
gl.glLightfv (GL10.GL_LIGHT0, GL10.GL_SPECULAR, light0_colorBuff);
gl.glEnable (GL10.GL_LIGHT1);
gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_AMBIENT, light1_colorBuff);
gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_DIFFUSE, light1_colorBuff);
gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_SPECULAR, light1_colorBuff);
gl.glLightf (GL10.GL_LIGHT1, GL10.GL_SPOT_CUTOFF, 40);
}
}