2

I need to put a glm math library mat4 into a sqlite3 BLOB and back. What would be the most efficient approach? Serialize mat4 to bytes?, Turn mat4 to a float array? BLOB will not take a mat4 directly

adding brute force conversion of mat4 to array of floats

             :) using namespace glm;

              mat4 fM;  
              float fma[16];

              vec4 t0 = fM[0];
              vec4 t1 = fM[1];
              vec4 t2 = fM[2];
              vec4 t3 = fM[3];



            fma[0] = t0[0];
            fma[1] = t0[1];
            fma[2] = t0[2];
            fma[3] = t0[3];

            fma[4] = t1[0];
            fma[5] = t1[1];
            fma[6] = t1[2];
            fma[7] = t1[3];

            fma[8] = t2[0];
            fma[9] = t2[1];
            fma[10] = t2[2];
            fma[11] = t2[3];

            fma[12] = t3[0];
            fma[13] = t3[1];
            fma[14] = t3[2];
            fma[15] = t3[3];

this has been tested to work correctly - but there should be a better way. the conversion from float[16] to mat4 is handled by glm library glm/gtc/type_ptr.hpp - works as it should. BLOB in sqlite3 will take a float array directly.

SOLVED:

#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

float *fM;
glm::mat4 matrix;
fM = glm::value_ptr(matrix);
sqlite3_bind_blob(stmt, 0, fM, sizeof(fM), SQLITE_STATIC);

the above converts the values for glm::mat4 into a float array which then can be saved in sqlite3 as a single BLOB type

4

1 回答 1

1

目前我正在做类似的事情,所以我希望这会对你有所帮助。我正在做的是:将矩阵转换为浮点数,然后转换为字符串并将其存储在 xml 中,您可以通过将浮点数组作为字符串存储在 SQLite 数据库中来做同样的事情。

于 2013-06-21T14:13:40.577 回答