Help with Shaders!

Started by Niles, April 28, 2016, 18:36:36

Previous topic - Next topic

Niles

Hello... I am creating a game/game engine using java... Please, if possible, tell me what i did wrong... It has something to do with Shaders, but I can't figure out what. Thanks!

Error:
Could not compile shader.
ERROR: 0:1: '' : syntax error: #version



package engineTester;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import renderEngine.DisplayManager;
import renderEngine.Loader;
import renderEngine.RawModel;
import renderEngine.Renderer;
import shaders.StaticShader;

public class Main {

	public static void main(String[] args) {
		
		DisplayManager.createDisplay();
		
		Loader loader = new Loader();
		Renderer renderer = new Renderer();
		StaticShader shader = new StaticShader();
		

float[] vertices = { -0.5f, 0.5f, 0f, -0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, -0.5f, 0f, 0.5f, 0.5f, 0f, -0.5f, 0.5f, 0f
};

	int[] indices = {
			0,1,3, //top left triangle V0, v1, v3
			3,1,2 //bottom right triangle
	};

		RawModel model = loader.loadToVAO(vertices,indices);
		
		while(!Display.isCloseRequested()) {
			renderer.prepare();
			//game logic
			shader.start();

			
			System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));
			renderer.render(model);
			shader.stop();
			DisplayManager.updateDisplay();
		}
		//shader.cleanUp();
		loader.cleanUp();
		DisplayManager.closeDisplay();

	}

}



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);
        bindAttributes();
        GL20.glLinkProgram(programID);
        GL20.glValidateProgram(programID);
    }
     
    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(System.getProperty("line.separator"));
            }
            
            reader.close();
        }catch(IOException e){
            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;
    }
 
}


package shaders;

public class StaticShader extends ShaderProgram{
	
	private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
	private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";
	
	public StaticShader() {
		super(VERTEX_FILE, FRAGMENT_FILE);
	}

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

}


Fragment Shader.txt
#version 120 core

in vec3 color;

out vec4 out_Color;

void main(void){

	out_Color = vec4(color,1.0);

}


Vertex Shader.txt
#version 120 core

in vec3 position;

out vec3 color;

void main(void){

	gl_Position = vec4(position,1.0);
	color = vec3(position.x+0.5,0.0,position.y+0.5);
}


SilverTiger

With GLSL version 1.20 you don't have core and compatibility profile.
You should use #version 120 in your shader files.

Alternatively you could bump your GLSL version to 1.50, if you are going to use OpenGL 3.2 and no deprecated stuff.
In that case you could use #version 150 core.