GLSL错误#132语法错误:“gl_position”解析错误 [英] GLSL Error #132 Syntax error: "gl_position" parse error

查看:1016
本文介绍了GLSL错误#132语法错误:“gl_position”解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尽力找到这个问题的答案或我在代码中犯的任何错误,但我找不到任何东西..

I've tried my best to find the answer to this problem or any error i had made in the code, but i just couldn't find anything..

如果这有帮助,我有蓝宝石Radeon HD 6950显卡,它使用AMD Radeon HD 6900系列驱动程序。

If this helps, i have a sapphire Radeon HD 6950 graphics card and it uses an AMD Radeon HD 6900 series driver.

错误信息:

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.

Vertex Shader:

Vertex 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);
}

FragmentShader:

FragmentShader:

#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);

}  

StaticShader(加载着色器):

StaticShader(loads the Shaders):

package shaders;

import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

import com.game.entities.Camera;
import com.game.entities.Light;
import com.game.toolbox.Maths;

public class StaticShader extends ShaderProgram 
{
    public static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
    public static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";

    private int location_transformationMatrix;
    private int location_projectionMatrix;
    private int location_viewMatrix;
    private int location_lightPos;
    private int location_lightCol;
    private int location_shineDamper;
    private int location_reflectivity;
    private int location_useFakeLighting;

    private int location_skyColor;


    public StaticShader() 
    {
        super(VERTEX_FILE, FRAGMENT_FILE);
    }


    @Override
    protected void bindAttributes() //bind variables in shader files with variables in java files.
    {
        super.bindAttribute(0, "position");
        super.bindAttribute(1, "textureCoords");
        super.bindAttribute(2, "normal");
    }


    @Override
    protected void getAllUniformLocations() 
    {
        location_transformationMatrix = super.getUniformLocation("transformationMatrix");
        location_projectionMatrix =     super.getUniformLocation("projectionMatrix");
        location_viewMatrix = super.getUniformLocation("viewMatrix");

        //Light
        location_lightPos = super.getUniformLocation("lightPosition");
        location_lightCol = super.getUniformLocation("lightColor");
        location_shineDamper = super.getUniformLocation("shineDamper");
        location_reflectivity = super.getUniformLocation("reflectivity");
        location_useFakeLighting = super.getUniformLocation("useFakeLighting");

        //Sky and Fog
        location_skyColor = super.getUniformLocation("skyColor");
    }

    public void loadSkyColor(float r, float g, float b)
    {
        super.loadVector(location_skyColor, new Vector3f(r, g, b));
    }

    public void loadFakeLightingVariable(boolean useFake)
    {
        super.loadBoolean(location_useFakeLighting, useFake);
    }

    public void loadShineVariables(float damper, float reflectivity)
    {
        super.loadFloat(location_shineDamper, damper);
        super.loadFloat(location_reflectivity, reflectivity);
    }

    public void loadTransformationMatrix(Matrix4f matrix)
    {
        super.loadMatrix(location_transformationMatrix, matrix);
    }

    public void loadLight(Light light)
    {
        super.loadVector(location_lightPos, light.getPos());
        super.loadVector(location_lightCol, light.getColor());
    }

    public void loadProjectionMatrix(Matrix4f projection)
    {
        super.loadMatrix(location_projectionMatrix, projection);    
    }

    public void loadViewMatrix(Camera camera)
    {
        Matrix4f viewMatrix = Maths.createViewMatrix(camera);
        super.loadMatrix(location_viewMatrix, viewMatrix);
    }
}

最后,
ShaderProgram类:

Finally, ShaderProgram Class:

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_ARB_separate_shader_objects 时,您必须重新声明 gl_PerVertex 块(它们必须匹配管道中连接的所有着色器)。我不确定为什么在这种情况下你需要重新声明(似乎在我的驱动程序上工作),可能是Java层正在使用一些需要使用它的扩展。重新定义结构可能会解决您的问题。在顶点着色器的顶部放置:

There are some reasons why the gl_PerVertex structure must be redeclared in your shader sources. For example, when using separable shader programs GL_ARB_separate_shader_objects, you must redeclare the gl_PerVertex blocks (and they must match for all shaders attached in a pipeline). I am unsure why in this case you would be required to redeclare (seems to work on my driver), it may be that the Java layer is utilizing some extension that requires its usage. Redeclaring the structure will likely solve your issue. At the top of your vertex shader put:

out gl_PerVertex { vec4 gl_Position; };

这篇关于GLSL错误#132语法错误:“gl_position”解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