简体   繁体   中英

OpenGL Fragment Shader Compilation Issue

I was following a tutorial on writing a game engine in Java using LWJGL. I wrote a fragment shader in GLSL and called it basicFragment.fs:

#version 330

out vec4 fragColor;

void main()
{
    fragColor = vec4(0,0, 1.0, 1.0, 1.0);
}

Now I followed the tutorial to write a way to compile the shader and it is here that I get an error. First, here is my Shader class.

package com.base.engine;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL21.*;
import static org.lwjgl.opengl.GL32.*;

public class Shader 
{
    private int program;

    public Shader()
    {
        program = glCreateProgram();

        if(program == 0)
        {
            System.err.println("Shader creation failed: Could not find valid memory location in constructor");
            System.exit(1);
        }
    }

    public void bind()
    {
        glUseProgram(program);
    }

    public void addVertexShader(String text)
    {
        addProgram(text, GL_VERTEX_SHADER);
    }

    public void addGeometryShader(String text)
    {
        addProgram(text, GL_GEOMETRY_SHADER);
    }

    public void addFragmentShader(String text)
    {
        addProgram(text, GL_FRAGMENT_SHADER);
    }

    public void compileShader()
    {
        glLinkProgram(program);

        if(glGetProgrami(program, GL_LINK_STATUS) == 0)
        {
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }

        glValidateProgram(program);

        if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0)
        {
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }
    }

    private void addProgram(String text, int type)
    {
        int shader = glCreateShader(type);

        if(shader == 0)
        {
            System.err.println("Shader creation failed: Could not find valid memory location when adding shader");
            System.exit(1);
        }

        glShaderSource(shader,text);
        glCompileShader(shader);

        if(glGetShaderi(shader, GL_COMPILE_STATUS) == 0)
        {
            System.err.println(glGetShaderInfoLog(shader, 1024));
            System.exit(1);
        }

        glAttachShader(program, shader);
    }
}

Then, in my game class I do the following. (Hopefully the code is self explanatory. Let me know if I'm unclear anywhere):

public class Game 
{
    private Mesh mesh;
    private Shader shader;

    public Game()
    {
        mesh = new Mesh();
        shader = new Shader();

        Vertex[] data = new Vertex[] {new Vertex(new Vector3f(-1,-1,0)),
                                      new Vertex(new Vector3f(-1,1,0)),
                                      new Vertex(new Vector3f(0,1,0))};

        mesh.addVertices(data);

        shader.addVertexShader(ResourceLoader.loadShader("basicVertex.vs"));
        shader.addFragmentShader(ResourceLoader.loadShader("basicFragment.fs"));
        shader.compileShader();
    }

    public void render()
    {
        shader.bind();
        mesh.draw();

    }

So basically, when I call the function,

shader.addFragmentShader(ResourceLoader.loadShader("basicFragment.fs"));

The function gives me this error:

3.3.0 - Build 8.15.10.2712
ERROR: 0:7: 'constructor' : too many arguments

I don't get any error when I just implement the vertex shader so I've concluded it's the fragment shader.

That error is getting thrown from this part of the Shader class:

if(glGetShaderi(shader, GL_COMPILE_STATUS) == 0)
{
    System.err.println(glGetShaderInfoLog(shader, 1024));
    System.exit(1);
}

Which leads me to believe that the problem is that the shader isn't compiling because some constructor has too many arguments. I don't know exactly what constructor it is referring to.

Any help would be appreciated.

I don't know exactly what constructor it is referring to.

The one in your shader code:

fragColor = vec4(0,0, 1.0, 1.0, 1.0);

That's a vec4 constructor. And you gave it 5 parameters. "0,0" is two of them. I rather suspect you meant to use a period rather than a comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM