Matrices

Started by
8 comments, last by dpadam450 13 years, 2 months ago
Hello, I am curious whether do you prefer do prefer to write your own Matrix/Vector class, with all stuff that's needed (multiply, add, inverse, rotate, scale etc...) or if you prefer to use some already-done-matrices/vectors(if they are available). So far I had written my own class and methods, but I would like to know whether there is already solution to this, so I don't have to reinvent wheel every time.

My language is C++

Thanks!
Advertisement
Use glm. This library is writte against the GLSL Spec and you can send them to your shaders directly.
Also nifty things from GLSL like vec3 m = vec3(vec2(1.0f, 2.0f), 1.0f); work.
It has also replacement functions for deprecated glOrtho and glTranslate etc.
Here a short usage sample:

// Send Projection Matrix
glm::mat4 projMatrix = glm::mat4(1);
projMatrix = glm::ortho<float>(0.0f, w, h, 0, -1.0f, 1.0f);
glUniformMatrix4fv(m_perspective_id, 1, false, glm::value_ptr(projMatrix));

And it is a header only library, so no need to compile it.
http://www.g-truc.ne...-0016.html#menu
HTH
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/
Thanks! Really helpful stuff :)

Use glm. This library is writte against the GLSL Spec and you can send them to your shaders directly.
Also nifty things from GLSL like vec3 m = vec3(vec2(1.0f, 2.0f), 1.0f); work.
It has also replacement functions for deprecated glOrtho and glTranslate etc.
Here a short usage sample:

// Send Projection Matrix
glm::mat4 projMatrix = glm::mat4(1);
projMatrix = glm::ortho<float>(0.0f, w, h, 0, -1.0f, 1.0f);
glUniformMatrix4fv(m_perspective_id, 1, false, glm::value_ptr(projMatrix));

And it is a header only library, so no need to compile it.
http://www.g-truc.ne...-0016.html#menu
HTH
There is also my library. See my signature.
The code would look something like

float mymatrix[16];
glhLoadIdentityf2(mymatrix);
glhTranslatef2(mymatrix, 0.0, 5.0, 6.0);
glhScalef2(mymatrix, 1.0, 1.0, 2.0);
glhPerspectivef2(mymatrix, 45.0, aspectRatio, 0.1, 1000.0);


and mymatrix is ready. You send it to GL
glUniformMatrix4fv(m_perspective_id, 1, FALSE, mymatrix);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I would use an existing library unless you have a particular interest in implementing one yourself.

You've already got some good suggestions, but if you're interested in more options, you can find a list of math libraries here.
I use my own based on the D3D matrix functions for the sole reason that it's what I'm used to and it helps with porting code from one API to the other. Otherwise I'd say definitely use an existing library.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Bout to rip your code V-Man since I'm lazy, really just going to steal the GL matrix emulation.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Actually nevermind, I thought it would implement its own stack rather than having to pass in a pointer for a matrix each time.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


dpadam450,
It doesn't do a stack although I had that at one point, I removed it. Your own classes should store the matrix for your objects.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yea, but I still wanted the stack based idea with a single stack so I'm just setting the object matrix at the end which winds the stack down and gives me the object matrix, either by passing in there, or returning a matrix.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement