Join Pablo Colapinto for an in-depth discussion in this video Cleaning up the code: Making an app struct, part of Learning OpenGL.
- In the lessons that follow, we include GLFW app dot hpp. This encapsulates much of the context creation and window management that we've been discussing. Helps us to reduce clutter and clean up our code. Let's take a look under the hood at what's going on. If we drag in our include folder in our example files folder, into our Xcode project. We'll talk about these other hpp files later on, let's take a look at GLFW app dot hpp.
GLFW app dot hpp itself includes GLFW window dot hpp, we'll look at that in a second. The app includes a window and a way to access the window and when we create the app it calls GLFW in it for us, creates a window passing itself as an argument, we get to that later on in the course. Initializes glue. And enables a few defaults. The two defaults to notice here are defaults that you're gonna wanna use when you're learning open GL.
The first is GL enable GL depth test. This will help us make sure that things that are behind other things, in space, will be excluded by them, so we won't see them. And we call GL depth funk GL less. The second parameter we want to set is GL enable GL blend. And we call the GL blend funk GL source alpha GL one minus source alpha. This will help us to create transparencies. If we want to draw something that is see-through, or partially see-through, and have something behind it, we need to enable blend testing.
Finally, I set our GL line width to three, just so we can see it clearly. It's important to make a note of these defaults, because if you're having trouble in your own code, you wanna make sure that these are set first. The app class is a method called start. Which starts our name loop. While the window shouldn't close, while our window is open, execute our draw loop. We'll look at what M window set view port does in a second, we clear the color to grey, clear our color buffer bits and our depth buffer bits, each frame, and then we call on draw before swapping our buffers.
When the app, closes, we call GLFW terminate. On draw is a virtual function which we'll overload in our subsequent lessons. If we take a look at an example that uses GLWF app, we notice that we create a subclass of app called "my app," instantiating it and in our overloaded on draw loop, we can put our code. Towards the end of the course, we'll investigate on mouse move, on mouse down, and on key down. For now, know that this is functionality that we've added to our app in order to handle events sent to us by GLFW.
If we run this program, we'll see our current mouse coordinates printed out in the console. This is in screen coordinates so zero, zero is at the top left and the full width and full height is down at the bottom right hand corner. This is a six 40 by four 80 window. If we click, it says that the mouse has been pressed. When we release, the mouse has been released. If we type on the keyboard, every letter that we type is printed to the console. It's useful to know that the app class contains these methods that you can access whenever you want.
If you wanna experiment with the lessons. The last thing to show you is the GLFW window class. This is included by the GLFW app class. You can safely ignore the interface glue for now. This is the glue that helps us find the on key down and on mouse move, ect. Just notice that, when we create a window, we have the same code that we've been discussing. We create a window, test to see that it was successful, set the context to current, and set swap interval to one.
Set view port test to find out the current frame buffer size of the window and sets the view port to that. That's our resizing function. The rest of the methods are just simple wrappers around GLFW functions. In this lesson, we introduced how our app class encapsulates much of GLFW's functionality and will make for clearer code writing later on.
- 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" ..