Created
August 25, 2017 10:36
-
-
Save novodimaporo/c253889f6f27747c7671697eeb312387 to your computer and use it in GitHub Desktop.
Hi I made this using patterns from other people. In this snippet, I am making an rx implementation of opengles renderer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnSubscribeRenderer(private val surfaceView: GLSurfaceView, | |
private val previewSize: Size) : Observable<SurfaceEvent>() { | |
// TODO make a github gist and ask to community for insights | |
override fun subscribeActual(observer: Observer<in SurfaceEvent>) { | |
val listener = Listener(observer, previewSize) | |
observer.onSubscribe(listener) | |
surfaceView.setEGLContextClientVersion(2) | |
surfaceView.setRenderer(listener) | |
surfaceView.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY | |
} | |
private class Listener(private val observer: Observer<in SurfaceEvent>, | |
private val previewSize: Size) : GLSurfaceView.Renderer, Disposable { | |
private val mMVPMatrix = FloatArray(16) | |
private val mProjMatrix = FloatArray(16) | |
private val mVMatrix = FloatArray(16) | |
private var disposed = false | |
override fun isDisposed() = disposed | |
override fun dispose() { | |
disposed = true | |
observer.onNext(Destroyed()) | |
observer.onComplete() | |
} | |
override fun onDrawFrame(gl: GL10?) { | |
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) | |
observer.onNext(Draw(mMVPMatrix)) | |
} | |
override fun onSurfaceChanged(gl: GL10?, glwidth: Int, glheight: Int) { | |
GLES20.glViewport(0, 0, glwidth, glheight) | |
val psheight = previewSize.width | |
val pswidth = previewSize.height | |
val glRatio = glheight.toFloat() / glwidth.toFloat() | |
val psRatio = psheight.toFloat() / pswidth.toFloat() | |
// CENTER CROPPING THE VIEW | |
if(glRatio > psRatio) { | |
// this means gl surface is taller than the preview size | |
val scaleWidth = glheight.toFloat() / psRatio | |
val difference = scaleWidth - glwidth.toFloat() | |
val percentage = (difference / scaleWidth) | |
// adjust both left and right edge of the frustum | |
Matrix.frustumM(mProjMatrix, 0, -1f + percentage, 1f - percentage, -1f, 1f, 3f, 7f) | |
} else { | |
// this means preview size is taller than the gl surface | |
val scaledHeight = glwidth.toFloat() / psRatio | |
val difference = scaledHeight - glheight.toFloat() | |
val percentage = (difference / scaledHeight) | |
// adjust both top and bottom edge of the frustum | |
Matrix.frustumM(mProjMatrix, 0, -1f, 1f, -1f + percentage, 1f - percentage, 3f, 7f) | |
} | |
Matrix.setLookAtM(mVMatrix, 0, 0f, 0f, -3f, 0f, 0f, 0f, 0f, 1f, 0f) | |
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0) | |
observer.onNext(Changed(previewSize)) | |
} | |
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { | |
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f) | |
observer.onNext(Created(config)) | |
} | |
} | |
companion object { | |
@JvmStatic | |
private val TAG: String = OnSubscribeRenderer::class.java.simpleName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment