/* 02_vertex_and_fragment_program.c - OpenGL-based example using a Cg
vertex and a Cg fragment programs 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 */
#include <math.h> /* for sin and cos */
#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,
myCgFragmentProfile;
static CGprogram myCgVertexProgram,
myCgFragmentProgram;
static const char *myProgramName = "02_vertex_and_fragment_program",
*myVertexProgramFileName = "C2E1v_green.cg",
/* Page 38 */ *myVertexProgramName = "C2E1v_green",
*myFragmentProgramFileName = "C2E2f_passthru.cg",
/* Page 53 */ *myFragmentProgramName = "C2E2f_passthru";
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");
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");
myCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(myCgFragmentProfile);
checkForCgError("selecting fragment profile");
myCgFragmentProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myFragmentProgramFileName, /* Name of file containing program */
myCgFragmentProfile, /* Profile: OpenGL ARB vertex program */
myFragmentProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating fragment program from file");
cgGLLoadProgram(myCgFragmentProgram);
checkForCgError("loading fragment program");
glutMainLoop();
return 0;
}
static void drawStar(float x, float y,
int starPoints, float R, float r)
{
int i;
double piOverStarPoints = 3.14159 / starPoints,
angle = 0.0;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); /* Center of star */
/* Emit exterior vertices for star's points. */
for (i=0; i<starPoints; i++) {
glVertex2f(x + R*cos(angle), y + R*sin(angle));
angle += piOverStarPoints;
glVertex2f(x + r*cos(angle), y + r*sin(angle));
angle += piOverStarPoints;
}
/* End by repeating first exterior vertex of star. */
angle = 0;
glVertex2f(x + R*cos(angle), y + R*sin(angle));
glEnd();
}
static void drawStars(void)
{
/* star outer inner */
/* x y Points radius radius */
/* ===== ===== ====== ====== ====== */
drawStar(-0.1, 0, 5, 0.5, 0.2);
drawStar(-0.84, 0.1, 5, 0.3, 0.12);
drawStar( 0.92, -0.5, 5, 0.25, 0.11);
drawStar( 0.3, 0.97, 5, 0.3, 0.1);
drawStar( 0.94, 0.3, 5, 0.5, 0.2);
drawStar(-0.97, -0.8, 5, 0.6, 0.2);
}
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");
cgGLBindProgram(myCgFragmentProgram);
checkForCgError("binding fragment program");
cgGLEnableProfile(myCgFragmentProfile);
checkForCgError("enabling fragment profile");
drawStars();
cgGLDisableProfile(myCgVertexProfile);
checkForCgError("disabling vertex profile");
cgGLDisableProfile(myCgFragmentProfile);
checkForCgError("disabling fragment 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);
cgDestroyProgram(myCgFragmentProgram);
cgDestroyContext(myCgContext);
exit(0);
break;
}
}
vertex_and_fragment_program.rar_Cg
版权申诉
18 浏览量
2022-09-21
03:44:44
上传
评论
收藏 28KB RAR 举报
小贝德罗
- 粉丝: 69
- 资源: 1万+
最新资源
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
- Apache Flink 未授权访问+远程代码执行.pdf
- hadoop-基于hive的聊天数据分析报表可视化案例数据源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