OpenGL is a specification that can be used to render interactive 2D and 3D graphics applications.
OpenGL has wide range of applications. OpenGL library comes in two formats.
- Deprecated fixed pipeline based API or legacy OpenGL ( releases before 3.0)
- Shader based API or Modern OpenGL (release 3.0 and onwards)
Modern OpenGL uses GPU instead CPU there by increasing user experience tremendously. legacy OpenGL is still supported for backward compatibility as of latest release 4.6.
In this tutorial we shall consider shader based approach.
Details
This tutorial attempts to teach basic to advanced concepts one at a time. The goal is to create an advanced , multifunctional library from scratch. The tutorials will be a set of C++ header (.h) files.
There are plenty of websites with rich documentation on different OpenGL related topics. For various topics, a short description will be provided along with a link for further details.
This tutorial is primarily targeted for windows OS. Visual Studio development environment is used for writing, compiling and debugging the code.
Prerequisites
OpenGL programming is different from normal programming. Following is a check list:
- 3D math such as vectors, planes, rays etc.
- Matrix arithmetic
- Trigonometry
- Understanding of motion related physics
- Intermediate C++ programming
- Visual studio 2015 or higher
- GLSL concepts and programming
Development Environment
Visual Studio 2022 IDE is used for building externals, library and lessons in the tutorials. The steps will be slightly different in the later versions.Tutorial Layout
The Tutorial directory structure is laid out as shown below.
Lessons
This folder contains source code and binaries of the applications related to the OpenGL topics and features discussed in the tutorial.
This folder contains source code and binaries of the applications related to the OpenGL topics and features discussed in the tutorial.
Source
These folders contains the projects and source of lessons on different topics. Each lesson is named as below:
Lesson<nn> <Description>.
Example, Lesson01 Creating OpenGL Context, Rendering
Each folder contains a vcxproj file that wraps source files related to the topic. After building , the generated executable are transferred to the bin folder.
All the lessons can be loaded from the solution file Modern-OpenGL-Tutorial-Lessons.sln.
These lessons refer to different features of OpenGL implemented in VedaGraphicsLib.
bin
This folder contains executables and other binaries of the lessons generated during the build.
Resources
This folder contains bitmaps, texture bitmaps, wireframe models used in the tutorial.
VedaGraphicsLib/Source
This folder contains source code of the applications related to the OpenGL topics and features discussed in the tutorial. It's a set of header files that can be included by the client applications.
The following discusses some of the aspects implemented by VedaGraphicsLib.
These are located under the folder VedaGraphicsLib.
Geometry
This folder defines some base classes responsible for rendering 3D objects such as Text, Cube etc. Their meshes and utility classes such as shaders, Lighting and Vertex data management.
Canvas
This folder contains classes responsible for creating OpenGL context and host window, Cameras and projection related data etc
VedaGraphicsLib/Externals
This folders contains 3rd party libraries used by the tutorial for loading OpenGL extensions, perform vector math, matrices calculations. loading texture, object model etc.
This folder contains classes responsible for creating OpenGL context and host window, Cameras and projection related data etc
VedaGraphicsLib/Externals
This folders contains 3rd party libraries used by the tutorial for loading OpenGL extensions, perform vector math, matrices calculations. loading texture, object model etc.
GLExtns
OpenGL is a widely adopted specification (API) for rendering 2D and 3D graphics that is implemented by hardware vendors. The specification, managed by the Khronos Group, defines a set of functions and behaviors that graphics card manufacturers incorporate into their drivers.
In Windows platform, these APIs are defined in the OpenGL header files and implemented in OpenGL.Lib. Client applications must include these header files and link with OpenGL.Lib to interact with the hardware.
The OpenGL header file included as a part of windows SDK supports OpenGL specification 1.1. The Modern OpenGL tutorial uses OpenGL specification 3.3 or above. The extensions will be loaded after the context is created. This will be discussed in detail in the next post.
As the OpenGL specification 3.3 or above is quite big, the extensions needs to be supplied by a separate set of source files. In addition, latest WGL extensions should also be included.
Both the header file and implementation of OpenGL specification 3.3 or above and WGL extensions can be generated from glad website as shown below.
In the example below, the required inputs are boxed. Note that the GL version in this case is 4.0 but it may vary based on your hardware. You can detect exact supported OpenGL version using tools from glew like visualinfo.exe.
The generated files are later extracted and saved as below. Note that the header files also contains implementation so no sperate GL.c or WGL.c files exist.
externals\GLExtns\Glad\GL.h
externals\GLExtns\Glad\WGL.h
Edit WGL.h and remove the following line
GLM
Rendering 3D context involves lot of vector math, matrix math, trigonometry etc.
GLM conveniently provides a math library including all these and more. In addition it also provides APIs for creation of Ortho and Perspective projection matrix, transformations such as translation, rotation and scaling, lookat matrix and much more.
The latest GLM can be downloaded from glm website.
GLM is implemented as set of header files. They are located in the folder.listed below:
externals\glm
stb
stb is a set of standalone header files providing an unique functionality. stb_image.h is used for loading textures, displaying bitmaps etc. The latest stb_image.h can be downloaded from github.
The stb_image.h is located under folder:
externals\stb\stb_image.h
assimp
assimp is a library for importing models that are built from 3D software such as Blender. This can be downloaded from github.
Unlike the above, this is not just a set of header files. Hence it needs to be built.
Follow these steps:
1. Download files to assimp-6.0.5 folder.
2. Run CMake to generate Visual Studio projects and the solution. Let the output folder be
assimp-6.0.5/build. The output type needs to be a Static library.
In Configure Step, Choose settings as shown below.
3. In generate step, Choose settings as shown below.
ASSIMP_DOUBLE_PRECISION OFF
ASSIMP_BUILD_TESTS OFF
5. After the successful build,
a. The entire folder
assimp-6.0.5/include folder should be to copied.
assimp-6.0.5/build/include folder should be to copied.
b. Similarly, the lib files should be copied from following locations.
assimp-6.0.5/build/lib/Debug/assimp-vc143-mtd.lib
assimp-6.0.5/build/lib/RelWithDebInfo/assimp-vc143-mt.lib
assimp-6.0.5/build/contrib/zlib/Debug/zlibstaticd.lib
assimp-6.0.5/build/contrib/RelWithDebInfo/zlibstatic.lib
c. Similarly, the bin files should be copied from following locations.
assimp-6.0.5/build/bin/Debug/assimp-vc143-mtd.dll
assimp-6.0.5/build/bin/RelWithDebInfo/assimp-vc143-mt.dll
6. The resulting folder structure will be as below.
externals\assimp\include
externals\assimp\bin\assimp-vc143-mt.dll
externals\assimp\bin\assimp-vc143-mtd.dll
externals\assimp\lib\assimp-vc143-mt.lib
externals\assimp\lib\assimp-vc143-mtd.lib
externals\assimp\lib\zlibstaticd.lib
externals\assimp\lib\zlibstatic.lib
Windows SDK and Libraries
In addition to the above, the tutorial also uses other facilities implicitly provided by the Windows SDK and the Compiler as discussed below. These are not listed in the diagram.
Window Creation and Management
Creating an OpenGL window requires using a platform-specific library, as OpenGL itself does not provide window or input management functionality. ATL (Activex Template Library) is used to create windows and manage them.
ATL is part of Visual Studio C++ installation and its source files are located under the folder.
C:\Program Files\Microsoft Visual Studio\2015\Professional\Common7\IDE\ItemTemplates\VC\ATL
Mouse and Keyboard Input
OpenGL itself is purely a graphics rendering API and does not have built-in functions for handling mouse and keyboard input. To manage mouse and keyboard inputs Win32 API provided by the Windows SDK are used.
OpenGL Context Creation
Creating an OpenGL (GL) context is the process of setting up the necessary environment and state machine for an application to use the OpenGL API. Because OpenGL is a platform-independent specification, the actual context creation is handled by platform-specific APIs or, more commonly, by cross-platform libraries.
For creating OpenGL context, WGL or Wiggle APIs are used. They are included as a part of Windows SDK.
The Windows SDK is located under the folder C:\Program Files (x86)\Windows Kits\
depedencies.props
This property sheet contains folders for header files, library files and script to extract them. This propertysheet should be applied to each project using the VedaGraphicsLib.
It's used by the compiler and the linker during the build.
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros" /> <PropertyGroup> <IncludePath>$(MSBuildThisFileDirectory)VedaGraphicsLib\externals\assimp\include;$(MSBuildThisFileDirectory)VedaGraphicsLib\externals\GLExtns;$(MSBuildThisFileDirectory)VedaGraphicsLib\externals\glm;$(MSBuildThisFileDirectory)VedaGraphicsLib\externals\stb;$(MSBuildThisFileDirectory)VedaGraphicsLib;$(IncludePath)</IncludePath> <LibraryPath>$(MSBuildThisFileDirectory)VedaGraphicsLib\externals\assimp\lib;$(LibraryPath)</LibraryPath> <_PropertySheetDisplayName>externals</_PropertySheetDisplayName> </PropertyGroup> <ItemDefinitionGroup> <Link> <AdditionalDependencies>opengl32.lib;gdiplus.lib;zlibstaticd.lib;assimp-vc143-mtd.lib;%(AdditionalDependencies)</AdditionalDependencies> </Link> </ItemDefinitionGroup> <ItemGroup /> </Project>
Creating new projects
Following steps can be used to create a new project using VS IDE
Create an Win32 project in VS IDE wizard under Lessons folder. Provide a valid name for the project.
Select Empty Project and no SDL check
Add property sheet depedencies.props to the project .
In the next post we will discuss creation of Creating OpenGL Context, Rendering and Mouse/Keyboard input support.



No comments:
Post a Comment