Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/234290576260988928

Shadering Shaders not linking correctly

Source Link
Darestium
  • 1.2k
  • 2
  • 15
  • 34

Shadering not linking correctly

I'm writing a rather simple "ShaderManager" which aids me with loading shaders in Open GL, altough I am having a few issues:

  1. The shaders don't link correctly
  2. The the attributes don't bind

Here's the output of my program:

The shaders did not link correctly.
Could not bind attribute: v_coord3d
Could not bind attribute: v_color
Could not bind uniform: mvp

And here's the relevant code and header file...

void ShaderManager::create_program(std::string name)  {
    GLuint vertexshader;
    GLuint fragmentshader;

    GLint link_ok = GL_FALSE;

    if ((vertexshader = create_shader(name + ".v.glsl", GL_VERTEX_SHADER)) == 0)  {
        print_shader_errors(&vertexshader);
    }

    if ((fragmentshader = create_shader(name + ".f.glsl", GL_FRAGMENT_SHADER)) == 0)  {
        print_shader_errors(&fragmentshader);
    }

    program_values.emplace_back(glCreateProgram());
    program_keys.emplace_back(name);
    glAttachShader(program_values[program_values.back()], vertexshader);
    glAttachShader(program_values[program_values.back()], fragmentshader);
    glLinkProgram(program_values[program_values.back()]);

    glGetProgramiv(program_values[program_values.back()], GL_LINK_STATUS, &link_ok);

    if (!link_ok)  {
        std::cout << "The shaders did not link correctly." << "\n";
    }
}


bool ShaderManager::bind_attribute(std::string shader_name, std::string attribute_name)  {
    GLuint attribute = glGetAttribLocation(get_program(shader_name), attribute_name.c_str());

    if (attribute != -1)  {
        attribute_values.emplace_back(attribute);
        attribute_keys.emplace_back(attribute_name);

        return true;
    }

    std::cout << "Could not bind attribute: " << attribute_name << "\n";

    return false;
}

// bind uniform function is much the same as the above

class ShaderManager {
    public:
        ShaderManager();
        virtual ~ShaderManager();

        static GLuint get_program(std::string shader_name);
        static int get_index_of_program_key(std::string program_name);

        static void create_program(std::string name);
        static bool bind_attribute(std::string shader_name, std::string attribute_name);
        static bool bind_uniform(std::string shader_name, std::string uniform_name);

    protected:
    private:
        static std::vector<GLuint> program_values;
        static std::vector<std::string> program_keys;

        static std::vector<GLuint> attribute_values;
        static std::vector<std::string> attribute_keys;

        static std::vector<GLuint> uniform_values;
        static std::vector<std::string> uniform_keys;

        static void print_shader_errors(GLuint *shader);
        static const char* file_read(std::string filename);
        static GLuint create_shader(std::string filename, GLenum type);
};

Here's the code which actually calls the functions:

ShaderManager::create_program("shader");
ShaderManager::bind_attribute("shader", "v_coord3d");
ShaderManager::bind_attribute("shader", "v_color");
ShaderManager::bind_uniform("shader", "mvp");

Another issue I have is that I can't access a string in a vector list with program_names[0], anyone know why?