PDA

View Full Version : How to use GLM in my engine



somboon
04-09-2011, 04:23 AM
I am in the process of redesign my renderer and has considered using GLM as a math liblary

The question is How should I use the liblary.

Should I declare every matrix/vector in my engine as GLM data type so that the math operation can be done quickly with out having to convert from raw data.




std::vector<glm::vec4> vertexList;
std::vector<glm::vec4> transformdVertexList;
glm::mat4 someMatrix;

//assume initialized of above data
for(unsigned int i=0;i<vertexList.size();i++){
transformdVertexList[i] = someMatrix*vertexList[i];
}

//send transformdVertexList to VBO (dont know the syntax)

which I dont know how to send the array of glm::vec4 to openGL/shader (the sample on site only demonstarte using glm::value_ptr to send individual object but no mention of array)

Or should I keep the data as raw float array like I always do (actually it encapsulate in simple struct...you know what I mean) and only create GLM datatype when doing math operation




typedef struct{
float element[4];
}raw_vec4;

typedef struct{
float element[16];
}raw_mat4;

std::vector<raw_vec4> vertexList;
std::vector<raw_vec4> transformedVertexList;
raw_mat4 someMatrix;

//some calculation
//assume initialized of above data

glm::mat4 someMatrixGLM(someMatrix.element);//is this correct syntax?
for(unsigned int i=0;i<vertexList.size();i++){
vec4 vertexGLM(vertexList[i].element);
vec4 tfVertexGLM = someMatrixGLM*vertexGLM();
memcpy(transformedVertexList[i].element,glm::value_ptr(tfVertexGLM),sizeof(float) *4);
}

//send transform array to VBO/shader as usual



Any suggestion would be appreciate.
somboon

Groovounet
04-10-2011, 08:58 AM
Personnally, I would avoid the raw_vec4 and raw_mat4 structures in most time because as data, glm::vec4 and glm::mat4 doesn't bring any overload but copying glm::vec4 to raw_vec4 can have a significant performance impact even though it's possible a use glm::vec4 as if it was a raw_vec4 but this is cucumbersome:

*(raw_vec4*)&amp;glm::vec4(...)

Ermmmmm.....

Arrays of GLM types can be "send" this way:
glm::vec4 Array[COUNT];
glBufferData(..., &amp;Array[0][0])
or
glBufferData(..., glm::value_ptr(Array[0]))