我将Vector3f作为统一变量加载到我的片段着色器中,但它作为空向量加载到着色器中。在将其加载到着色器之前,向量为(1,0,0)。当它传递到片段着色器时,它是(0,0,0)。所有其他载体都装得很好。
Fragment Shader:
#version 400 core
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector[4];
in vec3 toCameraVector;
out vec4 out_color;
uniform sampler2D backgroundTexture;
uniform sampler2D rTexture;
uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;
uniform vec3 lightColor[4];
uniform vec3 attenuation[4];
uniform float shineDamper;
uniform float specularity;
void main(void) {
vec4 blendMapColor = texture(blendMap, pass_textureCoords);
float backgroundAmount = 1 - (blendMapColor.r + blendMapColor.g + blendMapColor.b);
vec2 tiledCoords = pass_textureCoords * 100.0;
vec4 backgroundColor = texture(backgroundTexture, tiledCoords) * backgroundAmount;
vec4 rColor = texture(rTexture, tiledCoords) * blendMapColor.r;
vec4 gColor = texture(gTexture, tiledCoords) * blendMapColor.g;
vec4 bColor = texture(bTexture, tiledCoords) * blendMapColor.b;
vec4 totalColor = backgroundColor + rColor + gColor + bColor;
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i = 0; i < 4; i++) {
if(lightColor[i].x == 0 && lightColor[i].y == 0 && lightColor[i].z == 0) {
continue;
}
float distance = length(toLightVector[i]);
float attenuationFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
vec3 unitLightVector = normalize(toLightVector[i]);
float nDotl = dot(unitNormal, unitLightVector);
float brightness = max(nDotl, 0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLight = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLight, unitVectorToCamera);
specularFactor = max(specularFactor, 0.0);
float dampedFactor = pow(specularFactor, shineDamper);
totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attenuationFactor;
totalSpecular = totalSpecular + (dampedFactor * specularity * lightColor[i]) / attenuationFactor;
}
totalDiffuse = max(totalDiffuse, 0.2);
vec4 color = vec4(totalDiffuse, 1.0) * totalColor;
if(color.a < 0.5) discard;
out_color = color + vec4(totalSpecular, 1.0);
out_color = vec4(attenuation[0].x, attenuation[0].y, attenuation[0].z, 1); **USING FOR DEBUGGING**
}
我在加载矢量的地方:
public void loadLights(List<Light> lights)
{
for(int i = 0; i < MAX_LIGHTS; i++)
{
if(i < lights.size())
{
super.loadVector(loc_lightColor[i], lights.get(i).getColor());
super.loadVector(loc_lightPosition[i], lights.get(i).getPosition());
//super.loadVector(loc_attenuation[i], lights.get(i).getAttenuation());
super.loadVector(loc_attenuation[i], new Vector3f(1, 0, 0));
} else {
super.loadVector(loc_lightColor[i], new Vector3f(0, 0, 0));
super.loadVector(loc_lightPosition[i], new Vector3f(0, 0, 0));
super.loadVector(loc_attenuation[i], new Vector3f(1, 0, 0));
}
}
}
在片段着色器中,我使输出颜色等于矢量的每个部分(r到x,g到y,b到z),结果总是黑色(0,0,0)。
编辑:loadVector方法的代码如下:
protected void loadVector(int loc, Vector3f val)
{
glUniform3f(loc, val.x, val.y, val.z);
}