Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn OpenGL

You're reading from   Learn OpenGL Beginner's guide to 3D rendering and game development with OpenGL and C++

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789340365
Length 208 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Frahaan Hussain Frahaan Hussain
Author Profile Icon Frahaan Hussain
Frahaan Hussain
Arrow right icon
View More author details
Toc

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.

You have been reading a chapter from
Learn OpenGL
Published in: Aug 2018
Publisher: Packt
ISBN-13: 9781789340365
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime