Compiling and running your first OpenGL application in Windows

There are several ways to set up an OpenGL project. Here, we create a sample project using Visual Studio 2013 or higher and provide a complete walkthrough for the first-time configuration of the OpenGL and GLFW libraries. These same steps can be incorporated into your own projects in the future.

Getting ready

Assuming that you have both Visual Studio 2013 and GLFW (version 3.0.4) installed successfully on your environment, we will start our project from scratch.

How to do it...

In Visual Studio 2013, use the following steps to create a new project and compile the source code:

  1. Open Visual Studio 2013 (VS Express 2013 for desktop).
  2. Create a new Win32 Console Application and name it as Tutorial1.
    How to do it...
  3. Check the Empty project option, and click on Finish.
    How to do it...
  4. Right-click on Source Files, and add a new C++ source file (Add | New Item) called main.cpp.
    How to do it...
  5. Copy and paste the source code from the previous section into the main.cpp and save it.
  6. Open Project Properties (Alt + F7).
  7. Add the include path of the GLFW library, C:Program Files (x86)glfw-3.0.4include, by navigating to Configuration Properties | C/C++ | General | Additional Include Directories.
    How to do it...

    Tip

    Downloading the example code

    You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  8. Add the GLFW library path, C:Program Files (x86)glfw-3.0.4lib, by navigating to Configuration Properties | Linker | General | Additional Library Directories.
    How to do it...
  9. Add the GLFW and OpenGL libraries (glu32.lib, glfw3.lib and opengl32.lib) by navigating to Configuration Properties | Linker | Input | Additional Dependencies.
    How to do it...
  10. Build Solution (press F7).
  11. Run the program (press F5).

Here is your first OpenGL application showing a rotating triangle that is running natively on your graphics hardware. Although we have only defined the color of the vertices to be red, green, and blue, the graphics engine interpolates the intermediate results and all calculations are performed using the graphics hardware. The screenshot is shown as follows:

How to do it...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset