1

I've been trying to implement very simple shaders into my OpenGL. Unfortunately I keep running into:

Cannot access private member declared in class 'sf::NonCopyable

I have tried figuring this out by myself, and I think that somewhere I am re declaring sf::Shader somewhere.

sf::Shader compile_shaders(void)
{
sf::Shader shader;

const std::string vertex_shader_source = \
    "#version 430 core  "\
    "                   "\
    "void main(void){   "\
    "   gl_position = vec4(0.0, 0.0, 0.5, 1.0);"\
    "}                  ";

const std::string fragment_shader_source = \
    "#version 430 core                  "\
    "                                   "\
    "out vec4 color;                    "\
    "                                   "\
    "void main(void){                   "\
    "   color = vec4(0.0, 0.8, 1.0, 1.0);"\
    "}                                  ";

shader.loadFromMemory(vertex_shader_source, fragment_shader_source);

return shader;
}

int main(){
int width = 800;
int height = 450;
sf::Window window(sf::VideoMode(width, height), "OpenGL Test", sf::Style::Default, sf::ContextSettings(32));

resize(width, height);
init();
window.setVerticalSyncEnabled(true);


sf::Shader shader = compile_shaders();


bool running = true;
while (running){
    sf::Event event;
    while (window.pollEvent(event)){
        if (event.type == sf::Event::Closed)
            running = false;
        else if (event.type == sf::Event::Resized){
            width = event.size.width;
            height = event.size.height;
            resize(width, height);
        }
        else if (event.type == sf::Event::KeyPressed & event.key.code == sf::Keyboard::Escape)
            running = false;
    }
    sf::Shader::bind(&shader);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(-1.5f,0.0f,-6.0f);

    glBegin(GL_TRIANGLES);

        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);

        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-1.0f,-1.0f, 0.0f);

        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();

    glTranslatef(3.0f, 0.0f, 0.0f);

    glColor3f(0.5f,0.5f,1.0f);
    glBegin(GL_QUADS);
        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f);
        glVertex3f(-1.0f,-1.0f, 0.0f);
    glEnd();
    window.display();

    sf::Shader::bind(NULL);
}

return 0;
}

resize() and init() are just setting up some OpenGL variables, so I'm pretty sure they are not the problem.

1
  • Minor note: the \ at the end of your string literals are redundant. You can (and should in my opinion) remove them if you want. Commented Apr 2, 2018 at 7:00

1 Answer 1

2

Looks like a design oversight by the sfml guys. This would work if sf::Shader would implement a move constructor. As it stands you can't move or copy a sf::Shader because its copy constructor is declared private and no move constructor is defined.

All you can do is allocate it on the heap and return a pointer to it from the function.

I would recommend you use

std::unique_ptr<sf::Shader> compile_shaders(void){
    auto shader = std::make_unique<sf::Shader>();
    //or auto shader = std::unique_ptr<sf::Shader>(new sf::Shader()); if you don't have make_unique available

    //... code ommitted                   

    shader->loadFromMemory(vertex_shader_source, fragment_shader_source);

    return shader;
}

and then make the call with

auto shader = compile_shaders();
//... code omitted
sf::Shader::bind(shader.get());

If you want to pass around the shader some more use a std::shared_ptr instead of std::unique_ptr

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I'll have to spend some time figuring out what all this is actually doing, but you got it to work so you rock!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.