Search code examples
androidiosiphoneopengl-es-2.0glkit

What's the most memory-efficient way of operating matrices in Objective C? GLKit or CC


I am learning Objective C by porting and Android OpenGl app and I have run into this question.

In my android app I have a model matrix for every OpenGL object. Whenever I apply a transformation to that object, I recompute the model matrix by multiplying the traslation, scale and rotation matrices. I don't store these matrices, just the result because I need the app to be memory-efficient.

When porting this code to Obj-C I don't know what library to use to perform these operations. I thought of using GLKMatrix4, but every operation in that library seems to return a new matrix.

In Android this would be a problem, because I would be releasing a reference to a matrix every time and that would fire the GC really often.

I don't know if memory management in iOS works in such a way that I don't need to take care of that or if I should use another library that reuses the matrix I have (as I do in Android with Matrix.setIdentity(myMatrix)).

What would be the beast approach in this case?


Solution

  • As already said in the comments you should not worry about the memory with the GLKMatrix4 as it is a C structure. If you have trouble imagining that think of it as an integer (int) in java.

    As for the efficiency and creating new objects it is quite efficient this way because you can not do a matrix multiplication without creating another stack (another matrix buffer) on which to write the values so there is really no way to do the operation with only 2 values (at least I don't know any). The only optimization that could be done is that there was another argument in the function which would receive the target matrix pointer so you would lose a copy of 16 floats. But then again the copy of 16 floats is quite fast as already mentioned.

    If you have doubts about using this matrix library it should be more then easy to create your own matrix library any way you please. For what is generally used in the openGL it should take someone C proficient less then an hour with testing included getting the equations from the web.