Skip to content

2.2. OpenGL

Ioannis Tsakpinis edited this page Dec 27, 2014 · 5 revisions

Display Creation using GLFW

The first command which should be called is:

GLFWErrorCallback errorCallback;
glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));

This line will make all GLFW errors print to the console Then, glfwInit() has to be called to initialize GLFW.

So far, the code would look something like this:

GLFWErrorCallback errorCallback;
glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
if ( glfwInit() != GL_TRUE ) {
    throw new IllegalStateException("Unable to initialize GLFW");
}

The next job to tackle, now that GLFW has been set up, is to create the display:

String title = "MyTitle";  // The title of the window, WARNING, if title is
                          // null, the code will segfault at glfwCreateWindow()
boolean resizable = true; // Whether or not the window is resizable

int m_width = 1024; //width of the window
int m_height = 768; //height of the window

glfwDefaultWindowHints(); // Loads GLFW's default window settings
glfwWindowHint(GLFW_VISIBLE, GL_TRUE); // Sets window to be visible
glfwWindowHint(GLFW_RESIZABLE, resizable ? GL_TRUE : GL_FALSE); // Sets whether the window is resizable

long id = glfwCreateWindow(m_width, m_height, title, NULL, NULL); //Does the actual window creation
if ( id == NULL ) throw new RuntimeException("Failed to create window");

glfwMakeContextCurrent(id); //glfwSwapInterval needs a context on the calling thread, otherwise will cause NO_CURRENT_CONTEXT error
GLContext.createFromCurrent();//Will let lwjgl know we want to use this context as the
                              //context to draw with

glfwSwapInterval(1); // How many draws to swap the buffer
glfwShowWindow(id); // Shows the window

Setting the mouse to grabbed

After the display has been created, this command can be used to set the mouse to grabbed

glfwSetInputMode(id, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

Hiding the mouse

If you want the mouse to disappear when over the window, but to still be movable, use this command (for example, you might want to draw your own mouse)

glfwSetInputMode(id, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);

Calling opengl functions

First, before any functions can be called, an OpenGL context needs to be created using

glfwMakeContextCurrent(id);
GLContext.createFromCurrent();

Then, you are good to go and can call OpenGL functions!

The static classes GL11, GL12, GL13, GL20, GL21, GL22,... can be used the access functions of a certain GL Version where GL11 would correspond to OpenGL 1.1, and GL20 would correspond to OpenGL 2.0

Note, only functions which were added in a specific OpenGL version are in a GLXX class (eg. functions in OpenGL 1.2 which were in OpenGL 1.1 are in GL11, not GL12)