我一直在尝试在我的 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 变量,所以我很确定它们不是问题。