We are going to consider the basic setup and the buffers. What is a buffer? As well as basic matrix operations that you already saw for transformations. So, the first question is what is a buffer? In OpenGL see, these are some common types of buffer. First, the color buffer. And you can have a front buffer, a back buffer, left and right. Left and right is for stereo. We don't need to consider it too much in this course. Front and back is interesting because you only see the front buffer, but there is a technique known as double buffering that draws into the back buffer and then swaps them. There is also a depth buffer to hold the z values, an accumulation buffer to add up contributions from many images, and things like a stencil buffer. Any drawing that you do writes to some buffer, and that's why these are very important concepts in OpenGL. Most commonly you would write to the front buffer or the back buffer if you were doing double buffering. And you would also modify the depth, because you will use the technique known as Z-buffering to only draw the objects that are directly visible, and eliminate objects that lie behind them. OpenGL does not include any specific aspects of window system interactions. And this is done mostly for portability. But we can use alternatives, such as GLUT, which you will be using. And there are a number of other tool kits (Motif, GLX, Tcl/Tk). And all of these implement callbacks, that is functions the user registers to handle events such as a mouse moving or a key press. And if you have looked at the source code for homework 0, you see the way these things are handled. So this is the basic setup code. And again we're showing a lot of code on these slides. You are welcome to use any of it if you think it's helpful in your assignments. So here is the main function. And this code is really just from the homework 0 program. And I'm going to build that program out slowly. So here's my main function. I initialized GLUT. And then I come to this quantity here. So glutInitDisplayMode. And what I'm going to do is request the single buffer, so I can have also double buffering, but this is single buffer, and I'm requesting the color buffer, RGB, red green and blue. I set the window size, window position, create the window and then this is something that happens just in order to setup my program, we won't go into it in detail, it's required to get all the functions to work correctly, glewInit, and finally I call my own initialization. In the next screen, I'm going to show you that I register callbacks for various keyboard and mouse actions. So, this was the earlier part of the main function. And now see, the display function is some function known as display. reshape, if I try to reshape my window, if I try to press the keyboard, I press the mouse, and also if I drag the mouse. After this you go to glutMainLoop, and all this does is, it waits for events, such as moving the mouse, pressing the keyboard, and then it calls the user function. The next part is to discuss matrix modes and we will talk about viewing in OpenGL. Of course you already have a reasonable idea of how this works in previous two lectures. We did consider gluLookAt and gluPerspective, and we did derive the matrices for them, in fact you will be implementing this in homework 1. Viewing in OpenGL we've already discussed consists of two parts. The first is to position the object and also to position the camera appropriately. This is known as the ModelView transformation matrix, and so Model and View. So View is for the camera, Model is for the model transformation. The final step is to project the 3D world onto the image, and that is known as the projection transformation matrix. We'll talk both about modern OpenGL, and we'll talk a little bit about old style OpenGL. In some cases, the concepts are actually easier to understand in old style OpenGL. But modern OpenGL responds to the notion that, today, you have programmable shaders. And there are therefore better ways of doing certain things. Now, I should mention that the old style OpenGL, for the most part, is still supported. There's a lot of legacy code out there. And in terms of doing your homeworks, you're welcome to use either, within the limits described in the homework assignment. So in older OpenGL there are two matrix stacks, the GL_MODELVIEW_MATRIX and the GL_PROJECTION_MATRIX. And what you do is, push and pop matrices onto these stacks. In new style OpenGL, you use the C++ standard template libraries to make stacks as needed, and in fact for homework 2 we do require that you use those. So in order to actually learn the material and homework 2, you can't just use the transformation stacks and old style OpenGL. This is in C++. For those of you who are not familiar, stack is a primitive type in C++ in the template library and this is a template, so I'm saying it's a stack of mat4. mat4 is defined in GLM, which is the auxiliary library provided, which is 4x4 matrices, and I defined modelview. So that's just a stack of modelview. And I can say modelview push the matrix on to the stack, in this case, the identity, and can pop from it, and so on. Now of course, old style OpenGL supported a lot of things that are removed in new OpenGL. So what do you do if you want to use them? The modern way to handle this is to use an auxiliary library, the GLmath library, which supports many of the deprecated commands in a modern way. And it includes the mat4 type, for example. OpenGL's camera is always at the origin, and it points in the -z direction. We've already seen this when we considered transformations. Therefore, transformations conceptually in OpenGL move objects relative to the camera. Of course, they are exactly equivalent, whether you think of the camera itself moving or whether you think of objects moving. We have in fact considered both viewpoints. So we have considered the viewpoint of the camera at a given location and how do you transform objects into that reference frame. But the 4x4 matrix we derived is exactly what's needed for the OpenGL convention or fixing the camera and moving objects. In old style OpenGL, matrices are column-major. This is a source of great confusion because the standard mathematical way of writing them is row-major. And furthermore, they right multiply on the top of the stack. So if you say I put another matrix on the stack, then it'll be the first transform applied. So the last transform in the code is the first transform actually applied. And there is some confusion in GLM because it stores things in row major order but then it seeks to support OpenGL, and so you should really be sure you are doing the right thing when you use OpenGL and GLM. So here is our basic initialization code for viewing, and I will go over each of the individual steps. So we've included GLUT, we've included the standard library. And these are just initializations of variables. So here is the clear color, so I just wanted it to be black. And these colors are red, green, blue and then there is an alpha channel and that's not relevant at this stage but it's used for compositing and for transparency. So, now I'm going to the matrix mode, GL_PROJECTION, and I'm setting it to the identity. Then I go to the matrix mode ModelView, load the identity, and then give my gluLookAt command. Remember that gluLookAt is first the eye. So 0, -2 and +2. So remember that eyeloc is equal to 2. Then what am I looking at? In this case, the origin. And then the up direction. So I set it to (0, 1, 1). Note that it's not normalized, but OpenGL will automatically normalize it and create a coordinate frame, as we discussed in gluLookAt. We will continue discussing the initialization later, and we'll discuss the geometry and shader setup that you will need.