I am running Mac OS X Sierra 10.12.6 (16G29). I am working on a macbook pro.
I have installed brew and the following packages:
brew install glfw3
brew install glew
brew install glm
Here is my c++ program:
#include <iostream>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <GL/gl.h>
int main(int argc, const char * argv[])
{
if (!glfwInit())
{
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640, 480, "Test 1", NULL, NULL);
if( window == NULL )
{
return -1;
}
glfwMakeContextCurrent(window);
// glGetString(GL_VERSION) is NULL at this point
return 0;
}
And here is the command line i am running to compile my program:
g++ program.cpp -I/opt/X11/include -L/opt/X11/lib -lglfw -lGL -lGLEW
There is a runtime problem because i get a NULL window.
I have compiled the same code on a Linux virtual machine and it works great...
This is answered in the GLFW docs: http://www.glfw.org/docs/latest/window_guide.html
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Also see: https://developer.apple.com/opengl/OpenGL-Capabilities-Tables.pdf
Related
I tried to create a window with the simplest code possible:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
int main(void) {
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac
GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window
if (window == NULL) {
fprintf(stderr, "Failed to create window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
return 0;
}
This is my Makefile:
game:
g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2
When I compile my code there is no error, but when I try to play my code I have this error:
Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above
Can someone help me? I don't know why my window doesn't want to be created...
GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR are not valid arguments to glfwWindowHint:
glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
^^^^^^^^^^^^^^^^^^ nope
glfwWindowHint(GLFW_VERSION_MINOR, 3);
^^^^^^^^^^^^^^^^^^ also nope
Use GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR instead.
As per the docs, emphasis mine:
GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.
Note: Do not confuse these hints with GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR, which provide the API version of the GLFW header.
I'm trying to use OpenGL with GLEW and GLFW. However, it appears that I cannot call glewInit() successfully and I get the following error message: GLEW error (1): Missing GL version. Similar questions at Stackoverflow haven't solved my problem.
When building my project, I am including the glew.c file and the directory where glew.h is.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
glfwInit();
glfwWindowHint(GLFW_SAMPLES, 0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Testing", nullptr, nullptr);
glfwMakeContextCurrent(window);
glewExperimental = true;
GLenum glewErr = glewInit();
if (glewErr != GLEW_OK)
{
std::cerr << "GLEW error (" << glewErr << "): " << glewGetErrorString(glewErr) << std::endl;
glfwTerminate();
return -1;
}
// Next, I paint some stuff using OpenGL ...
}
Everything looks ok in the code. Furthermore, for some reason, if I use GLAD instead of GLEW, my code works just fine.
System: Ubuntu 18.04.5 LTS.
OGL version: 3.3.0 NVIDIA 340.108
OGL vendor: NVIDIA Corporation
Renderer: GeForce 820M/PCIe/SSE2
GLSL version: 3.30 NVIDIA via Cg compiler
UPDATE:
Diving deep into glew.c code I found where the error pops up, though I can't still understand why. Here it is the piece of code
static GLenum GLEWAPIENTRY glewContextInit ()
{
PFNGLGETSTRINGPROC getString;
const GLubyte* s;
GLuint dot;
#ifdef _WIN32
getString = glGetString; // Not executed, I'm on Ubuntu
#else
getString = (PFNGLGETSTRINGPROC) glewGetProcAddress((const GLubyte*)"glGetString");
if (!getString)
return GLEW_ERROR_NO_GL_VERSION;
#endif
/* query opengl version */
s = getString(GL_VERSION); // <<< ERROR: s gets a null pointer
dot = _glewStrCLen(s, '.');
if (dot == 0)
return GLEW_ERROR_NO_GL_VERSION; // <<< Function escapes here
// ... more statement down here
}
There is something strange happening with gl3w's isSupported function. When I call isSupported(4, 0) it returns false, meaning OpenGL 4.0 isn't supported. However, when I call glGetString(GL_VERSION) it says OpenGL version 4.0.
Does this mean I can use OpenGL 4.0 functions?
I'm using gl3w in C++ and Visual Studio 2017
#include <GL/gl3w.h>
#include <GLFW/glfw3.h>
int main(int argc, char** argv){
if(!glfwInit()) {
FATAL_ERROR("Failed to initialise GLFW");
}
glfwSetErrorCallback(glfwErrorCallback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL", nullptr, nullptr);
//If i put glfwMakeContextCurrent here gl3wInit fails
//glfwMakeContextCurrent(window);
if (!window) {
glfwTerminate();
FATAL_ERROR("Window creation failed");
}
if(!gl3wInit()) {} // handle that
glfwMakeContextCurrent(window);
bool support = gl3wIsSupported(4, 0); // returns false
const char* version = glGetString(GL_VERSION); // return "4.0.0"
}
You have to make a GL context current before you call gl3wInit() or regular OpenGL functions otherwise they won't do anything useful.
In the OpenGL wiki you can read:
The GL3W library focuses on the core profile of OpenGL 3 and 4. It
only loads the core entrypoints for these OpenGL versions. It supports
Windows, Mac OS X, Linux, and FreeBSD.
Note: GL3W loads core OpenGL
only by default. All OpenGL extensions will be loaded if the --ext
flag is specified to gl3w_gen.py.
And this is confirmed looking inside the code:
int gl3wIsSupported(int major, int minor)
{
if (major < 3) // <<<<=========== SEE THIS
return 0;
if (version.major == major)
return version.minor >= minor;
return version.major >= major;
}
You are asking with glfwWindowHint for an old 2.0 version. Thus, gl3wIsSupported will return false and gl3wInit will return GL3W_ERROR_OPENGL_VERSION.
For glGetString(GL_VERSION) returning "4.0" means that, yes, you can use that 4.0 version. Ask for it with glfwWindowHint.
I fixed it by switching over to glad instead
if (!glfwInit()) {
FATAL_ERROR("Failed to initialise GLFW");
}
glfwSetErrorCallback(glfwErrorCallback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL", nullptr, nullptr);
if (!window) {
glfwTerminate();
FATAL_ERROR("Window creation failed");
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
glfwDestroyWindow(window);
glfwTerminate();
FATAL_ERROR("Failed to initialise OpenGL context");
}
PRINT("OpenGL Version: " << GLVersion.major << "." << GLVersion.minor);
I can't open an OpenGL window, due to following error message (I'm on Windows):
GLFW Error Code 65543: WGL: OpenGL profile requested but WGL_ARB_create_context_profile is unavailable.
It is likely that my problem is a driver problem. I tried to update them (using Intel Driver Update Utility), but it didn't do the trick (and my driver seemed to be already up to date). I use built-in Intel HD Graphics 3000. I also installed a OpenGL viewer, which tells me that my OpenGL version is 3.1).
Also, I tried this solution.
The whole C++ code is quite huge so I won't copy it all , but here is the interesting part:
if( !glfwInit() )
{
std::cerr<<"Failed to initialize GLFW\n"<<std::endl;
return -1;
}
glfwSetErrorCallback(glfwErrorCallback);
// Create the OpenGL window
glfwWindowHint(GLFW_DEPTH_BITS, 16);
glfwWindowHint(GLFW_SAMPLES, 4);
//Those stop GLFW from initializing successfully?
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open OpenGL fullscreen window
gGLFWWindow = glfwCreateWindow(gWidth,gHeight,"GLFW OpenGL Window",nullptr,nullptr);
if(!gGLFWWindow)
{
std::cerr<<"Failed to open GLFW window\n"<<std::endl;
glfwTerminate();
return -1;
}
// Disable VSync (we want to get as high FPS as possible!)
glfwMakeContextCurrent(gGLFWWindow);
glfwSwapInterval( 1 );
// Setting this is necessary for core profile (tested with MSVC 2013 x64, Windows 7)
glewExperimental = GL_TRUE;
// GLEW wraps all OpenGL functions and extensions
GLenum err = glewInit();
if(err != GLEW_OK)
{
std::cerr<<"Failed to initialize GLEW"<<std::endl;
std::cerr<<(char*)glewGetErrorString(err)<<std::endl;
glfwTerminate();
return -1;
}
glGetError(); //GLEW might cause an 'invalid enum' error, safely ignore it?
// Print OpenGL context information to console
ogl::printContextInformation();
// Perform our initialization (OpenGL states, shader, camera, geometry)
if(!init())
return -1;
It fails at this line :
gGLFWWindow = glfwCreateWindow(gWidth,gHeight,"GLFW OpenGL Window",nullptr,nullptr);
Does anyone have an idea of what I could do to solve this issue?
The answer is : I was requesting a Core 3.3 context whereas my version was OpenGL 3.1.
Deleting/commenting those lines will do the trick :
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
I to had the same problem because my last machine has an openGl of version 3.3 and now because the one am using now has but an openGl 3.1 made me almost got frustrated
But De-comenting the "window hints", it solves the problem
I have the following code:
void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
glfwSetErrorCallback(error_callback);
window = glfwCreateWindow( 1024, 768, "Tutorial 16 - Shadows", NULL, NULL);
if( window == NULL ){
//fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
GLenum err;
glewExperimental = GL_TRUE; // Needed for core profile
if ((err = glewInit()) != GLEW_OK) {
std::cout << glewGetErrorString(err) << std::endl;
return -1;
}
...
}
The problem is that I'm receiving the following message: https://github.com/glfw/glfw/blob/master/src/nsgl_context.m#L101
And, indeed, GLFW won't give me a OpenGL 3+ context without setting the forward-compatibility flag (in Mac OS X).
Why is that? Is there any way to get a OpenGL 3+ context in Mac OS X 10.9 without forward-compatibility? Is it a limitation of OpenGL implementation for OS X or a problem of GLFW?
This is actually the correct behavior, as defined in the OpenGL Programming Guide for Mac.
Mac OS X simply does not support the compatibility profile for OpenGL 3.x/4.x, so you must request a core (or forward-compatible) context. This implies that you will not be able to use any deprecated functions when programming against OpenGL 3.x/4.x on a Mac.
It might be worth to make a feature request in the GLFW issue tracker to set the core profile flag implicitly when requesting a 3.x/4.x context on Mac OS X.