Book Image

Learn OpenGL

By : Frahaan Hussain
Book Image

Learn OpenGL

By: Frahaan Hussain

Overview of this book

Learn OpenGL is your one-stop reference guide to get started with OpenGL and C++ for game development. From setting up the development environment to getting started with basics of drawing and shaders, along with concepts such as lighting, model loading, and cube mapping, this book will get you up to speed with the fundamentals. You begin by setting up your development environment to use OpenGL on Windows and macOS. With GLFW and GLEW set up using absolute and relative linking done, you are ready to setup SDL and SFML for both the operating systems. Now that your development environment is set up, you'll learn to draw using simple shaders as well as make the shader more adaptable and reusable. Then we move on to more advanced topics like texturing your objects with images and transforming your objects using translate, rotate and scale. With these concepts covered, we'll move on to topics like lighting to enable you to incorporate amazing dynamic lights in your game world. By the end of the book, you'll learn about model loading, right from setting up ASSIMP to learning about the model class and loading a model in your game environment. We will conclude by understanding cube mapping to bring advance worlds to your game.
Table of Contents (13 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating the OpenGL rendering window using SFML


Check out the below mentioned steps: 

  1. Go to your main.cpp file in Visual Studio or Xcode and begin typing the following code:
#include <iostream> 
  1. Here, we'll include the GLEW and SFML libraries in our project:
#include <GL/glew.h> 
 
#include <SFML/Window.hpp> 
 
const GLint WIDTH = 800, HEIGHT = 600; 

In the preceding lines of code, we've defined the GLint constant. The reason we're creating constant global variables is so that we can easily use these wherever we need them in the code, whether that's for initially creating the window or for manipulating some sort of shape.

  1. Next, let's define our entry point:
int main( ) 
{ 
   sf::ContextSettings settings; 
   settings.depthBits = 24; settings.stencilBits = 8; 

In the preceding lines of code, we've defined some settings for our application and rendering window:

settings.majorVersion = 3; 
settings.minorVersion = 3; 
settings.attributeFlags = sf::ContextSettings::Core; 

Here, the majorVersion and minorVersion that we defined in the preceding lines of code are for setting the version of OpenGL. Here, we set the version as 3.3 by setting the minorVersion and the majorVersion to 3. If you wish to set up for any other version, you'll have to make changes accordingly. ThemajorVersionis to the left of the decimal point and theminorVersionis to the right of the decimal point. Then, we defined that we're using core modern OpenGL by settingContextSettingstoCore.

  1. Next, you want to define sf::Window. Here, we're going to put sf::VideoMode, and we're going to put WIDTH, HEIGHT, and 32 for the pixel depth. Then, we'll add OpenGL SFML as the title of our window. And then, we add sf::Style::Titlebar and sf::Style::Close to have a title bar and a close button for our window:
sf::Window window( sf::VideoMode( WIDTH, HEIGHT, 32 ), "OpenGL     SFML", sf::Style::Titlebar | sf::Style::Close, settings ); 
  1. Now, we'll try to initialize GLEW by setting it to TRUE and if it's unsuccessful, then we'll display a Failed to initialize GLEW message to the developer. And then, we're going to do return EXIT_FAILURE because it has failed:
   glewExperimental = GL_TRUE; 
 
   if ( GLEW_OK != glewInit( ) ) 
   { 
      std::cout << "Failed to initialize GLEW" << std::endl; 
 
      return EXIT_FAILURE; 
   } 
 
   bool running = true; 
  1. Next, we are going to create a while loop and define certain conditions in it:
 
while ( running ) 
{ 
   sf::Event windowEvent; 
 
   while ( window.pollEvent( windowEvent ) ) 
   { 
      switch ( windowEvent.type ) 
      { 
      case sf::Event::Closed: 
         running = false; 
 
         break; 
      } 
  }  

In the preceding while loop, we are stating that if the window is closed, we are going to stop running our application and break out of our loop.

  1. Then, we'll add some color to our window and define a space to draw:
 
      glClearColor( 0.2f, 0.3f, 0.3f, 1.0f ); 
      glClear( GL_COLOR_BUFFER_BIT ); 
 
      // draw OpenGL 
 
      window.display( ); 
   } 
 
   window.close( ); 
 
   return EXIT_SUCCESS; 
  }
}   

Let's run our code and check whether there are any errors. If no errors pop up, we'll get a rendering window as output, similar to what we have witnessed in the previous sections.