NVIDIA Cg programs with Xcode on Mac

Problem Description

While trying to follow along with the NVIDIA CG Tutorial, I was not able to find a way to compile and run the files that were provided along with the CG Toolkit, which is available for download at NVIDIA’s website. This made the tutorial difficult to follow along with. I’m aware that Cg has been discontinued by NVIDIA, but Unity Game Engine is still using it for it’s shader programming. So I wanted to learn more about Cg in order write shaders for Unity.

Problem Source

The source of the problem is that neither the NVIDIA CG Tutorial nor the web describes properly how to install and setup CG toolkit so that one can compile and run CG programs using Xcode.

Problem Solution

In this blog, I will give a step by step description of the following:

  1. Installing CG Toolkit on the Mac.
  2. Finding the framework and tutorial example files installed together with CG Toolkit.
  3. Creating and setting up a Xcode project for CG programs.
  4. Compiling and running the CG Tutorial examples using Xcode.

Installing CG Toolkit on Mac

To install the CG Toolkit just follow this link. Download the latest release. Once the .pkg file is downloaded, launch it and follow the instructions to run the install script.

Location of Framework and Tutorial example files

Once the installation is complete you will need to remember two important locations containing CG files that you will need in order to follow along with the CG tutorials (it is kind of frustrating that the download doesn’t contain any information regarding where the important files are installed). The two locations are given below for the Mac:

  1. Location of CG Framework: /Library/Frameworks
  2. Location of Example files to follow along with the NVIDIA CG Tutorials: /Developer/NVIDIA/Cg/examples/OpenGL/basic

Creating and Setting up Xcode project

  1. Start Xcode and create a Command Line Tool Project.

    Creating Xcode CG Project
    Create Xcode CG project which is going to be a Command Line Tool project.
  2. Choose C as the Language, an arbitary location with an arbitary project name.

    Choosing Programming Language
    Choosing the programming language, location of the project and the project name.
  3. Delete the main.c file from the project. Now the project should be empty.
  4. Next, select project in the Project Navigation window and choose the single available target.

    Configuring Project Build Phases
    Go to Build Phases
  5. Go to Build phases and add Cocao Framework and Cg Framework under Link Binary with Libraries. The Cg Framework can be found at the location provided in the section above.

    Add the CG and Cocoa frameworks to the Build Phases
    Add the Cg and Cocoa frameworks to the Build Phases
  6. Next, navigate to the location of the example files (the location of the example files is provided in the section above). I chose to import the 06_vertex_twisting project files because I only need to import 3 files. One file is the C OpenGL code, the other two are vertex and fragment functions written in .cg files (If you choose to import other projects, double check that you know which are all the required files). Select all the required files and drag them to the Project Navigation window of Xcode.

    Importing Cg Project Files
    Select the required project files and drag them to the Project Navigation window of Xcode.
  7. Once the required files have been dragged to the Xcode Project Navigation window, you will be presented with a dialog. Check the Copy items if needed checkbox and make sure that the checkbox for Add to targets is checked.

    Importing Files Xcode Dialog
    Importing Files Xcode Dialog with the correct options chosen.
  8. Once the files have been imported your Project Navigator window should look something like the screenshot below.

    Project Navigator WIndow after import
    Project Navigator WIndow after import
  9. Now select the .c file so that the code is revealed. Try now to build the program. The first error you will get is regarding GL/glew.h. For Mac, you need to make some changes to the code, in order to be able to compile it for Mac. The necessary changes are marked with a comment starting with CHANGE (in capital letters) in the listing below of the 06_vertex_twisting.c file.

  10. After making the necessary changes as described in the previous step. You can now try to build the project again. This time, the project builds successfully with only some warnings about glut functions being deprecated. You can safely ignore these warnings, if the purpose is only to be able to run the example projects. Of course if you are an expert in OpenGL programming you can most probably make the warnings go away. I’m not at all accustomed to OpenGL, so I just chose to ignore the warnings.
  11. Before running the program, you need to take one more step. If you run the program now, you will get an error message in the console window of Xcode saying, The file could not be read. This error occurs because the c program is not able to find the .cg files. If you have a look at lines 49 and 51 of the source code provided above, you will notice that we are providing the program with cg file names. Either you need to give the absolute path to the files on line 49 and 51 or you make use of a better solution i.e. to let Xcode bundle .cg files with the executable. We go back to our notorious Build Phases again. Under Build Phases, you will find a section called Copy Files. Add the .Cg files to this section and set it up according to the screenshot below.

    Configure Xcode to bundle the .cg files together with the executable.
    Configure Xcode to bundle the .cg files together with the executable.
  12. Now you are ready to rebuild your program and run it. Voila! Now you should be presented with a window similar to the one near the heading of this post.

Note that if you still get an error,”The file could not be read”. Then make sure that you rebuild your program before running it.

Please leave a comment and share the post if you found it useful! Thanks!