diff --git a/src/com/homelinux/holtrop/opengltest/MyRenderer.java b/src/com/homelinux/holtrop/opengltest/MyRenderer.java index 78e80c5..4cd8f39 100644 --- a/src/com/homelinux/holtrop/opengltest/MyRenderer.java +++ b/src/com/homelinux/holtrop/opengltest/MyRenderer.java @@ -25,6 +25,14 @@ import java.lang.Math; public class MyRenderer implements GLSurfaceView.Renderer { + class Tile + { + public boolean flipping; + public boolean side; + public float rotation; + public int axis; + }; + private int m_program; private final String DBGTAG = "JoshsOpenGL"; private FloatBuffer m_quad_attrib_buffer; @@ -36,15 +44,29 @@ public class MyRenderer implements GLSurfaceView.Renderer private int m_height; private final int GRID_WIDTH = 16; private final int GRID_HEIGHT = 9; - private float[][] m_tiles = new float[GRID_WIDTH][GRID_HEIGHT]; + private Tile[][] m_tiles = new Tile[GRID_WIDTH][GRID_HEIGHT]; private float m_aspect = 1.0f; private final float DISTANCE = (float) (GRID_HEIGHT / 2.0 / Math.tan(Math.toRadians(30))); + private long m_last_time = SystemClock.uptimeMillis(); + private final float[][] AXES = { + {1, 0, 0}, + {0, 1, 0}, + {-1, 0, 0}, + {0, -1, 0} + }; public MyRenderer(AssetManager am, Resources resources) { m_asset_manager = am; m_resources = resources; + for (int x = 0; x < GRID_WIDTH; x++) + { + for (int y = 0; y < GRID_HEIGHT; y++) + { + m_tiles[x][y] = new Tile(); + } + } } public void checkGLError(String glOperation) @@ -149,10 +171,33 @@ public class MyRenderer implements GLSurfaceView.Renderer GLES20.glClearColor(1.0f, 0.6f, 0.1f, 1.0f); } + private void update_tiles(long elapsed) + { + for (int x = 0; x < GRID_WIDTH; x++) + { + for (int y = 0; y < GRID_HEIGHT; y++) + { + Tile t = m_tiles[x][y]; + if (t.flipping) + { + t.rotation += elapsed * (180.0f / 2000.0f); + if (t.rotation >= 180.0f) + { + t.flipping = false; + t.side = !t.side; + } + } + } + } + } + public void onDrawFrame(GL10 unused) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); + long time = SystemClock.uptimeMillis(); + update_tiles(time - m_last_time); + int attr_pos = GLES20.glGetAttribLocation(m_program, "pos"); int attr_color = GLES20.glGetAttribLocation(m_program, "color"); checkGLError("glGetAttribLocation"); @@ -173,10 +218,17 @@ public class MyRenderer implements GLSurfaceView.Renderer { for (int x = 0; x < GRID_WIDTH; x++) { + Tile t = m_tiles[x][y]; Matrix.setIdentityM(m_modelview, 0); Matrix.translateM(m_modelview, 0, x - GRID_WIDTH / 2.0f, y - GRID_HEIGHT / 2.0f, -DISTANCE); + if (t.flipping) + { + Matrix.rotateM(m_modelview, 0, + t.rotation, + AXES[t.axis][0], AXES[t.axis][1], AXES[t.axis][2]); + } GLES20.glUniformMatrix4fv( GLES20.glGetUniformLocation(m_program, "modelview"), 1, false, m_modelview, 0); @@ -189,6 +241,8 @@ public class MyRenderer implements GLSurfaceView.Renderer GLES20.glDisableVertexAttribArray(attr_pos); GLES20.glDisableVertexAttribArray(attr_color); checkGLError("glDisableVertexAttribArray"); + + m_last_time = time; } public void onSurfaceChanged(GL10 unused, int width, int height) @@ -213,6 +267,20 @@ public class MyRenderer implements GLSurfaceView.Renderer if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_MOVE) { + int x = (int) (GRID_WIDTH * e.getX() / (float) m_width); + int y = (int) (GRID_HEIGHT * (m_height - e.getY()) / (float) m_height); + + if (x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) + { + Tile t = m_tiles[x][y]; + if (!t.flipping) + { + t.flipping = true; + t.rotation = 0.0f; + t.axis = (int) (Math.random() * 4); + } + } + return true; }