gpt4 book ai didi

java - glsl 中的操作 '*' 类型错误?

转载 作者:行者123 更新时间:2023-12-02 03:35:22 26 4
gpt4 key购买 nike

我正在用 java 中的 lwjgl 制作 3d 游戏,并且使用 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);
}

地形顶点着色器代码:

#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 没有那么丰富的经验,所以可能有一个我没有注意到的错误。

最佳答案

片段着色器的第 34 行:

totalDiffuse = totalDiffuse + brightness * lightColour;

您忘记指定光颜色的索引:

totalDiffuse = totalDiffuse + brightness * lightColour[i];

关于java - glsl 中的操作 '*' 类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com