在Objective C中运行矩阵的内存效率最高的方法是什么? GLKit或CC

时间:2022-09-07 09:42:17

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

我通过移植和Android OpenGl应用程序学习Objective C,我遇到了这个问题。

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.

在我的Android应用程序中,我为每个OpenGL对象都有一个模型矩阵。每当我对该对象应用变换时,我都会通过乘以traslation,scale和rotation矩阵来重新计算模型矩阵。我不存储这些矩阵,只是结果,因为我需要应用程序具有内存效率。

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.

将此代码移植到Obj-C时,我不知道用于执行这些操作的库。我想过使用GLKMatrix4,但该库中的每个操作似乎都返回一个新的矩阵。

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.

在Android中,这将是一个问题,因为我每次都会释放对矩阵的引用,这会经常引发GC。

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)).

我不知道iOS中的内存管理是否以这样的方式工作,我不需要处理它,或者我是否应该使用另一个重用我所拥有的矩阵的库(就像我在Android中使用Matrix.setIdentity一样) myMatrix的))。

What would be the beast approach in this case?

在这种情况下,野兽的做法是什么?

1 个解决方案

#1


0  

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.

正如评论中已经说过的那样,你不应该担心GLKMatrix4的内存,因为它是一个C结构。如果你很难想象它在java中被认为是一个整数(int)。

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.

至于效率和创建新对象,这种方式非常有效,因为你不能在不创建另一个堆栈(另一个矩阵缓冲区)的情况下进行矩阵乘法,在该堆栈上写入值,因此实际上没有办法只用2进行操作价值观(至少我不知道)。可以完成的唯一优化是函数中有另一个参数将接收目标矩阵指针,因此您将丢失16个浮点数的副本。但是,如上所述,16浮动的副本再次非常快。

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.

如果您对使用此矩阵库有疑问,那么您可以更轻松地创建自己的矩阵库。对于通常在openGL中使用的内容,它应该需要一个C级精通不到一个小时的测试,包括从Web获取方程式。

#1


0  

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.

正如评论中已经说过的那样,你不应该担心GLKMatrix4的内存,因为它是一个C结构。如果你很难想象它在java中被认为是一个整数(int)。

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.

至于效率和创建新对象,这种方式非常有效,因为你不能在不创建另一个堆栈(另一个矩阵缓冲区)的情况下进行矩阵乘法,在该堆栈上写入值,因此实际上没有办法只用2进行操作价值观(至少我不知道)。可以完成的唯一优化是函数中有另一个参数将接收目标矩阵指针,因此您将丢失16个浮点数的副本。但是,如上所述,16浮动的副本再次非常快。

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.

如果您对使用此矩阵库有疑问,那么您可以更轻松地创建自己的矩阵库。对于通常在openGL中使用的内容,它应该需要一个C级精通不到一个小时的测试,包括从Web获取方程式。