请求兼容性时的核心 OpenGL 上下文?
Core OpenGL context when Compatibility requested?
这是我的代码:
// Display.cpp
#include <memory>
#include <SFML/Graphics.hpp>
#include <GL/glew.h>
namespace Display
{
constexpr static int WIDTH = 1280; constexpr static int HEIGHT = 720;
std::unique_ptr<sf::RenderWindow> window;
void init() {
sf::ContextSettings settings;
settings.depthBits = 24;
settings.majorVersion = 3;
settings.minorVersion = 3; // OpenGL 3.3
settings.attributeFlags = sf::ContextSettings::Default;
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT),
"Fcku",
sf::Style::Close,
settings);
glewInit();
glViewport(0, 0, WIDTH, HEIGHT);
}
void close() {
window->close();
}
void clear() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
void update() {
window->display();
}
void checkForClose() {
sf::Event e;
while (window->pollEvent(e))
if (e.type == sf::Event::Closed) close();
}
bool isOpen() {
return window->isOpen();
}
} // namespace Display
int main()
{
Display::init();
while (Display::isOpen()) {
Display::clear();
Display::update();
Display::checkForClose();
}
return 0;
}
我这样编译上面的文件:
g++ Display.cpp -Wall -O2 --std=c++14 -fexceptions -o test.o -lsfml-graphics -lsfml-audio -lsfml-network -lsfml-window -lsfml-system -lGL -lGLU -lGLEW -DGLEW_STATIC
(还没开始写 makefile)
这会生成一个名为 test
的二进制文件,但是当我 运行 它时,我收到以下警告:
Warning: The created OpenGL context does not fully meet the settings that were requested
Requested: version = 4.1 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = false ; debug = false ; sRGB = false
Created: version = 3.3 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = true ; debug = false ; sRGB = false
这确实创建了一个黑色 window(正如预期的那样),但我怀疑一旦我开始使用 SFML/Graphics.hpp
中的绘图函数,它就会出现段错误,因为当我尝试编译一个示例文件(它也打印了同样的错误)。
当我创建我的 sf::ContextSettings
时,我将它的 attributeFlags
设置为 sf::ContextSettings::Default
所以根据我的理解它应该创建一个兼容性上下文(因为 SFML 使用一些遗留代码这是必须的) .
P.S。如果重要的话,我在 Void Linux 上,并且我从 repos
安装了我在此处使用的所有内容的最新版本
好的,似乎解决方案是使用 OpenGL 3.0 而不是支持兼容性配置文件的 3.3。但是现在我不能使用 GLSL 3.30,而且很乱,所以我会尝试 SDL2、GLFW 和 Raylib。
这是我的代码:
// Display.cpp
#include <memory>
#include <SFML/Graphics.hpp>
#include <GL/glew.h>
namespace Display
{
constexpr static int WIDTH = 1280; constexpr static int HEIGHT = 720;
std::unique_ptr<sf::RenderWindow> window;
void init() {
sf::ContextSettings settings;
settings.depthBits = 24;
settings.majorVersion = 3;
settings.minorVersion = 3; // OpenGL 3.3
settings.attributeFlags = sf::ContextSettings::Default;
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT),
"Fcku",
sf::Style::Close,
settings);
glewInit();
glViewport(0, 0, WIDTH, HEIGHT);
}
void close() {
window->close();
}
void clear() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
void update() {
window->display();
}
void checkForClose() {
sf::Event e;
while (window->pollEvent(e))
if (e.type == sf::Event::Closed) close();
}
bool isOpen() {
return window->isOpen();
}
} // namespace Display
int main()
{
Display::init();
while (Display::isOpen()) {
Display::clear();
Display::update();
Display::checkForClose();
}
return 0;
}
我这样编译上面的文件:
g++ Display.cpp -Wall -O2 --std=c++14 -fexceptions -o test.o -lsfml-graphics -lsfml-audio -lsfml-network -lsfml-window -lsfml-system -lGL -lGLU -lGLEW -DGLEW_STATIC
(还没开始写 makefile)
这会生成一个名为 test
的二进制文件,但是当我 运行 它时,我收到以下警告:
Warning: The created OpenGL context does not fully meet the settings that were requested
Requested: version = 4.1 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = false ; debug = false ; sRGB = false
Created: version = 3.3 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = true ; debug = false ; sRGB = false
这确实创建了一个黑色 window(正如预期的那样),但我怀疑一旦我开始使用 SFML/Graphics.hpp
中的绘图函数,它就会出现段错误,因为当我尝试编译一个示例文件(它也打印了同样的错误)。
当我创建我的 sf::ContextSettings
时,我将它的 attributeFlags
设置为 sf::ContextSettings::Default
所以根据我的理解它应该创建一个兼容性上下文(因为 SFML 使用一些遗留代码这是必须的) .
P.S。如果重要的话,我在 Void Linux 上,并且我从 repos
安装了我在此处使用的所有内容的最新版本好的,似乎解决方案是使用 OpenGL 3.0 而不是支持兼容性配置文件的 3.3。但是现在我不能使用 GLSL 3.30,而且很乱,所以我会尝试 SDL2、GLFW 和 Raylib。