The Visual Studio version for this tutorial will be the 2017 (seems to work with the 2015 and 2019 as well).
The glew version will be the 2.2.0.
The freeglut version will be the 3.0.0.
The glew library stands for GL Extension Wrangler.
At the end of this OpenGL tutorial you will be able to display a window with a white square displayed on a black background.
We are going to use the 32-bit or the 64-bit version.
You have the choice, and it will be specified in the tutorial.
Notice that the version is only based on which platform you want to compile for.
If you plan to create a program to be executed on a 32-bit platform, so use the 32-bit version.
Every platform accept 32-version, so the 64-bit version is only for specific purpose only.
And so the 32-bit version will work on every Windows system.
You'll need glew headers that could be find on the sourceforge website of the OpenGL Extension Wrangler Library:
You'll also need the freeglut version for Microsoft Visual Studio:
Click the link inside the part freeglut 3.0.0 MSVC Package.
The name of the file you have to download is: freeglut-MSVC-3.0.0-2.mp.zip.
Once you have downloaded it, there is a folder named freeglut inside.
Extract it and rename it to freeglut-3.0.0 in order to have exactly the same name used in this tutorial.
The general directory for our setup will be:
The directory where projects are:
The directory where libraries are:
Open it and create an empty project > File > New > Project > Templates > Visual C++ > Empty Project.
Then write:
OK > Finish.
Your Shapes2D project has been created into your BadprogTutorial solution.
Let's add a main.cpp > Right click your project > Add > New Item > Visual C++ > C++ File > write the name main.cpp > Add.
Right click your Shapes2D project > Properties > On the top left there is a drop down menu > Configuration > Select All Configurations (instead of Debug)
Right click your Shapes2D project > Properties > Configuration Properties > C/C++ > General > Additional Include Directories > Click it.
On the right there is a drop down menu, click <Edit...>.
A new window has appeared: Additional Include Directories.
Click the New Line icon > Click the browse button > Then select the two following folders:
Click OK > Apply
Right click your Shapes2D project > Properties > Configuration Properties > Linker > General > Additional Library Directories > Click it.
On the right there is a drop down menu, click <Edit...>.
A new window has appeared: Additional Library Directories.
Click the New Line icon > Click the browse button > Then select the two following folders:
Click OK > Apply
Right click your Shapes2D project > Properties > Configuration Properties > Linker > Input > Additional Dependencies > Click it.
On the right there is a drop down menu, click <Edit...>.
A new window has appeared: Additional Dependencies.
Click the white area and write:
Click OK > Apply > OK.
Your Visual Studio IDE is now ready to play with OpenGL!
To test the setup, let's code the most basic code HelloWorld! that you can write with OpenGL.
// BadproG.com #include <GL\glew.h> #include <GL\freeglut.h> /** * glVertex2f(float x, float y). * The point (0.0, 0.0) represents the middle of the window (not the top left corner). * The "2f" suffix means 2 values of float type (x and y). */ void displayMe(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); // bottom left glVertex2f(0.5, 0.0); // bottom right glVertex2f(0.5, 0.5); // top right glVertex2f(0.0, 0.5); // top left glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE); glutInitWindowSize(300, 300); // window size glutInitWindowPosition(500, 500); // distance from the top-left screen glutCreateWindow("BadproG - Hello world :D"); // message displayed on top bar window glutDisplayFunc(displayMe); glutMainLoop(); return 0; }
On the Visual Studio's top menu there is a dropdown menu with x86 selected.
Click the black tiny triangle on the right of the x86 and select Configuration Manager.
A new window has appeared: Configuration Manager.
On the right select x86 in the dropdown menu and click New.
A new window has appeared: New Solution Platform.
On the right click the the Active solution platform dropdown menu and select:
Click OK.
You could now close the Configuration Manager and as you can see the platform is now specified as x64.
And you could easily click either x86 or x64 (depending of your libraries version).
Before building it, let's copy (not move!) the .dll of these two libraries:
Copy:
To the following directory:
Copy:
To the following directory:
If you don't want to copy/paste your freeglut.dll and glew32.dll from the library directory to your project directory then you have to set their paths directly in your Environment variables.
If you don't know where to find your Environment variables, open your Windows settings and search for Environment variables.
A system properties window should appear > Advanced tab > Environment variables > In the System variables click New... > Enter, for example for the x64 Freeglut version, the following:
And for the x64 Glew32 version the following:
Then click OK > OK.
Do the same for the x86 version except the paths that must, of course, correspond to your x86 versions.
Then close your Visual Studio IDE (yes close it if it still open) in order to get the last Environment variables set.
And you are done, this time you won't have to copy/paste your .DLL because Windows is now able to find them automatically.
Open Visual Studio again to build and compile your first OpenGL application.
On the Visual Studio's top menu, select either x86 or x64 and build the project.
If there is no error, right click the Shapes2D project > Debug > Start new instance.
If all is OK, you should see a black window with a white square.
If you had some errors like these ones, it's because you try to play with a different version from the one specified in the setup configuration.
For example if you have set up the configuration with the 64-bit version, you have to build it with the x64 configuration.
Same thing for the 32-bit version, set up with the 32-bit library version and build it as a x86 configuration.
So double check the paths you've just entered in the setup configuration.
Here the errors:
All packages are already installed and there is nothing to restore. NuGet package restore finished. 1>------ Build started: Project: Shapes2D, Configuration: Debug Win32 ------ 1>main.obj : error LNK2019: unresolved external symbol __imp__glutInitWindowPosition@8 referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol __imp__glutInitWindowSize@8 referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol __imp__glutInitDisplayMode@4 referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol __imp__glutMainLoop@0 referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol __imp__glutDisplayFunc@4 referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol __imp____glutInitWithExit@12 referenced in function _glutInit_ATEXIT_HACK@8 1>main.obj : error LNK2019: unresolved external symbol __imp____glutCreateWindowWithExit@8 referenced in function _glutCreateWindow_ATEXIT_HACK@4 1>C:\dev\lib\freeglut-3.0.0\lib\x64\freeglut.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86' 1>C:\dev\visual-studio-c++\BadprogTutorial\Debug\Shapes2D.exe : fatal error LNK1120: 7 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Now that your Visual Studio has been set up, you could start creating OpenGL applications.
Well done, you did it.
Comments
Quang Nhat (not verified)
Wednesday, December 16, 2015 - 9:10am
Permalink
Thank you very much, the
Thank you very much, the tutorial is very detailed. it is really good for a beginer like as me and you clearly show many things that I cannot see at others. I have learned many things from it.
amartuvshin (not verified)
Sunday, January 31, 2016 - 8:01am
Permalink
Thank a lot...
Thank a lot...
Jase (not verified)
Sunday, May 15, 2016 - 6:40am
Permalink
Many thanks. Your tute had me
Many thanks. Your tute had me up and running in 15 mins and other tutes I was still stuck after many hours.
Varian (not verified)
Monday, June 20, 2016 - 6:25am
Permalink
Sorry but I get this error(s)
Sorry but I get this error(s) :
1. cannot open source file windows.h (file freeglut_std.h)
2. cannot open source file GL/glu.h (file freeglut_std.h)
3. cannot open source file GL/glut.h (file freeglut_std.h)
4. cannot open source file stdlib.h (file freeglut_std.h)
5. identifier "exit" is undefined (file freeglut_std.h)
6. cannot open source file GL/glu.h (file glew.h)
Any solution related to this error? Many Thanks Before.
Quetos (not verified)
Wednesday, August 17, 2016 - 8:23am
Permalink
Just got mine to work for the
Just got mine to work for the first tutorial here: https://www.badprog.com/c-opengl-setting-up-visual-studio
I'm using VS Express for Desktop 2015. I got both Glew and Freeglut from the links provided in the tutorial above - glew has been updated since the tutorial was uploaded (now 2.0.0).
I had 2 problems when trying to run this:
1. The tutorial offers a 32 and 64 bit version. Stick to the 32 bit. No idea if the 64 bit works but I couldn't get it to work without creating a new project.
2. As stated above, you have to copy the freeglut.dll and glew32.dll into the Debug directory of the program your writing... so for me it's:
D:\C++\BadprogTutorial\Debug\
So Varian, I've seen your errors before - think it's the 64 bit problem? restart a project and set it all to 32 bit. You'll get an error then that it can't open freeglut.dll - that's the second problem. Mine works now... yippee!!!
Hope this helps fellow travellers...
juaco (not verified)
Friday, September 9, 2016 - 2:38am
Permalink
Thanks!
Thanks!
tulio (not verified)
Monday, November 14, 2016 - 3:51pm
Permalink
Doing as the tutorial says,
Doing as the tutorial says, still having some problems.
1>mapview.obj : error LNK2001: unresolved external symbol ___glewUniform1f
1>paintershaderprogram.obj : error LNK2001: unresolved external symbol ___glewUniform1f
1>mapview.obj : error LNK2001: unresolved external symbol ___glewUniform2f
1>paintershaderprogram.obj : error LNK2001: unresolved external symbol ___glewUniform2f
1>coordsbuffer.obj : error LNK2001: unresolved external symbol ___glewBindBuffer
1>painterogl1.obj : error LNK2001: unresolved external symbol ___glewBindBuffer
1>painterogl2.obj : error LNK2001: unresolved external symbol ___glewBindBuffer
1>coordsbuffer.obj : error LNK2001: unresolved external symbol ___glewBufferData
1>framebuffer.obj : error LNK2001: unresolved external symbol ___glewBindFramebuffer
and more
Does anyone got same problem?
Ana (not verified)
Thursday, January 12, 2017 - 5:02pm
Permalink
Thank you very much for this
Thank you very much for this tutorial. Very well written, clear steps. Thanks! ;)
Bima (not verified)
Monday, February 20, 2017 - 3:03am
Permalink
Thank you very much. Your
Thank you very much. Your tutorial helped me a lot. So clear that i didn't get any error in my first try
Bluegene (not verified)
Saturday, March 4, 2017 - 4:53pm
Permalink
First tutorial out of many to
First tutorial out of many to be clear, concise, and actually work. Thank you!
Kvothe (not verified)
Saturday, March 25, 2017 - 8:48pm
Permalink
This is fantastic. After an
This is fantastic. After an afternoon of trying to get visual studio and opengl working together this tutorial does it first time.
Kelly (not verified)
Thursday, April 27, 2017 - 10:54am
Permalink
Thank you very much. The
Thank you very much. The tutorial is explained clearly and it works.
Subhadip (not verified)
Saturday, September 2, 2017 - 11:23am
Permalink
Thank you. I used Visual
Thank you. I used Visual Studio 2017 and able to run it
Nditah (not verified)
Thursday, October 12, 2017 - 10:24pm
Permalink
Sir, your tutorial was very
Sir, your tutorial was very helpful.
I applied it to VS Studio 2017 and it was smooth.
I even tried making modifications on my own and it was still ok
I appreciate your effort.
God Bless!
Theis (not verified)
Monday, November 20, 2017 - 1:46pm
Permalink
Study saving tutorial, so
Study saving tutorial, so thank you for this.
Just wanted to add, that people should make sure the project properties they are modifing has cofinguration put at debug, when adding directory and library paths.
D Ganguly (not verified)
Thursday, January 25, 2018 - 9:41am
Permalink
The first program works fine.
The first program works fine. Thanks!!
The Project Dependencies are not translated to a new project that I am setting up even if it is under the same solution. How is it possible for the new project to inherit the project properties? Would be extremely grateful if this is possible.
Dhaval (not verified)
Monday, February 11, 2019 - 2:04pm
Permalink
I'm getting this error:
I'm getting this error:
LINK1104 cannot open file 'freeglut.lib'
me (not verified)
Friday, July 12, 2019 - 9:48pm
Permalink
IT WORKED!
IT WORKED!
The first time I didn't set the include directories right and got
cannot open source file "GL\glew.h"
But then I set the include directory of the project and it worked!
(visual studio 2019 glew 2.1.0 freeglut 3.0.0)
Add new comment