1

我一直在尝试在我的 OpenGL 中实现非常简单的着色器。不幸的是,我一直遇到:

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

我已经尝试自己解决这个问题,并且我认为我在某个地方重新声明了 sf::Shader。

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() 和 init() 只是设置了一些 OpenGL 变量,所以我很确定它们不是问题。

4

1 回答 1

2

看起来像是 sfml 家伙的设计疏忽。sf::Shader如果实现移动构造函数,这将起作用。就目前而言,您不能移动或复制 asf::Shader因为它的复制构造函数被声明为私有并且没有定义移动构造函数。

你所能做的就是在堆上分配它并从函数返回一个指向它的指针。

我建议你使用

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;
}

然后拨打电话

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

如果你想传递着色器更多使用 astd::shared_ptr而不是std::unique_ptr

于 2013-09-10T03:36:26.637 回答