/* 25_uniform_fog.c - OpenGL-based example demonstrating uniform
exponential fog based on radial vertex distance using Cg programs from
Chapter 9 of "The Cg Tutorial" (Addison-Wesley, ISBN 0321194969). */
/* Requires the OpenGL Utility Toolkit (GLUT) and Cg runtime (version
1.5 or higher). */
#include <assert.h> /* for assert */
#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>
#include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#include <GL/glext.h>
#endif
#include <Cg/cg.h> /* Can't include this? Is Cg Toolkit installed! */
#include <Cg/cgGL.h>
#include "matrix.h"
static CGcontext myCgContext;
static CGprofile myCgVertexProfile,
myCgFragmentProfile;
static CGprogram myCgVertexProgram,
myCgFragmentProgram;
static CGparameter myCgVertexParam_modelViewProj,
myCgVertexParam_modelView,
myCgVertexParam_fogDensity,
myCgFragmentParam_fogColor,
myCgFragmentParam_decal;
static const char *myProgramName = "25_uniform_fog",
*myVertexProgramFileName = "C9E2v_fog.cg",
/* Page 240 */ *myVertexProgramName = "C9E2v_fog",
*myFragmentProgramFileName = "C9E1f_fog.cg",
/* Page 240 */ *myFragmentProgramName = "C9E1f_fog";
static float myProjectionMatrix[16];
static float eyeHeight = 30.0f; /* Vertical height of light. */
static float eyeAngle = 0.53f; /* Angle in radians eye rotates around knight. */
static float fogDensity = 0.08f;
static float fogColor[3] = { 0.8, 0.9, 0.8 }; /* Green-ish gray */
static int city_height_mode = 0;
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 reshape(int width, int height);
static void display(void);
static void menu(int item);
static void keyboard(unsigned char c, int x, int y);
static void mouse(int button, int state, int x, int y);
static void motion(int x, int y);
/* Other forward declared functions. */
static void loadDecalFromDDS(const char *filename);
/* Use enum to assign unique symbolic OpenGL texture names. */
enum {
TO_BOGUS = 0,
TO_SIDES,
TO_ROOF,
TO_PAVEMENT,
};
int supports_texture_anisotropy = 0;
int main(int argc, char **argv)
{
glutInitWindowSize(800, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow(myProgramName);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
/* Clear to fog color */
glClearColor(fogColor[0], fogColor[1], fogColor[2], 1.0);
glEnable(GL_DEPTH_TEST);
supports_texture_anisotropy = glutExtensionSupported("GL_EXT_texture_filter_anisotropic");
myCgContext = cgCreateContext();
checkForCgError("creating context");
cgGLSetDebugMode(CG_FALSE);
//cgGLSetManageTextureParameters(myCgContext, CG_TRUE);
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");
#define GET_PARAM(name) \
myCgVertexParam_##name = \
cgGetNamedParameter(myCgVertexProgram, #name); \
checkForCgError("could not get " #name " parameter");
GET_PARAM(fogDensity);
GET_PARAM(modelViewProj);
GET_PARAM(modelView);
cgSetParameter1f(myCgVertexParam_fogDensity, fogDensity);
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");
myCgFragmentParam_fogColor =
cgGetNamedParameter(myCgFragmentProgram, "fogColor");
checkForCgError("could not get fogColor parameter");
cgSetParameter3fv(myCgFragmentParam_fogColor, fogColor);
myCgFragmentParam_decal =
cgGetNamedParameter(myCgFragmentProgram, "decal");
checkForCgError("getting decal parameter");
cgGLSetTextureParameter(myCgFragmentParam_decal, TO_SIDES);
checkForCgError("setting decal 2D texture");
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#define GL_CLAMP_TO_EDGE 0x812F
/* Load the decal map the fragment program will sample. */
glBindTexture(GL_TEXTURE_2D, TO_SIDES);
loadDecalFromDDS("BuildingWindows.dds");
if (supports_texture_anisotropy) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0);
}
glBindTexture(GL_TEXTURE_2D, TO_ROOF);
loadDecalFromDDS("BuildingRoof.dds");
if (supports_texture_anisotropy) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, TO_PAVEMENT);
loadDecalFromDDS("Pavement.dds");
if (supports_texture_anisotropy) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0);
}
/* Create GLUT menu. */
glutCreateMenu(menu);
glutAddMenuEntry("[ ] Cycle city configuration", ' ');
glutAddMenuEntry("[+] Increase fog density", ' ');
glutAddMenuEntry("[-] Decrease fog density", ' ');
glutAddMenuEntry("[w] Toggle wireframe", 'w');
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
static void reshape(int width, int height)
{
double aspectRatio = (float) width / (float) height;
double fieldOfView = 40.0; /* Degrees */
/* Build projection matrix once. */
makePerspectiveMatrix(fieldOfView, aspectRatio,
1.0, 500.0, /* Znear and Zfar */
myProjectionMatrix);
glViewport(0, 0, width, height);
}
/** Simple image loaders for DirectX's DirectDraw Surface (DDS) format **/
/* Structure matching the Microsoft's "DDS File Reference" documentation. */
typedef struct {
int magic; /* must be "DDS\0" */
int size; /* must be 124 */
int flags;
int height;
int width;
int pitchOrLinearSize;
int depth;
int mipMapCount;
int reserved[11];
struct {
int size;
int flags;
int fourCC;
int bitsPerPixel;
int redMask;
int greenMask;
int blueMask;
int
cg for fog program
需积分: 0 174 浏览量
2008-10-30
11:38:04
上传
评论
收藏 127KB RAR 举报
yyb12006
- 粉丝: 0
- 资源: 24
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0