Writing in the front
Glut3 + GLAD, GLFW3 + GLM, STB_image, ASSIMP, AntTweakBar, The end result is perfect for an OpenGL tutorial. I’ll end with a link to the compiled and integrated common library collection (including two VS project templates).
Introduction to third-party Libraries
- Glut: Cross-platform OpenGL application toolkit, define and control Windows, detect events;
- 1. Freeglut: is an alternative to glut;
- Glu: Included in glut is an encapsulation of the underlying OpenGL interface, including mathematical methods such as vectors and manipulation matrices.
- Glew: enhanced version of GLU, but does not include mathematical methods requiring GLM;
- GLM: Mathematics library (for GLFW);
- Glad: instead of glew;
- GLFW is an upgrade to FreeGlut.
- Stb_image: decoding library for image loading;
- Assimp: library for model loading;
- AntTweakBar: Interface library that adds some lightweight and intuitive GUI effects (equivalent to a console) to your program.
8, 9, and 10 are extensions, not necessary.
OpenGL development history and library replacement
OpenGL development
OpenGL version | Version features |
---|---|
OpenGL1.x | Supports vertex arrays, textures, buffer objects and a range of optimizations |
OpenGL2.x | Programmable coloring, can achieve richer effects; DX9 class hardware |
OpenGL3.x | Add geometric coloring rendering, with stronger cross-platform capabilities; Represents DX10 class hardware |
OpenGL4.x | Several details optimization; Represents DX11 class hardware |
You can check out the detailsThe official documentation. | |
##### library classification | |
function | The replacement of the library |
— | — |
Window creation, IO interaction, OpenGL context handling | glut –> freeglut –> glfw |
Support for the graphics card underlying interface | glu –> glew –> glad |
Generally, GLFW is used with GLAD (also with GLEW) and FreeGLUT with Glew.
The corresponding version of the library
library | version |
---|---|
GLUT | OpenGL1.0/2.0 |
FREEGLUT | OpenGL 1.0/2.0/3.0 |
GLEW | OpenGL2.0 above |
GLFW | OpenGL3.0 above |
GLAD | OpenGL3.0 above |
Environment set up
Two methods of setting up the environment are provided here, but both are based on ==VS2015 and above == (the author uses VS2019). Method 1 applies to using GLUT or FreeGLUT; Method 2 provides GLFW + GLAD.
In line with the principle of learning new not learning old, more recommended method two; But method one is much easier to learn and build.
Method 1: NuGet package management
- Create a new project and right-click the project name – Manage the NuGet package in Solution Manager (or project – Manage the NuGet package in the toolbar above)
Turn on the NuGet regulator
Turn on the NuGet regulator
- Search nupengl to find the first one and install it
Install nupengl
- NuGet package management has installed the OpenGL environment for this project, which contains OpenGL, FreeGLUT, GLEW, Game, GLFW, GLUT, nativePackage.
Method two: Use an integrated environment
Download the zip package from the link: OpenGL Integration package.
- Unzip the package and write down the path
unzip
- Open VS (demo: VS2019), toolbar – project – properties – VC++ directory add the include folder and lib folder in our unzipped package to the include directory and library directory respectively
Link libraries
- That’s it, let’s test the code:
#include <glad\glad.h>
#include <glfw\glfw3.h>
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"glfw3.lib")
#pragma comment(lib,"glad.lib")
#pragma comment(lib,"stb_image.lib")
#pragma comment(lib,"assimp.lib")
#pragma comment(lib,"winmm.lib") // Tell the connector to connect to the library because we want to play multimedia sounds
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
void minit(a)
{
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
}
// The MAIN function, from here we start the application and run the game loop
int main(a)
{
std: :cout << Starting GLFW context, OpenGL 3.3 << std: :endl;
minit();
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "ink".nullptr.nullptr);
if (window == nullptr)
{
std: :cout << "Failed to create GLFW window" << std: :endl;
glfwTerminate();
return - 1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Initialize GLEW to setup the OpenGL Function pointers
if(! gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {std: :cout << "Failed to initialize GLad" << std: :endl;
return - 1;
}
// Define the viewport dimensions
glViewport(0.0, WIDTH, HEIGHT);
// Enter the Game loop
while(! glfwWindowShouldClose(window)) {// Check if any events have been activiated (pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2 f.0.3 f.0.3 f.1.0 f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
Sleep(20);
}
// Clear any resources allocated by GLFW to destroy GLFW.
glfwTerminate();
return 0;
}
// The keyboard event Is called whenever a key Is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std: :cout << key << std: :endl;
if (key == GLFW_KEY_Z && action == GLFW_PRESS)
{
std: :cout << "!!!!" << std: :endl; glfwSetWindowShouldClose(window, GL_TRUE); }}Copy the code
If the following information is displayed, enter the keyboardZOutput “!! “Proves success.
Successful example
Reference:
- Glut, FreeGLUT, GLFW, GLEw, GLAD, GL3W libraries
- OpenGL common library