teapot.c
上传用户:xk288cn
上传日期:2007-05-28
资源大小:4876k
文件大小:13k
源码类别:

GIS编程

开发平台:

Visual C++

  1. /*
  2.  * This program is under the GNU GPL.
  3.  * Use at your own risk.
  4.  *
  5.  * written by David Bucciarelli (tech.hmw@plus.it)
  6.  *            Humanware s.r.l.
  7.  */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <time.h>
  13. #ifdef _WIN32
  14. #include <windows.h>
  15. #endif
  16. #include <GL/glut.h>
  17. #if defined(GL_VERSION_1_1)
  18. /* Routines called directly. */
  19. #elif defined(GL_EXT_texture_object) && defined(GL_EXT_copy_texture) && defined(GL_EXT_subtexture)
  20. #define glBindTexture(A,B)     glBindTextureEXT(A,B)
  21. #define glGenTextures(A,B)     glGenTexturesEXT(A,B)
  22. #else
  23. #define glBindTexture(A,B)
  24. #define glGenTextures(A,B)
  25. #endif
  26. #include "image.h"
  27. static int WIDTH=640;
  28. static int HEIGHT=480;
  29. #define FRAME 50
  30. #define BASESIZE 10.0
  31. #define BASERES 12
  32. #define TEAPOTRES 3
  33. /* Some <math.h> files do not define M_PI... */
  34. #ifndef M_PI
  35. #define M_PI 3.14159265358979323846
  36. #endif
  37. extern void teapot(GLint, GLdouble, GLenum);
  38. extern void shadowmatrix(GLfloat [4][4], GLfloat [4], GLfloat [4]);
  39. extern void findplane(GLfloat [4], GLfloat [3], GLfloat [3], GLfloat [3]);
  40. static float obs[3]={5.0,0.0,1.0};
  41. static float dir[3];
  42. static float v=0.0;
  43. static float alpha=-90.0;
  44. static float beta=90.0;
  45. static GLfloat baseshadow[4][4];
  46. static GLfloat lightpos[4]={2.3,0.0,3.0,1.0};
  47. static GLfloat lightdir[3]={-2.3,0.0,-3.0};
  48. static GLfloat lightalpha=0.0;
  49. static int fog=1;
  50. static int bfcull=1;
  51. static int usetex=1;
  52. static int help=1;
  53. static int joyavailable=0;
  54. static int joyactive=0;
  55. static GLuint t1id,t2id;
  56. static GLuint teapotdlist,basedlist,lightdlist;
  57. static float gettime(void)
  58. {
  59.   static clock_t told=0;
  60.   clock_t tnew,ris;
  61.   tnew=clock();
  62.   ris=tnew-told;
  63.   told=tnew;
  64.   return(ris/(float)CLOCKS_PER_SEC);
  65. }
  66. static void calcposobs(void)
  67. {
  68.   dir[0]=sin(alpha*M_PI/180.0);
  69.   dir[1]=cos(alpha*M_PI/180.0)*sin(beta*M_PI/180.0);
  70.   dir[2]=cos(beta*M_PI/180.0);
  71.   obs[0]+=v*dir[0];
  72.   obs[1]+=v*dir[1];
  73.   obs[2]+=v*dir[2];
  74. }
  75. /* ARGSUSED1 */
  76. static void special(int k, int x, int y)
  77. {
  78.   switch(k) {
  79.   case GLUT_KEY_LEFT:
  80.     alpha-=2.0;
  81.     break;
  82.   case GLUT_KEY_RIGHT:
  83.     alpha+=2.0;
  84.     break;
  85.   case GLUT_KEY_DOWN:
  86.     beta-=2.0;
  87.     break;
  88.   case GLUT_KEY_UP:
  89.     beta+=2.0;
  90.     break;
  91.   }
  92. }
  93. /* ARGSUSED1 */
  94. static void key(unsigned char k, int x, int y)
  95. {
  96.   switch(k) {
  97.   case 27:
  98.     exit(0);
  99.     break;
  100.     
  101.   case 'a':
  102.     v+=0.005;
  103.     break;
  104.   case 'z':
  105.     v-=0.005;
  106.     break;
  107.   case 'j':
  108.     joyactive=(!joyactive);
  109.     break;
  110.   case 'h':
  111.     help=(!help);
  112.     break;
  113.   case 'f':
  114.     fog=(!fog);
  115.     break;
  116.   case 't':
  117.     usetex=(!usetex);
  118.     break;
  119.   case 'b':
  120.     if(bfcull) {
  121.       glDisable(GL_CULL_FACE);
  122.       bfcull=0;
  123.     } else {
  124.       glEnable(GL_CULL_FACE);
  125.       bfcull=1;
  126.     }
  127.     break;
  128.   }
  129. }
  130. static void reshape(int w, int h) 
  131. {
  132.   WIDTH=w;
  133.   HEIGHT=h;
  134.   glMatrixMode(GL_PROJECTION);
  135.   glLoadIdentity();
  136.   gluPerspective(45.0,w/(float)h,0.2,40.0);
  137.   glMatrixMode(GL_MODELVIEW);
  138.   glLoadIdentity();
  139.   glViewport(0,0,w,h);
  140. }
  141. static void printstring(void *font, char *string)
  142. {
  143.   int len,i;
  144.   len=(int)strlen(string);
  145.   for(i=0;i<len;i++)
  146.     glutBitmapCharacter(font,string[i]);
  147. }
  148. static void printhelp(void)
  149. {
  150.   glEnable(GL_BLEND);
  151.   glColor4f(0.5,0.5,0.5,0.5);
  152.   glRecti(40,40,600,440);
  153.   glDisable(GL_BLEND);
  154.   glColor3f(1.0,0.0,0.0);
  155.   glRasterPos2i(300,420);
  156.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"Help");
  157.   glRasterPos2i(60,390);
  158.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"h - Togle Help");
  159.   glRasterPos2i(60,360);
  160.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"t - Togle Textures");
  161.   glRasterPos2i(60,330);
  162.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"f - Togle Fog");
  163.   glRasterPos2i(60,300);
  164.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"b - Togle Back face culling");
  165.   glRasterPos2i(60,270);
  166.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"Arrow Keys - Rotate");
  167.   glRasterPos2i(60,240);
  168.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"a - Increase velocity");
  169.   glRasterPos2i(60,210);
  170.   printstring(GLUT_BITMAP_TIMES_ROMAN_24,"z - Decrease velocity");
  171.   glRasterPos2i(60,180);
  172.   if(joyavailable)
  173.     printstring(GLUT_BITMAP_TIMES_ROMAN_24,"j - Togle jostick control (Joystick control available)");
  174.   else
  175.     printstring(GLUT_BITMAP_TIMES_ROMAN_24,"(No Joystick control available)");
  176. }
  177. static void drawbase(void)
  178. {
  179.   int i,j;
  180.   float x,y,dx,dy;
  181.   glBindTexture(GL_TEXTURE_2D,t1id);
  182.   dx=BASESIZE/BASERES;
  183.   dy=-BASESIZE/BASERES;
  184.   for(y=BASESIZE/2.0,j=0;j<BASERES;y+=dy,j++) {
  185.     glBegin(GL_QUAD_STRIP);
  186.     glColor3f(1.0,1.0,1.0);
  187.     glNormal3f(0.0,0.0,1.0);
  188.     for(x=-BASESIZE/2.0,i=0;i<BASERES;x+=dx,i++) {
  189.       glTexCoord2f(x,y);
  190.       glVertex3f(x,y,0.0);
  191.       glTexCoord2f(x,y+dy);
  192.       glVertex3f(x,y+dy,0.0);
  193.     }
  194.     glEnd();
  195.   }
  196. }
  197. static void drawteapot(void)
  198. {
  199.   static float xrot=0.0;
  200.   static float zrot=0.0;
  201.   glPushMatrix();
  202.   glRotatef(lightalpha,0.0,0.0,1.0);
  203.   glMultMatrixf((GLfloat *)baseshadow);
  204.   glRotatef(-lightalpha,0.0,0.0,1.0);
  205.   glTranslatef(0.0,0.0,1.0);
  206.   glRotatef(xrot,1.0,0.0,0.0);
  207.   glRotatef(zrot,0.0,0.0,1.0);
  208.   glDisable(GL_TEXTURE_2D);
  209.   glDisable(GL_DEPTH_TEST);
  210.   glDisable(GL_LIGHTING);
  211.   glColor3f(0.0,0.0,0.0);
  212.   glCallList(teapotdlist);
  213.   glEnable(GL_DEPTH_TEST);
  214.   glEnable(GL_LIGHTING);
  215.   if(usetex)
  216.     glEnable(GL_TEXTURE_2D);
  217.   glPopMatrix();
  218.   glPushMatrix();
  219.   glTranslatef(0.0,0.0,1.0);
  220.   glRotatef(xrot,1.0,0.0,0.0);
  221.   glRotatef(zrot,0.0,0.0,1.0);
  222.   glCallList(teapotdlist);
  223.   glPopMatrix();
  224.   xrot+=2.0;
  225.   zrot+=1.0;
  226. }
  227. static void drawlight1(void)
  228. {
  229.   glPushMatrix();
  230.   glRotatef(lightalpha,0.0,0.0,1.0);
  231.   glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
  232.   glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,lightdir);
  233.   glPopMatrix();
  234. }
  235. static void drawlight2(void)
  236. {
  237.   glPushMatrix();
  238.   glRotatef(lightalpha,0.0,0.0,1.0);
  239.   glTranslatef(lightpos[0],lightpos[1],lightpos[2]);
  240.   glDisable(GL_TEXTURE_2D);
  241.   glCallList(lightdlist);
  242.   if(usetex)
  243.     glEnable(GL_TEXTURE_2D);
  244.   
  245.   glPopMatrix();
  246.   lightalpha+=1.0;
  247. }
  248. static void dojoy(void)
  249. {
  250. #ifdef _WIN32
  251.   static UINT max[2]={0,0};
  252.   static UINT min[2]={0xffffffff,0xffffffff},center[2];
  253.   MMRESULT res;
  254.   JOYINFO joy;
  255.   res=joyGetPos(JOYSTICKID1,&joy);
  256.   if(res==JOYERR_NOERROR) {
  257.     joyavailable=1;
  258.     if(max[0]<joy.wXpos)
  259.       max[0]=joy.wXpos;
  260.     if(min[0]>joy.wXpos)
  261.       min[0]=joy.wXpos;
  262.     center[0]=(max[0]+min[0])/2;
  263.     if(max[1]<joy.wYpos)
  264.       max[1]=joy.wYpos;
  265.     if(min[1]>joy.wYpos)
  266.       min[1]=joy.wYpos;
  267.     center[1]=(max[1]+min[1])/2;
  268.     if(joyactive) {
  269.       if(fabs(center[0]-(float)joy.wXpos)>0.1*(max[0]-min[0]))
  270. alpha-=2.5*(center[0]-(float)joy.wXpos)/(max[0]-min[0]);
  271.       if(fabs(center[1]-(float)joy.wYpos)>0.1*(max[1]-min[1]))
  272. beta+=2.5*(center[1]-(float)joy.wYpos)/(max[1]-min[1]);
  273.       if(joy.wButtons & JOY_BUTTON1)
  274. v+=0.005;
  275.       if(joy.wButtons & JOY_BUTTON2)
  276. v-=0.005;
  277.     }
  278.   } else
  279.     joyavailable=0;
  280. #endif
  281. }
  282. static void draw(void)
  283. {
  284.   static int count=0;
  285.   static char frbuf[80];
  286.   float fr;
  287.   dojoy();
  288.   glEnable(GL_DEPTH_TEST);
  289.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  290.   if(usetex)
  291.     glEnable(GL_TEXTURE_2D);
  292.   else
  293.     glDisable(GL_TEXTURE_2D);
  294.   if(fog)
  295.     glEnable(GL_FOG);
  296.   else
  297.     glDisable(GL_FOG);
  298.   glEnable(GL_LIGHTING);
  299.   glShadeModel(GL_SMOOTH);
  300.   glPushMatrix();
  301.   calcposobs();
  302.   gluLookAt(obs[0],obs[1],obs[2],
  303.     obs[0]+dir[0],obs[1]+dir[1],obs[2]+dir[2],
  304.     0.0,0.0,1.0);
  305.   drawlight1();
  306.   glCallList(basedlist);
  307.   drawteapot();
  308.   drawlight2();
  309.   glPopMatrix();
  310.   
  311.   if((count % FRAME)==0) {
  312.     fr=gettime();
  313.     sprintf(frbuf,"Frame rate: %f",FRAME/fr);
  314.   }
  315.   glDisable(GL_LIGHTING);
  316.   glDisable(GL_TEXTURE_2D);
  317.   glDisable(GL_DEPTH_TEST);
  318.   glDisable(GL_FOG);
  319.   glShadeModel(GL_FLAT);
  320.   glMatrixMode(GL_PROJECTION);
  321.   glLoadIdentity();
  322.   glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0);
  323.   glMatrixMode(GL_MODELVIEW);
  324.   glLoadIdentity();
  325.   glColor3f(1.0,0.0,0.0);
  326.   glRasterPos2i(10,10);
  327.   printstring(GLUT_BITMAP_HELVETICA_18,frbuf);
  328.   glRasterPos2i(350,470);
  329.   printstring(GLUT_BITMAP_HELVETICA_10,"Teapot V1.2 Written by David Bucciarelli (tech.hmw@plus.it)");
  330.   if(help)
  331.     printhelp();
  332.   reshape(WIDTH,HEIGHT);
  333.   glutSwapBuffers();
  334.   count++;
  335. }
  336. static void inittextures(void)
  337. {
  338.   IMAGE *img;
  339.   GLenum gluerr;
  340.   
  341.   glGenTextures(1,&t1id);
  342.   glBindTexture(GL_TEXTURE_2D,t1id);
  343.   if(!(img=ImageLoad("tile.rgb"))) {
  344.     fprintf(stderr,"Error reading a texture.n");
  345.     exit(-1);
  346.   }
  347.   glPixelStorei(GL_UNPACK_ALIGNMENT,4);
  348.   if((gluerr=gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY, GL_RGB,
  349.        GL_UNSIGNED_BYTE, (GLvoid *)(img->data)))) {
  350.     fprintf(stderr,"GLULib%sn",gluErrorString(gluerr));
  351.     exit(-1);
  352.   }
  353.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  354.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  355.   
  356.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_LINEAR);
  357.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  358.   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  359.   glGenTextures(1,&t2id);
  360.   glBindTexture(GL_TEXTURE_2D,t2id);
  361.   if(!(img=ImageLoad("bw.rgb"))) {
  362.     fprintf(stderr,"Error reading a texture.n");
  363.     exit(-1);
  364.   }
  365.   glPixelStorei(GL_UNPACK_ALIGNMENT,4);
  366.   if((gluerr=gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY, GL_RGB,
  367.        GL_UNSIGNED_BYTE, (GLvoid *)(img->data)))) {
  368.     fprintf(stderr,"GLULib%sn",gluErrorString(gluerr));
  369.     exit(-1);
  370.   }
  371.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  372.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  373.   
  374.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_LINEAR);
  375.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  376.   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  377. }
  378. static void initlight(void)
  379. {
  380.   float lamb[4]={0.2,0.2,0.2,1.0};
  381.   float lspec[4]={1.0,1.0,1.0,1.0};
  382.   glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,70.0);
  383.   glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,20.0);
  384.   glLightfv(GL_LIGHT0,GL_AMBIENT,lamb);
  385.   glLightfv(GL_LIGHT0,GL_SPECULAR,lspec);
  386.   glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,20.0);
  387.   glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,lspec);
  388.   glEnable(GL_LIGHT0);
  389. }
  390. static void initdlists(void)
  391. {
  392.   GLUquadricObj *lcone,*lbase;
  393.   GLfloat plane[4];
  394.   GLfloat v0[3]={0.0,0.0,0.0};
  395.   GLfloat v1[3]={1.0,0.0,0.0};
  396.   GLfloat v2[3]={0.0,1.0,0.0};
  397.   findplane(plane,v0,v1,v2);
  398.   shadowmatrix(baseshadow,plane,lightpos);
  399.   teapotdlist=glGenLists(1);
  400.   glNewList(teapotdlist,GL_COMPILE);
  401.   glRotatef(90.0,1.0,0.0,0.0);
  402.   glCullFace(GL_FRONT);
  403.   glBindTexture(GL_TEXTURE_2D,t2id);
  404.   teapot(TEAPOTRES,0.6,GL_FILL);
  405.   glCullFace(GL_BACK);
  406.   glEndList();
  407.   basedlist=glGenLists(1);
  408.   glNewList(basedlist,GL_COMPILE);
  409.   drawbase();
  410.   glEndList();
  411.   lightdlist=glGenLists(1);
  412.   glNewList(lightdlist,GL_COMPILE);
  413.   glDisable(GL_LIGHTING);
  414.  
  415.   lcone=gluNewQuadric();
  416.   lbase=gluNewQuadric();
  417.   glRotatef(45.0,0.0,1.0,0.0);
  418.   glColor3f(1.0,1.0,1.0);
  419.   glCullFace(GL_FRONT);
  420.   gluDisk(lbase,0.0,0.2,12.0,1.0);
  421.   glCullFace(GL_BACK);
  422.   glColor3f(0.5,0.0,0.0);
  423.   gluCylinder(lcone,0.2,0.0,0.5,12,1);
  424.   gluDeleteQuadric(lcone);
  425.   gluDeleteQuadric(lbase);
  426.   glEnable(GL_LIGHTING);
  427.   glEndList();
  428. }
  429. int main(int ac, char **av)
  430. {
  431.   float fogcolor[4]={0.025,0.025,0.025,1.0};
  432.   fprintf(stderr,"Teapot V1.2nWritten by David Bucciarelli (tech.hmw@plus.it)n");
  433.   /*
  434.     if(!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)) {
  435.     fprintf(stderr,"Error setting the process class.n");
  436.     return 0;
  437.     }
  438.     if(!SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL)) {
  439.     fprintf(stderr,"Error setting the process priority.n");
  440.     return 0;
  441.     }
  442.     */
  443.   glutInitWindowPosition(0,0);
  444.   glutInitWindowSize(WIDTH,HEIGHT);
  445.   glutInit(&ac,av);
  446.   glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
  447.   glutCreateWindow("Teapot");
  448.   reshape(WIDTH,HEIGHT);
  449.   glShadeModel(GL_SMOOTH);
  450.   glEnable(GL_DEPTH_TEST);
  451.   glEnable(GL_CULL_FACE);
  452.   glEnable(GL_TEXTURE_2D);
  453.   glEnable(GL_FOG);
  454.   glFogi(GL_FOG_MODE,GL_EXP2);
  455.   glFogfv(GL_FOG_COLOR,fogcolor);
  456.   glFogf(GL_FOG_DENSITY,0.04);
  457. #ifdef FX
  458.   glHint(GL_FOG_HINT,GL_NICEST);
  459. #endif
  460.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  461.   calcposobs();
  462.   inittextures();
  463.   initlight();
  464. #ifndef FX
  465.   glDisable(GL_TEXTURE_2D);
  466.   usetex=0;
  467. #endif
  468.   initdlists();
  469.   glClearColor(fogcolor[0],fogcolor[1],fogcolor[2],fogcolor[3]);
  470.   glutReshapeFunc(reshape);
  471.   glutDisplayFunc(draw);
  472.   glutKeyboardFunc(key);
  473.   glutSpecialFunc(special);
  474.   glutIdleFunc(draw);
  475.   glutMainLoop();
  476.   return 0;             /* ANSI C requires main to return int. */
  477. }