Compare commits
10 Commits
simple-dra
...
master
Author | SHA1 | Date | |
---|---|---|---|
0f6428a1ca | |||
13ee04baea | |||
20953f9141 | |||
76423f3729 | |||
03c1d29ea0 | |||
1fc70c5266 | |||
2ddae706eb | |||
490f8973d2 | |||
c7e0b9696d | |||
a0bf1f8e4d |
@ -1,8 +1,8 @@
|
||||
|
||||
varying vec2 tex_coord_i;
|
||||
uniform sampler2D tex;
|
||||
uniform bool side;
|
||||
varying vec3 color_i;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = texture2D(tex, tex_coord_i);
|
||||
gl_FragColor = vec4(side ? color_i.bgr : color_i, 1);
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 modelview;
|
||||
attribute vec3 pos;
|
||||
attribute vec2 tex_coord;
|
||||
varying vec2 tex_coord_i;
|
||||
uniform vec2 offset;
|
||||
|
||||
attribute vec2 pos;
|
||||
attribute vec3 color;
|
||||
|
||||
varying vec3 color_i;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * modelview * vec4(pos, 1);
|
||||
tex_coord_i = tex_coord;
|
||||
gl_Position = projection * modelview * vec4(pos, 0, 1);
|
||||
color_i = color;
|
||||
}
|
||||
|
@ -21,9 +21,18 @@ 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;
|
||||
@ -33,14 +42,31 @@ 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 float m_x = 0.0f;
|
||||
private float m_y = 0.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)
|
||||
@ -93,10 +119,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
|
||||
public void onSurfaceCreated(GL10 unused, EGLConfig config)
|
||||
{
|
||||
final float attribs[] = {
|
||||
1, 1, 0, 1, 1,
|
||||
-1, 1, 0, 0, 1,
|
||||
-1, -1, 0, 0, 0,
|
||||
1, -1, 0, 1, 0,
|
||||
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
|
||||
};
|
||||
checkGLError("onSurfaceCreated");
|
||||
ByteBuffer bb = ByteBuffer.allocateDirect(attribs.length * 4);
|
||||
@ -122,6 +148,7 @@ 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];
|
||||
@ -139,41 +166,94 @@ 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() % 4000L;
|
||||
float angle = 0.090f * ((int) time);
|
||||
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");
|
||||
|
||||
GLES20.glEnableVertexAttribArray(attr_pos);
|
||||
GLES20.glEnableVertexAttribArray(attr_color);
|
||||
checkGLError("glEnableVertexAttribArray");
|
||||
|
||||
m_quad_attrib_buffer.position(0);
|
||||
GLES20.glVertexAttribPointer(attr_pos, 2, 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,
|
||||
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, 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);
|
||||
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);
|
||||
int attr_pos = GLES20.glGetAttribLocation(m_program, "pos");
|
||||
int attr_tex_coord = GLES20.glGetAttribLocation(m_program, "tex_coord");
|
||||
checkGLError("glGetAttribLocation");
|
||||
GLES20.glEnableVertexAttribArray(attr_pos);
|
||||
GLES20.glEnableVertexAttribArray(attr_tex_coord);
|
||||
checkGLError("glEnableVertexAttribArray");
|
||||
m_quad_attrib_buffer.position(0);
|
||||
GLES20.glVertexAttribPointer(attr_pos, 3, GLES20.GL_FLOAT, false,
|
||||
5 * 4, m_quad_attrib_buffer);
|
||||
m_quad_attrib_buffer.position(3);
|
||||
GLES20.glVertexAttribPointer(attr_tex_coord, 2, GLES20.GL_FLOAT, false,
|
||||
5 * 4, m_quad_attrib_buffer);
|
||||
checkGLError("glVertexAttribPointer");
|
||||
GLES20.glUniform1i(
|
||||
GLES20.glGetUniformLocation(m_program, "side"),
|
||||
t_side ^ side);
|
||||
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
|
||||
checkGLError("glDrawArrays");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLES20.glDisableVertexAttribArray(attr_pos);
|
||||
GLES20.glDisableVertexAttribArray(attr_tex_coord);
|
||||
GLES20.glDisableVertexAttribArray(attr_color);
|
||||
checkGLError("glDisableVertexAttribArray");
|
||||
|
||||
m_last_time = time;
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 unused, int width, int height)
|
||||
@ -184,9 +264,7 @@ public class MyRenderer implements GLSurfaceView.Renderer
|
||||
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
|
||||
Matrix.orthoM(m_proj_matrix, 0,
|
||||
-m_aspect, m_aspect,
|
||||
-1, 1, 1, -1);
|
||||
Matrix.perspectiveM(m_proj_matrix, 0, 60.0f, 16.0f/9.0f, 0.1f, 10.0f);
|
||||
|
||||
GLES20.glUseProgram(m_program);
|
||||
|
||||
@ -200,11 +278,19 @@ public class MyRenderer implements GLSurfaceView.Renderer
|
||||
if (e.getAction() == MotionEvent.ACTION_DOWN ||
|
||||
e.getAction() == MotionEvent.ACTION_MOVE)
|
||||
{
|
||||
float x = m_aspect * ((e.getX() / (float) m_width) * 2.0f - 1.0f);
|
||||
float y = -((e.getY() / (float) m_height) * 2.0f - 1.0f);
|
||||
int x = (int) (GRID_WIDTH * e.getX() / (float) m_width);
|
||||
int y = (int) (GRID_HEIGHT * (m_height - e.getY()) / (float) m_height);
|
||||
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user