📜 ⬆️ ⬇️

We configure Windows for programming of OpenGL

The post is focused on beginners. It does not cover the configuration of Visual Studio. He will teach you to customize some text editors to compile OpenGL applications, or to do this from the console manually.

Recently, finally, the 8th edition of the book OpenGL programming guide , which I have been waiting for more than 2 years. A little earlier, I met a wonderful text editor Sublime Text 2 and decided to customize it instead of installing heavy Visual Studio.

So, first we need a compiler - I stopped at G ++ . It is part of GCC , and its port under Windows is called MinGW . Let's start with it:

Mingw

Download the installer from the official site and launch it. You can choose to download fresh packages immediately upon installation, or update them manually later. We will not need compilers Fortran, Ada and ObjC, we can not put them. MSYS Basic System can be useful, we put it. I specified " C:\MinGW " as the address, but if it bothers you, you can choose another one, for example, " C:\Program Files\Common Files\MinGW ".
')
After installation, you should add two new folders to the PATH system variable : " MinGW\bin " and " MinGW\msys\1.0\bin ". To do this, go to Start -> Control Panel -> System -> Advanced System Settings ; In the Advanced tab, click below the button Environment Variables . From the list of system variables, select Path , and separate the full addresses to the folders “ MinGW\bin ” and “ MinGW\msys\1.0\bin ” with a semicolon.

If you did not choose to download fresh packages during installation, you should manually update the necessary packages. To do this, open the console ( Win + R -> cmd ), and write the following commands:
 mingw-get update 
Update information about available packages.
 mingw-get show g mingw-get show g++ 
Display the full gcc and g ++ package names,
 mingw-get upgrade mingw32-gcc mingw-get upgrade mingw32-gcc-g++ 
Update these packages to the latest available MinGW versions.

Now that we have a compiler, we can start installing the necessary OpenGL libraries.

Freeglut

Since OpenGL is an API that relates exclusively to graphics, it does not include functions for creating windows or processing input devices. Moreover, the solution to this problem depends on the operating system. Therefore, it was taken to a separate library and named GLUT - the OpenGL Utility Toolkit. However, the last release of this library was a long time ago and now it has been replaced by freeglut .

We download from the official site binaries under MinGW . We unpack, freeglut.dll from the bin folder we throw in " C:\Windows\System32 " (for x86) or " C:\Windows\SysWOW64 " (for x64), or any other folder that is specified in the PATH . The contents of the include and lib folders can be copied either to " MinGW\include " and " MinGW\lib " (this will be easier), or to separate created folders " MinGW\freeglut\include " and " MinGW\freeglut\lib ".

On this with freeglut everything. To connect it you need to add #include <GL/freeglut.h>

glew

The glew library allows you to determine what the video card supports and what doesn't. Its installation is a little more difficult for our case.
We download the source from the official site , because the binaries are sharpened under Visual Studio and will not work for us. Now these sources need to be compiled. I practically understand nothing of this, so I turned to Google and found help here :
Unpack the downloaded archive, open the console, go to the resulting folder and write the following commands:
 gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c gcc -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 ar cr lib/libglew32.a src/glew.o 
As a result, 3 files appeared in the lib folder: glew32.dll , glew32.dll.a and glew32.a .
glew32.dll copied to the same place where freeglut.dll previously copied, the other 2 - to the folder " MinGW\lib " or " MinGW\glew\lib ".
From the include folder, copy the contents into " MinGW\include " or " MinGW\glew\include ".

On this with glew everything. To connect it you need to add #include <GL/glew.h> , but this must be done before connecting other gl libraries.

We check the performance

Create a minimal test.cpp:
 #include <iostream> #include <GL/glew.h> #include <GL/freeglut.h> #include <GL/gl.h> int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512, 512); glutInitContextVersion(4, 1); //  OpenGL,     glutInitContextProfile(GLUT_CORE_PROFILE); glutCreateWindow(argv[0]); if (glewInit()) { std::cerr << "Unable to initialize GLEW ... exiting\n"; exit(EXIT_FAILURE); } glutMainLoop(); } 

Note the glutInitContextVersion function — it will terminate the execution of the program if it failed to create the context for the specified version of OpenGL. To find out the version of OpenGL that your video card supports, you can look at its specification or download the glew binaries from the official site and run glewinfo.exe from the bin folder. It will generate the glewinfo.txt file, which will describe in detail what your video card supports and what doesn't. Sometimes the functionality can be expanded by installing a more recent video driver.

Open the console, go to the folder with test.cpp and write the following command:
 g++ -o test.exe test.cpp -lglew32 -lfreeglut -lopengl32 -lglu32 -Wl,--subsystem,windows 
In case you created separate folders freeglut and glew , here you must additionally indicate the corresponding additional folders include and lib . Then the command example looks like this:
 g++ -o test.exe test.cpp -lglew32 -lfreeglut -lopengl32 -lglu32 -Wl,--subsystem,windows -I"C:\MinGW\freeglut\include" -L"C:\MinGW\freeglut\lib" -I"C:\MinGW\glew\include" -L"C:\MinGW\glew\lib" 

The -Wl,--subsystem,windows flag -Wl,--subsystem,windows indicates to the compiler that it should create a graphical application, not a console one.
If everything test.exe out, you should get the test.exe file, which when launched creates a white window 512x512.

When you want to share your application, make sure that along with the executable file you transfer the used libraries - freeglut.dll and glew32.dll .

Sublime Text 2

And finally, I will give an example of setting up Sublime. You can download it from here , introductory video tutorials in English can be found here .
After installation, open the C ++ build configuration file:
" C:\Users\%Username%\AppData\Roaming\Sublime Text 2\Packages\C++\C++.sublime-build "
Save its copy as
" C:\Users\%Username%\AppData\Roaming\Sublime Text 2\Packages\User\C++\++ OpenGL.sublime-build "

Line
 "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"], 
We supplement the connection of libraries:
 "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "-lfreeglut -lopengl32 -lglew32 -Wl,--subsystem,windows"], 

Similarly, the line
 "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"] 
supplement to
 "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' -lglew32 -lfreeglut -lopengl32 -lglu32 -Wl,--subsystem,windows && '${file_path}/${file_base_name}'"] 
If you created separate folders under freeglut and glew, these commands should be supplemented by specifying the addresses of additional folders include and lib .

To Sublime did not refuse to display the Cyrillic alphabet in its console, after
 "selector": "source.c, source.c++", 
add
 "encoding": "cp1251", 

Now that we have test.cpp open, choose Tools -> Build system -> C ++ OpenGL .
Press Ctrl + B to compile, Ctrl + Shift + B to compile and run.

That's all!

UPD : When it came to the code with gl-functions from the latest versions, it turned out that connecting the GL/gl.h header file is not enough. It contains outdated version 1.1, and this file is no longer being updated. In addition to this, you need to connect GL/glext.h (the latest version of which you need to download from here ) and GL/wglext.h ( from here ). For Linux, the last file is replaced with GL/glxext.h ( link ). The downloaded files need to be replaced with their outdated versions at the address " MinGW/include/GL/ ".

Source: https://habr.com/ru/post/177885/


All Articles