OpenGL Setup In VS2008

来源:互联网 发布:电脑定时重启软件 编辑:程序博客网 时间:2024/05/17 09:17

VS2008中配置OpenGL

原文地址:http://3dgep.com/?p=636;  原本没打算把原文整理并翻译,因为这些自己已经很熟悉,但是看到这么详细的介绍,就打算先转过来,有空的话再适当整理下,对于刚接触opengl的人来说还是很直接明了的。

In this article I will demonstrate a basic introduction in OpenGL. It will be in tutorial format that the reader can follow along on their own. The final result should be a working template that can be used to create your own projects using OpenGL.

 

History

OpenGL was introduced bySilicon Graphics Inc in 1992 and was used in various fields that required complex visualization of 2D and 3D images such asCAD,virtual reality,flight simulators, and scientific visualization. It was also very obvious to enthusiasts that OpenGL could be used to render graphics in video games as well.
In an effort from Silicon Graphics Inc (SGI) to get more vendors to produce software that would run on their workstations, SGI together withDigital Equipment Corporation, IBM, Intel, and Microsoft formed the OpenGL Architecture Review Board (ARB).  On July 1, 1992 version 1.0 of the OpenGL specification was released.
In 2006 SGI transfered control of the OpenGL standard from the ARB to a new working group withinThe Khronos Group.
OpenGL is both free and platform-independent making it the most widely used, supported, and best documented 2D and 3D graphics API.
So enough boring history, lets make something!

 

Prerequisite

The only prerequisite for developing OpenGL applications using the core OpenGL API is that you have a supported development environment such as Microsoft’s Visual Studio 2008 and that you have supported drivers from your graphics card manufacturer.  Installing Visual Studio 2008 will provide you with the headers and libs that are required to build your OpenGL applications, and your graphics vendor will provide the OpenGL DLL that is used at runtime and implements the OpenGL functionality.

 

GLUT

Optionally, you can use the OpenGL Utility Toolkit (GLUT) if you want to be able to create code that is portable to other platforms with the least amount of changes to your source.  You can obtain the source code for GLUT from the OpenGL website, or you can download the pre-built windows binaries from Nate Robin’s GLUT for Win32 website.
  • GLUT Source: http://www.opengl.org/resources/libraries/glut/
  • GLUT pre-built binaries for Win32: http://www.xmission.com/~nate/glut.html

 

GLEW or GLEE

If you plan on using extensions in your program, you can use either the OpenGL Extension Wrangler Library (GLEW), or The OpenGL Easy Extension Library (GLEE).
  • GLEW: http://glew.sourceforge.net/
  • GLEE: http://www.opengl.org/sdk/libs/GLee/

 

OpenGL Project

In this tutorial, I will show how to create a project in Visual Studio 2008 that can be used as a starting point for future OpenGL projects. I choose to use VS2008 because it is still a very widely used development environment and many samples and tutorials will still use VS2008 for their C++ projects.
We will start by creating a new project in Visual Studio 2008.
In Visual Studio 2008, create a new project by either going to the “File” menu and selecting “New” → ”Project…” or use the short-cut “Ctrl-Shift+N”:

From the “New Project” dialog box that appears, select “Visual C++\General” from the “Project types:” list and select the “Empty Project” template in the “Templates:” frame on the right.  Choose any name you want for your project (I will use “OpenGL Template”) and choose a location to store your new project.

Once you click “OK” on the “New Project” dialog, you will be presented with an empty solution which has only one project.

 

Creating the Source

Create a new source file called “main.cpp” in the OpenGL Template project by right-clicking the project node in the “Solution Explorer” and select “Add”→”New Item…”

You will be presented with the “Add New Item” dialog box.  Select “Visual C++\Code” from the “Categories:” list and select the “C++ File (.cpp)” from the “Templates:” frame.  Choose “main” for the name of the file to create.  You can choose to have the new file created in the default location which is the same location as that of the project file, but I always like to keep my compiled source files in a folder called “src”.

After you click the “Add” button, the “main.cpp” source file should be added to your project’s “Source Files” folder and opened in the editor.

 

Headers

We will first include a few headers into our main source code that we will be using in this example:

     main.cpp

  #define _USE_MATH_DEFINES   #include <math.h>   #include <iostream>   #include <ctime> 
