Reputation: 79
After running several tests on the code, I have determined that both GLFW and GLEW are initialised successfully yet when I try and create a GLFWwindow*
object to be used with GLFW functions, the glfwCreateWindow()
function returns a nullptr
. Why is this and how do I fix it? Here is my code:
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLuint windowWidth = 500, windowHeight = 500;
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(windowWidth, windowHeight, "Learn OpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window!" << std::endl;
char myvar1; std::cin >> myvar1;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GL_TRUE) {
std::cout << "Failed to initialize GLEW" << std::endl;
char myvar2; std::cin >> myvar2;
return -1;
}
glViewport(0, 0, windowWidth, windowHeight);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
Upvotes: 2
Views: 6266
Reputation: 121
This is probably because you are specifying version 3.3 for the context creation and your opengl version is lower than 3.3.
OpenGL: GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR are not hard constraints, but creation will fail if the OpenGL version of the created context is less than the one requested.
This might happen if you are using a laptop that has 2 GPU's. They do that for power-consumption reasons, most applications will be run with the standard GPU and for games for example it will use the high performance one.
For example my laptop has a built-in Intel(R) HD Graphics 3000(3.1 opengl version) GPU and a NVIDIA geforce gt 630M(4.4 opengl version) GPU.
You can see if your laptop has this functionality if you right click on an application shortcut and have the option "run with graphics processor": - "High performance (NVIDIA) processor" - "Integrated graphics (default)"
The problem is that the editor(eclipse/ms visual studio, etc..)(in which you run your code) will use the default one and usually has a much lower version of opengl than your other GPU.
You can fix this by always running your editor program with your high performance GPU.
If you're not using a laptop or only have one GPU then try updating your drivers.
Upvotes: 5