游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2115|回复: 2

小菜鸟在OpenGL里学写第一个cg

[复制链接]

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2005-6-22 13:44:00 | 显示全部楼层 |阅读模式
安好cg toolkit
就能在include 里找到header files, library files啦,别忘了include 和link它们
bin里的dll文件,丢到运行的文件夹使得运行文件可以link到, 当然罗, windows专用, windows rocks! 哈



  1. /**main.cpp*/
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. /**include cg headers*/
  5. #include <Cg/cg.h>
  6. #include <Cg/cgGL.h>

  7. #include <gl/glut.h>

  8. /**retain program reference for releasing*/
  9. CGprogram program;
  10. /**retain context reference for releasing*/
  11. CGcontext context;

  12. /**
  13. display callback function
  14. */
  15. void display(void)
  16. {
  17.    glClear (GL_COLOR_BUFFER_BIT);/* clear all pixels  */
  18.    glColor3f (1.0, 1.0, 1.0);
  19.    glBegin(GL_POLYGON);/* draw white polygon with corners at(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)*/
  20.       glVertex2f (0.25, 0.25);
  21.       glVertex2f (0.75, 0.25);
  22.       glVertex2f (0.75, 0.75);
  23.       glVertex2f (0.25, 0.75);
  24.    glEnd();
  25.    glFlush ();/* start processing buffered OpenGL routines  */
  26. }

  27. /**
  28. output cg errors
  29. */
  30. static void CheckCgError(int no)
  31. {
  32.   CGerror err = cgGetError();

  33.   if (err != CG_NO_ERROR)
  34.    {
  35.            //output error number
  36.            printf("%d: ",no);
  37.      printf("CG error: %s\n", cgGetErrorString(err));

  38.    }
  39. }

  40. void init (void)
  41. {
  42.         //create cg context
  43.         context=cgCreateContext();
  44.         CheckCgError(1);
  45.         //get a valid profile
  46.         CGprofile vertexProfile;
  47.         if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1)){
  48.                 //store this profile
  49.                 vertexProfile=CG_PROFILE_ARBVP1;
  50.                 CheckCgError(2);
  51.        
  52.                 //get cg source code from a file named "MYVP1.cg", its entry function is "program1"
  53.                 program=cgCreateProgramFromFile(context, CG_SOURCE,"MYVP1.cg",
  54.                                                                                                 vertexProfile, "program1",NULL);
  55.                 CheckCgError(3);
  56.                 //load this program
  57.                 cgGLLoadProgram(program);
  58.                 CheckCgError(4);
  59.                 //output the compiled information
  60.                 printf("---- PROGRAM BEGIN ----\n%s---- PROGRAM END ----\n",
  61.                         cgGetProgramString(program, CG_COMPILED_PROGRAM));
  62.                 //enable the profile
  63.                 cgGLEnableProfile(vertexProfile);
  64.                 //bind the program
  65.                 cgGLBindProgram(program);
  66.         }
  67.        

  68.    glClearColor (0.0, 0.0, 0.0, 0.0);/* select clearing color  */
  69.    glMatrixMode(GL_PROJECTION);
  70.    glLoadIdentity();
  71.    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);/* initialize viewing values  */
  72. }


  73. //release cg resource before end of program
  74. void destoryApp(){
  75.         cgDestroyProgram(program);
  76.         cgDestroyContext(context);
  77. }

  78. int main(int argc, char** argv)
  79. {
  80.    glutInit(&argc, argv);
  81.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/*Declare initial display mode(single buffer and RGBA).*/
  82.    glutInitWindowSize (250, 250); /*Declare initial window size.*/
  83.    glutInitWindowPosition (100, 100);/*Declare initial window position.*/
  84.    glutCreateWindow ("hello");/*Open window with "hello"in its title bar.*/  
  85.    init ();/*Call initialization routines.*/
  86.    glutDisplayFunc(display); /*Register callback function to display graphics.*/
  87.    glutMainLoop();/*Enter main loop and process events.*/

  88.                
  89.         destoryApp();
  90.    return 0;   /* ANSI C requires main to return int. */
  91. }













  92. //MYVP1.cg
  93. struct Output
  94. {
  95.     float4 position : POSITION;
  96.     float4 color : COLOR;
  97. };

  98. Output program1(float2 position : POSITION)
  99. {
  100.     Output OUT;

  101.     OUT.position = float4(position,0,1);
  102.     if(position.x>0.5)
  103.         OUT.color = float4(0,1,0,1);
  104.     else
  105.         OUT.color = float4(1,0,0,1);

  106.     return OUT;
  107. } // main
复制代码

139

主题

2005

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
QQ
发表于 2005-6-22 18:04:00 | 显示全部楼层

Re:小菜鸟在OpenGL里学写第一个cg

强……

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2005-6-22 18:15:00 | 显示全部楼层

Re: 小菜鸟在OpenGL里学写第一个cg

寒 [em22] [em22]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-26 03:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表