Code after glfwTerminate() never runs - c++

I've been trying some modern OpenGL tutorials, and I came across what seems to be strange behaviour. Here is the code, reduced to its (almost) bare essentials.
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using std::cout;
using std::endl;
int main(){
std::cout << "Begin program" << std::endl;
glfwInit();
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);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", 0, 0); // Windowed
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
cout << "before glfwTerminate()" << endl;
glfwTerminate();
cout << "after glfwTerminate()" << endl; cout.flush();
std::cout << "End program" << std::endl;
return 0;
}
Looking at the GLFW docs, the call to glfwTerminate() should not exit the program, but nothing is output to the console after "before glfwTerminate()" and the program ends.
I'm on ArchLinux (freshly updated) and I tried with both xfce4 and KDE (minimal install for the latter, just to make sure it wasn't an xfce4 issue).
Would someone have a hint as to what is going on?

It is not finishing the program. Indeed it is processing the lines after glfwTerminate(), but somehow glfw is changing how stdout is bound to output stream.
It is a bug. You can confirm this by changing cout to cerr

Related

Wndow is not being created in Eclipse c++

I use the Eclipse IDE and the glfw, glew, and glm libraries. I want to create a window, but when I run the code nothing happens. Earlier, when I tried to launch it, it was written in the console "Info: Do not build anything", but now this phrase appears for a moment and disappears. And if I erase the contents of int main(), and write for example: cout << "hello world<<endl;, then the text will be output to the console, but for some reason, the code for creating the window does not work.
I tried reinstalling Eclipse, but nothing changes. I am new to programming in Eclipse, I had to spend about 10 hours fixing other errors that did not even allow me to run the code.
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
int WIDTH = 1280;
int HEIGHT = 720;
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Window", nullptr, nullptr);
if(window == nullptr) {
cerr << "Failed to create GLFW Window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK) {
cerr << "Failed to initialize GLEW" << endl;
return -1;
}
glViewport(0,0, WIDTH, HEIGHT);
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
glfwSwapBuffers(window);
}
return 0;
}

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

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

glfw does not create a window

when i create a simple glfw window and set these two flags:
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
the window will be NULL and i have no idea why.
The window will be created if those two things are not called.
Here is the code Below:
#include <iostream>
using namespace std;
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit()) {
cout << "glfw did not initialize!" << endl;
return -1;
}
glfwWindowHint(GL_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640, 300, "ARoo", NULL, NULL);
if (!window) {
cout << "window == null" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
im using visual studi comunity 2015, windows 8.1 x64(glfw and glew are x32)
my video drivers are up to date.
The problem could be related to the requested OpenGL profile.
You can have diagnostic messages about what's happening, you just need to set a callback, which can be done before glfwInit():
static void glfwError(int id, const char* description)
{
std::cout << description << std::endl;
}
int main()
{
glfwSetErrorCallback(&glfwError);
glfwInit();
...

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.