OpenGL Context and Window Stefano Markidis and Sergio Rivas-Gomez Four Key-points

• OpenGL does not include window creation and management. You will need separate libraries to manage the windows • As window creation and management, we suggest that you use either GLUT/FreeGLUT or GLFW • GLUT/FreeGLUT are easy to use and likely already installed on your systems, GLFW is the future but you need to install it • When using OpenGL, it is better to install and use a library for OpenGL function pointer management. We will install and use GLAD OpenGL Initialization … Without OpenGL!

• We initialize OpenGL, by creating an OpenGL context, which is essentially a state machine that stores all data related to the rendering of our application. • Problem: Creating a window and an OpenGL context is not part of the OpenGL specification. • The reason that this is done differently on different platforms and OpenGL can’t guarantee portability. • There are libraries, not included in OpenGL, out there that supports window creation and management. Typical Program Flow with Window Management Libraries

Every application making use of real-time graphics will have a program flow that comes down to this render loop Suggested Libraries to Create/Manage Windows

• GLUT: OpenGL Utility Toolkit (GLUT) library • Easiest to use • Already installed on lab machines and likely on your laptop. • Not designed for core-profile OpenGL • Free-GLUT newly developed version of GLUT, supported and new license type • GLFW: • Little more difficult to use than GLUT • Most modern and best integration with modern OpenGL. • It doesn’t support deprecated immediate-mode legacy OpenGL Create a Window with GLUT / Free GLUT

• We first initialize window with glutInit, glutInitWindowSize(), glutCreateWindow() • We tell GLUT what is the function responsible for the rendering and define it. • This function (renderScene()) will be called by GLUT every time the window needs to be drawn • We tell GLUT that we are ready to get in the event processing loop with glutMainLoop. Create and Manage a Window with GLFW

• To use GLFW instead of GLUT, we will need to install it first (follow instructions online at http://www.glfw.org/) • Going into details of GLFW window creation and management in the next lecture. Double Buffer • We you might have noticed the GLUT function glutSwapBuffers() and the GLFW glfwSwapBuffers(). But what do they do? • Windowing applications apply a for rendering: • The front buffer contains the final output image that is shown at the screen • All the rendering commands draw to the back buffer. • As soon as all the rendering commands are finished we swap the back buffer to the front buffer so the image is instantly displayed to the user Function Pointers Management for OpenGL It is up to the driver manufacturer to implement the specification to a driver that the specific graphics card supports: • the location of most of its functions is not known at compile-time and needs to be queried at run-time. • It is then our task to retrieve the location of the functions we need and store them in function pointers for later use. • Code for this looks complex and somehow cumbersome. • The GLAD library can do this for use • We will need to install it for using OpenGL. GLAD (http://glad.dav1d.de/)

• GLAD is an open source library for handling OpenGL function pointers • GLAD uses a web service where we can tell GLAD for which version of OpenGL. • set to ++, select an OpenGL version of at least 3.3 • Profile set to Core and that the Generate a loader option is ticked. • Click Generate to produce the resulting library files. • GLAD by now should have provided you a zip file containing: • Two include folders. Copy both include folders (glad and KHR) into your include(s) directory • Add the glad.c file to your project. To Summarize

• OpenGL doesn’t include window creation and management. You will need separate libraries to manage the windows • As window creation and management, we suggest that you use either GLUT/FreeGLUT or GLFW. • GLUT/FreeGLUT are easy to use and likely already installed on your systems, GLFW is the future but you need to install it. • When using OpenGL, it is better to install and use a library for function pointer management. We suggest that you use GLAD.