PDA

View Full Version : shader chaining



siddharth.cmdz
11-17-2014, 12:33 PM
hi all,

I'm trying to chain my fragment and vertex shaders. Its just a simple lighting shader. Below is the resulting chained vertex shader. When I try to get the location of the uniform "enableNormalColor", i get a -1. Should all the uniforms be at the top of the shader?


#version 440 compatibility

layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec4 vColor;
layout(location = 2) in vec4 vNormal;

uniform mat4 mvp;
uniform mat4 mv;
uniform mat4 colorMat;
uniform mat4 normalMat;

vec4 getVertexColor() {
return vColor;
}

vec4 getVertexNormal() {
return vNormal;
}

vec4 getVertexPosition() {
return vPosition;
}

mat4 getMVPMatrix() {
return mvp;
}

mat4 getMVMatrix() {
return mv;
}

mat4 getMVITMatrix() {
return normalMat;
}

mat4 getColorMatrix() {
return colorMat;
}

uniform vec4 solidColor;
uniform int enableGradientColor = 0;
uniform int enableNormalColor = 0;
uniform int enableSolidColor = 0;

vec4 getSolidColor() {
return solidColor;
}

int getEnableGradientColor() {
return enableGradientColor;
}

int getEnableNormalColor() {
return enableNormalColor;
}

int getEnableSolidColor() {
return enableSolidColor;
}

vec4 getColor() {
vec4 color = vec4(1f, 0f, 0f, 1f);
if(enableGradientColor == 1) {
color = colorMat * getVertexPosition();
color.x = abs(color.x);
color.y = abs(color.y);
color.z = abs(color.z);

} else if(enableNormalColor == 1) {
mat4 scaleBias = mat4(0.5f, 0f, 0f, 0.5f,
0f, 0.5f, 0f, 0.5f,
0f, 0f, 0.5f, 0.5f,
0f, 0f, 0f, 1f);
color = normalize(scaleBias * getVertexNormal());
} else if(enableSolidColor == 1) {
color = solidColor;
} else {
color = vColor;
}

return color;
}

out vec4 normal;
out vec4 eye;


void main() {
gl_Position = getMVPMatrix()* getVertexPosition();
normal = normalize(getMVITMatrix() * getVertexNormal());
eye = -(getMVMatrix() * getVertexPosition());
}


thanks

carsten neumann
11-17-2014, 01:05 PM
I'm trying to chain my fragment and vertex shaders


Hmm, that is not a term I'm familiar with - that being said, I don't think it is related to your specific problem: If I'm reading it correctly 'enableNormalColor' is only used in the function 'getColor()' which itself is not called (directly or indirectly) from your 'main' function. The GLSL compiler removes unused variables and only the used ones (called 'active' uniforms in the spec) are assigned locations.
You can use queries like 'glGetProgramiv(..., GL_ACTIVE_UNIFORMS, ...)' to find out the number of active uniforms and 'glGetActiveUniform' to get information about a specific active uniform.

siddharth.cmdz
11-17-2014, 01:57 PM
Hmm, that is not a term I'm familiar with - that being said, I don't think it is related to your specific problem: If I'm reading it correctly 'enableNormalColor' is only used in the function 'getColor()' which itself is not called (directly or indirectly) from your 'main' function. The GLSL compiler removes unused variables and only the used ones (called 'active' uniforms in the spec) are assigned locations.
You can use queries like 'glGetProgramiv(..., GL_ACTIVE_UNIFORMS, ...)' to find out the number of active uniforms and 'glGetActiveUniform' to get information about a specific active uniform.

Ahh i see...well by shade chaining...im just having an appending different pieces of shader codes into one shader before compiling them. So i search for a "#include someshader.vert", i explode that shader into a final shader string before passing to opengl for compiling. I'll work on this and get back..thanks for going through the code.

siddharth.cmdz
11-17-2014, 02:01 PM
Works! thanks for the extra pair of eyes...appreciate it.