我的目标是从Android OpenGL ES教程中概括形状的创建:http://developer.android.com/training/graphics/opengl/shapes.html现在看起来是这样的:
val squareCoords = Array( -0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f ) // top right
val vertexBuffer = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
squareCoords.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(squareCoords)
.position(0)
所以,float的大小是硬编码的。但我也有一些短数组:
val drawOrder = Array[Short] ( 0, 1, 2, 0, 2, 3 )
val drawListBuffer = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2)
.order(ByteOrder.nativeOrder())
.asShortBuffer()
drawListBuffer.put(drawOrder)
drawListBuffer.position(0)
代码看起来类似,但挑战的部分是选择正确的方法asFloatBuffer()/asShortBuffer()在这种情况下,并使用正确的原语值。
我写了下面的代码来解决这个问题:
import scala.reflect.runtime.universe._
implicit class SByteBuffer[T <: AnyVal : TypeTag] (coords: Array[T]) {
private val length = coords.length
def buffer(): Buffer = {
def allocateBb(mod: Int) = {
ByteBuffer.allocateDirect(length * mod)
.order(ByteOrder.nativeOrder())
}
val buffer = typeOf[T] match {
case t if t =:= typeOf[Float] => allocateBb(4).asFloatBuffer().put(coords.asInstanceOf[Array[Float]])
case t if t =:= typeOf[Short] => allocateBb(2).asShortBuffer().put(coords.asInstanceOf[Array[Short]])
}
buffer.position(0)
}
}
val squareCoords = Array( -0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f ) // top right
val vertexBuffer = squareCoords.buffer()
但是它看起来太复杂了,而且有代码重复。您能建议如何重构这段代码,使其看起来更简洁,并可能摆脱运行时类型检查吗?
您最好为您想要支持的每种基本类型创建单独的隐式类:
abstract class ArrayOpsBase[T <: AnyVal] ( val coords: Array[T] ) {
protected val length = coords.length
protected def allocateBb(mod: Int) = {
ByteBuffer.allocateDirect(length * mod)
.order(ByteOrder.nativeOrder())
}
def buffer(): Buffer
}
implicit class FloatArrayBufferOps( coords: Array[Float] ) extends ArrayOpsBase[Float]( coords ) {
def buffer = allocateBb(4).asFloatBuffer.put( coords ).position( 0 )
}
implicit class ShortArrayBufferOps( coords: Array[Short] ) extends ArrayOpsBase[Short]( coords ) {
def buffer = allocateBb(2).asShortBuffer.put( coords ).position( 0 )
}