Java GLSL错误#132语法错误:";德国劳埃德船级社“位置”;解析错误
我已经尽力找到这个问题的答案,或者我在代码中犯的任何错误,但是我什么也找不到 如果这有帮助的话,我有一个sapphire Radeon HD 6950图形卡,它使用AMD Radeon HD 6900系列驱动程序 错误消息:Java GLSL错误#132语法错误:";德国劳埃德船级社“位置”;解析错误,java,opengl,glsl,Java,Opengl,Glsl,我已经尽力找到这个问题的答案,或者我在代码中犯的任何错误,但是我什么也找不到 如果这有帮助的话,我有一个sapphire Radeon HD 6950图形卡,它使用AMD Radeon HD 6900系列驱动程序 错误消息: Wed May 27 13:55:50 CDT 2015 INFO:Use Java PNG Loader = true Vertex shader failed to compile with the following errors: ERROR: 0:26: erro
Wed May 27 13:55:50 CDT 2015 INFO:Use Java PNG Loader = true
Vertex shader failed to compile with the following errors:
ERROR: 0:26: error(#132) Syntax error: "gl_Position" parse error
ERROR: error(#273) 1 compilation errors. No code generated
Could not Compile Shader.
顶点着色器:
#version 400 core
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
uniform float useFakeLighting;
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;
vec3 actualNormal = normal;
if(useFakeLighting > 0.5)
{
actualNormal = vec3(0.0, 1.0, 0.0);
}
surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = vec4(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 400 core
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;
out vec4 out_Color;
uniform sampler2D modelTexture;
uniform vec3 lightColor;
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColor;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.2);
vec3 diffuse = brightness * lightColor;
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
specularFactor = max(specularFactor, 0.0);
float dampedFactor = pow(specularFactor, shineDamper);
vec3 finalSpecular = dampedFactor * lightColor * reflectivity;
vec4 textureColor = texture(modelTexture, pass_textureCoords);
if(textureColor.a < 0.5)
{
discard;
}
out_Color = vec4(diffuse, 1.0) * texture(modelTexture,pass_textureCoords) + vec4(finalSpecular, 1.0);
out_Color = mix(vec4(skyColor, 1.0), out_Color, visibility);
}
最后,,
着色器程序类:
package shaders;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
public abstract class ShaderProgram
{
private int programID;
private int vertexShaderID;
private int fragmentShaderID;
private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
public ShaderProgram(String vertexFile, String fragmentFile)
{
vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
bindAttributes();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
getAllUniformLocations();
}
protected abstract void getAllUniformLocations();
protected int getUniformLocation(String uniformName)
{
return GL20.glGetUniformLocation(programID, uniformName);
}
public void start()
{
GL20.glUseProgram(programID);
}
public void stop()
{
GL20.glUseProgram(0);
}
public void cleanUp()
{
stop();
GL20.glDetachShader(programID, vertexShaderID);
GL20.glDetachShader(programID, fragmentShaderID);
GL20.glDeleteShader(vertexShaderID);
GL20.glDeleteShader(fragmentShaderID);
GL20.glDeleteProgram(programID);
}
protected abstract void bindAttributes();
protected void bindAttribute(int attribute, String variableName)
{
GL20.glBindAttribLocation(programID, attribute, variableName);
}
protected void loadFloat(int location, float value)
{
GL20.glUniform1f(location, value);
}
protected void loadInt(int location, int value)
{
GL20.glUniform1i(location, value);
}
protected void loadVector(int location, Vector3f vector)
{
GL20.glUniform3f(location, vector.x, vector.y, vector.z);
}
protected void loadBoolean(int location, boolean value)
{
float toLoad = 0;
if(value)
toLoad = 1;
GL20.glUniform1f(location, toLoad);
}
protected void loadMatrix(int location, Matrix4f matrix)
{
matrix.store(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4(location, false, matrixBuffer);
}
private static int loadShader(String fileName, int type) //opens up the source code files for the shaders and reads them to get their outputs.
{
StringBuilder shaderSource = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while((line = reader.readLine()) != null)
{
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Couldnt read shader file!");
e.printStackTrace();
System.exit(-1);
}
int shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);
if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE)
{
System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
System.err.println("Could not Compile Shader.");
System.exit(-1);
}
return shaderID;
}
}
为什么必须在着色器源中重新声明
gl_PerVertex
结构,有一些原因。例如,使用可分离着色器程序时,必须重新声明gl_PerVertex
块(并且它们必须与管道中连接的所有着色器匹配)。我不确定为什么在这种情况下需要重新声明(似乎在我的驱动程序上工作),这可能是因为Java层正在使用一些需要使用它的扩展。重新定义结构可能会解决您的问题。在顶点着色器的顶部放置:
out gl_PerVertex { vec4 gl_Position; };
看起来不错,你能用最小(一条线)顶点和碎片着色器工作吗?是的,最小着色器似乎工作得很好。这似乎解决了问题!谢谢
out gl_PerVertex { vec4 gl_Position; };