Created
April 24, 2013 09:20
-
-
Save scan/5450869 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package fyretail | |
package opengl | |
import java.nio.ByteBuffer | |
import java.nio.ByteOrder | |
import scala.collection.mutable.Buffer | |
import android.opengl.GLES20 | |
import fyretail.Camera | |
import fyretail.Rect | |
import fyretail.Sizeable | |
import fyretail.math.Vector | |
import android.os.Debug | |
import android.util.Log | |
trait Texture extends Sizeable { | |
private[fyretail] val handle: Int | |
def bind() = GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, handle) | |
} | |
trait VertexManager { | |
val vertexShaderCode: String | |
val fragmentShaderCode: String | |
lazy val shader = { | |
def makeShader(stype: Int, code: String) = { | |
val shader = GLES20.glCreateShader(stype) | |
GLES20.glShaderSource(shader, code) | |
GLES20.glCompileShader(shader) | |
shader | |
} | |
val vertexShader = makeShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode) | |
val fragmentShader = makeShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode) | |
val prog = GLES20.glCreateProgram() | |
GLES20.glAttachShader(prog, vertexShader) | |
GLES20.glAttachShader(prog, fragmentShader) | |
GLES20.glLinkProgram(prog) | |
prog | |
} | |
import VertexManager._ | |
def render(f: SpriteContext => Unit)(implicit camera: Camera) { | |
val context = new SpriteContext {} | |
f(context) | |
val vertexBuffer = { | |
val verPos = context.vertices.map(_.position.toArray).flatten | |
val bb = ByteBuffer.allocateDirect(verPos.length * 4) | |
bb.order(ByteOrder.nativeOrder()) | |
val buf = bb.asFloatBuffer() | |
buf.put(verPos.toArray) | |
buf.position(0) | |
buf | |
} | |
val drawListBuffer = { | |
val bb = ByteBuffer.allocateDirect(context.indices.length * 2) | |
bb.order(ByteOrder.nativeOrder()) | |
val buf = bb.asShortBuffer() | |
buf.put(context.indices.toArray) | |
buf.position(0) | |
buf | |
} | |
GLES20.glUseProgram(shader) | |
val positionHandle = GLES20.glGetAttribLocation(shader, "vPosition") | |
GLES20.glEnableVertexAttribArray(positionHandle) | |
GLES20.glVertexAttribPointer(positionHandle, 2, | |
GLES20.GL_FLOAT, false, | |
vertexStride, vertexBuffer) | |
val colorHandle = GLES20.glGetUniformLocation(shader, "vColor") | |
val matrixHandle = GLES20.glGetUniformLocation(shader, "uMVPMatrix") | |
GLES20.glUniform4fv(colorHandle, 1, Array(1, 1, 1, 1), 0) | |
GLES20.glUniformMatrix4fv(matrixHandle, 1, false, camera.viewProj, 0) | |
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, context.vertices.length / 3) | |
GLES20.glDisableVertexAttribArray(positionHandle) | |
} | |
} | |
object VertexManager { | |
case class Vertex(position: Vector) | |
private val drawOrder = Seq[Short](0, 1, 2, 0, 2, 3) | |
private val vertexStride = 2 * 4 | |
sealed trait SpriteContext { | |
val vertices = Buffer.empty[Vertex] | |
val indices = Buffer.empty[Short] | |
def draw(rect: Rect) { | |
val base = vertices.size | |
vertices += Vertex(Vector(rect.left, rect.top)) | |
vertices += Vertex(Vector(rect.right, rect.top)) | |
vertices += Vertex(Vector(rect.right, rect.bottom)) | |
vertices += Vertex(Vector(rect.left, rect.bottom)) | |
indices ++= drawOrder.map(x => (x + base).toShort) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment