2

I'm trying to write a wrapper for the following bit of scala code:

val vertBuffer: FloatBuffer = createFloatBuffer(vertex_positions.length)
vertBuffer.put(vertex_positions)
vertBuffer.flip()

However, the class will be used almost exclusively in things like this:

val vboId = glGenBuffers()
glBindBuffer(GL_ARRAY_BUFFER, vboId)
glBufferData(GL_ARRAY_BUFFER, vertBuffer, GL_STATIC_DRAW)

I'm looking for a way to avoid having to type something similar to

vertBufferWrapper.getWrapped

and be able to write just

vertBufferWrapper

Thank you for your time.

  • But vertBufferWrapper is not the same type as vertBuffer. How do you expect that to work? – Robert Harvey Jun 9 '15 at 15:45
  • When something receives a vertBufferWrapper, I want the wrapper to just deliver a vertBuffer, without field accessors, just for simplicity of code. Currently, I'll have to put vertBufferWrapper.vertBuffer (or get(), or whichever) everywhere I need a vertBuffer. Edit: I suppose I could define vertBufferWrapper as vertBufferWrapper.get()? – Jeff Jun 9 '15 at 15:59
  • myVertBuffer = vertBufferWrapper.getVertBuffer(). Then use myVertBuffer in your subsequent code. – Robert Harvey Jun 9 '15 at 16:00
2

Don't store it in a wrapped class in the first place. Just create a transformation function from the data structure you have to the one you need:

def createFlippedBuffer(vertex_positions: Positions) : FloatBuffer = {
  val vertBuffer: FloatBuffer = createFloatBuffer(vertex_positions.length)
  vertBuffer.put(vertex_positions)
  vertBuffer.flip()
  vertBuffer
}

It's not clear if you had other reasons to choose a wrapper in the first place, but another option is the pimp my library feature, which allows you to add methods to existing classes without explicitly using a wrapper.

  • I'll take a look at pimp my library. The original data type is provided by java.nio, and is used by lwjgl to feed values into opengl functions without the risk of the GC mishandling memory. Thank you! – Jeff Jun 9 '15 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.