Giorgos Tsolakidis
Giorgos Tsolakidis

Reputation: 89

Can't use the * operator during matrix multiplication

In my OpenGL "main.cpp" file (Windows-MinG, I've constructed view, model and model-view matrices (vMat, mMat and mvMat accordingly), which are of type glm::mat4, used in this code:

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

glm::mat4 vMat, mMat, mvMat; 
void applyMatrices(GLFWwindow* window){
   
    mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
    projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
    
    glfwGetFramebufferSize(window, &width, &height);
    aspect = (float)width / (float)height;
    projMat = glm::perspective(1.0472f, aspect, 0.1f, 1000.0f);

    // glm::mat4(1.0f) : an identity matrix
    vMat = glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, -1.0f, -1.0f));
    mMat = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
    
    mvMat = vMat * mMat;

    glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
    glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projMat));
}

void display(GLFWwindow* window, double currentTime) {
    ...

    applyMatrices(window);
   
   ...
}

int main() {
   ...
    while (!glfwWindowShouldClose(window)) {
        display(window, glfwGetTime());
        ...
    }

   ...
}

The problem is in line

mvMat = vMat * mMat;

where multiplication of matrices happens, which if I **remove** will make the program compile successfully, but if not, will throw this error regarding the GLM library's mat4 type:

 In file included from C:.../Project/dependencies/glm/glm/glm.hpp:120,
                 from C:\Users\...\Project\src\main.cpp:5:

c:\users\...\project\dependencies\glm\glm\detail\type_mat4x4.inl: 

In instantiation of 'constexpr glm::mat<4, 4, T, Q> glm::operator*(const glm::mat<4, 4, T, Q>&, const glm::mat<4, 4, T, Q>&) [with T = float; glm::qualifier Q = (glm::qualifier)0u]':

C:\Users\...\Project\src\main.cpp:150:20:   required from here

c:\users\...\project\dependencies\glm\glm\detail\type_mat4x4.inl:642:19: error: uninitialized variable 'Result' in 'constexpr' function
   mat<4, 4, T, Q> Result;
                   ^~~~~~

For context, I'm using Windows and the MinGW compiler. I've tried looking into iniating all three matrices **beforehand** like this:

glm::mat4 vMat = glm::mat4(1.0f);
glm::mat4 mMat = glm::mat4(1.0f);
glm::mat4 mvMat = glm::mat4(1.0f);

but this didn't change anything. I've looked whether my C++ version (C++ 20) was incompatible with the GLM's matrix workings, but even changing to older C++ version didn't help. I'm not sure if it's GLM struggling to work with my compiler (MinGW version 6.3), but I'm not sure.

Upvotes: -1

Views: 106

Answers (2)

Giorgos Tsolakidis
Giorgos Tsolakidis

Reputation: 89

Seems like I needed to take the latest 'glm' branch from the https://github.com/g-truc/glm repository, which had a merged pull request addressing this exact problem with the constexpr values of the matrices.

Upvotes: 0

karlphillip
karlphillip

Reputation: 93468

I think the important thing is to make sure you are using a compiler and C++ standard that are actually supported by GLM:

  • GCC 4.7 and higher
  • Intel C++ Compose XE 2013 and higher
  • Clang 3.4 and higher
  • Apple Clang 6.0 and higher
  • Visual C++ 2013 and higher
  • CUDA 9.0 and higher (experimental)
  • Any C++11 compiler

Here are other relevant suggestions:

  • Use at least c++11 to build the code with GLM. It might be a good idea to try to build the code using older standards like c++11 or c++14, instead of c++20.
  • Try defining the macro below to make sure types are properly aligned for SIMD operations (Single Instruction, Multiple Data):
#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
  • Make sure to initialize the 4x4 matrices:
glm::mat4 vMat = glm::mat4(1.0f);
glm::mat4 mMat = glm::mat4(1.0f);
glm::mat4 mvMat = glm::mat4(1.0f);

Upvotes: 0

Related Questions