Preparing the development environment
Before we create a new console project in Visual Studio, we are first going to download some third-party dependencies for convenience; otherwise we will have to implement the construction of the OpenGL context completely by ourselves, and some important but complex functionality such as image reading and displaying. To make this process less lengthy, we will choose only two external libraries here:
- FreeGLUT
- FreeImage
Both of them are open source and easy to understand.
External libraries will be used as dependencies for each of our C++ projects, so we can make use of the OpenGL-based drawing and image loading functions, which are important for future work.
FreeGLUT is a complete alternative to the famous OpenGL Utility Toolkit (GLUT), allowing developers to create and manage OpenGL contexts with just a few functions and readable callbacks. Its official website is:
http://freeglut.sourceforge.net/
But, we can directly download the prebuilt Windows package containing the DLL, import library, and header files from:
http://files.transmissionzero.co.uk/software/development/GLUT/freeglut-MSVC.zip
Then unzip the downloaded file to a proper path for later use.
FreeImage is a fast and stable library for reading and writing to many popular graphics image formats. It also provides basic image manipulations such as rotating, resizing, and color adjustment. Its official website is:
http://freeimage.sourceforge.net/
The prebuilt Windows package is located at:
http://downloads.sourceforge.net/freeimage/FreeImage3153Win32.zip
Download and unzip it too. Find the DLLs, library files, and headers of both, and place them in separate subdirectories (for example, bin
, lib
, and include
), so that we can manage and use these dependencies efficiently, as shown in the following screenshot:
Other dependencies include OpenGL and the Kinect SDK. The OpenGL library is automatically integrated with every Visual Studio project, so we don't have to worry about the installation.
From now on, the variable ${MYPROJECT_DIR}
will be used to indicate the project folder, which contains both project files and all third-party dependencies, and ${KINECTSDK10_DIR}
is used to indicate the location of the Microsoft Kinect SDK. You can either set these two environment variables in the Windows system, or replace it manually with the actual paths while setting project properties.
Building the Visual Studio project
Now, we are going to create our first application with Visual Studio. Please note that the Microsoft Kinect SDK is only for Windows users. If you want to develop Kinect-based applications on other platforms, you may prefer OpenNI instead, which is also introduced in the previous chapter of this book.
- Create a new C++ console project by navigating to File | New | Project and choose Win32 Console Application from the Visual C++ menu. Set the project name to
FirstProgram
or any name you like. Select Empty project in the next dialog and click on OK. - Add include directories by right-clicking on the project and selecting Properties. Navigate to C/C++ | General | Additional Include Directories and input the following dependency paths:
${MYPROJECT_DIR}/include; ${KINECTSDK10_DIR}/inc
- Navigate to Linker | Input | Additional Dependencies in the Property page. Input the library paths and names as follows:
opengl32.lib; glu32.lib; ${MYPROJECT_DIR}/lib/freeglut.lib; ${MYPROJECT_DIR}/lib/FreeImage.lib; ${KINECTSDK10_DIR}/lib/x86/Kinect10.lib
Note
For X64 configuration, we should use
${KINECTSDK10_DIR}/lib/amd64/Kinect10.lib
instead, and make sure the FreeGLUT and FreeImage libraries are also built for X64 systems (you may have to build them from the source code). - Now add some initial code to make our first program work.
#include <GL/freeglut.h> #include <iostream> // The updating callback void update() { glutPostRedisplay(); } // The rendering callback void render() { glutSwapBuffers(); } // The window resizing callback void reshape( int w, int h ) { glViewport( 0, 0, w, h ); } // The keyboard callback: make sure we can exit when press // Esc or 'Q' key. void keyEvents( unsigned char key, int x, int y ) { switch ( key ) { case 27: case 'Q': case 'q': glutLeaveMainLoop(); return; } glutPostRedisplay(); } int main( int argc, char** argv ) { // Initialize a GLUT window and make it full-screen glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH|GLUT_MULTISAMPLE ); glutCreateWindow( "ch2_01_OpenGL_Env" ); glutFullScreen(); // Register necessary callbacks glutIdleFunc( update ); glutDisplayFunc( render ); glutReshapeFunc( reshape ); glutKeyboardFunc( keyEvents ); // Start the main loop glutMainLoop(); return 0; }
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
- The source code may be a little long for beginners to read. But this is exactly the structure we are going to use in the entire book. The updating of Kinect and user data will be done in
update()
. The rendering of color/depth images and the skeleton, as well as other virtual objects will be done inrender()
. - It's still a little too complicated and too redundant to write all the source code for loading textures with FreeImage, or drawing meshes with OpenGL vertex array features. Fortunately, we have already provided some useful functions for immediate use.
- Drag the files in the
common
folder (from the downloaded package) onto your project icon. Now you should have two more source files namedGLUtilities.cpp
andTextureManager.cpp
. - Compile and build all the source code into an executable file. Put the output into
${MYPROJECT_DIR}/bin
and run it. You will only see a black screen, which we can quit by pressing the Esc key.
Here, we just create a standard console application with FreeGLUT, FreeImage, and Kinect SDK as dependencies. The framework we created cannot do anything at present but we will soon add something interesting to make it more colorful.
Please note that the added source files GLUtilities.cpp
and TextureManager.cpp
are not used. But they will play an important role in the following examples to render all kinds of textures and geometries.
Note
If you are still not familiar enough with the OpenGL API, or want to do something interesting before stepping into Kinect programming, there are some good references for you to read and test at:
- The OpenGL official website: http://www.opengl.org/
- The OpenGL wiki page: http://en.wikipedia.org/wiki/OpenGL
- The NeHe OpenGL examples: http://nehe.gamedev.net/ (good for exercising; see the Legacy Tutorials section)