diff options
-rw-r--r-- | opengl/tests/lighting1709/src/com/android/lightingtest/ClearActivity.java | 181 |
1 files changed, 37 insertions, 144 deletions
diff --git a/opengl/tests/lighting1709/src/com/android/lightingtest/ClearActivity.java b/opengl/tests/lighting1709/src/com/android/lightingtest/ClearActivity.java index 3dc31cc..3ae8c5c 100644 --- a/opengl/tests/lighting1709/src/com/android/lightingtest/ClearActivity.java +++ b/opengl/tests/lighting1709/src/com/android/lightingtest/ClearActivity.java @@ -34,8 +34,6 @@ import android.view.MotionEvent; public class ClearActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { - instance = counter++; - Log.e("ClearActivity", ":::::: onCreate: instance" + instance + " is created"); super.onCreate(savedInstanceState); mGLView = new ClearGLSurfaceView(this); setContentView(mGLView); @@ -43,96 +41,37 @@ public class ClearActivity extends Activity { @Override protected void onPause() { - Log.e("ClearActivity", ":::::: instance" + instance + " onPause: is called"); super.onPause(); mGLView.onPause(); } @Override protected void onResume() { - Log.e("ClearActivity", ":::::: instance" + instance + " onResume: is called"); super.onResume(); mGLView.onResume(); } - - @Override - protected void onStop() { - Log.e("ClearActivity", ":::::: instance" + instance + " onStop: is called"); - super.onStop(); - } - - @Override - protected void onDestroy() { - Log.e("ClearActivity", ":::::: instance" + instance + " onDestroy: is called"); - super.onDestroy(); - } - private GLSurfaceView mGLView; - - private static int counter = 0; - private int instance; } class ClearGLSurfaceView extends GLSurfaceView { public ClearGLSurfaceView(Context context) { super(context); - instance = counter++; - Log.e("ClearGLSurfaceView", ":::::: instance" + instance + " is created"); mRenderer = new ClearRenderer(); setRenderer(mRenderer); } - public boolean onTouchEvent(final MotionEvent event) { - switch (event.getAction()) { - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_MOVE: {// falling through on purpose here - Log.e("ClearGLSurfaceView", ":::::: instance" + instance + " onTouchEvent: handling down or move action"); - queueEvent(new Runnable(){ - public void run() { - mRenderer.setColor(event.getX() / getWidth(), - event.getY() / getHeight(), 1.0f); - }} - ); - return true; - } - case MotionEvent.ACTION_UP: { - // launch a second instance of the same activity - Log.e("ClearGLSurfaceView", ":::::: instance" + instance + " onTouchEvent: handling up action"); - // Intent intent = new Intent(); - // intent.setClass(getContext(), ClearActivity.class); - // getContext().startActivity(intent); - } - - } - return true; - } - - @Override - protected void onDetachedFromWindow() { - Log.e("ClearGLSurfaceView", ":::::: instance" + instance + " onDetachedFromWindow: is called"); - super.onDetachedFromWindow(); - } - ClearRenderer mRenderer; - - private static int counter = 0; - private int instance; } class ClearRenderer implements GLSurfaceView.Renderer { public ClearRenderer() { - instance = counter++; - Log.e("ClearRenderer", ":::::: instance" + instance + " is created"); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Do nothing special. - Log.e("ClearRenderer", ":::::: instance" + instance + " onSurfaceCreated: is called"); } public void onSurfaceChanged(GL10 gl, int w, int h) { - Log.e("ClearRenderer", ":::::: instance" + instance + " onSurfaceChanged: is called"); - // Compute the projection matrix gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); @@ -153,129 +92,83 @@ class ClearRenderer implements GLSurfaceView.Renderer { } public void onDrawFrame(GL10 gl) { - // gl.glClearColor(mRed, mGreen, mBlue, 1.0f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); - float lightOff[] = {0.0f, 0.0f, 0.0f, 1.0f}; - float lightAmbient[] = {5.0f, 0.0f, 0.0f, 1.0f}; - float lightDiffuse[] = {0.0f, 2.0f, 0.0f, 0.0f}; - float lightPosAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f}; - float lightPosSpot[] = {0.0f, 0.0f, -8.0f, 1.0f}; + final float lightOff[] = {0.0f, 0.0f, 0.0f, 1.0f}; + final float lightAmbient[] = {5.0f, 0.0f, 0.0f, 1.0f}; + final float lightDiffuse[] = {0.0f, 2.0f, 0.0f, 0.0f}; + final float lightPosSpot[] = {0.0f, 0.0f, -8.0f, 1.0f}; + final float pos[] = { + -5.0f, -1.5f, 0.0f, + 0.0f, -1.5f, 0.0f, + 5.0f, -1.5f, 0.0f, + }; - float v[] = new float[9]; + final float v[] = new float[9]; ByteBuffer vbb = ByteBuffer.allocateDirect(v.length*4); vbb.order(ByteOrder.nativeOrder()); FloatBuffer vb = vbb.asFloatBuffer(); gl.glDisable(GL10.GL_DITHER); - gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightOff, 0); - gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightOff, 0); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0); - gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosAmbient, 0); + gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0); + gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightOff, 0); + gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosSpot, 0); gl.glEnable(GL10.GL_LIGHT0); - - gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, lightOff, 0); - gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse, 0); - gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightOff, 0); - gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosSpot, 0); - gl.glLightf(GL10.GL_LIGHT1, GL10.GL_CONSTANT_ATTENUATION, 1.0f); - gl.glLightf(GL10.GL_LIGHT1, GL10.GL_LINEAR_ATTENUATION, 0.0f); - gl.glLightf(GL10.GL_LIGHT1, GL10.GL_QUADRATIC_ATTENUATION, 0.022f); - gl.glEnable(GL10.GL_LIGHT1); - + gl.glEnable(GL10.GL_LIGHTING); - // draw upper left triangle - gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = -6f; v[1] = 0.5f; v[2] = -10f; - v[3] = -5f; v[4] = 2.5f; v[5] = -10f; - v[6] = -4f; v[7] = 0.5f; v[8] = -10f; - vb.put(v).position(0); - gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); - gl.glNormal3f(0, 0, 1); - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); - // draw upper middle triangle gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = -1f; v[1] = 0.5f; v[2] = -10f; - v[3] = 0f; v[4] = 2.5f; v[5] = -10f; - v[6] = 1f; v[7] = 0.5f; v[8] = -10f; - vb.put(v).position(0); - gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); gl.glNormal3f(0, 0, 1); - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); + - // draw upper right triangle - gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = 4f; v[1] = 0.5f; v[2] = -10f; - v[3] = 5f; v[4] = 2.5f; v[5] = -10f; - v[6] = 6f; v[7] = 0.5f; v[8] = -10f; + // draw first 3 triangles, without using transforms + for (int i=0 ; i<3 ; i++) { + v[0] = -1; v[1] =-1; v[2] = -10; + v[3] = 0; v[4] = 1; v[5] = -10; + v[6] = 1; v[7] =-1; v[8] = -10; + for (int j=0 ; j<3 ; j++) { + v[j*3+0] -= pos[i*3+0]; + v[j*3+1] -= pos[i*3+1]; + v[j*3+2] -= pos[i*3+2]; + } + vb.put(v).position(0); + gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); + gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); + } + + // draw the 2nd batch this time with transforms + v[0] = -1; v[1] =-1; v[2] = -10; + v[3] = 0; v[4] = 1; v[5] = -10; + v[6] = 1; v[7] =-1; v[8] = -10; vb.put(v).position(0); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); - gl.glNormal3f(0, 0, 1); - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); // draw lower left triangle gl.glPushMatrix(); - gl.glTranslatef(-5.0f, -1.5f, 0.0f); - gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = -1; v[1] = -1; v[2] = -10; - v[3] = 0; v[4] = 1; v[5] = -10; - v[6] = 1; v[7] = -1; v[8] = -10; - vb.put(v).position(0); - gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); - gl.glNormal3f(0, 0, 1); + gl.glTranslatef(pos[0], pos[1], pos[2]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); gl.glPopMatrix(); // draw lower middle triangle gl.glPushMatrix(); - gl.glTranslatef(0.0f, -1.5f, 0.0f); - gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = -1; v[1] = -1; v[2] = -10; - v[3] = 0; v[4] = 1; v[5] = -10; - v[6] = 1; v[7] = -1; v[8] = -10; - vb.put(v).position(0); - gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); - gl.glNormal3f(0, 0, 1); + gl.glTranslatef(pos[3], pos[4], pos[5]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); gl.glPopMatrix(); // draw lower right triangle gl.glPushMatrix(); - gl.glTranslatef(5.0f, -1.5f, 0.0f); - gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); - v[0] = -1; v[1] = -1; v[2] = -10; - v[3] = 0; v[4] = 1; v[5] = -10; - v[6] = 1; v[7] = -1; v[8] = -10; - vb.put(v).position(0); - gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb); - gl.glNormal3f(0, 0, 1); + gl.glTranslatef(pos[6], pos[7], pos[8]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); gl.glPopMatrix(); - } public int[] getConfigSpec() { - Log.e("ClearRenderer", ":::::: instance" + instance + " getConfigSpec: is called"); int[] configSpec = { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE }; return configSpec; } - - public void setColor(float r, float g, float b) { - Log.e("ClearRenderer", ":::::: instance" + instance + " setColor: is called"); - mRed = r; - mGreen = g; - mBlue = b; - } - - private float mRed; - private float mGreen; - private float mBlue; - - private static int counter = 0; - private int instance; } |