GLSL Shader Draws Only Black Screen LWJGL

492 views Asked by At

I am very new to shaders, and I got some GLSL code to compile properly (well without any compiler errors), and for some reason I keep getting a black screen. I am using LWJGL. I keep getting a black screen when I try to render a triangle, even though it should be white according to the color that I passed to the fragment shader. I've posted snippets of the code below, hopefully there's enough to figure out what the problem is.

Fragment Shader Source Code

void main()
{
    gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
{

Vertex Shader Source Code

void main()
{
    gl_Position = ftransform();
}

Shader Reader Code

        vertShaderString = shaderName + ".vert";
        fragShaderString = shaderName + ".frag";
        shader = GL20.glCreateProgram();
        vertShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);

        try{
            String temp;
            BufferedReader reader = new BufferedReader(new FileReader(new File(vertShaderString)));
            while ((temp = reader.readLine()) != null){
                vertSource.append(temp).append("\n");
            }
            reader.close();


            BufferedReader fragReader = new BufferedReader(new FileReader(new File(fragShaderString)));
            String otherTemp;
            while ((otherTemp = fragReader.readLine()) != null){
                fragSource.append(otherTemp).append("\n");
            }
            fragReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }

Shader Setup Code

        GL20.glShaderSource(vertShader, vertSource);
        GL20.glCompileShader(vertShader);

        if (GL20.glGetShaderi(vertShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE){
            System.err.println("Failed to compile vertex shader");
        }


        GL20.glShaderSource(fragShader, fragSource);
        GL20.glCompileShader(fragShader);

        if (GL20.glGetShaderi(fragShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE){
            System.err.println("Failed to compile fragment shader");
        }

        GL20.glAttachShader(shader, vertShader);
        GL20.glAttachShader(shader, fragShader);
        GL20.glLinkProgram(shader);
        GL20.glValidateProgram(shader);

Enable and Disable Shader Code

public void begin(){
    GL20.glUseProgram(shader);
}

public void end(){
    GL20.glUseProgram(0);
}

Render Method

public void render(){
        GL11.glClearColor(0, 0, 0, 1);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glColor4f(1, 0, 0, 1);
        shader.begin();
        GL11.glBegin(GL11.GL_TRIANGLES);
        GL11.glVertex2i(0,  0);
        GL11.glVertex2i(500, 0);
        GL11.glVertex2i(250, 250);
        GL11.glEnd();
        shader.end();

    }
1

There are 1 answers

0
devCorner On

The problem has been solved, thanks to jozxyqk. I had forgotten to initialize the fragment shader variable.

fragShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);

By adding this line of code the problem was solved.