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;
}
Related
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;
}
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);
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;
}
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();
...
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.