move spinning quad to touch point

This commit is contained in:
Josh Holtrop 2012-11-15 22:33:31 -05:00
parent 569ff8ac00
commit 76fd3ad35d
3 changed files with 38 additions and 3 deletions

View File

@ -7,6 +7,6 @@ varying vec3 color_i;
void main() void main()
{ {
gl_Position = projection * modelview * vec4(pos * 0.75, 1); gl_Position = projection * modelview * vec4(pos, 1);
color_i = color; color_i = color;
} }

View File

@ -3,15 +3,25 @@ package com.homelinux.holtrop.opengltest;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.view.MotionEvent;
public class MyGLSurfaceView extends GLSurfaceView public class MyGLSurfaceView extends GLSurfaceView
{ {
MyRenderer m_renderer;
public MyGLSurfaceView(Context context, AssetManager am) public MyGLSurfaceView(Context context, AssetManager am)
{ {
super(context); super(context);
setEGLContextClientVersion(2); setEGLContextClientVersion(2);
setRenderer(new MyRenderer(am)); m_renderer = new MyRenderer(am);
setRenderer(m_renderer);
}
@Override
public boolean onTouchEvent(MotionEvent e)
{
return m_renderer.onTouchEvent(e);
} }
} }

View File

@ -16,6 +16,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import android.opengl.Matrix; import android.opengl.Matrix;
import android.os.SystemClock; import android.os.SystemClock;
import android.view.MotionEvent;
public class MyRenderer implements GLSurfaceView.Renderer public class MyRenderer implements GLSurfaceView.Renderer
{ {
@ -25,6 +26,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
private AssetManager m_asset_manager; private AssetManager m_asset_manager;
private float m_proj_matrix[] = new float[16]; private float m_proj_matrix[] = new float[16];
private float m_modelview[] = new float[16]; private float m_modelview[] = new float[16];
private int m_width;
private int m_height;
private float m_x = 0.0f;
private float m_y = 0.0f;
public MyRenderer(AssetManager am) public MyRenderer(AssetManager am)
{ {
@ -119,7 +124,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
long time = SystemClock.uptimeMillis() % 4000L; long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time); float angle = 0.090f * ((int) time);
Matrix.setRotateM(m_modelview, 0, angle, 0, 0, 1); 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.2f, 0.2f, 0.2f);
GLES20.glUniformMatrix4fv( GLES20.glUniformMatrix4fv(
GLES20.glGetUniformLocation(m_program, "modelview"), GLES20.glGetUniformLocation(m_program, "modelview"),
1, false, m_modelview, 0); 1, false, m_modelview, 0);
@ -144,6 +152,9 @@ public class MyRenderer implements GLSurfaceView.Renderer
public void onSurfaceChanged(GL10 unused, int width, int height) public void onSurfaceChanged(GL10 unused, int width, int height)
{ {
m_width = width;
m_height = height;
GLES20.glViewport(0, 0, width, height); GLES20.glViewport(0, 0, width, height);
if (width < height) if (width < height)
@ -164,4 +175,18 @@ public class MyRenderer implements GLSurfaceView.Renderer
GLES20.glGetUniformLocation(m_program, "projection"), GLES20.glGetUniformLocation(m_program, "projection"),
1, false, m_proj_matrix, 0); 1, false, m_proj_matrix, 0);
} }
public boolean onTouchEvent(MotionEvent e)
{
if (e.getAction() == MotionEvent.ACTION_DOWN)
{
float x = (e.getX() / (float) m_width) * 2.0f - 1.0f;
float y = -((e.getY() / (float) m_height) * 2.0f - 1.0f);
m_x = x;
m_y = y;
}
return false;
}
} }