GLFW error #65544 "Failed to initialize GLFW" - c++

here's my code:
#include <iostream>
#include <GLFW/glfw3.h>
int main(void){
GLFWwindow* window;
if(!glfwInit()){
std::cout << "Failed to initialize GLFW!" << std::endl;
return -1;
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if(!window){
glfwTerminate();
std:: cout << "Failed to initialize Window!" << std::endl;
return -1;
}
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I successfully linked it and compile, but I'm having a runtime error
it says that glfw failed to initialize.I tried to use glfw in C but this error shows
Wayland: Failed to connect to display
The GLFW library is not initialized
main: ./src/posix_thread.c:64: _glfwPlatformGetTls: Assertion `tls->posix.allocated == GLFW_TRUE' failed.
Aborted (core dumped)
. I'm using popOS 22.04 LTS, I'm new to c++ and glfw pls help me fix this problem

Related

ImGui error: Assertion failed: (bd != __null && "Did you call ImGui_ImplOpenGL3_Init()?")

I've been working with SDL2 for a long time and now I wanted to switch to GLFW with Dear ImGui. So I downloaded GLFW and integrated it into the project. The simple GLFW window works without problems. Then I integrated ImGui and I get these errors all the time:
Failed to initialize OpenGL loader!
Assertion failed: (bd != __null && "Did you call ImGui_ImplOpenGL3_Init()?"), function ImGui_ImplOpenGL3_NewFrame, file imgui_impl_opengl3.cpp, line 337.
although I'm on line 33:
ImGui_ImplOpenGL3_Init();
I have never worked with ImGui before and would be really happy if you could help me
My code:
int main() {
//init glfw
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW" << std::endl;
return -1;
}
//create window
GLFWwindow* window = glfwCreateWindow(640, 480, "Text Editor", NULL, NULL);
if (!window) {
std::cout << "Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//init glew
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui::StyleColorsDark();
ImGui_ImplOpenGL3_Init();
//main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
And my includes:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
Thank you for your answer :)
Your code runs fine on my machine, it didn't crash at the ImGui_ImplOpenGL3_NewFrame function, but at ImGui::Render(); which might be the problem. You need to render in the order below:
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); // <-- Added
// do stuffs here
ImGui::Render();
ImGui::EndFrame(); // <-- Added
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}

Getting GLFW together with glad to work on windows [duplicate]

I'm having a problem where the following lines of code always print "Failed to initialize glad" and then exits the program:
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
I have been using https://learnopengl.com/ as a guide and have been following the steps in the getting started section. I am writing this using Visual Studio, I have moved the glad.c source file into the build to get this working and added the header files to the same location where I specified the glfw header would be, but I haven't been able to find anyone with a problem similar to mine.
Commenting out return -1; line results in an access violation exception, so it is definitely here that the program is having trouble.
Here is the entire program in case there is something else I am missing:
#include "stdafx.h"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
using namespace std;
void init_glfw();
void framebuffer_size_callback(GLFWwindow*, int, int);
int main(int argc, char **argv)
{
init_glfw();
GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void init_glfw()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
You never made your GL context current via glfwMakeContextCurrent(). Unlike other GL windowing frameworks GLFW doesn't leave the GL context current when glfwCreateWindow() succeeds.
Call glfwMakeContextCurrent() after glfwCreateWindow() succeeds:
GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent( window );
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}

Failed glewInit: Missing GL version

I began to study OpenGL on the course.Download glew-2.1.0(64) and glfw-3.2.1(64). Like everything connected and IDE not swear but initialization produces the following output in the command line:
Error initialization GLEW: Missing GL version
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
const char* APP_TITLE = "Introduction in modern openGL";
const int gWindowWidth = 800;
const int gWindowHeight = 600;
int main()
{
if (!glfwInit())
{
std::cerr << "Failed in initialization GLFW" << std::endl;
return -1;
}
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* pWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, APP_TITLE, NULL, NULL);
if (pWindow)
{
std::cerr << "Failed in create Windows" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(pWindow);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
std::cerr << "Error initialization GLEW: " << glewGetErrorString(err) << std::endl;
glfwTerminate();
return -1;
}
while (!glfwWindowShouldClose(pWindow))
{
glfwPollEvents();
glfwSwapBuffers(pWindow);
}
glfwTerminate();
return 0;
}
What is the problem? I searched on the Internet, but there are solutions to problems of the type:
add glfwMakeContextCurrent(game_window); before glewInit()
Very stupid mistake
I wrote
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
I should have
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

Glad failing to initialize

I'm having a problem where the following lines of code always print "Failed to initialize glad" and then exits the program:
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
I have been using https://learnopengl.com/ as a guide and have been following the steps in the getting started section. I am writing this using Visual Studio, I have moved the glad.c source file into the build to get this working and added the header files to the same location where I specified the glfw header would be, but I haven't been able to find anyone with a problem similar to mine.
Commenting out return -1; line results in an access violation exception, so it is definitely here that the program is having trouble.
Here is the entire program in case there is something else I am missing:
#include "stdafx.h"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
using namespace std;
void init_glfw();
void framebuffer_size_callback(GLFWwindow*, int, int);
int main(int argc, char **argv)
{
init_glfw();
GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void init_glfw()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
You never made your GL context current via glfwMakeContextCurrent(). Unlike other GL windowing frameworks GLFW doesn't leave the GL context current when glfwCreateWindow() succeeds.
Call glfwMakeContextCurrent() after glfwCreateWindow() succeeds:
GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent( window );
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}

Creating OpenGL 4.3 window fails with GLFW3

I set up a minimal application to open a blank window with GLFW3:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
void glfwErrorCallback(int error, const char *description)
{
std::cerr << "GLFW error " << error << ": " << description << std::endl;
}
int main(int argc, char **argv)
{
GLFWwindow* window;
glfwSetErrorCallback(glfwErrorCallback);
if(!glfwInit())
{
std::cerr << "Failed to initialize GLFW...\n";
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1024, 768, "GLFW window", NULL, NULL);
if(!window)
{
std::cerr << "Failed to open GLFW window...\n";
glfwTerminate();
return -1;
}
glewExperimental = GL_TRUE;
if (glewInit())
{
std::cerr << "Failed to initialize GLEW...\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
It results in the following error:
GLFW error 65540: Context profiles only exist for OpenGL version 3.2 and above
Failed to open GLFW window...
The application is run on Linux with Bumblebee's optirun. The code works when using freeglut instead of GLFW. What is wrong with the code that results in the error?
This is pretty simple:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // Major = 4
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Major was 4, now it is 3.
// Minor = ??? [Something < 2]
You need to use glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3); instead for the second hint.