GLSL shader uniform locations can't be found - c++

I'm working on a vertex skinning shader, and for some reason my program can't find the uniform locations.
Vertex shader code:
#version 330
const int MAX_JOINTS = 30;
const int MAX_WEIGHTS = 3;
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
in ivec3 boneIndices;
in vec3 weights;
out vec4 fragPos;
out vec3 n;
out vec2 texCoords;
out vec4 mcolor;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 normalMatrix;
uniform mat4[MAX_JOINTS] boneTransforms;
void main() {
vec4 totalLocalPos = vec4(0.0);
vec4 totalNormal = vec4(0.0);
for(int i = 0; i < 3; i++){
mat4 boneTransform = boneTransforms[boneIndices[i]];
vec4 posePosition = boneTransform * vec4(position, 1);
totalLocalPos += posePosition * weights[i];
vec4 worldNormal = boneTransform * vec4(normal, 1);
totalNormal += worldNormal * weights[i];
}
texCoords = textureCoords;
fragPos = modelMatrix * vec4(position,1);
n = totalNormal.xyz;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * totalLocalPos;
}
The boneTransforms uniform doesn't seem to be set correctly; if I query the active uniforms with
GLint uniforms;
glGetProgramiv(shaderProgramID, GL_ACTIVE_UNIFORMS, &uniforms);
for (int i = 0; i < uniforms; i++){
int name_len = -1, num = -1;
GLenum type = GL_ZERO;
char name[100];
glGetActiveUniform(shaderProgramID, GLuint(i), sizeof(name) - 1,
&name_len, &num, &type, name);
name[name_len] = 0;
}
i always get zero; However, if I just put gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position,1) I get the expected result (correct rendering without any vertex skinning), so the other transforms appear to be working despite it telling me they don't exist?
EDIT: this is sometimes the case, other times I get the model at position (0,0,0) but otherwise rendered correctly with this.
I have read about the compiler stripping unused/inactive uniforms, but if I use boneTransforms to calculate totalLocalPos and use that for gl_Positions the uniform should be active.
I try to set the uniform with
vector<glm::mat4> boneTransforms = model.getBoneTransforms();
int location = glGetUniformLocation(shaderProgramID, "boneTransforms");
glUniformMatrix4fv(location, boneTransforms.size(), false, (GLfloat*)&boneTransforms);
location is always -1.
Is there something wrong with how I try to set this particular uniform, or is the mistake in the shader code?
EDIT2: I just noticed that the behaviour of my shader changes when I add or remove objects (which use a different shader) from the scene. I don't know what to make of that.
EDIT3: If I remove all other meshes from my scene the shader crashes with an access violation. As long as one other object is being rendered there are currently no crashes.
another EDIT: Apparently accessing the weights variable crashes my shader.

I was reading this quick tutorial about vertex skinning shader found here: khronos and it seems to be using a slightly older version of GLSL how ever they do make a mention about the multiplication of the MVP matrix(model view proj matrix) or in your case the PVM matrix( proj view model matrix) with the vec4 for total position in you case and storing it back into gl_Position and they claim that the w may not always have a value of 1 so to be safe they recommend doing this instead and I'll use your code as their example to fix this possible problem.
Change this:
gl_Position = projectionMatrix * viewMatrix * modelMatrix * totalLocalPos;
To this:
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(totalLocalPos.xyz, 1.0);
To see if this helps you out. I don't know if this is the cause of your problem or not but from what you have shown your shader appears to be okay other than that.

I saw that the way you push the mat4 into the shader is a bit off. When I do "glUniformMatrix4fv" I say the first parameter is the ID of your shader, the one you get from "glCreateProgram()". The second is the name of the variable in the shader, so in your case "boneTransforms". The third is a "1". The fourth I put "GL_FALSE" not "false" but I don't think that should make a difference. The next is your first float of the mat4 which would look more like this :
&boneTransforms[0][0]

Related

uniform sampler2D in Vertex Shader

