天天看點

第一章 在VS2008下如何配置好CG環境

第一步:去官網上下載下傳Cg ToolKit并安裝

https://developer.nvidia.com/cg-toolkit-download

寫此文時,最新版本是

Cg Toolkit 3.1 - April 2012 (3.1.0013)

  • Windows 32/64-bit
    • installer for Windows XP, Vista and Win7.
  • Mac OS X ppc/i386/x86_64
    • dmg for Tiger, Leopard and Snow Leopard.
  • Linux 32-bit
    • tgz tarball
    • rpm for RedHat
    • deb for Debian and Ubuntu.
  • Linux 64-bit
    • tgz tarball
    • rpm for RedHat
    • deb for Debian and Ubuntu.

選擇适合自己的作業系統,下載下傳好了,就安裝吧!

我的安裝路徑D:\Program Files\NVIDIA Corporation\Cg

第二步:設定路徑

1.建立使用者變量

變量:CG_HOME

變量值:D:\Program Files\NVIDIA Corporation\Cg

2.在path路徑添加:D:\Program Files\NVIDIA Corporation\Cg\bin;

3.VS2008工具-選項-項目和解決方案-VC++目錄

包含檔案目錄添加:$(CG_HOME)\include

庫檔案目錄添加:$(CG_HOME)\lib

第三步:嘗試在OPENGL裡面使用(因為DX暫時沒學。。。)

1.先打開D:\Program Files\NVIDIA Corporation\Cg\examples\OpenGL\basic\01_vertex_program檔案夾。你可以看下裡面的内容,這就是我們程式所需要的一些檔案,這是書上的例子

2.建立-項目(我這裡項目名是The_first_Cg_program),這裡選擇Win32控制台應用程式

3.項目-屬性-配置屬性-連結器-輸入-附加依賴項 ,寫上cg.lib cgGl.lib

4.建立源檔案(我這裡源檔案名是main.cpp),将先前已打開的檔案夾下面的01_vertex_program.c裡面檔案内容複制到源檔案中

5.需要将cg源檔案複制到項目目錄下,将先前打開的檔案夾下面的C2E1v_green.cg複制到你的項目檔案夾(我這裡是E:\《The Cg Turorial》\chap1\The_first_Cg_program\The_first_Cg_program)。

另外補充說明,其實這樣就可以執行了,但是了友善的編輯Cg源檔案,這裡強烈推薦這樣做

找到解決方案資料總管,右鍵單擊The_first_Cg_program項目,添加-建立篩選器,命名為Cg,右鍵點選Cg檔案夾,添加-現有項,選擇剛才拷貝過來的C2E1v_green.cg,這樣以後就可以直接在vs2008裡面編輯Cg源檔案了

第一章 在VS2008下如何配置好CG環境

6.OK,編譯執行。綠色三角形終于出來了,結果如下圖:

第一章 在VS2008下如何配置好CG環境

最後,為了友善那些伸手黨,我給main.cpp和C2E1v_green.cg的源碼貼上

main.cpp檔案

/* 01_vertex_program.c - OpenGL-based very simple vertex program example
   using Cg program from Chapter 2 of "The Cg Tutorial" (Addison-Wesley,
   ISBN 0321194969). */

/* Requires the OpenGL Utility Toolkit (GLUT) and Cg runtime (version
   1.0 or higher). */

#include <stdio.h>    /* for printf and NULL */
#include <stdlib.h>   /* for exit */
#if __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <Cg/cg.h>    /* Can't include this?  Is Cg Toolkit installed! */
#include <Cg/cgGL.h>

static CGcontext   myCgContext;
static CGprofile   myCgVertexProfile;
static CGprogram   myCgVertexProgram;

static const char *myProgramName = "01_vertex_program",
                  *myVertexProgramFileName = "C2E1v_green.cg",
/* Page 38 */     *myVertexProgramName = "C2E1v_green";

static void checkForCgError(const char *situation)
{
  CGerror error;
  const char *string = cgGetLastErrorString(&error);

  if (error != CG_NO_ERROR) {
    printf("%s: %s: %s\n",
      myProgramName, situation, string);
    if (error == CG_COMPILER_ERROR) {
      printf("%s\n", cgGetLastListing(myCgContext));
    }
    exit(1);
  }
}

/* Forward declared GLUT callbacks registered by main. */
static void display(void);
static void keyboard(unsigned char c, int x, int y);

int main(int argc, char **argv)
{
  glutInitWindowSize(400, 400);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInit(&argc, argv);

  glutCreateWindow(myProgramName);
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

  glClearColor(0.1, 0.3, 0.6, 0.0);  /* Blue background */

  myCgContext = cgCreateContext();
  checkForCgError("creating context");
  cgGLSetDebugMode(CG_FALSE);
  cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);

  myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
  cgGLSetOptimalOptions(myCgVertexProfile);
  checkForCgError("selecting vertex profile");

  myCgVertexProgram =
    cgCreateProgramFromFile(
      myCgContext,              /* Cg runtime context */
      CG_SOURCE,                /* Program in human-readable form */
      myVertexProgramFileName,  /* Name of file containing program */
      myCgVertexProfile,        /* Profile: OpenGL ARB vertex program */
      myVertexProgramName,      /* Entry function name */
      NULL);                    /* No extra compiler options */
  checkForCgError("creating vertex program from file");
  cgGLLoadProgram(myCgVertexProgram);
  checkForCgError("loading vertex program");

  glutMainLoop();
  return 0;
}

static void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  cgGLBindProgram(myCgVertexProgram);
  checkForCgError("binding vertex program");

  cgGLEnableProfile(myCgVertexProfile);
  checkForCgError("enabling vertex profile");

  /* Rendering code verbatim from Chapter 1, Section 2.4.1 "Rendering
     a Triangle with OpenGL" (page 57). */
  glBegin(GL_TRIANGLES);
    glVertex2f(-0.8, 0.8);
    glVertex2f(0.8, 0.8);
    glVertex2f(0.0, -0.8);
  glEnd();

  cgGLDisableProfile(myCgVertexProfile);
  checkForCgError("disabling vertex profile");

  glutSwapBuffers();
}

static void keyboard(unsigned char c, int x, int y)
{
  switch (c) {
  case 27:  /* Esc key */
    /* Demonstrate proper deallocation of Cg runtime data structures.
       Not strictly necessary if we are simply going to exit. */
    cgDestroyProgram(myCgVertexProgram);
    cgDestroyContext(myCgContext);
    exit(0);
    break;
  }
}
           

C2E1v_green.cg源碼,注意這裡隻用了頂點着色器

// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 38.

struct C2E1v_Output {
  float4 position : POSITION;
  float3 color    : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)
{	
  C2E1v_Output OUT;

  OUT.position = float4(position,0,1);
  OUT.color = float3(0,1,0);

  return OUT;	
}