Chinaunix首页 | 论坛 | 博客
  • 博客访问: 286141
  • 博文数量: 48
  • 博客积分: 2931
  • 博客等级: 少校
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-19 22:12
文章分类

全部博文(48)

文章存档

2012年(1)

2011年(6)

2010年(33)

2009年(5)

2008年(3)

我的朋友

分类:

2010-01-05 12:27:40

openal教程(二)

openal教程(二)
循环和消退
希望你觉得上一章有用,这一章将更容易。
  #include
#include
#include
#include
#include
#include
#include

// 存储声音数据.
ALuint Buffer;

// 用于播放声音.
ALuint Source;

//  源声音的位置.
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };

// 源声音的速度.
ALfloat SourceVel[] = { 0.0, 0.0, 0.1 };

// 听者的位置.
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };

// 听者的速度
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };

// 听者的方向 (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
这一章与上一章唯一的不同是源速度的改变,他的‘Z’现在是0.1.
ALboolean LoadALData()
{
    // 载入变量
    ALenum format;
    ALsizei size;
    ALvoid* data;
    ALsizei freq;
    ALboolean loop;

    // 载入WAV数据.

    alGenBuffers(1, &Buffer);

    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    alutLoadWAVFile("wavdata/Footsteps.wav", &format, &data, &size, &freq, &loop);
    alBufferData(Buffer, format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

    // 捆绑源

    alGenSources(1, &Source);

    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    alSourcei (Source, AL_BUFFER,   Buffer   );
    alSourcef (Source, AL_PITCH,    1.0f     );
    alSourcef (Source, AL_GAIN,     1.0f     );
    alSourcefv(Source, AL_POSITION, SourcePos);
    alSourcefv(Source, AL_VELOCITY, SourceVel);
    alSourcei (Source, AL_LOOPING,  AL_TRUE  );

    // 做错误检测并返回

    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    return AL_TRUE;
}
在这一节中有两处改变,首先是导入“FOOTSTES。WAV”,设置源‘AL_LOOPING’
为‘AL_TRUE’。这意味着源播放直到停止时结束。他将不断的循环播放。
void SetListenervalues()
{
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
}

void KillALData()
{
    alDeleteBuffers(1, &Buffer);
    alDeleteSources(1, &Source);
    alutExit();
}
这里没有改变。
int main(int argc, char *argv[])
{
    //  初始OPENAL并清错误字节
    alutInit(NULL,0);
    alGetError();

    // 载入WAV数据.
    if (LoadALData() == AL_FALSE)
        return 0;

    SetListenervalues();

    //  设置退出函数.
    atexit(KillALData);

    // 开始源的播放.
    alSourcePlay(Source);

    //循环
    ALint time = 0;
    ALint elapse = 0;

    while (!kbhit())
    {
        elapse += clock() - time;
        time += elapse;

        if (elapse > 50)
        {
            elapse = 0;

            SourcePos[0] += SourceVel[0];
            SourcePos[1] += SourceVel[1];
            SourcePos[2] += SourceVel[2];

            alSourcefv(Source, AL_POSITION, SourcePos);
        }
    }


    return 0;
}
这里唯一的改变是增加了一个循环。他将代替播放和停止按钮。
阅读(1307) | 评论(0) | 转发(0) |
0

上一篇:OpenAL教程(一)

下一篇:OpenAL教程(三)

给主人留下些什么吧!~~