Compare commits

..

No commits in common. "master" and "simple-drag" have entirely different histories.

3 changed files with 38 additions and 127 deletions

View File

@ -1,8 +1,8 @@
uniform bool side;
varying vec3 color_i;
varying vec2 tex_coord_i;
uniform sampler2D tex;
void main()
{
gl_FragColor = vec4(side ? color_i.bgr : color_i, 1);
gl_FragColor = texture2D(tex, tex_coord_i);
}

View File

@ -1,15 +1,12 @@
uniform mat4 projection;
uniform mat4 modelview;
uniform vec2 offset;
attribute vec2 pos;
attribute vec3 color;
varying vec3 color_i;
attribute vec3 pos;
attribute vec2 tex_coord;
varying vec2 tex_coord_i;
void main()
{
gl_Position = projection * modelview * vec4(pos, 0, 1);
color_i = color;
gl_Position = projection * modelview * vec4(pos, 1);
tex_coord_i = tex_coord;
}

View File

@ -21,18 +21,9 @@ import android.view.MotionEvent;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.opengl.GLUtils;
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;
@ -42,31 +33,14 @@ public class MyRenderer implements GLSurfaceView.Renderer
private float m_modelview[] = new float[16];
private int m_width;
private int m_height;
private final int GRID_WIDTH = 16;
private final int GRID_HEIGHT = 9;
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}
};
private float m_x = 0.0f;
private float m_y = 0.0f;
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)
@ -119,10 +93,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
public void onSurfaceCreated(GL10 unused, EGLConfig config)
{
final float attribs[] = {
0.5f, 0.5f, 0.8f, 0.8f, 1,
-0.5f, 0.5f, 0, 0, 1,
-0.5f, -0.5f, 0.8f, 0.8f, 1,
0.5f, -0.5f, 0, 0, 1
1, 1, 0, 1, 1,
-1, 1, 0, 0, 1,
-1, -1, 0, 0, 0,
1, -1, 0, 1, 0,
};
checkGLError("onSurfaceCreated");
ByteBuffer bb = ByteBuffer.allocateDirect(attribs.length * 4);
@ -148,7 +122,6 @@ public class MyRenderer implements GLSurfaceView.Renderer
Log.e(DBGTAG, "Program log: " + info_log);
}
/*
Bitmap texture = BitmapFactory.decodeResource(m_resources,
R.drawable.texture);
int[] textures = new int[1];
@ -166,94 +139,41 @@ public class MyRenderer implements GLSurfaceView.Renderer
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
checkGLError("glGenerateMipmap");
texture.recycle();
*/
GLES20.glClearColor(1.0f, 0.6f, 0.1f, 1.0f);
GLES20.glEnable(GLES20.GL_CULL_FACE);
}
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 / 1500.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);
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
Matrix.setIdentityM(m_modelview, 0);
Matrix.translateM(m_modelview, 0, m_x, m_y, 0);
Matrix.rotateM(m_modelview, 0, angle, 0, 0, 1);
Matrix.scaleM(m_modelview, 0, 0.4f, 0.4f, 0.4f);
GLES20.glUniformMatrix4fv(
GLES20.glGetUniformLocation(m_program, "modelview"),
1, false, m_modelview, 0);
int attr_pos = GLES20.glGetAttribLocation(m_program, "pos");
int attr_color = GLES20.glGetAttribLocation(m_program, "color");
int attr_tex_coord = GLES20.glGetAttribLocation(m_program, "tex_coord");
checkGLError("glGetAttribLocation");
GLES20.glEnableVertexAttribArray(attr_pos);
GLES20.glEnableVertexAttribArray(attr_color);
GLES20.glEnableVertexAttribArray(attr_tex_coord);
checkGLError("glEnableVertexAttribArray");
m_quad_attrib_buffer.position(0);
GLES20.glVertexAttribPointer(attr_pos, 2, GLES20.GL_FLOAT, false,
GLES20.glVertexAttribPointer(attr_pos, 3, GLES20.GL_FLOAT, false,
5 * 4, m_quad_attrib_buffer);
m_quad_attrib_buffer.position(2);
GLES20.glVertexAttribPointer(attr_color, 3, GLES20.GL_FLOAT, false,
m_quad_attrib_buffer.position(3);
GLES20.glVertexAttribPointer(attr_tex_coord, 2, GLES20.GL_FLOAT, false,
5 * 4, m_quad_attrib_buffer);
checkGLError("glVertexAttribPointer");
for (int y = 0; y < GRID_HEIGHT; y++)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
Tile t = m_tiles[x][y];
for (int side = 0; side < 2; side++)
{
int t_side = t.side ? 1 : 0;
if (!t.flipping && (side == 1))
continue;
Matrix.setIdentityM(m_modelview, 0);
Matrix.translateM(m_modelview, 0,
x + 0.5f - GRID_WIDTH / 2.0f,
y + 0.5f - GRID_HEIGHT / 2.0f,
-DISTANCE);
if (t.flipping)
{
Matrix.rotateM(m_modelview, 0,
t.rotation + side * 180.0f,
AXES[t.axis][0], AXES[t.axis][1], AXES[t.axis][2]);
}
GLES20.glUniformMatrix4fv(
GLES20.glGetUniformLocation(m_program, "modelview"),
1, false, m_modelview, 0);
GLES20.glUniform1i(
GLES20.glGetUniformLocation(m_program, "side"),
t_side ^ side);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
checkGLError("glDrawArrays");
}
}
}
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
checkGLError("glDrawArrays");
GLES20.glDisableVertexAttribArray(attr_pos);
GLES20.glDisableVertexAttribArray(attr_color);
GLES20.glDisableVertexAttribArray(attr_tex_coord);
checkGLError("glDisableVertexAttribArray");
m_last_time = time;
}
public void onSurfaceChanged(GL10 unused, int width, int height)
@ -264,7 +184,9 @@ public class MyRenderer implements GLSurfaceView.Renderer
GLES20.glViewport(0, 0, width, height);
Matrix.perspectiveM(m_proj_matrix, 0, 60.0f, 16.0f/9.0f, 0.1f, 10.0f);
Matrix.orthoM(m_proj_matrix, 0,
-m_aspect, m_aspect,
-1, 1, 1, -1);
GLES20.glUseProgram(m_program);
@ -278,19 +200,11 @@ 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);
float x = m_aspect * ((e.getX() / (float) m_width) * 2.0f - 1.0f);
float y = -((e.getY() / (float) m_height) * 2.0f - 1.0f);
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);
}
}
m_x = x;
m_y = y;
return true;
}