I tried to realize height map with GLSL.
For it, i need to sent my picture to VertexShader and get grey component.
glActiveTexture(GL_TEXTURE0);
Texture.bind();
glUniform1i(mShader.getUniformLocation("heightmap"), 0);
mShader.getUniformLocation uses glGetUniformLocation and work good for other uniforms values, that used in Fragment, Vertex Shaders. But for heightmap return -1...
VertexShader code:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoords;
layout (location = 3) in vec3 normal;
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoords;
out vec4 ourColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform sampler2D heightmap;
void main()
{
float bias = 0.25;
float h = 0.0;
float scale = 5.0;
h = scale * ((texture2D(heightmap, texCoords).r) - bias);
vec3 hnormal = vec3(normal.x*h, normal.y*h, normal.z*h);
vec3 position1 = position * hnormal;
gl_Position = projection * view * model * vec4(position1, 1.0f);
FragPos = vec3(model * vec4(position, 1.0f));
Normal = mat3(transpose(inverse(model))) * normal;
ourColor = color;
TexCoords = texCoords;
}
may be algorithm of getting height is bad, but error with getting uniformlocation stops my work..
What is wrong? Any ideas?
UPD: texCoords (not TexCoords) of course is using in
h = scale * ((texture2D(heightmap, texCoords).r) - bias);
my mistake, but it doesn't solve the problem. Having same error..
My bet is your variable has been optimized out by driver or the shader did not compile/link properly. After trying to compile your shader (on my nVidia) I got this in the logs:
0(9) : warning C7050: "TexCoords" might be used before being initialized
You should always check the GLSL compile/link logs ? see
How to debug GLSL Fragment shader
especially how the glGetShaderInfoLog is used.
In line
h = scale * ((texture2D(heightmap, TexCoords).r) - bias);
You are using TexCoords which is output variable and not yet set so the behavior is undefined and most likely your gfx driver throw that line away (and may be others) removing the TexCoords from shader completely but that is just my assumption.
What driver and gfx card you got?
What returns the logs on your setup?

GLSL - Object disappears when multiplying by matrix

