Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Preparatory to setting ripple centre

  • Loading branch information...
commit 5b62a6f05656a69ed1f2d99f2a87741fdc6542ab 1 parent 7fdf96f
@paulbutcher authored
View
12 jni/ripple.cpp
@@ -123,15 +123,3 @@ Java_com_paulbutcher_scalakey_Mesh_ripple(JNIEnv* env, jobject obj, jlong elapse
}
}
}
-
-// def warp(x: Float, y: Float, ix: Int, iy: Int): (Float, Float) = {
-// val (dx, dy) = (x - centreX, y - centreY)
-//
-// if (abs(radius * radius - (dx * dx + dy * dy)) < wavelength * wavelength) {
-// val theta = atan(dx / dy)
-// val (xx, yy) = (x + amplitude * sin(theta), y + cos(theta))
-// (xx.asInstanceOf[Float], yy.asInstanceOf[Float])
-// } else {
-// (x, y)
-// }
-// }
View
16 src/Mesh.scala
@@ -23,6 +23,20 @@ class Mesh private (width_ : Float, height_ : Float, columns_ : Int, rows_ : Int
initializeBuffers
+ var rippleX = 0.0f
+ var rippleY = 0.0f
+ var startTime = 0L
+
+ def startRipple(x: Float, y: Float) {
+ startTime = System.currentTimeMillis
+ rippleX = x
+ rippleY = y
+ }
+
+ def update() {
+ ripple(System.currentTimeMillis - startTime)
+ }
+
private def allocateDirectFloatBuffer(size: Int) = {
val sizeofFloat = 4
val byteBuffer = ByteBuffer.allocateDirect(size * sizeofFloat)
@@ -37,7 +51,7 @@ class Mesh private (width_ : Float, height_ : Float, columns_ : Int, rows_ : Int
byteBuffer.asShortBuffer
}
- @native def ripple(elapsed: Long)
+ @native private def ripple(elapsed: Long)
@native private def initializeBuffers()
}
View
27 src/RippleRenderer.scala
@@ -14,23 +14,22 @@ import android.opengl.GLUtils
class RippleRenderer(context: Context) extends Renderer with Logger {
val aspectRatio = 1.5f
-
- val height = 2.0f
- val width = (height * aspectRatio).toFloat
+ val meshHeight = 2.0f
+ val meshWidth = (meshHeight * aspectRatio).toFloat
val rows = 65
val columns = ((rows - 1) * aspectRatio + 1).toInt
+ val mesh = Mesh.create(meshWidth, meshHeight, columns, rows)
+
+ var windowWidth: Int = _
+ var windowHeight: Int = _
val planeDistance = (1.0 / -math.tan(math.Pi / 8)).toFloat
-
- val mesh = Mesh.create(width, height, columns, rows)
val textures = new Array[Int](1)
- var startTime = 0L;
-
def onSurfaceCreated(gl: GL10, config: EGLConfig) {
d("onSurfaceCreated")
-
+
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
gl.glShadeModel(GL10.GL_SMOOTH)
gl.glClearDepthf(1.0f)
@@ -63,7 +62,7 @@ class RippleRenderer(context: Context) extends Renderer with Logger {
def onDrawFrame(gl: GL10) {
d("onDrawFrame")
- mesh.ripple(System.currentTimeMillis - startTime)
+ mesh.update
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT)
gl.glLoadIdentity
@@ -77,6 +76,9 @@ class RippleRenderer(context: Context) extends Renderer with Logger {
def onSurfaceChanged(gl: GL10, width: Int, height: Int) {
d("onSurfaceChanged: "+ width +", "+ height)
+ windowWidth = width
+ windowHeight = height
+
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(GL10.GL_PROJECTION)
gl.glLoadIdentity
@@ -85,7 +87,10 @@ class RippleRenderer(context: Context) extends Renderer with Logger {
gl.glLoadIdentity
}
- def startRipple() {
- startTime = System.currentTimeMillis
+ def startRipple(x: Float, y: Float) {
+ val worldX = x / windowWidth * meshWidth - meshWidth / 2
+ val worldY = y / windowHeight * meshHeight - meshHeight / 2
+
+ mesh.startRipple(worldX, worldY)
}
}
View
6 src/RippleView.scala
@@ -18,9 +18,13 @@ class RippleView(context: Context) extends GLSurfaceView(context) with Logger {
override def onTouchEvent(event: MotionEvent) = {
if (event.getAction == MotionEvent.ACTION_DOWN) {
+ val actionIndex = event.getActionIndex
+ val x = event.getX(actionIndex)
+ val y = event.getY(actionIndex)
+
queueEvent(new Runnable() {
def run() {
- renderer.startRipple
+ renderer.startRipple(x, y)
}
})
true
Please sign in to comment.
Something went wrong with that request. Please try again.