Setting Up a Cg Project In Visual Studio


This article explains how to set up a project in Microsoft Visual Studio 6.0, which allows you to easily write, compile, and integrate Cg shaders with your applications. These steps were only tested under Windows XP but may work under other versions of Windows.

The following steps assume that you have created a workspace and project in Microsoft Visual Studio 6.0.

Adding the Cg Compiler Environment Variable

To allow your system to know where the Cg compiler resides, you need to set up an environment variable in Windows XP, using the following steps:

1. Click Start -> Settings -> Control Panel
2. Double click the System icon
3. Select the Advanced tab
4. Click the Environment Variables button
5. Click the New button in the System variables area
6. Type CG_COMPILER_EXE in the Variable name textbox
7. Type c:\path_to_compiler\cgc.exe in the Variable value textbox



Adding the Cg Include Directory

Next, you have to set up your Visual Studio workspace. The first thing you have to do is to add the path to the CgGL and/or CgD3D include files, using the following steps:

1. Click Project -> Settings and select the C/C++ tab
2. Select All Configurations from the Settings For drop-down list box
3. Select Preprocessor from the Category drop-down list box
4. Add Cg-runtime\Cg\include to the Additional include directories textbox (separate each directory in your list with a comma)
 



Adding the Cg Static Library Paths

Now you have to add the CgGL and/or CgD3D static libraries to your project's linker settings, using the following steps:

1. Click Project -> Settings and select the Link tab
2. Select Win32 Release from the Settings For drop-down list box
3. If you are using OpenGL, add CgGL.lib to the list of libraries in the Object/library modules text field
4. If you are using Direct3D, add CgD3D.lib to the list of libraries in the Object/library modules text field
5. Select Input from the Category drop-down list box
6. If you are using OpenGL, add cg-runtime\cg\cgGL\Release to the Additional library path textbox (separate each item with a comma)
7. If you are using Direct3D, add cg-runtime\cg\cgD3D\Release to the Additional library path textbox (separate each item with a comma)
8. Select Win32 Debug from the Settings For drop-down list box
9. If you are using OpenGL, add cg-runtime\cg\cgGL\Debug to the Additional library path textbox (separate each item with a comma)
10. If you are using Direct3D, add cg-runtime\cg\cgD3D\Debug to the Additional library path textbox (separate each item with a comma)
 



Creating a Folder for your Cg Shaders

Next, you need to create a folder for all your Cg shaders. To do this:

1. Select File View in the Workspace pane on the left side of Visual Studio
2. Right click on your project and select New Folder... in the popup menu
3. Set the name of the folder to Cg Shaders and the extension to .cg and click OK
4. Right click on the newly created folder and select Add Files to Folder... from the popup menu
5. Add any Cg shaders that you have into the folder.
 



Add a Custom Build Step for your Cg Shaders

A Custom Build Step allows you to use a specific tool to build your Cg shaders. In this case, we're going to use the Cg compiler, cgc.exe. The following are the steps you have to take to accomplish this:

1. Right click on a shader in the Cg Shaders folder created in the last step and select Settings from the popup menu
2. Select All Configurations from the Settings For drop-down list box
3. Add cgc $(InputPath) -o $(InputName).vp -profile vp20 to the Commands textbox
4. Add $(InputName).vp to the Outputs textbox
 



Syntax Highlighting

To help to increase the readability of your code, you can turn on syntax highlighting when editing your Cg files in Visual Studio 6.0. Here's how:

1. Copy usertype.dat (included on the Cg CD) to the Visual Studio bin directory (typically C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin
2. Start regedit (Start -> Run -> regedit) and go to HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Text Editor\Tabs/Language Settings\C/C++
3. Add cg to the end of the FileExtensions key (each extension in the list should be separated with a semicolon)
4. Restart Visual Studio

Your shaders should now have syntax highlighting.