We will also use GLUT in this project, so we will add the GLUT header as well:

     main.cpp

#include <GL/glut.h> 

 

If we decided to use GLUT, or any other 3rd party library that isn’t magically part of Visual Studios search paths for headers and libraries, then we must tell Visual Studio explicitly where it can find the headers and libraries that are needed to compile and link our program.  If you would like your project to be portable, this usually means that the 3rd party headers and libraries need to be included together with the source for your project.
In the case of GLUT, we will copy the header to a directory relative to our project folder called “include\GL” and the lib file to a directory in our project called “lib”.  Then we need to tell Visual Studio where to find these headers and libs.
Right-click on the project node in the solution explorer and select “Properties” from the pop-up menu that appears.
In the “Property Pages” template that appears, select “Configuration Properties\C/C++\General” from the list.

Project properties are always described relative to the location of the project file itself.  So directories to our additional include folder and additional library directories should also be described relative to our include folder.
In the “Additional Include Directories” text field, add “include” or whatever name you used for the directory that contains your header files.
Next, we need to specify the folder where our compiled libraries are located.
Select the “Linker\General” from the list view and in the “Additional Library Directories” text field, add “lib” or whatever name you used for the directory that contains your compiled library files.

And finally, we need to specify exactly what libs we want to link in our program.
Select the “Linker\Input” option in the list and in the “Additional Dependencies” text field add the name of the 3rd party libs we want to link in our program. In the case of GLUT, we would specify “glut32.lib“.

We could actually argue that the inclusion of this lib as an additional dependency issuperfluous because the library will be included automatically by the following code in the glut.h header file:
glut.h  #   pragma comment (lib, "opengl32.lib")  /* link with Microsoft OpenGL lib */ #   pragma comment (lib, "glu32.lib")     /* link with Microsoft OpenGL Utility lib */ #   pragma comment (lib, "glut32.lib")    /* link with Win32 GLUT lib */ 


 

So as long as your linker can find these libraries in the “Additional Include Directories”, these libs will be automatically linked into your final project without needing to specify them in the “Additional Dependencies” property.
And the final step is to copy the pre-compiled DLL file (glut32.dll) into the same folder where our binary will be generated for our program.  We usually specify a folder relative to our project file called “bin” that will be used to store the compiled result of our program as well as 3rd party compiled DLL’s.  In order for our program to find and load these DLL’s, we must be sure the DLL’s are in the default search paths for executable files.  When we run our program in Visual Studio 2008, by default the working folder is the location of the project file.  This is usually undesirable  because then the runtime will not find the DLL’s in our bin folder because the bin folder is not in the default search paths.  To resolve this we will modify the default working folder that is used when we run our program.
In the configuration properties page, select the “Configuration Properties\Debugging”. In the “Working Directory” text field, we will specify the special macro “$(OutDir)” as the working directory when we debug our program using the Visual Studio debugger.  This will also guarantee that the location of our executable and the 3rd party DLLs we placed there will be found in the default search paths when the runtime is looking for DLL’s to load.

It should be noted that the options specified in the “Debugging” properties are not saved with the project’s settings in the project file, instead these options are stored in the project’s “user settings” file (the file that sits next to the project file that looks like “ProjectName.vcproj.COMPUTERNAME.username.user” (where “COMPUTERNAME” is the name you specified for your computer and “username” is your username on this computer).  This user file is not something you generally package with your project files and source files because it is specific to this user on this computer.  What you can do, is make a copy of this file and remove the “COMPUTERNAME.username” part from the filename which would result in a file with the name “ProjectName.vcproj.user” and this file will be used as the default user file for all new users who will work on your project (and for you the next time you want to build and run your program after a fresh install of your operating system for example).  Visual Studio will automatically generate a new file for the user that is specific to that user on that computer based on the file that is called “ProjectName.vcproj.user”.  Anytime you make changes to the default settings in the debugging options, you will need to copy and rename the file again.  If you want other users to get the changes you specified in the “.user” file, they need to delete their existing “.COMPUTERNAME.username.user” file and Visual Studio will regenerate their user settings file based on the default “.user” file the next time they open the project.


 

Now that we have our project setup with all our headers, libs and DLL’s we can continue programming!



 

原创粉丝点击