I've got this vertex shader:
#version 400
layout(location=0) in vec4 vertPosition;
layout(location=1) in vec2 vertUV;
layout(location=2) in vec3 vertNormal;
uniform mat4 MVP;
out vec3 fragPosition;
out vec2 fragUV;
out vec3 fragNormal;
void main(void){
gl_Position = MVP * vertPosition;
fragPosition = (MVP * vertPosition).rgb;
fragUV = vertUV;
fragNormal = (MVP * vec4(vertNormal,0)).rgb;
}
As soon as I'm multiplying vertPosition by MVP the object to render disappears, even if MVP is an identity matrix.
Here's the code where I'm setting MVP:
mat4 model = translate(mat4(), vec3(0,0,0));
mat4 view = translate(mat4(), vec3(0,0,-10));
mat4 proj = perspective(90.0f, (float)1280/720, 0.001f, 1000.0f);
mat4 MVP = model * view * proj;
GLint uniform_loc = glGetUniformLocation(this->_shaderID, "MVP");
glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, value_ptr(matrix));
I tried different multiplication orders and multiplicating in the shader.
You've got the order in which you multiple MVP reversed. OpenGL matrix multiplication is column major right associative, hence it should be
mat4 MVP = proj * view * model;
A near clipping plane distance of 0.0001 is far too low. Essentially you're compressing all of the depth buffer resolution into a very small range. Make near as large as possible.
I don't get why you're selecting .rgb for the fragPosition and fragNormal.
From the look of your example you have forgotten to set the shader program before setting the uniform. Try using glUseProgram(this->program); (depending on where you've stored your program) before setting the matrix!

Skeletal Animation ussing ASSIMP and GLSL: bone uniform array size

I'm working on an ASSIMP skeletal animation loader and renderer and right now all the data is correctly loaded and interpolated at its current timeframe. However, there is still one part that isn't working as it should and that's the vertex shader stage.
Via a VBO I pass in two vec4s that contain the bone IDs and the weights for each vertex (up to a maximum of 4 bones/weights per vertex) and the vertex shader has a matrix array of 100 bone transformations (pre-calculated per frame) that are indexed via the bone IDs.
However, it seems that the bones uniform doesn't contain the proper transformations. For debugging purposes I colored the model with the weight values and the bone IDs value and they contain a color (and thus valid values). However, when I transform my vertex via the bone transformation and color the model with the result, the entire model is colored black, meaning the transformation matrices are all 0.0. So they're not initialized properly.
I think the problem is with passing the matrices to the uniform array, or perhaps the maximum size of uniforms allowed (I also tried setting the number of uniform matrices to 32 (number of bones on current model) but without effect)?
Before passing the information to the shader, the transformation matrices are indeed valid matrices (not identity/empty matrices) so the fault should probably be in the GLSL shader or the passing of the uniforms.
The following code is from the vertex shader:
#version 330
layout (location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 tangent;
layout(location = 3) in vec3 color;
layout(location = 4) in vec2 texCoord;
layout(location = 5) in ivec4 boneIDs;
layout(location = 6) in vec4 weights;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 bones[100];
out vec2 TexCoord;
out vec4 colorz;
void main()
{
vec4 newPos = vec4(position, 1.0);
colorz = vec4(0.0, 1.0, 0.0, 1.0);
if (weights != vec4(0.0, 0.0, 0.0, 0.0))
{
mat4 boneTransform = bones[boneIDs[0]] * weights[0];
boneTransform += bones[boneIDs[1]] * weights[1];
boneTransform += bones[boneIDs[2]] * weights[2];
boneTransform += bones[boneIDs[3]] * weights[3];
// newPos = boneTransform * vec4(position, 1.0);
vec4 test = vec4(1.0);
colorz = boneTransform * test;
// newPos = boneTransform * newPos;
}
TexCoord = texCoord;
gl_Position = projection * view * model * newPos;
}
The following snippet of code pass the matrix data to the GLSL shader:
// Sets bone transformation matrices
void Shader::SetBoneMatrix(GLint index, aiMatrix4x4 matrix)
{
glm::mat4 mat = glm::transpose(glm::make_mat4(&matrix.a1));
glUniformMatrix3fv(boneLocations[index], 1, GL_FALSE, glm::value_ptr(mat));
}
Also the code that gets all the uniform locations of the bones array:
for(unsigned int i = 0; i < 100; i++)
{
string name = "bones[";
string number;
stringstream ss;
ss << i;
ss >> number;
name += number;
name += ']';
boneLocations[i] = glGetUniformLocation(this->program, name.c_str());
}
Oké, via glslDevil I came across a continous GL_INVALID_OPERATION error when setting the bone matrix to the shader via glUniformMatrix. The origin of the problem was indeed at the stage where the program passes the information along to the shader.
It is quite a stupid mistake actually since I'm using glUniformMatrix3f instead of glUniformMatrix4f. Changing this did indeed solve the problem and the animations are working perfectly right now.

fragment shader performs differently on different computers

When I run the program on my computer, it works exactly how I expected it to be working. However, when I try to run it on my campus lab computers, the fragment shader is all kinds of strange.
Right now it's just a simple Phong lighting calculation with a point source light at the origin. However, on the lab computers, it looks more like some strange cross between cel shading and a flashlight. I run an ATI graphics card, while the lab computers run NVIDIA. The shading works as expected on Macs as well (no idea about the graphics card).
The NVIDIA cards support up to OpenGL 3.1, though I run it on this Linux distribution at 2.1. I've tried clamping the shader version to 1.2 (GLSL), among a slew of other things, but they achieve the same results. The strangest thing is that when I do vertex shading rather than pixel shading, the result is the same on both computers...I've exhausted my ideas about how to fix this.
Here's the vertex shader:
#version 120
attribute vec2 aTexCoord;
attribute vec3 aPosition;
attribute vec3 aNormal;
attribute vec3 camLoc;
attribute float mat;
varying vec3 vColor;
varying vec2 vTexCoord;
varying vec3 normals;
varying vec3 lightPos;
varying vec3 camPos;
varying float material;
uniform mat4 uProjMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uNormMatrix;
uniform vec3 uLight;
uniform vec3 uColor;
void main()
{
//set up object position in world space
vec4 vPosition = uModelMatrix * vec4(aPosition, 1.0);
vPosition = uViewMatrix * vPosition;
vPosition = uProjMatrix * vPosition;
gl_Position = vPosition;
//set up light vector in world space
vec4 vLight = vec4(uLight, 1.0) * uViewMatrix;
lightPos = vLight.xyz - vPosition.xyz;
//set up normal vector in world space
normals = (vec4(aNormal,1.0) * uNormMatrix).xyz;
//set up view vector in world space
camPos = camLoc.xyz - vPosition.xyz;
//set up material shininess
material = mat;
//pass color and vertex
vColor = uColor;
vTexCoord = aTexCoord;
}
And the fragment shader:
#version 120
varying vec3 lightPos;
varying vec3 normals;
varying vec3 camPos;
varying vec2 vTexCoord;
varying vec3 vColor;
varying float material;
uniform sampler2D uTexUnit;
uniform mat4 uProjMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
void main(void)
{
float diffuse;
float diffuseRed, diffuseBlue, diffuseGreen;
float specular;
float specRed, specBlue, specGreen;
vec3 lightColor = vec3(0.996, 0.412, 0.706); //color of light (HOT PINK) **UPDATE WHEN CHANGED**
vec4 L; //light vector
vec4 N; //normal vector
vec4 V; //view vector
vec4 R; //reflection vector
vec4 H; //halfway vector
float red;
float green;
float blue;
vec4 texColor1 = texture2D(uTexUnit, vTexCoord);
//diffuse calculations
L = vec4(normalize(lightPos),0.0);
N = vec4(normalize(normals),0.0);
N = uModelMatrix * N;
//calculate RGB of diffuse light
diffuse = max(dot(N,L),0.0);
diffuseRed = diffuse*lightColor[0];
diffuseBlue = diffuse*lightColor[1];
diffuseGreen = diffuse*lightColor[2];
//specular calculations
V = vec4(normalize(camPos),0.0);
V = uModelMatrix * V;
R = vec4(-1.0 * L.x, -1.0 * L.y, -1.0 * L.z, 0.0);
float temp = 2.0*dot(L,N);
vec3 tempR = vec3(temp * N.x, temp * N.y, temp * N.z);
R = vec4(R.x + tempR.x, R.y + tempR.y, R.z + tempR.z, 0.0);
R = normalize(R);
H = normalize(L + V);
specular = dot(H,R);
specular = pow(specular,material);
specRed = specular*lightColor[0];
specBlue = specular*lightColor[1];
specGreen = specular*lightColor[2];
//set new colors
//textures
red = texColor1[0]*diffuseRed + texColor1[0]*specRed*0.7 + texColor1[0]*.05;
green = texColor1[1]*diffuseBlue + texColor1[1]*specBlue*0.7 + texColor1[1]*.05;
blue = texColor1[2]*diffuseGreen + texColor1[2]*specGreen*0.7 + texColor1[2]*.05;
//colors
red = vColor[0]*diffuseRed + vColor[0]*specRed*0.7 + vColor[0]*.05;
green = vColor[1]*diffuseBlue + vColor[1]*specBlue*0.7 + vColor[1]*.05;
blue = vColor[2]*diffuseGreen + vColor[2]*specGreen*0.7 + vColor[2]*.05;
gl_FragColor = vec4(red, green, blue, 1.0);
}
Your code looks wrong in many, many ways.
The following code is wrong. The comment is misleading (it’s in clip space, not world space). But the major problem is that you overwrite vPosition with the clip space coordinates while using it as if it was in view space several lines after this part.
//set up object position in world space
vec4 vPosition = uModelMatrix * vec4(aPosition, 1.0);
vPosition = uViewMatrix * vPosition;
vPosition = uProjMatrix * vPosition;
gl_Position = vPosition;
The following code is wrong, too. First you need matrix * vector, not vector * matrix. But also, the comment says world space, yet you compute vLight in view space and add vPosition which is in clip space!
//set up light vector in world space
vec4 vLight = vec4(uLight, 1.0) * uViewMatrix;
lightPos = vLight.xyz - vPosition.xyz;
Again here, matrix * vector:
//set up normal vector in world space
normals = (vec4(aNormal,1.0) * uNormMatrix).xyz;
Now what is this? camPos is computed in world coordinates, yet you apply the model matrix which converts model space to world space.
//specular calculations
V = vec4(normalize(camPos),0.0);
V = uModelMatrix * V;
I have no idea why your shader performs differently on different computers, but I am pretty sure none of these computers shows anything remotely close to the expected result.
You really need to read your shaders again, and each time you see a vector, ask yourself “in what coordinate space is this vector meaningful?” and each time you see a matrix, ask yourself “what coordinate spaces does this matrix convert from and to?”

GLSL 330 Matrix-Computation Error {No compile error}

Edit:
Alright got it now :D
Problem: Completly forgot glm uses colum-major matrices. Just had to change GL_TRUE, to GL_FALSE and everything is alright.
I try to compute my ModelMatrix with my ProjectionMatrix. Like so:
#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
uniform mat4 projectionMatrix; //This are the Matrixes from my cpp-app
uniform mat4 modelMatrix; //With a debugger that can show all active uniforms i checked their values: They're right!
uniform mat4 testUni; //Here I checked if its working when I precompute the model and perspective matrices in my source: works
mat4 viewMatrix = mat4(1.0f);
noperspective out vec4 vertColor;
mat4 MVP = projectionMatrix * modelMatrix ; //Should actually have the same value like testUni
void main()
{
gl_Position = testUni * position ; //Well... :) Works
gl_Position = MVP * position ; //Well... :) Doesn't work [Just the perspective Transforn]
vertColor = position;
}
Move the statement
mat4 MVP = projectionMatrix * modelMatrix ; //Should actually have the same value like testUni
into main(). Shader execution starts at main. If you want to avoid per-vertex computations, precompute the matrix outside and supply it as a uniform.