GLFW_OPENGL_CORE_PROFILE causes segmentation fault - c++

I'm following the OpenGL tutorial at http://www.learnopengl.com/#!Getting-started/Hello-Window
I have the following code written as taught in the tutorial, however setting the profile causes an immediate segmentation fault. The program gives the expected output without that line.
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //SEGFAULT
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
glViewport(0, 0, 800, 600);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
If it helps, the output of glxinfo | grep OpenGL is below:
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Sandybridge Mobile
OpenGL core profile version string: 3.1 (Core Profile) Mesa 10.1.3
OpenGL core profile shading language version string: 1.40
OpenGL core profile context flags: (none)
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 10.1.3
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:

It turns out that core and compatibility profile modes were added in OpenGL 3.2. Since my system has 3.0, setting that parameter caused a crash.
So for versions lower than 3.2, you don't need to set compatibility mode or core profile.

Related

Can't run OpenGL on WSL2

I am trying to run an OpenGL code on WSL2 but am getting the following error on trying to run the executable:
GLFW error 65543: GLX: Failed to create context: GLXBadFBConfig
Unable to create GLFW window
This is the code I am using to create a window:
....
GLFWwindow* initialize() {
int glfwInitRes = glfwInit();
if (!glfwInitRes) {
fprintf(stderr, "Unable to initialize GLFW\n");
return nullptr;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(1280, 720, "InitGL", nullptr, nullptr);
if (!window) {
fprintf(stderr, "Unable to create GLFW window\n");
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
....
return window;
}
....
GLFWwindow* window = initialize();
I am using VcxSrv as my X server.
Following is from the output for glxinfo
direct rendering: No (LIBGL_ALWAYS_INDIRECT set)
server glx vendor string: SGI
server glx version string: 1.4
client glx vendor string: Mesa Project and SGI
client glx version string: 1.4
GLX version: 1.4
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce 940MX/PCIe/SSE2
OpenGL version string: 1.4 (4.6.0 NVIDIA 457.30)
The following fix worked for me.
As #dratenik mentioned above, the problem persists because of direct rendering: No. To solve this, do the following:
In the bashrc/zshrc file, add the following:
export LIBGL_ALWAYS_INDIRECT=0
Or you could just remove export LIBGL_ALWAYS_INDIRECT=1 line from your bashrc/zshrc file if you have added it.
Then, start a new instance of VcxSrv with and unselect the Native opengl box on the Extra Settings page, and select the Disable access control box.
After doing this, direct rendering should be turned on, and you should get the following on running glxinfo:
direct rendering: Yes
server glx vendor string: SGI
server glx version string: 1.4
...

OpenGL compiling with incorrect version

I am trying to compile my application with OpenGL 3.3. I've searched my graphics card online and it supports up to 4.4.
Here is the return of glxinfo | grep OpenGL
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 520 (Skylake GT2)
OpenGL core profile version string: 4.5 (Core Profile) Mesa 17.2.3
OpenGL core profile shading language version string: 4.50
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 17.2.3
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.2 Mesa 17.2.3
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
OpenGL ES profile extensions:
I am telling GLFW to use major version 3 minor version 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
and do use core profile:
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
I built glad pointing to version 3.3 and core profile:
python -m glad --api "gl=3.3" --generator c --out-path ./output --profile core --spec gl
But when I call
glGetString(GL_VERSION)
I get back
3.0 Mesa 17.2.3
I cannot for the life of me figure out what i'm missing.
Section running this code
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_FLOATING, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (!glfwInit()) {
std::cout << "glfw not inited" << std::endl;
}
glfwSetErrorCallback(error_callback);
m_game_window = glfwCreateWindow(800, 600, "window", NULL, NULL);
if (!m_game_window) {
std::cout << "window creation failed" << std::endl;
glfwTerminate();
//crash
}
glfwMakeContextCurrent(m_game_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glViewport(0, 0, 800, 600);
char *version = (char*)glGetString(GL_VERSION);
std::cout << version;
}
You must not call any GLFW functions before glfwInit(). In your case, the window hints will be completely reset by the glfwInit().

How do I utilize a different OpenGL profile?

I would like to replicate tutorials (sprite rendering), that use OpenGL version >= 3.3.
Geometry shaders for example were introduced in 3.2, and I get this error:
error: ‘GL_GEOMETRY_SHADER’ was not declared in this scope
I updated my mesa driver to the latest; I don't really understand though, how I can choose a newer version of OpenGL when compiling/linking in c++:
➜ glxinfo | grep -i "version"
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
Version: 17.1.4
Max core profile version: 4.5
Max compat profile version: 3.0
Max GLES1 profile version: 1.1
Max GLES[23] profile version: 3.1
OpenGL core profile version string: 4.5 (Core Profile) Mesa 17.1.4
OpenGL core profile shading language version string: 4.50
OpenGL version string: 3.0 Mesa 17.1.4
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.1 Mesa 17.1.4
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10
I cannot really interpret this, but it says there is some kind of core profile using version 4.5. How do I utilize this profile?
I initialize the GL context in my code like this:
if(!glfwInit()) e_glfw_init();
m_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE.c_str(), nullptr, nullptr);
if(m_window == nullptr) e_window_context();
glfwMakeContextCurrent(m_window);
glewExperimental = true;
if(glewInit() != GLEW_OK) e_glew_init();
According to the glfw documentation, you can specify the OpenGL version and profile by using the glfwWindowHint function:
if(!glfwInit()) e_glfw_init();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE.c_str(),
nullptr, nullptr);
I had solved this issue on Linux by including these three headers when while playing with GLFW and shaders:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>

