Tuesday, September 22, 2015

How to Set up FreeGLUT on NetBeans IDE 8.1

I will show you how to setup FreeGLUT on NetBeans IDE 8.1 so that you can code OpenGL programs in C/C++ on Windows.

The first thing you need to do is install MinGW-w64, MSY2 (for GNU make.exe), and activate the C/C++ plugin. I already have a blog post with instruction how to do that here: How to Create a 64-bit C/C++ Windows Program with NetBeans 7.4, 8.0 or Higher. (Updated 04/18/2016) Note that this is to set up a 64-bit toolchain, but you can set up a 32-bit toolchain by using the 32-bit installations of MinGW and MSYS2. Although you can compile 32-bit programs with 64-bit MinGW, only the 64-bit glu32 library is included.  If your 32-bit program requires the glu32 library, you will need the 32-bit MinGW package.

FreeGLUT does not provide compiled libraries on their site, but one of the community developers offers the compiled library here: freeglut Windows Development Libraries. At the time of this writing, the latest package is freeglut-MinGW-3.0.0-1.mp.zip. This package works for building 64-bit or 32-bit programs. A 64-bit and 32-bit Windows dll (freeglut.dll) is included.
  1. Unzip the contents of the FreeGLUT zip file to a convenient location. In this example the path is "C:\Developer\mingw64\freeglut". The directory structure is show below:
FreeGLUT Directory Structure
  1. The project source code used in these instructions can be downloaded from here: Sphere Mapping Example.
  2. Extract the files to a folder named "FreeGlut-TeaPot".
  3. Delete the Makefile from the project folder because NetBeans will create it's own Makefile. There are other files you may delete from the folder, but it is not required. The following screenshot shows the required files for the project.
Required Project Files
  1. Next, create a C++ Application in NetBeans. Choose File >> New Project. Then under "Categories," choose "C/C++" and under "Projects," choose "C/C++ Application". See the following screenshot:
NetBeans 8.1 New Project Dialog Box
  1. Click "Next" and on the next dialog box and name the project the same name as the project folder from step 4. 
  2. Next, uncheck "Create Main File," since the project already has one. See the following screenshot.
NetBeans 8.1 New C/C++ Application Dialog Box
  1. Next you will add the source and header files to the NetBeans project's logical folders shown in the Project window. This is required so they are included in the project's classpath.  This is accomplished by right clicking on the "Source Files" folder and choosing "Add Existing Items...". Choose all files with "c" and "cpp" extensions. Do the same for the "Header Files" folder and add all the files that have an "h" extension. If you like, you can add the remaining image file to the "Resource Files" folder. The following screenshot shows what the project structure should look like.
NetBeans 8.1 FreeGlut-TeaPot Project Folder Structure
  1. You will now see project include errors because NetBeans does not know where the "glut.h" file is located. Right click on the project name in the Projects window and choose "Properties". Under Categories, click the "C Compiler" sub node and on the right you will see an "Include Directories" field. Click the "..." button and navigate to "C:\Developer\mingw64\freeglut\includes" and add the directory. Do the same for the C++ Compiler. See the following screenshot.
NetBeans 8.1 Project Properties: C++ Compiler
  1. Now NetBeans' code assistant will find the required header files and the project should not show any include errors. The code assistant will now be FreeGlut functional for this project only. 
  2. Next, choose the Linker sub nod and click the "..." button next to "Additional Library Directories". Navigate to "C:\Developer\mingw64\freeglut\lib\x64". (If you are using MinGW 32-bit, the 32-bit libraries are under the "\lib" folder.)
  3. On the same panel, enter the require linker options in "Additional Options". Since there is only one library folder, you can enter the linker options directly in the field, otherwise you would need to click the "..." button. Enter the options: -lfreeglut -lglu32 -lopengl32 -Wl,--subsystem,windows. See the following screenshot. 
NetBeans 8.1 Project Properties Dialog: Linker
  1. Choose the "Run" sub node and set the "Console Type" to "Standard Output". You may also set it to "External Terminal". 
  2. If you don't want the project to build every time you run it form NetBeans, uncheck "Build First". See the following screenshot.
NetBeans 8.1 Project Properties: Run
  1. Now you will be able to build the project. Right click on the project's name in the Project window and choose "Build". 
  2. After the project is done building, you should see "BUILD SUCCESSFUL..." in the Output window.
  3. Before you can run the project, you will need to make sure freeglut.dll is in your Windows system path or in the project's run directory. If you don't want to edit the Windows system path, you can put the dll in any folder that already in your system path or you can put it in the project's "/dist/release/MinGW-Windows" folder. (Note this folder does not exist until you build the project the first time.) Either way, make sure you are using the 64-bit dll for a 64-bit Windows program, and the 32-bit dll for a 32-bit program. 
  4. Now click on the project's name in the Project window and choose "Run". You should see a shiny tea pot as shown in the screenshot below.
OpenGL Sphere Mapping Example
You can zoom the tea pot by moving your mouse cursor left or right while simultaneously holding down the left and middle mouse button. You can spin the tea pot with your mouse cursor while holding down the left mouse button. You can also translate the tea pot by holding down the middle mouse button. (See the source code to reassign the buttons.)

For more information about using FreeGLUT, MSYS2 and MinGW-w64, please visit the following URLs:

4 comments:

  1. Thank you!

    After searching for hours, this is the best guide I've found on how to set up How to Set up FreeGLUT on NetBeans IDE 8.1. Really grateful for the pictures too.

    It solved my problem of how to make NetBeans find the file.

    ReplyDelete
  2. My experience was similar to that of June 2017. I read through several how-to posts, but this one worked! Thanks.

    ReplyDelete