Java OpenGL着色错误

时间:2014-10-25 15:00:55

标签: java opengl shader

尝试运行此程序时,我的控制台中出现以下错误

ERROR: 0:8: 'g1_Position' : undeclared identifier 
ERROR: 0:8: 'assign' :  cannot convert from '4-component vector of float' to 'float'


could not compile shader

我似乎无法找到问题所在。

vertexShaders.txt

#version 400 core

in vec3 position;

out vec3 color;

void main (void){
    g1_Position = vec4(position,1.0);
    color = vec3(position.x+0.5,1.0,position.y+0.5);
}

fragmentShaders.txt

#version 400 core

in vec3 color;

out vec4 out_Color;

void main(void){
    out_Color = vec4(color, 1.0);
}

ShaderProgram.java

package shaders;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

public abstract class ShaderProgram {

    private int programID;
    private int vertexShaderID;
    private int fragmentShaderID;

    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);
        GL20.glLinkProgram(programID);
        GL20.glValidateProgram(programID);
        bindAttributes();
    }

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

    private static int loadShader(String file, int type){
        StringBuilder shaderSource = new StringBuilder();
        try{
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while((line = reader.readLine())!=null){
                shaderSource.append(line).append("\n");
            }
            reader.close();
        }catch(IOException e){
            System.err.println("could not read 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;

    }
}

StaticShader.java

package shaders;

public class StaticShader extends ShaderProgram{

    private static final String VERTEX_FILE = "src/shaders/vertexShaders.txt";
    private static final String FRAGMENT_FILE = "src/shaders/fragmentShaders.txt";

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

    protected void bindAttributes(){
        super.bindAttribute(0, "position");
    }
}

非常感谢任何帮助。

0 个答案:

没有答案