GLFW cannot create a window: "GLX: Failed to create context: GLXBadFBConfig"

I'm trying to create a glfw window in my Debian Stretch system.
The code for initialize glfw:
// Initialize GLFW
void initGLFW()
{
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwSetErrorCallback(errorCallback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "GLSL4.3 + GLM + VBO + VAO", NULL, NULL);
if (!window)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
//system("pause");
exit(EXIT_FAILURE);
}
}
When I run the executable I get the messages above. Why?
GLX: Failed to create context: GLXBadFBConfig
Failed to open GLFW window.
Running with LIBGL_DEBUG=verbose I get this
libGL: Can't open configuration file /home/rafael/.drirc: No such file or directory.
libGL: pci id for fd 5: 8086:0a16, driver i965
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/tls/i965_dri.so
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/i965_dri.so
libGL: Can't open configuration file /home/rafael/.drirc: No such file or directory.
libGL: Using DRI3 for screen 0
Some useful infos:
$ lspci | grep VGA
00:02.0 VGA compatible controller: Intel Corporation Haswell-ULT Integrated Graphics Controller (rev 0b)
$ glxinfo | grep version
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
Max core profile version: 3.3
Max compat profile version: 3.0
Max GLES1 profile version: 1.1
Max GLES[23] profile version: 3.0
OpenGL core profile version string: 3.3 (Core Profile) Mesa 11.2.2
OpenGL core profile shading language version string: 3.30
OpenGL version string: 3.0 Mesa 11.2.2
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 11.2.2
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
The initGLFW function is the first function called from main.
You are trying to create an OpenGL 4.0 Core profile context:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Your driver/OpenGL implementation only supports up to 3.3:
OpenGL core profile version string: 3.3 (Core Profile) Mesa 11.2.2
Max core profile version: 3.3
Mesa 11.2.2 could support OpenGL 4.1 but only on certain drivers (from the release notes of 11.0.0):
OpenGL 4.1 on radeonsi, nvc0
Mesa 12.0.0 seems to support OpenGL 4.3 on i965:
OpenGL 4.3 on nvc0, radeonsi, i965 (Gen8+)
The fix would be to update your graphics card, Mesa3D or to create a 3.3 context instead:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

Using OpenGL core profile with Mesa 10 and GLFW3

I am running Arch Linux with Mesa 10 on an HP laptop with an Intel HD 3000 GPU. (There is also an ATI card but I shut it off at boot.) I am trying to run OpenGL code with the core profile. OpenGL 3.1 and GLSL 1.4 should be supported according to glxinfo:
-> % glxinfo | grep version
OpenGL core profile version string: 3.1 (Core Profile) Mesa 10.0.1
OpenGL core profile shading language version string: 1.40
OpenGL version string: 3.0 Mesa 10.0.1
OpenGL shading language version string: 1.3
However, when I compile a GLFW program, try to force the core profile, and ask for the OpenGL version like so:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
// Use OpenGL 3.1 core profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_CONTEXT_REVISION, 0);
// Create opengl context
int window_width = 1024;
int window_height = 768;
GLFWwindow* window = initialize_glfw(window_width, window_height);
if (!window)
{
glfwTerminate();
std::exit(EXIT_FAILURE);
}
// Display OpenGL version
int major, minor, rev, client, forward, profile;
glfwGetVersion(&major, &minor, &rev);
std::cout << "OpenGL - " << major << "." << minor << "." << rev << std::endl;
}
as well as compile shaders with GLSL #version 140, this is the printed output:
-> % ./main
OpenGL - 3.0.3
Shader compilation failed with this message:
0:1(10): error: GLSL 1.40 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES
So, it seems like OpenGL 3.1 and GLSL 1.4 should be supported, but they are not being used in my GLFW program. Can anyone tell me what might be wrong?
After re-reading the documentation, there seem to have been two problems. One, as elmindreda pointed out, is that calling init after making window hints sets the window hints back to their defaults, so init must be called first.
Second, I am using OpenGL 3.1, and the GLFW docs say "If requesting an OpenGL version below 3.2, GLFW_OPENGL_ANY_PROFILE must be used." I was trying to use GLFW_OPENGL_CORE_PROFILE.
You need to initialize GLFW before calling other functions. You also need to make the OpenGL context current before calling functions on it.