Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble Including / Using GLM library

Tags:

c++

glm-math

I am having trouble properly including / using the glm math library (http://glm.g-truc.net/ ) in my c++ project. Since glm is a "header only" library, I thought I could just include it with this line:

#include "glm/glm.hpp"

At first this seemed to be working, as I could create and use matrices and vectors. However when I tried to use the glm::translate(...) function I got this error:

error: ‘translate’ is not a member of ‘glm’

On the GLM website, they recommend including the library with triangle brackets, like so:

#include <glm/glm.hpp>

...but isn't it correct to think that I can include it the other way, given that it is inside the project directory structure?

I have set up the test below to illustrate the problem I am having. The glm folder is sitting next to the testglm.cpp file.

#include <iostream>

#include "glm/glm.hpp"

using namespace std;

int main(void) {

    // works:
    glm::mat4 testMatrix1 = glm::mat4(5.0f) * glm::mat4(2.0f);

    cout << testMatrix1[0][0] << endl; // output: 10

    // doesn't work - (error: ‘translate’ is not a member of ‘glm’):
    glm::mat4 testMatrix2 = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));

}

I am building this test with this build command from the terminal, on osx:

g++ -o bin/glm_test src/testglm.cpp

I'm not sure if my problem relates to how I'm including the library, how I'm using it, or how I'm building the project. Google does not give me any hits for that error message, so I wonder if I am doing something fundamentally wrong. Advice would be much appreciated. Thanks.

like image 998
null Avatar asked Oct 11 '13 21:10

null


2 Answers

yngum's suggestion lead me to look more closely at the documentation, and I realised that glm::translate is actually part of a module that extends the glm core. I needed to include both the glm core and the matrix_transform extension:

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"

Now the test example works. (I also noticed that I had also made a silly mistake in the test which would have prevented it from compiling. That has been fixed in the original question now for the sake of future readers who may experience the problem I had for the same reason.)

like image 72
null Avatar answered Sep 18 '22 17:09

null


Maybe Im a bit late but instead of

#include "glm/glm.hpp"

one could use

#include "glm/ext.hpp"
like image 25
Tqq Avatar answered Sep 21 '22 17:09

Tqq