我在java中使用lwjgl製作3D遊戲,並使用GLSL作爲着色器。嘗試運行的代碼後,我得到:在glsl中操作類型'*'錯誤?
Sun May 29 17:53:13 BST 2016 INFO:Use Java PNG Loader = true
ERROR: 0:34: '' : wrong operand types no operation '' exists that takes a left-hand operand of type 'float' and a right operand of type 'uniform 10-element array of 3-component vector of float' (or there is no acceptable conversion)
Could not compile shader!
的地形片段着色器代碼:
#version 140 core
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector[10];
in vec3 toCameraVector;
in float visibility;
out vec4 out_Color;
uniform sampler2D backgroundTexture;
uniform sampler2D rTexture;
uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;
uniform vec3 lightColour[10];
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;
void main(void){
vec4 blendMapColour = texture(blendMap, pass_textureCoords);
float backTextureAmount = 1 - (blendMapColour.r + blendMapColour.g + blendMapColour.b);
vec2 tiledCoords = pass_textureCoords * 40.0;
vec4 backgroundTextureColour = texture(backgroundTexture, tiledCoords) * backTextureAmount;
vec4 rTextureColour = texture(rTexture, tiledCoords) * blendMapColour.r;
vec4 gTextureColour = texture(gTexture, tiledCoords) * blendMapColour.g;
vec4 bTextureColour = texture(bTexture, tiledCoords) * blendMapColour.b;
vec4 totalColour = backgroundTextureColour + rTextureColour + gTextureColour + bTextureColour;
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i=0;i<10;i++){
vec3 unitLightVector = normalize(toLightVector[i]);
float nDotl = dot(unitNormal,unitLightVector);
float brightness = max(nDotl,0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);
float specularFactor = dot(reflectedLightDirection , unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
totalDiffuse = totalDiffuse + brightness * lightColour[i];
totalSpecular = totalSpecular + dampedFactor * reflectivity * lightColour[i];
}
totalDiffuse = max(totalDiffuse,0.2);
out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);
}
vertext着色地形代碼:
#version 140 core
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector[10];
out vec3 toCameraVector;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition[10];
const float density = 0.0035;
const float gradient = 5.0;
void main(void){
vec4 worldPosition = transformationMatrix * vec4(position,1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal,0.0)).xyz;
for(int i=0;i<10;i++){
toLightVector[i] = lightPosition[i] - worldPosition.xyz;
}
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
頂點着色器的代碼:
#version 140 core
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector[10];
out vec3 toCameraVector;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition[10];
uniform float useFakeLighting;
uniform float numberOfRows;
uniform vec2 offset;
const float density = 0.0035;
const float gradient = 5.0;
void main(void){
vec4 worldPosition = transformationMatrix * vec4(position,1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = (textureCoords/numberOfRows) + offset;
vec3 actualNormal = normal;
if(useFakeLighting>0.5){
actualNormal = vec3(0.0,1.0,0.0);
}
surfaceNormal = (transformationMatrix * vec4(actualNormal,0.0)).xyz;
for(int i=0;i<10;i++){
toLightVector[i] = lightPosition[i] - worldPosition.xyz;
}
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
對於片段着色器的代碼:
#version 140 core
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector[10];
in vec3 toCameraVector;
in float visibility;
out vec4 out_Color;
uniform sampler2D modelTexture;
uniform vec3 lightColour[10];
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i=0;i<10;i++){
vec3 unitLightVector = normalize(toLightVector[i]);
float nDotl = dot(unitNormal,unitLightVector);
float brightness = max(nDotl,0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);
float specularFactor = dot(reflectedLightDirection , unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
totalDiffuse = totalDiffuse + brightness * lightColour;
totalSpecular = totalSpecular + dampedFactor * reflectivity * lightColour[i];
}
totalDiffuse = max(totalDiffuse,0.2);
vec4 textureColour = texture(modelTexture,pass_textureCoords);
if(textureColour.a<0.5){
discard;
}
out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);
}
我已經通過我自己看了很多次,但我不是,與GLSL經歷了那麼有可能是我還沒有注意到一個錯誤。您的片段着色器的
它看起來像你試圖通過一個浮點數來擴展一個RGB值的矢量,這顯然不被支持 –
@Nasso我只是做了你所說的但是它仍然不起作用 – NathanProgrammer