Join Pablo Colapinto for an in-depth discussion in this video Creating a context and a window, part of Learning OpenGL.
- Let's take a look at using GLFW to create a window. We're gonna include the same header files as before and initialize GLFW and create a GLFW window as we did in the last lesson. We'll also make it our current context. Here's some new code. On the lines 45 to 57 we define our main loop. This is the draw loop that's going to be rendered once per frame. The default is 60 frames per second.
We're going to execute this code while the window is open. GLFW window should close is a method that takes in a window and returns whether we want to close the window. If we don't want to close the window, we're gonna execute this code. It's a bit of double negative but it works. GL viewport is the rectangular region of the window in which we would like to draw. GL viewports takes as parameters the x and y-coordinate of the top left hand part of the rectangular region we'd like to draw.
In this case zero zero, referring to the left top hand corner of our window. And a width and height of the rectangular region in which we'd like to draw. We want to just use the full window that we make. We're going to feed it the full width and height of the window. GL clear color specifies the background color that we'd like to set. In this case we're specifying red. One for red, zero for green, zero for blue, and one for alpha. GL clear color actually executes the clearing of that color.
Calling GL color buffer bit. Eventually we are also going to want to add GL depth buffer bit, to clear the GL depth buffer. On line 53 is where we would actually put our drawing code. On lines 55 and 56 we swap the front and back buffers and listen for events. Once the window has received a signal that it should close we exit this loop. On line 60 we destroy the window.
On line 61 we terminate the GLFW session and exit our program. Let's compile this program and make our GLFW window. Here we have a GLFW window with a background that's red. It's 1024 pixels wide and 768 pixels tall. In this lesson we've seen how we can use GLFW to make a window context for our open GL calls. In subsequent lessons we will be creating these windows in every subsequent lesson.
- What is OpenGL?
- Setting up an OpenGL workflow
- Creating a window with GLFW
- Working with geometric primitives
- Rotating, translating, and scaling
- Understanding matrix transformations
- Creating mesh data structures
- Buffering mesh data
- Getting mouse and keyboard input
Skill Level Intermediate
Q: Why am I getting errors saying I don’t have GLEW installed when I try to create the Xcode projects with Cmake?
1) Download, Install, and Run Xcode
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install glew
./bootstrap && make && make install
brew install cmake
cd Exercise Filesmkdir xcodecd xcodecmake -G "Xcode" ..