This tutorial is part of a Collection: OpenGL Tutorials
rate up
3
rate down
2629
views
bookmark
03. Creating a window

In this tutorial we will learn how to create a GLFW window.

Main.cpp 1.95 kb
410 downloads
####Creating a Window#### First let's create a .cpp file. Click on your Solution Explorer and the right click on your project and select Add -> New Item. Select C++ file and name it main. Don't forget to click Apply. +[https://www.braynzarsoft.net/image/100308][C++ File] Now that we have our file let's check if we linked GLEW and GLFW correctly. If you got an error you didn't linked everything correctly, so go back to the previous tutorial and check if you did all the steps correctly. #include <iostream> #include <GL/glew.h> #include <GLFW/glfw3.h> Be sure that you included GLEW before GLFW or you may get some errors. Now let's create the ****main**** function and in there we will initialize GLFW and we will also pass some window hints. ##Window creation## int main() { glfwInit(); //This function initializes GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Version of OpenGL we Will be using (3.3) glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Version of OpenGL we Will be using (3.3) glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We tell GLFW we want to use Core-profile glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //Window can't be resized by the user return 0; } This function sets hints for the next call to ****glfwCreateWindow****. The hints, once set, retain their values until changed by a call to glfwWindowHint, or until the library is terminated. This function does not check whether the specified hint values are valid. If you set hints to invalid values this will instead be reported by the next call to glfwCreateWindow. glfwWindowHint(int hint, int value); **hint**: The window hint to set **value**: The new value of the window hint If you are using MAC you need to add the following hint to your code for it to work glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); Make sure you have OpenGL versions 3.3 or higher installed on your systemotherwise the application will crash or display undefined behavior. To find the OpenGL version on your machine a utility like the OpenGL Extension Viewer for Windows. If your supported version is lower try to check if your video card supports OpenGL 3.3+. Next we're required to create a window object. This window object holds all the windowing data and is used quite frequently by GLFW's other functions. const int windowWidth = 600; const int windowHeight = 400; GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "OpenGL", nullptr, nullptr); This function creates a window and its associated OpenGL context. Most of the options controlling how the window and its context should be created are specified with window hints. glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow * share); **width**: Desired window width. This must be greater than zero. **height**: Desired window height. This must be greater than zero. **title**: Window title **monitor**: The monitor to use for full screen, or NULL for windowed mode **share**: The window whose context to share resources with, or NULL to not share resources. When creating glfw window we should also check if there were any errors with window creation. That way we will know what the error was (if there will be any). After the error check we also need to make our window current. //We check if there were any errors if(window == nullptr) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwTerminat() function destroys all remaining windows, frees any allocated resources and sets the library to an uninitialized state. Once this is called, you must again call ****glfwInit()**** successfully before you will be able to use most GLFW functions. glfwMakeContextCurrent (GLFWwindow * window); **window**: The window whose context to make current, or NULL to detach the current context. This function makes the context of the specified window current on the calling thread. A context can only be made current on a single thread at a time and each thread can have only a single current context at a time. ##GLEW## In the previous tutorial we mentioned that GLEW manages function pointers for OpenGL so we want to initialize GLEW before we call any OpenGL functions. glewExperimental = GL_TRUE; //We set the glew features to true if (glewInit() != GLEW_OK) //We check if GLEW was initialized { std::cout << "Failed to initialize GLEW" << std::endl; return -1; } Setting glewExperimental to true ensures GLEW uses more modern techniques for managing OpenGL functionality. Leaving it to its default value of GL_FALSE might give issues when using the core profile of OpenGL. ##Window viewport## Before we can start rendering we have to do one last thing. We have to tell OpenGL the size of the rendering window so OpenGL knows how we want to display the data and coordinates with respect to the window. We can set those dimensions via the glViewport function. We also need to retrieve the size, in pixels, of the framebuffer of the specified window. We can do that via the glfwGetFrameBufferSize function. int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. glViewport(int x, int y, GLsizei width, GLsizei height); **x, y**: Specify the lower left corner of the viewport rectangle, in pixels. **width, height**: Specify the width and height of the viewport. ##Game loop## We don't want the application to draw a single image and then immediately quit and close the window. We want the application to keep drawing images and handling user input until the program has been explicitly told to stop. For this reason we have to create a while loop, that we now call the game loop, that keeps on running until we tell GLFW to stop. while(!glfwWindowShouldClose(window)) { glfwPollEvents(); glfwSwapBuffers(window); } The glfwWindowShouldClose function takes one parameter (GLFW winodw). It return true if the window has been instructed to close and false if it hasn't been. The glfwPollEvents function checks if any events have been triggered(keyboard, mouse input...) and calls the corresponding function(the callback methods, we Will learn about them in the upcomming torials). glfwSwapBuffers function also takes one parameter (GLFW window). It will swap the color buffer (a large buffer that contains color values for each pixel in GLFW's window) that has been used to draw in during this iteration and show it as output to the screen. The last thing we have to do is add the glfwTerminate function at the end of the program. glfwTerminate(); return 0; This will clean up all the resources and properly exit the application. Now try to compile your application and if everything went well you should see the following output: +[https://www.braynzarsoft.net/image/100312][GLFW window] ##Adding color## We also want our window to have some color. At the start of each render iteration we always want to clear the screen otherwise we would still see the results from the previous iteration. We can clear the screen's color buffer using the glClear function where we pass in buffer bits to specify which buffer we would like to clear. The possible bits we can set are GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT. Right now we only care about the color values so we only clear the color buffer. glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); The glClearColor function takes four parameters, The amount of red, green and blue color where the minimal amount is 0.0 and the maximal amount is 1.0f and the alpha value. Now your window should look like this: +[https://www.braynzarsoft.net/image/100315][GLFW window]
Comments
I just realized I hadn't got around to it since Nov of 2015.
on Jan 14 `17
Caseofgames