Sep 04, 2012 From OpenGL wiki: 'For most libraries you are familiar with, you simply #include a header file, make sure a library is linked into your project or makefile, and it all works. OpenGL doesn't work that way.' I work on Windows 64 and I need OpenGL to use it in C application. What library I should use? NVIDIA provides full OpenGL 4.6 support and functionality on NVIDIA GeForce and Quadro graphics card with one of the following Turing, Volta, Pascal, Maxwell (first or second generation) or Kepler based GPUs: GeForce RTX: GeForce RTX 2080 Ti, GeForce RTX 2080, GeForce RTX 2070, GeForce RTX 2060. GeForce GTX: GeForce GTX 1660 Ti, GeForce GTX. Mesa, also called Mesa3D and The Mesa 3D Graphics Library, is an open source software implementation of OpenGL, Vulkan, and other graphics API specifications. Mesa translates these specifications to vendor-specific graphics hardware drivers. Its most important users are two graphics drivers mostly developed and funded by Intel and AMD for their. OpenGL (Open Graphics Library) is a specificatioSpecification At its most basic level, OpenGL is a specification, meaning it is simply a document that describes a set of functions and the precise behaviours that they must perform. From this specification, hardware vendors create implementations libraries of functions created to match the functions stated in the OpenGL specification, making use.
We need a C/C++ compiler, either GCC (GNU Compiler Collection) from MinGW or Cygwin (for Windows), or Visual C/C++ Compiler, or others.
We need the following sets of libraries in programming OpenGL:
- Core OpenGL (GL): consists of hundreds of functions, which begin with a prefix '
gl
' (e.g.,glColor
,glVertex
,glTranslate
,glRotate
). The Core OpenGL models an object via a set of geometric primitives, such as point, line, and polygon. - OpenGL Utility Library (GLU): built on-top of the core OpenGL to provide important utilities and more building models (such as qradric surfaces). GLU functions start with a prefix '
glu
' (e.g.,gluLookAt
,gluPerspective
) - OpenGL Utilities Toolkit (GLUT): provides support to interact with the Operating System (such as creating a window, handling key and mouse inputs); and more building models (such as sphere and torus). GLUT functions start with a prefix of '
glut
' (e.g.,glutCreatewindow
,glutMouseFunc
).
Quoting from the opengl.org: 'GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits. GLUT is simple, easy, and small.'
Alternative of GLUT includes SDL, .... - OpenGL Extension Wrangler Library (GLEW): 'GLEW is a cross-platform open-source C/C++ extension loading library. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform.' Source and pre-build binary available at http://glew.sourceforge.net/.
Each of the software package consists of:
- A header file: '
gl.h
' for core OpenGL, 'glu.h
' for GLU, and 'glut.h
' (or 'freeglut.h
') for GLUT, typically kept under 'includeGL
' directory. - A static library: for example, in Win32, '
libopengl32.a
' for core OpenGL, 'libglu32.a
' for GLU, 'libglut32.a
' (or 'libfreeglut.a
' or 'glut32.lib
') for GLUT, typically kept under 'lib
' directory. - An optional shared library: for example, '
glut32.dll
' (for 'freeglut.dll
') for GLUT under Win32, typically kept under 'bin
' or 'c:windowssystem32
'.
It is important to locate the directory path and the actual filename of these header files and libraries in your operating platform in order to properly setup the OpenGL programming environment.
Eclipse CDT with Cygwin or MinGW
Installing Eclipse CDT / Cygwin or MinGW, OpenGL, GLU and GLUT
Step 1: Setup the Eclipse CDT (C Development Toolkit)
Read 'How to install Eclipse CDT'.
Step 2: Setup a GCC Compiler
We could use either MinGW or Cygwin.
- MinGW: For MinGW, we need to install GLUT separately. Download freeglut (@ http://freeglut.sourceforge.net/index.php). I recommend using the pre-package version for MinGW (freeglut 2.8.0 MinGW Package) available at http://www.transmissionzero.co.uk/software/freeglut-devel/.
Download, unzip and copy header files from 'includeGL
' to '<MINGW_HOME>includeGL
'; the libraries from 'lib
' to '<MINGW_HOME>lib
', and shared library from 'bin
' to '<MINGW_HOME>bin
' (which should be included in the PATH environment variable), where<MINGW_HOME>
is the MinGW installed directory.
Take note of the headers and libraries:- Headers: the OpenGL header '
gl.h
', GLU header 'glu.h
' and GLUT header 'glut.h
' (or 'freeglut.h
') are kept in '<MINGW_HOME>includeGL
' directory. Since '<MINGW_HOME>include
' is in the implicit include-path. We can include the headers as<GL/glut.h>
,<GL/glt.h>
, and<GL/gl.h>
. - Libraries: the OpenGL library '
libopengl32.a
', GLU library 'libglu32.a
' and GLUT library 'libfreeglut.a
' are kept in '<MINGW_HOME>lib
' directory. This directory is in the implicit library-path.
Nonetheless, we need to include these libraries in linking. They shall be referred to as 'opengl32
', 'glu32
', 'freeglut
' without the prefix 'lib
' and suffix '.a
'.
glut.h
' to '<MINGW_HOME>includeGL
', 'glut32.lib
' to '<MINGW_HOME>lib
', and 'glut32.dll
' to '<MINGW_HOME>bin
' (which should be included in the PATH)) - Headers: the OpenGL header '
- Cygwin: We need to install '
gcc
', 'g++
', 'gdb
', 'make
' (under the 'Devel
' category) and 'opengl
', 'freeglut
' (under the 'Graphics
' category).- Headers: the OpenGL header '
gl.h
', GLU header 'glu.h
', and GLUT header 'glut.h
' are provided in the '<CYGWIN_HOME>usrincludew32apiGL
' directory. As '<CYGWIN_HOME>usrincludew32api
' is in the implicit include-path. We can include the headers as<GL/glut.h>
,<GL/glt.h>
, and<GL/gl.h>
. - Libraries: the OpenGL library '
libopengl32.a
', GLU library 'libglu32.a
' and GLUT library 'libglut32.a
' are provided in the '<CYGWIN_HOME>libw32api
' directory. This directory is in the implicit library-path.
Nonetheless, we need to include these libraries in linking. They shall be referred to as 'opengl32
', 'glu32
', 'glut32
' without the prefix 'lib
' and suffix '.a
'.
- Headers: the OpenGL header '
Step 3: Configuring the Include-Path, Lib-Path and Library: We can configure on per-project basis by right-click on the project ⇒ Properties ⇒ C/C++ general ⇒ Paths and Symbols ⇒ Use 'Includes' panel to configure the Include-Path; 'Library Paths' panel for the Lib-Path; and 'Libraries' panel for individual libraries. We will do this later.
On command-line (for GCC), we could use option -I<dir>
for include-path, -L<dir>
for lib-path, and -l<lib>
for library.
Writing Your First OpenGL Program
- Launch Eclipse.
- Create a new C++ project: Select 'File' menu ⇒ New ⇒ Project... ⇒ C/C++ ⇒ C++ Project ⇒ Next.
In 'Project name', enter 'Hello
' ⇒ In 'Project type', select 'Executable', 'Empty Project' ⇒ In 'Toolchain', select 'Cygwin GCC' or 'MinGW GCC' (depending on your setup) ⇒ Next ⇒ Finish. - Create a new Source file: Right-click on the project node ⇒ New ⇒ Other... ⇒ C/C++ ⇒ Source file ⇒ Next.
In 'Source file', enter 'GL01Hello.cpp
' ⇒ Finish. - In the editor panel for '
GL01Hello.cpp
', type the following source codes:
NOTE: For Windows, you should include 'windows.h
' header before the OpenGL headers. - Configuring the 'include-paths', 'library-paths' and 'libraries':
Right-click on the project ⇒ Property ⇒ C/C++ general ⇒ Paths and Symbols.
Open the 'Libraries' tab ⇒ Add ⇒ Enter 'glut32
' (Cygwin) or 'freeglut
' (MinGW with freeglut) ⇒ Add ⇒ Enter 'glu32
' ⇒ Add ⇒ Enter 'opengl32
'.
There is no need to configure the 'include-paths' and 'library-paths', as they are implicitly defined. - Build (right-click on the project node ⇒ 'Build Project') and Run (right-click on the project node ⇒ Run As ⇒ Local C/C++ Application).
GCC Compilation in Command-line
Observe the Eclipse's console. It uses the following commands to build the project:
The most important option is -l<lib>
, which specifies the 'library' to be linked. You might also need -I<dir>
to specify the 'include-path' and -L<dir>
to specify the 'lib-path', if the header files and libraries are not kept in implicit directories used by the compiler.
You can find the commands used in Eclipse CDT for build the project at '.metadata.pluginsorg.eclipse.cdt.uiglobal-build.log
'.
CodeBlocks / MinGW
Installing CodeBlocks, MinGW and GLUT
- Install CodeBlocks and MinGW: Read 'How to install CodeBlocks'.
- Install GLUT: Refer to the above 'step' in Eclipse/MinGW setup.
Writing Your First OpenGL Program
- Create a new project: File ⇒ New ⇒ Project... ⇒ Console Application ⇒ GO ⇒ C++ ⇒ In 'Project title', enter 'hello' ⇒ Next ⇒ Finish.
- Open '
main.cpp
', and replace with the code, as shown in the previous section. - Configure the libraries: Right-click on the project ⇒ Build Option... ⇒ Linker Settings ⇒ In 'Link Libraries' ⇒ Add ⇒ enter '
freeglut
' ⇒ Add ⇒ enter 'glu32
' ⇒ Add ⇒ enter 'opengl32
'.
There is no need to configure the 'include-paths' and the 'library-paths', as they are implicitly defined. - Build (right-click on the project ⇒ Build) and Run (Select 'Build' menu ⇒ Run).
GCC Compilation in Command-line
Refer to the above 'section' on 'Eclipse with GCC'.
Visual C++ 2010 Express
Installing VC++, OpenGL, GLU and GLUT
You need to install:
- Visual C++ Express 2010: Read 'How to install Visual C++ Express'. VC++ would be installed in '
C:Program FilesMicrosoft Visual Studio 10.0VC
', with headers in sub-directory 'include
' and libraries in 'lib
'. - Windows SDK which includes OpenGL and GLU (OpenGL Utility). The Visual C++ 2010 Express bundles the Microsoft Windows SDK, which would be installed in '
C:Program FilesMicrosoft SDKsWindowsv7.0A
'. (Otherwise, you need to download and install the Windows SDK separately).
The followings are used from Windows SDK:gl.h
,glu.h
: header for OpenGL and GLU in directory 'C:Program FilesMicrosoft SDKsWindowsv7.0Aincludegl
'.opengl32.lib
,glu32.lib
: libraries for OpenGL and GLU in directory 'C:Program FilesMicrosoft SDKsWindowsv7.0Alib
'.opengl32.dll
,glu32.dll
: dynamic link libraries for OpenGL and GLU in directory 'C:WindowsSystem32
'. This directory is to be included in PATH environment variable.
If you use the VC++ IDE, the include-path and lib-path would have been set correctly. If you use the CMD shell, you need to run the batch file '
vcvarsall.bat
' (in 'C:Program FilesMicrosoft Visual Studio 10.0VCbin
'), or 'vcvars32.bat
' in the earlier version, to set the environment variables. - GLUT (OpenGL Utility Toolkit): Download Nate Robin's original Win32 port of GLUT from @ http://www.xmission.com/~nate/glut.html (or freeglut @ http://freeglut.sourceforge.net). Unzip and copy '
glut.h
' to 'C:Program FilesMicrosoft SDKsWindowsv7.0Aincludegl
','glut32.lib
' to 'C:Program FilesMicrosoft SDKsWindowsv7.0Alib
', and 'glut32.dll
' to 'C:WindowsSystem32
' (that is, the same locations as OpenGL and GLU).
Writing Your First OpenGL Program
To Write your first OpenGL program with GLUT:
- Launch Visual C++ 2010 Express.
- Create a new 'Win32 Console Application' project: Select 'File' menu ⇒ New ⇒ Project... ⇒ In 'Project Types', select 'Visual C++', 'Win32'. In 'Templates', select 'Win32 Console Application'. In 'Location', set your working directory. In 'Name', enter '
hello
' ⇒ Next ⇒ Check 'Empty Project' ⇒ Finish. - Create a new Source file: Right-click on the 'Source Files' of the project name ⇒ Add ⇒ New Item... ⇒ In 'Categories', select 'Visual C++', 'Code'. In 'Templates', select 'C++ File (.cpp)'. In 'Name', type '
GL01Hello.cpp
' ⇒ Add. - In the editor panel for '
GL01Hello.cpp
', replace with the above code (in the previous section). - Build the solution ('Build' menu ⇒ Build Solution) and run the program ('Debug' menu ⇒ 'Start Without Debugging').
Common OpenGL Programming Errors (for VC++)
- Linkage Error: Right-click on the project name ⇒ In 'Configuration' drop-down menu, select 'All Configurations' (i.e., release and debug) ⇒ Expand the 'Configuration Properties' node. Expand the 'Linker' sub-node ⇒ Select 'Input' ⇒ In 'Additional Dependencies', type '
opengl32.lib glu32.lib glut32.lib
'.
Alternatively, you could also use pre-processor directives to instruct compiler about the libraries used (I prefer this approach, which avoids manual configuration for every project): - 'error C2664: 'xxxx' : cannot convert parameter 2 from 'const char [xx]' to 'LPCWSTR': Right-click on the project name ⇒ In 'Configuration' drop-down menu, select 'All Configurations' (i.e., release and debug) ⇒ Expand the 'Configuration Properties' node ⇒ General ⇒ Character Set ⇒ Set to 'Use Multi-Byte Character Set'.
Alternative, you can use preprocessor directives: - Cannot find '
glaux.h
': Try remove '#include <gl/glaux.h>
' from the source code. (Nehe's tutorial codes included a header 'glglaux.h
' for reading BMP file from Lesson #6 onwards. Lesson #1 to #5 do not require this header. GLUAX is obsolete and excluded from Windows SDK v6.0a.) If that does not work, you may have to download and install GLUAX package.
VC Compilation in Command-line
You can use the following command-line options to compile and link an OpenGL program. (You should study the command-line options produced by VC++. Right-click on the project name ⇒ Properties ⇒ Expand 'Configuration Properties' node ⇒ Expand 'C/C++' or 'Linker' sub-node ⇒ Command-line.) Assume that the headers (gl.h
, glu.h
, glut.h
) path is set in the INCLUDE
environment variable, and the link libraries (opengl32.lib
, glu32.lib
, glut32.lib
) path is set in the LIB
environment variable:
Nehe's OpenGL Tutorial
Nehe's production maintains an excellent OpenGL tutorial @ http://nehe.gamedev.net.
OpenGL is language neutral, i.e., you can write your OpenGL programs in Windows, Java, Linux, Mac, etc. Under Windows, there are again many ways to write a OpenGL program. e.g., Win32 API directly, GLUT or FreeGLUT, SDL (Single DirectMedia Layer), Allegro, etc.
Nehe's lessons are explained in Win32 API, which is complex, cumbersome and hard and to understand. Nonetheless, the solutions are also ported to all the other platforms. I strongly suggest that you follow the solution using GLUT (which greatly simplifies interaction to Windows).
Link to OpenGL/Computer Graphics References and ResourcesLoading images from files is outside the scope of the OpenGL specification. It is handled by user-made code or one of the image handling libraries below.
Single format libraries
- lodepng: Very small library for loading PNG files without any dependencies.
- libpng: The official library for loading PNG files. It is very widely used. Many other libraries use this to handle PNG.
- libspng: An alternative to libpng with a simple C API.
- libjpg: A library for loading JPEG (or JPG) files. As with libpng, other libraries use libjpg to handle JPEG.
- libktx: Library for the KTX file format. The KTX format is closely related to OpenGL, but not used by most tools. LibKTL also has functions to take a loaded KTX image and convert it into an OpenGL texture.
Multi format libraries
OpenGL Image
Last Update: Jan 11, 2014
GLI (OpenGL Image) is a small cross-platform C++ image library able to load DDS textures (DDS9 and DDS10), compressed or uncompressed.
Opengl Library Linux
It is licensed under the MIT license.
Unofficial OpenGL SDK
Last Update: August 10, 2012
Unofficial OpenGL SDK has an image loading component called GL Image. This is a C++ library for loading images. It currently supports DDS9 textures (and compression), PNG, JPEG, TGA, BMP, and GIF formats.
It has functions to load images into OpenGL textures, either user-created ones or ones created by the system itself. It supports creating immutable storage for textures as well as those with mutable storage. It even supports direct state access, to minimize state changes when creating them.
It is licensed under the MIT license.
SAIL
Last Update: November 1, 2020
SAIL (Squirrel Abstract Image Library) is a fast C/C++ image decoding library providing simple yet powerful APIs, from one-liners to complex use cases with custom I/O sources.
It does not have functions to load images directly into OpenGL textures.
Supported image formats: APNG, PNG, JPEG, GIF, TIFF. Work to add more image formats is ongoing.
Opengl Library For C++
Supported platforms: Windows, macOS, Linux.
It is released under the MIT license.
DevIL
Last Update: January 1, 2017
DevIL stands for Developers Image Library (it once was called 'OpenIL', but that was trademarked, so they had to change it). It supports many image formats for reading and writing, it supports several compilers and OS (Win, Linux, Mac OSX). The library has a OpenGL-like syntax.
It does have functions to load images directly (more or less) into OpenGL textures, though these are part of 'ilut' and not 'il' proper.
DevIL uses the LGPL license, version 2.1.
Opengl Library Contains How Many Components
FreeImage
Last Update: March 23, 2014
FreeImage is an cross-platform image-loading library, with very wide support for image formats (including some HDR formats like OpenEXR).
It does not have functions to load images directly into OpenGL textures.
It is released under multiple licenses, GPL 2 and 3, as well as their own FreeImage Public License.
GLraw
Last Update: March 28, 2014
GLraw is an utility that converts Qt-supported images into various OpenGL-compatible raw formats. glraw reduces the process of loading textures at run-time to a minimum: A file-content dump into GPU memory.
Image to OpenGL texture conversion can be done either by glraw's command line interface, e.g., within an existing tool-chain, or at run-time with glraw linked as asset library (requires linking Qt). For loading image files, just source copy the minimal Raw-File reader or integrate the dependency free, C++ loader library.
It is licensed under the MIT license.
GLAUX
DO NOT USE THIS! This is a very old library that should not be used anymore. Most people who encounter GLAUX are people who see the NeHe tutorials which haven't been updated in years. NeHe uses auxDIBImageLoad() in order to read BMP files. GLAUX is no longer available.