Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learn OpenGL
  • Table Of Contents Toc
Learn OpenGL

Learn OpenGL

By : Frahaan Hussain
2.5 (4)
close
close
Learn OpenGL

Learn OpenGL

2.5 (4)
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 (8 chapters)
close
close

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. The majorVersion is to the left of the decimal point and the minorVersion is to the right of the decimal point. Then, we defined that we're using core modern OpenGL by setting ContextSettings to Core.

  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.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learn OpenGL
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon