dunworray
dunworray

Reputation: 49

Fragment shader won't compile

I'm trying to get the shaders from this tutorial to work: https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php

I copied the source code into two files, but neither will compile.

This is the code I have:

    shaderProgram = glCreateProgram();
    vertexShader  = glCreateShader(GL_VERTEX_SHADER);
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    StringBuilder vertexShaderSource   = new StringBuilder();
    StringBuilder fragmentShaderSource = new StringBuilder();

    try{
        BufferedReader reader = new BufferedReader(new FileReader("src/shader.vert"));
        String line;
        line = reader.readLine();

        while(line != null){
            line = reader.readLine();

            if(line != null){
                vertexShaderSource.append(line).append('\n');
            }
        }
        reader.close();


    } catch (IOException e) {
        System.err.println("Vertex no load.");
        Display.destroy();
        System.exit(1);
    }

    try{
        BufferedReader reader = new BufferedReader(new FileReader("src/shader.frag"));
        String line;
        line = reader.readLine();

        while(line != null){
            line = reader.readLine();

            if(line != null){
                fragmentShaderSource.append(line).append('\n');
            }
        }
        reader.close();


    } catch (IOException e) {
        System.err.println("Frag no load.");
        Display.destroy();
        System.exit(1);
    }
    System.out.println(fragmentShaderSource);


    glShaderSource(vertexShader, vertexShaderSource);
    glCompileShader(vertexShader);
    if(glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println("Vertex no compile");
    }
    glShaderSource(fragmentShader, fragmentShaderSource);
    glCompileShader(fragmentShader);
    if(glGetShader(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println("frag no compile");
    }


    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glValidateProgram(shaderProgram);

I've had shaders work before, so its not the setup (that I know of) but when I copied and pasted neither file would compile anymore.

Is my setup wrong somehow?

EDIT >>>

I can't seem to import glGetShaderiv

I am using LWJGL and imported everything from there. Could I be missing a JAR file?

EDIT >>>

Related Question:

I get error "Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282)", what it is?, how I can fix it?

Upvotes: 0

Views: 1112

Answers (1)

javac
javac

Reputation: 2441

You are reading the source files incorrectly.

line = reader.readLine();

while(line != null){
    line = reader.readLine();

    if(line != null){
        fragmentShaderSource.append(line).append('\n');
    }
}

Here, you first read the first line of the file. If it isn't null, you continue reading lines and appending them to the buffer. However, you don't actually append the line after the readLine() statement outside of the loop, which means it is discarded and the shader compiler gets a source file with the first line missing. Therefore it doesn't know about varying vec2 vTexCoord; or sampler2D myTexture;, raising compiler errors when they are used later in the code.

There are several ways to fix this:

  1. line = reader.readLine();
    if (line != null) {
        fragmentShaderSource.append(line).append('\n');
    }
    
    while (line != null) {
        // ...
    }
    
  2. while (true) {
        line = reader.readLine();
    
        if (line == null) {
            break;
        }
    
        fragmentShaderSource.append(line).append('\n');
    }
    
  3. while ((line = reader.readLine()) != null) {
        fragmentShaderSource.append(line).append('\n');
    }
    

Which one to use depends on your preferences. 1 contains redundant code, which is almost never the best solution. 2 is a commonly used way of reading arbitrary files; and while 3 is shorter, some people don't like assigning variables in loop control statements.

Upvotes: 3

Related Questions