1

I did run into some trouble setting up my animation shader for my OpenGL application. Basically it takes in an array of 50 glm::mat4 matrices and should set them as uniform in my GLSL shader. Yet only the first value is actually send to the shader, all other array entries in the shader are set to 0.

I think the problem occurs when passing from C++ to GLSL:

class model{
...
glm::mat4 finalBoneTransforms[50];
...
}

model::draw(){
//Set Joints
int jointLoc = glGetUniformLocation(shaderID, "jointTransforms");
glUniformMatrix4fv(jointLoc, 50 , GL_FALSE, glm::value_ptr(finalBoneTransforms[0])); 
...
}

So how comes that only the first value is passed? Shouldn't OpenGL take in 50 elements stored in the contiguous memory to the first element, which is referenced via the value_ptr? I would highly prefer to use arrays instead of vectors to make sure not to suffer any pointer loss due to reallocation. Arent elements in an array stored in contiguous memory? Any other obvious mistakes causing that weird behaviour?

Edit: Heres the shader code:

#version 330 core
const int MAX_JOINTS = 50;                              
const int MAX_WEIGHTS = 4;                              

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormals;
layout (location = 2) in vec2 aTexCoord;
layout (location = 3) in vec4 aBoneWeight;
layout (location = 4) in ivec4 aBoneIndex;

out vec2 texCoords;                             

uniform mat4 jointTransforms[MAX_JOINTS];                   
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(void) {                                           
    vec4 totalLocalPos = vec4(0.0);                         
    vec4 totalNormal = vec4(0.0);                       
    for (int i = 0; i<MAX_WEIGHTS; i++) {                       
        mat4 jointTransform = jointTransforms[aBoneIndex[i]];   
        vec4 posePosition = jointTransform * vec4(aPos, 1.0);
        totalLocalPos += posePosition * aBoneWeight[i]; 

    }                                                       
    gl_Position = projection*view * totalLocalPos;              
    texCoords = aTexCoord;                              

};
user3808217
  • 135
  • 3
  • 12
  • GLSL is not C/C++. Please read [Useful related](https://stackoverflow.com/questions/44589361/opengl-uniform-nof-found-if-uniform-block-gets-too-big) – Ripi2 Aug 21 '17 at 18:27
  • 3
    Elements in an array will always be contiguous in memory. I don't see any obvious problem with this code, can you edit your post to also include the shader ? – gan_ Aug 21 '17 at 18:30
  • yep, added the shader code in OP now. – user3808217 Aug 21 '17 at 18:34
  • 1
    @Ripi2 Feel free to elaborate your first sentence further, since its totally useless as it is right now. Read the post you linked, is not related to my problem though, even reducing the size to 2 mat4s doesnt change a thing. – user3808217 Aug 21 '17 at 18:42
  • 1
    OGL 3.3 guarantees a minimum uniform size of 1024 bytes, it can be greater on some GPUs. In your shader `jointTransforms` is 4x4x50x4 = 3200 bytes. In C/C++ you would get overflow or crash or undefined. In GLSL you don't know. Try reducing '50' in both shader and `glUniformMatrix4fv` – Ripi2 Aug 21 '17 at 19:06
  • @Ripi2 thanks for pointing out, I reduced it to a size of 2 and it still doesnt work. – user3808217 Aug 21 '17 at 19:09
  • 1
    Was this ever resolved? I'm dealing with the same exact issue trying to send a vector of matrices over to glsl – Danny Feb 09 '20 at 22:04

0 Answers0