⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.c

📁 计算机图形学~想必是很多人需要的~在此共享一下
💻 C
📖 第 1 页 / 共 2 页
字号:
/* demo.c   */
/* Rotating cube demo*/
/* demonstrates many OpenGL features including */
/* smooth shading, antialiasing, texture mapping, */
/* color interpolation */

/* uses a trackball interface */

/* E. Angel, Interactive Computer Graphics */
/* A Top-Down Approach with OpenGL, Third Edition */
/* Addison-Wesley Longman, 2003 */
/* Prints out instructions *//*Both normals and colors are assigned to the vertices *//*Cube is centered at origin so (unnormalized) normalsare the same as the vertex values */#include <stdlib.h>#include <math.h>#include <GL/glut.h>#include <GL/glu.h>#define bool int#define false 0#define true 1
#define M_PI 3.14159
/*** Global settings controlled by keyboard input.*/bool texEnabled 		= false;bool mipmapEnabled 		= false;bool fastTexture 		= true;bool fogEnabled 		= false;bool depthEnabled 		= true;bool lineAAEnabled 		= false;bool lightingEnabled 		= false;bool smoothEnabled 		= false;bool drawLines 			= true;bool idleSpin 			= true;bool perspectiveXform 		= false;/*** Global settings.*/float 	nnear = 3.0;    	/* near clipping plane in eye coords */float 	nfar = 7.0;    	/* far clipping plane in eye coords */float 	viewxform_z = -5.0;int 	winWidth, winHeight;float 	angle = 0.0, axis[3], trans[3];bool 	trackballEnabled = true;bool 	trackballMove = false;bool 	trackingMouse = false;bool 	redrawContinue = false;GLfloat lightXform[4][4] = {    {1.0, 0.0, 0.0, 0.0},    {0.0, 1.0, 0.0, 0.0},    {0.0, 0.0, 1.0, 0.0},    {0.0, 0.0, 0.0, 1.0}};GLfloat objectXform[4][4] = {    {1.0, 0.0, 0.0, 0.0},    {0.0, 1.0, 0.0, 0.0},    {0.0, 0.0, 1.0, 0.0},    {0.0, 0.0, 0.0, 1.0}};GLfloat *trackballXform = (GLfloat*)objectXform;void display(void);void spinCube(void);void setMenuEntries(bool);/*----------------------------------------------------------------------*//* ** Materials setup.*/typedef struct materialStruct {   GLfloat ambient[4];   GLfloat diffuse[4];   GLfloat specular[4];   GLfloat shininess;} materialStruct;materialStruct brassMaterials = {    {0.33F, 0.22F, 0.03F, 1.0F},    {0.78F, 0.57F, 0.11F, 1.0F},    {0.99F, 0.91F, 0.81F, 1.0F},    27.8F};materialStruct redPlasticMaterials = {    {0.3F, 0.0F, 0.0F, 1.0F},    {0.6F, 0.0F, 0.0F, 1.0F},    {0.8F, 0.6F, 0.6F, 1.0F},    32.0F};materialStruct colorCubeMaterials = {    { 1.0F, 1.0F, 1.0F, 1.0F },    { 1.0F, 1.0F, 1.0F, 1.0F },    { 1.0F, 1.0F, 1.0F, 1.0F },    {100.0F}};materialStruct *currentMaterials = &redPlasticMaterials;void materials( materialStruct *materials){    /* define material proerties for front face of all polygons */    glMaterialfv(GL_FRONT, GL_AMBIENT, materials->ambient);    glMaterialfv(GL_FRONT, GL_DIFFUSE, materials->diffuse);    glMaterialfv(GL_FRONT, GL_SPECULAR, materials->specular);    glMaterialf(GL_FRONT, GL_SHININESS, materials->shininess);}/*----------------------------------------------------------------------*//*** Lighting setup.*/typedef struct lightingStruct {    GLfloat ambient[4];    GLfloat diffuse[4];    GLfloat specular[4];} lightingStruct;lightingStruct whiteLighting = {    {0.0F, 0.0F, 0.0F, 1.0F},    {1.0F, 1.0F, 1.0F, 1.0F},    {1.0F, 1.0F, 1.0F, 1.0F}};lightingStruct colorCubeLighting = {    {0.2F, 0.0F, 0.0F, 1.0F},    {0.0F, 1.0F, 0.0F, 1.0F},    {0.0F, 0.0F, 1.0F, 1.0F}};lightingStruct *currentLighting = &whiteLighting;void lighting(lightingStruct *lightSettings){    /* set up light 0 ambient, diffuse, specular, and spotlight */    glLightfv(GL_LIGHT0, GL_AMBIENT, lightSettings->ambient);    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightSettings->diffuse);    glLightfv(GL_LIGHT0, GL_SPECULAR, lightSettings->specular);    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0F);    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0F);    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0F);    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0F);    glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0F);    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);}/*----------------------------------------------------------------------*//*** Light position setup.*/void setLightPos(){    GLfloat light0_pos[4] = { 0.90F, 0.90F, 2.25F, 0.00F };    GLfloat light0_spotDir[3] = {0.0F, 0.0F, -1.0F};    glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spotDir);}/*----------------------------------------------------------------------*//*** Initial texture settings.*/void texture(){   GLubyte image[64][64][3];   int i, j, r, c;   for(i=0;i<64;i++)   {      for(j=0;j<64;j++)      {	 c = ((((i&0x8)==0)^((j&0x8))==0))*255;	 image[i][j][0]= (GLubyte) c;	 image[i][j][1]= (GLubyte) c;	 image[i][j][2]= (GLubyte) c;      }   }   glPixelStorei(GL_UNPACK_ALIGNMENT,1);   glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0,GL_RGB,GL_UNSIGNED_BYTE, image);   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);   gluBuild2DMipmaps(GL_TEXTURE_2D,3,64,64,GL_RGB,GL_UNSIGNED_BYTE, image);}/*** Set initial state.*/void initSettings(void){    texture();    glLineWidth(3.0);    setMenuEntries(true);}/*----------------------------------------------------------------------*//*** Set state according to user interaction.*/void userSettings(void) {    lighting(currentLighting);    materials(currentMaterials);    if (lightingEnabled) {	glEnable(GL_LIGHTING);	glEnable(GL_LIGHT0);    } else {	glDisable(GL_LIGHTING);	glDisable(GL_LIGHT0);    }    if (smoothEnabled) {	glShadeModel(GL_SMOOTH);    } else {	glShadeModel(GL_FLAT);    }    if(idleSpin) {        glutIdleFunc(spinCube);    } else {	glutIdleFunc(NULL);    }    if (texEnabled) {	glEnable(GL_TEXTURE_2D);    } else {	glDisable(GL_TEXTURE_2D);    }    if (fastTexture) {	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);    } else {	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    }    if (mipmapEnabled) {	glTexParameterf(GL_TEXTURE_2D,	    GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST);    } else {	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);    }    if (fogEnabled) {	float fogColor[] = {0.7, 0.6, 0.6, 1.0};	glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]);	glEnable(GL_FOG);	glFogi(GL_FOG_MODE, GL_LINEAR);	glFogf(GL_FOG_DENSITY, 1.0);	glFogf(GL_FOG_START, nnear);	glFogf(GL_FOG_END, nfar);	glFogfv(GL_FOG_COLOR, fogColor);    } else {	glDisable(GL_FOG);	glClearColor(0.0,0.0,0.0,1.0);    }    if (lineAAEnabled) {	glEnable(GL_BLEND);    } else {	glDisable(GL_BLEND);    }    if (lineAAEnabled) {	glEnable(GL_LINE_SMOOTH);	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    } else {	glDisable(GL_LINE_SMOOTH);    }    if (depthEnabled) {	glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */    } else {	glDisable(GL_DEPTH_TEST);    }    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    if(perspectiveXform) {	glFrustum(-1.25, 1.25, -1.25, 1.25, nnear, nfar);	viewxform_z = -5.0;    } else {	glOrtho(-2.0, 2.0, -2.0, 2.0, nnear, nfar);	viewxform_z = -5.0;    }    glMatrixMode(GL_MODELVIEW);}/*----------------------------------------------------------------------*//*** Draw the cube.*/GLfloat vertices[][3] = {    {-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0},     {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};GLfloat normals[][3] = {    {-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0},     {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};GLfloat fnormals[][3] = {    { 0.0, 0.0,-1.0}, { 0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0},    { 1.0, 0.0, 0.0}, { 0.0, 0.0, 1.0}, { 0.0,-1.0, 0.0}};GLfloat colors[][3] = {    {0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0},     {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};void polygon(int a, int b, int c , int d, int face){    /* draw a polygon via list of vertices */    if(drawLines) {        glColor3f(1.0,1.0,1.0);        glBegin(GL_LINE_LOOP);  	glVertex3fv(vertices[a]);  	glVertex3fv(vertices[b]);  	glVertex3fv(vertices[c]);  	glVertex3fv(vertices[d]);        glEnd();    }    else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -