Using Modern OpenGL in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is a Cherno and welcome back to my opengl series so last time we set up an OpenGL windowing and context library called gfw which basically just gave us the ability to credit cross-platform window and initialize a graphics context for OpenGL where we could do all about drawing if you haven't seen that video yet make sure you check it out because it's gonna be important for this video here and we even got a triangle onto the screen and everything looks like it was working properly and well it was however what you might have noticed in the previous video is that we actually linked something called OpenGL 32 dot lib and the header files that we actually ended up using for sophistic specifically for concealed functions we're basically the ones that either JFW provided which in turn adjust the windows ones now windows has a graphics API already it's called DirectX or direct3d to be a little bit more specific that's what Windows uses that's what you should kind of be using according to Microsoft that's what you should be using to do graphics on Windows and it makes sense and me personally for a for a real kind of game for a real project for a game engine I would be using direct3d to render on Windows because that's what's native to the platform that's what you kind of should be using OpenGL though remember is not kind of up to Windows to decide if that's a thing or not it's up to our GPU manufacturers is up to the GPU drivers and and and the GPU drivers from Nvidia and AMD and Intel they all support OpenGL as a rendering API so what we need to do now is not really rely on Windows and they're kind of OpenGL header files which only go up to opengl 1.1 which is kind of the last version of OpenGL which is also kind of the first version 1.0 was kind of a bit weird we don't talk about that 1.1 Orajel 1.1 was kind of the last the first real version of OpenGL and that that's as far as we discuss with it so since we're on Windows we need a way to actually get all of the modern OpenGL functions not even I'm not even talking about modern ones I'm just talking about anything newer then like 1997 right OpenGL one by one anything anything newer than that if we want to actually call those functions you now say plus plus code we need to get them from somewhere and as I mentioned in the welcome to weapon GL video which was the first get up there OpenGL functions it's not something you download really if stuff that is actually in your graphics drivers the OpenGL functions are implemented in your GPU drivers so what we need to do now is in order to use any kind of functionality that is newer that opengl 1.1 we need to actually get into those drivers pull out the functions and call them now what I say pull out the functions I don't literally mean pull out the functions what we actually need to do is basically just get the function declarations and then link against the functions as well so we need to access the driver DLL files and just retrieve function pointers to the functions inside those those libraries that's what we need to do now OpenGL passed 1.1 so basically modern OpenGL and all that there are a lot of functions there are a lot of functions so whilst we could theoretically do this manually there are a few problems with that first of all it would not be cross-platform so to basically access the drivers and pull out functions from that we need to we need to use some win32 api calls or foreign windows right load library and load function points all that that's not great because it's gonna be windows only and then the second problem is if there are like I don't know over a thousand functions or something like that then we need to go through all them manually and write code for that that's gonna just be a terrible plan it's just not gonna be fun at all so what we're gonna do now is they're actually gonna use another library now there are a few libraries that actually do this for you but I just want you guys to understand that what this library actually does is actually really straightforward and you can look at the source code for this library for yourself and see what it does if you want if you like but basically all it does is it provides the OpenGL API specification kind of function declarations and symbol declarations and constants and all that stuff for you in a header file and then the kind of the behind-the-scenes file the CPP file on this guy's I think it's about safe file the actual implementation of the library goes into your EDI it identifies what graphics drivers you're using finds the appropriate DLL file and then loads all the function pointers that's what it does all it does is very very boring it's not magical at all don't think that these libraries implement the functions or anything they don't they just access the functions that are already on your computer in binary form and the library that we're going to use that does that for us is something called glue or GL e W or the OpenGL extension Wrangler there's also another library called glad you can use that if you like it's a bit more specific with extensions to OpenGL and all that stuff and you can configure it a little bit better I don't really care about that we're just trying to write OpenGL code and glue does everything I need is a little bit simpler so we're gonna use glue so the first thing we're going to do is go to glue tool sourceforge.net again link will be in the description this is the glue website again we have the opportunity to download the source code if we want to and that is what I would absolutely be doing if this was a serious project or a game engine however because this is just about learning OpenGL and this is something that enables us to write OpenGL code I don't care about that I'm just going to get the binaries for Windows so I'm going to click on this link here and then once it's downloaded I'm going to open the zip archive and there'll be a folder there called Glu 2.1 in this case i'm just going to copy that i'm gonna go back into our actual directory of our opengl solution and just like last time where we put yellow w inside the dependencies folder i'm going to paste glue into this dependency folder as well okay so here we go and inside that glue folder we have everything we need now one thing I'm actually gonna do is rename this to just glue okay not necessary and does kind of strip the version it's obviously before it was obvious to tell if this was glue to point 1.0 now you don't really know all there's no closures I'm just doing this for simplification kind of purposes just so that it's a bit easier for us but now we have glue and gfw as well so inside this we have a few different folders the ones that we kind of care about for linking as well as include and live this is exactly the same as linking GLW so that was an episode to check that out if you haven't already I also just made a video about how linking libraries in c++ actually works and how you can use libraries in c++ that's nice up there check that out for sure that'll help you understand what's going on here because i'm gonna try and keep this brief this time so dock also includes all the kind of documentation you want you can see there's an HTML format so you can just open up index dot HTML that's the main webpage let's open it in Chrome and we have something that looks exactly the same at the web page but you can see that this one is a local so this is where reading documentation is kind of important if we go to usage first of all it tells us how to actually initialize this now I'm going to point out a few issues that you might encounter with glue if you don't read the documentation there's two that I can think of right now that might not be apparent the first one is actually literally the first thing that this documentation says and that is first you need to create a valid OpenGL rendering context and called blue in it to initialize the extension entry points so you can't use OpenGL functions from glue until you call blue in it that's the first one and second of all as it says you need to create a valid OpenGL rendering context before you call glue in it so over here in our source code we have GL FW in it if we were to call gluing it here would it work and what I'm not gonna answer that right now we'll see anyway back to the documentation it tells us exactly how to kind of initialize this glue and it is what we call and then we can check to see if that return code from glue in it if that returned integer or jelly num which is just an integer is blue okay or not and if it's not glory care that means we have a problem so pretty easy to tell here there's a bunch of stuff about extensions here as well and all that stuff now in the building section of this we have a bunch of things that are important for building this pool again we don't really care about that and also in the installation section you can see that there are some things listed about how to build the library as well if you're trying to use it as a shared library all that stuff anyway we'll kind of explore it and find out for ourselves as well but just with just another reminder if you're using a new library read the documentation it doesn't hurt to do that it might take a little bit of time but overall you might see things that first of all are going to prevent you from accidentally doing things incorrectly and then pulling your hair up because it's not working whereas it appears that it it might it should be and also you might be able to say that oh I can actually use this in way that in think of the really important thing though is that this is C++ we're running code in C++ and C++ allows you to do things in many many many ways many different ways and especially if you're new to C++ you might think okay every library I use follows this progression follows this kind of style you can't assume that there's so many ways to do stuff in C++ and everyone thinks that they're doing it the right way so what you need to do is read the documentation and don't don't make assumptions because they might be doing something crazy they might be defining their own meta language for certain things or whatever you can't kind of assume that stuff so documentation important to read anyway I've done this before so I'm not going to read all this right now but let's set this up with our project so I've extracted all of this here again we've looked at the documentation bin contains inside release we have access to 4.32 which has our dll file I don't really care about that because we're going to link this statically remember you pretty much always want to link statically if you can so here inside include we have our actual includes you're probably realizing right now that this is exactly the path that we need to add to our actual project to our compiler include paths and you are correct so I'm going to copy the end of that project after dependencies go to our project right click on OpenGL hit properties and then I'm going to go over here into configuration you can see such a debug just going to make sure that that's on all configurations platform when 32 is fine inside say C++ in general I'm going to add a semicolon to the end of this and paste in this path I've copied and then just stick solution directory to the front of that now if there's a lot of stuff here you don't really want to add semicolons or scroll through this you can just drop down this box here and here edit you can see that we have a list of them here and they're much easier to see okay fantastic let's talk about linking in the library now so if we go back to our folder here just gonna go back a bit into dependencies glue and then live now inside release and win32 which is the one we care about we've got to which one of these do you think is the static library and which one do you think is the dynamic library well plot-twists they're both technically static libraries but this one is used if you want to link with the DLL this one is used if you want linked with just the static library because this is the entire static library so the two things that give it away is the fact that this has an S here and also that the size is significantly larger than the other one remember if you have an S that probably stands for static again things that you kind of just learned with experience so this is the one that we want to link against I'm going to copy this directory path go into Lincoln General and then additional library directories I'm gonna drop this down here to make it easier for you guys to see get this new kind of thing button and then just paste in a solution directory over here and my path so there we go looks pretty good to me and then the one we're linking whether remember is glue doesn't really matter about the order here in this case either glue 32s don't lib is what I want okay fantastic done now I should be able to call glue in it and also let's just go ahead and include my path GL / glue dot H now if you actually look into here the reason I know it's GL and I'll glue whatever Jill W has gel W but glue dozen because of course if I look in the actual include directory is a folder called GL and then glue dot H so remember the path the West that's fine here is the path from this compiler include directory that we've just included okay great so back over here you consider this works successfully let's try building this so I'll right click here and hit build ok so it's right away we actually get an arrow and it says GL dot h included before glue dot h this is coming from if we look at the output this is coming from our actual glue dot H header file so we can double click here if we want and we can take and we can take a look at this so the reason this is happening is because there's literally a hash arrow defined which is a compile-time error that happens if these symbols are defined so basically all this is asking you to do is please include this before you include any other opengl things so GL of w happens to include GH so what we're going to do is just move this glue so that it's before this and that's it let's try building this again okay awesome we compiled successfully however we get a link error and that link error is because our results our symbol glue in it now we did link everything didn't we so what's the issue well let's take a look at how glue is actually set up if we actually go to the glue header file and we look for this glue init function you can see that we have this glue API kind of macro defined before the actual return type let's take a look at what that means and if I jump over here we see this as well as a comment which kind of explains it for us so glue static is defined for static library and glue builders defined for building the DLL library right now we don't have lucite defined so what's happening is it's coming on over here glue build isn't defined so it's coming over here and it's actually defining glue API as extern decals spec DLL import which is an M sv c compiler intrinsic which basically tells the linker this is coming from a dll file you need to dll import this now we're not using the DLL version of glue are we waiting the static library so if we write code like this it's nothing because it's talked about to link it it's it's just it's just not gonna work we need to actually just use it like any other function and of course if you look up here you can see that if glue static is defined we don't get that kind of deco spec DLL import or export export of courses if you're actually building it import is for when we want to import it from a deal so we want to use this and the way we do that is we define the glue static again something that you could definitely get if you read the documentation but if you don't you might have to kind of reverse engineer it and actually look at how it works to figure out how to get this stuff to compile and Link I just wanted to show you that font basically so if we go back to here we need to define glue static and I'm going to do that by right-clicking on my project going to properties going to say C++ preprocessor and then over here in preprocessor definitions I'm going to type in glue underscore static and hit enter again making sure that I'm I'm on all configurations and win32 I'll hit OK and now I'm going to build my application okay awesome so we don't get any errors here in our output and our application is fine so we know the gluon it returns a value if we actually look at this you can see there's no documentation in the header file so we need to go back here if we go back to our usage in the documentation you can see that this return value from clue in it if it actually isn't clear okay then we have a problem so let's go ahead and check that I want to say if gluon is doesn't equal glue okay then maybe we can just print something like this just for now and I'll put a break one on this line before we start also have to include iostream of course to use cialis so let's hit f5 ok so we're all on this line of code let's hit f10 Oh No look at that it's not equal to clue okay and we get an error printing what happened well remember when I mentioned whether it was okay to call glue in it after G on top of you in it and then I asked you guys if it was and then maybe you said something or maybe you just ignored me and waited for the result well it's not okay we can't call blue net here the reason we can't is because the documentation clearly states that you need to create a valid OpenGL rendering context where do we do this gia left all you make context current that is when we create that OpenGL context okay so what we need to do is move this code so that is after we have a valid context and a valid window and all that so now if I move this down here put a breakpoint here hit f5 now if I hit f10 look at that it skips over this because it is okay and everything is good and that's all there is to it now we have access to all of the open shell functions that we actually have up to the Karmapa job version and if I want to use any of the kind of representations that are in new events weapon gels such as GL gen buffers or something like that you can see that I can just type it in there and it's fine if you look at the actual glue header file so if I right click here and go to open document this is actually a really large file if you scroll down at the bottom you can see it's 23 thousand lines of code it might even slow your computer down a bit but this has everything that it is and the way that it works if we loosely look at it is that for a lot of functions what it does is it actually defines function pointers which are all which is what all of these are so if you were to use a function such as GL gen buffers like I just did it actually and let's just make this let's just loosely make this kind of work so we want one buffer a it actually has to be an unsigned int but anyway there's our function if we actually go to that function definition you can see it's just a macro as to hash define so what we actually want to do if we will not if we want to look at the function signature is go through this blue gen buffers and then go to the type of this function pointer and then we get the return type which is void as well as the parameters that X and so size and an and an unsigned int but of course we'll be looking at this and discovering a lot more of this as our pin gel journey goes on so that's it that's how you initialize glue that's how you get to the modern OpenGL versions one more thing will do for today is actually print our OpenGL version to just make sure we're up to date this has nothing to do with actual glue by the way but it's just useful information that we should have so what I'm going to do is once we have a valid OpenGL texts I'm basically just going to write STD C out and then GL get string GL underscore version just like that I'm going to remove this break point hit f5 and you can see that what it says over here is open GL 4.4 bilbo blah I'm actually running on a laptop right now which has an Intel GPU as well as the Nvidia one it's obvious from this from this line here that it's actually using the Intel GPU for running this application if I switch to Nvidia it would have said the Nvidia driver version and probably the word and video as well as a higher OpenGL version but it's always useful to know why you're actually running so printing that at the console is useful stuff anyway I hope you guys enjoy this video if you did you can hit that like button you can also help support this series and get rewards such as videos early contribute to video planning your name in the credits all that kind of fun stuff by going to patreon.com/scishow no it really does help support the series and make sure that I can make these videos more frequently for you guys I will see you next time goodbye [Music]
Info
Channel: The Cherno
Views: 274,570
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, opengl, graphics, glew, glfw, visual studio, windows, how to opengl
Id: H2E3yO0J7TM
Channel Id: undefined
Length: 18min 20sec (1100 seconds)
Published: Wed Oct 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.