I'm using LWJGL to try to make a 3D game, but I've run into an error I can't resolve on my own. Whenever I run the program, I get the error:
java.lang.Exception: Error compiling shader code: 2
I set this error up myself to alert me if the shader failed to compile, which it seems to be doing every time, and I can't understand why.
public static String loadResource(String file) throws Exception{
BufferedReader reader = new BufferedReader(new FileReader(new File(file)));
String out = "";
String s = reader.readLine();
while (s != null){
out += s + "\n";
s = reader.readLine();
}
reader.close();
return out;
}
That is the method I use to load the text of my shader code and place it into a string. I've asked it to print it out afterwards, and as far as i can tell there aren't any issues with it.
public void createVertexShader(String shaderCode) throws Exception{
vertexShaderId = createShader(shaderCode, GL_VERTEX_SHADER);
}
public void createFragmentShader(String shaderCode) throws Exception {
vertexShaderId = createShader(shaderCode, GL_FRAGMENT_SHADER);
}
These two methods above use the following method to create the vertex and fragment shaders:
public int createShader(String shaderCode, int shaderType) throws Exception {
int shaderId = glCreateShader(shaderType);
if(shaderId == 0){
throw new Exception("Error creating shader code: " + shaderId);
}
glShaderSource(shaderId, shaderCode);
glCompileShader(shaderId);
if(glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0){
throw new Exception("Error compiling shader code: " + shaderId);
}
glAttachShader(programId, shaderId);
return shaderId;
}
The shader gets created with no errors, but when I check to see if it's been compiled properly, it throws the exception and stops my program. Here are my vertex and fragment shaders:
vertex.vs
#version 150
layout (location=0) in vec3 pos;
void main()
{
gl_Position = vec4(position, 1.0);
}
fragment.fs
#version 150
out vec4 fragColor;
void main()
{
fragColor = vec4(0.0, 0.5, 0.5, 1.0);
}
I have verified that I am using the correct GLSL version in my shaders, but I got the actual shader code from a tutorial, so I don't know if the syntax is strictly correct or not.
layout (location=0) in vec3 pos;
this line will give you syntax error with #version 150 or below. Try #version 330 or higher in your vertex shader.
Also, in the vertex shader pos
and position
are different symbols and you've failed to provide a definition for position
.