游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4059|回复: 5

发一个例子

[复制链接]

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2005-6-9 11:42:00 | 显示全部楼层 |阅读模式

  1. /**********************************************
  2. *Change by TheCell(TheCell61@hotmail.com)
  3. *I did not change much, other than headers and
  4. *library included to the project
  5. *Also, once you've downloaded the sdk, you must
  6. *copy the files you've got from the sdk
  7. *according to this template
  8. *$MSDEV/
  9. *                |
  10. *                include/
  11. *                                |
  12. *                                al/
  13. *                                |
  14. *                                |  al.h
  15. *                                |  alc.h
  16. *                                |  alut.h
  17. *                |
  18. *                lib/
  19. *                        |
  20. *                        !  alut.lib
  21. *                        |  openal32,lib
  22. *
  23. *If you don't copy all the .h files into the
  24. *include dir, you'll get compile error
  25. *and if you don't copy the .lib files
  26. *to the correct directory AND and add them to
  27. *the project, you'll get link error
  28. ***********************************************/

  29. /**********************************************
  30. *Change:        Added stdio.h
  31. *                        changed al/alut.c to al/alut.h
  32. *
  33. ***********************************************/

  34. /**********************************************
  35. *Explanation        1:Since we use printf, we MUST
  36. *                                include stdio.h to use this
  37. *                                function
  38. *
  39. *                                2:I have been searching for
  40. *                                the alut.c files, but I have
  41. *                                not found it.  So I assume
  42. *                                that alut.h was intended
  43. *                                instead
  44. ************************************************/



  45. #include <stdio.h>


  46. #include <al/al.h>
  47. #include <al/alc.h>
  48. //#include <al/alut.c>                Since I have not found alut.c anywhere, I suppose we use alut.h
  49. #include <al/alut.h>                //Added by TheCell
  50.                                                         //See explanation 2
  51. #include <GL/glut.h>


  52. /*
  53. * These are OpenAL "names" (or "objects"). They store and id of a buffer
  54. * or a source object. Generally you would expect to see the implementation
  55. * use values that scale up from '1', but don't count on it. The spec does
  56. * not make this mandatory (as it is OpenGL). The id's can easily be memory
  57. * pointers as well. It will depend on the implementation.
  58. */

  59. // Buffers to hold sound data.
  60. ALuint Buffer;

  61. // Sources are points of emitting sound.
  62. ALuint Source;


  63. /*
  64. * These are 3D cartesian vector coordinates. A structure or class would be
  65. * a more flexible of handling these, but for the sake of simplicity we will
  66. * just leave it as is.
  67. */

  68. // Position of the source sound.
  69. ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };

  70. // Velocity of the source sound.
  71. ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };


  72. // Position of the Listener.
  73. ALfloat ListenerPos[] = { 2.0,0.0, -10.0 };

  74. // Velocity of the Listener.
  75. ALfloat ListenerVel[] = { 10.0, 10.0, 10.0 };

  76. // Orientation of the Listener. (first 3 elements are "at", second 3 are "up")
  77. // Also note that these should be units of '1'.
  78. ALfloat ListenerOri[] = { 100.0, 100.0, -100.0,  0.0, 1.0, 0.0 };



  79. /*
  80. * ALboolean LoadALData()
  81. *
  82. *        This function will load our sample data from the disk using the Alut
  83. *        utility and send the data into OpenAL as a buffer. A source is then
  84. *        also created to play that buffer.
  85. */
  86. ALboolean LoadALData()
  87. {
  88.         // Variables to load into.

  89.         ALenum format;
  90.         ALsizei size;
  91.         ALvoid* data;
  92.         ALsizei freq;
  93.         ALboolean loop;

  94.         // Load wav data into a buffer.

  95.         alGenBuffers(1, &Buffer);

  96.         if(alGetError() != AL_NO_ERROR)
  97.                 return AL_FALSE;

  98.         alutLoadWAVFile("wavdata/awp1.wav", &format, &data, &size, &freq, &loop);
  99.         alBufferData(Buffer, format, data, size, freq);
  100.         alutUnloadWAV(format, data, size, freq);

  101.         // Bind the buffer with the source.

  102.         alGenSources(1, &Source);

  103.         if(alGetError() != AL_NO_ERROR)
  104.                 return AL_FALSE;

  105.         alSourcei (Source, AL_BUFFER,   Buffer   );
  106.         alSourcef (Source, AL_PITCH,    1.0      );
  107.         alSourcef (Source, AL_GAIN,     1.0      );
  108.         alSourcefv(Source, AL_POSITION, SourcePos);
  109.         alSourcefv(Source, AL_VELOCITY, SourceVel);
  110.         alSourcei (Source, AL_LOOPING,  loop     );

  111.         // Do another error check and return.

  112.         if(alGetError() == AL_NO_ERROR)
  113.                 return AL_TRUE;

  114.         return AL_FALSE;
  115. }



  116. /*
  117. * void SetListenerValues()
  118. *
  119. *        We already defined certain values for the Listener, but we need
  120. *        to tell OpenAL to use that data. This function does just that.
  121. */
  122. void SetListenerValues()
  123. {
  124.         alListenerfv(AL_POSITION,    ListenerPos);
  125.         alListenerfv(AL_VELOCITY,    ListenerVel);
  126.         alListenerfv(AL_ORIENTATION, ListenerOri);
  127. }



  128. /*
  129. * void KillALData()
  130. *
  131. *        We have allocated memory for our buffers and sources which needs
  132. *        to be returned to the system. This function frees that memory.
  133. */
  134. void KillALData()
  135. {
  136.         alDeleteBuffers(1, &Buffer);
  137.         alDeleteSources(1, &Source);
  138.         alutExit();
  139. }


  140. void display ()
  141. {
  142.         glClear(GL_COLOR_BUFFER_BIT);
  143.         glColor3f(0.8,0.5,0.8);
  144.         glPushMatrix();
  145.                 glTranslatef(ListenerPos[0],ListenerPos[1],ListenerPos[2]);

  146.                 glutWireSphere(0.2,20,16);
  147.         glPopMatrix();
  148.         glColor3f(1,0.5,0.8);
  149.         glPushMatrix();
  150.                 glTranslatef(SourcePos[0],SourcePos[1],SourcePos[2]);
  151.                 glutWireSphere(0.3,20,16);
  152.         glPopMatrix();
  153.         glFlush();
  154. }

  155. void reshape (int w, int h)
  156. {
  157.    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  158.    glMatrixMode (GL_PROJECTION);
  159.    glLoadIdentity ();
  160.    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  161.    glMatrixMode(GL_MODELVIEW);
  162.    glLoadIdentity();
  163.    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  164. }

  165. void keyboard( unsigned char key, int x, int y )
  166. {

  167.     switch( key ) {
  168.     case 27:exit( 0 );break;

  169.         case 'p':
  170.         case 'P':
  171.         if(LoadALData() == AL_FALSE)
  172.         {
  173.             printf("Error loading data.");

  174.         }
  175.         SetListenerValues();
  176.         alSourcePlay(Source);
  177.     break;

  178.         default:
  179.         break;
  180.     }

  181. }


  182. int main(int argc, char *argv[])
  183. {
  184.         glutInit( &argc, argv );
  185.         glClearColor(0,0,0.5,0);
  186.     glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
  187.     glutInitWindowSize( 640, 480 );
  188.     glutInitWindowPosition( 0, 0 );
  189.     glutCreateWindow("play wav");
  190.         glutKeyboardFunc(keyboard);
  191.     glutReshapeFunc( reshape );
  192.     glutDisplayFunc( display );


  193.         // Initialize OpenAL and clear the error bit.

  194.         alutInit(NULL, 0);
  195.         alGetError();
  196.     atexit(KillALData);       

  197.     glutMainLoop( );
  198.         return 0;
  199. }

复制代码

14

主题

245

帖子

256

积分

中级会员

Rank: 3Rank: 3

积分
256
QQ
发表于 2005-6-9 12:05:00 | 显示全部楼层

Re:发一个例子

这是哪里的例子?

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2005-6-9 12:08:00 | 显示全部楼层

Re:发一个例子

我从哪里的例子改来的,忘啦^_^

139

主题

2005

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
QQ
发表于 2005-6-9 17:07:00 | 显示全部楼层

Re:发一个例子

倒。al不是antking地专利么?

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2005-6-9 20:13:00 | 显示全部楼层

Re:发一个例子

所以一版都是他的文章,哈哈

14

主题

245

帖子

256

积分

中级会员

Rank: 3Rank: 3

积分
256
QQ
发表于 2005-6-10 09:49:00 | 显示全部楼层

Re:发一个例子

俺可是每个文章头都加了他的版权头了!

等俺有了好东西,俺也会放上来的。。嘿嘿
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-25 23:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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