Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8052 in orxonox.OLD for branches


Ignore:
Timestamp:
Jun 1, 2006, 11:12:42 AM (18 years ago)
Author:
amaechler
Message:

branches/atmospheric_engine: sound now with gain control, new cloud thoughts / todo..maaaan

Location:
branches/atmospheric_engine/src/lib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc

    r7977 r8052  
    2222#include "glincl.h"
    2323//#include "graphics_engine.h"
     24#include "material.h"
    2425#include <math.h>
    2526
     
    6970
    7071        // Generate noise map a
    71         CloudEffect::genNoiseMap(cloudMap32_a);
     72        //CloudEffect::genNoiseMap(cloudMap32_a);
    7273       
    7374        if (this->cloudAnimTimeStep > 0) {
    7475                // Generate noise map b
    75                 CloudEffect::genNoiseMap(cloudMap32_b);
     76                //CloudEffect::genNoiseMap(cloudMap32_b);
    7677        }
     78
     79  this->material = new Material();
     80
    7781}
    7882
     
    8084{
    8185        PRINTF(0)( "Activating CloudEffect\n");
    82         if (this->cloudAnimTimeStep == 0) {
    83                 for (int i = 0; i < 32*32; i++)
    84                         cloudMap32_c[i] = cloudMap32_a[i];
    8586
    86                 CloudEffect::overlapOctaves();
    87                 CloudEffect::expFilter();
    88                 CloudEffect::genCloudTexture();
    89         }
     87
    9088}
    9189
     
    9593}
    9694
    97 void CloudEffect::genCloudTexture() {
    98         for(int i=0; i<256; i++)
    99                 for(int j=0; j<256; j++)
    100                 {
    101                         float color = cloudMap256[i*256+j];
    102                         cloudTexture[i][j][0] = (char) color;
    103                         cloudTexture[i][j][1] = (char) color;
    104                         cloudTexture[i][j][2] = (char) color;
    105                 }
    106 
    107         glBindTexture(GL_TEXTURE_2D, texID[0]);
    108 
    109         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, cloudTexture);
    110         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, cloudTexture);
    111 
    112 }
    113 
    114 void CloudEffect::calcAnimMap(float timer) {
    115 
    116         for (int x=0; x<32*32; x++)
    117                 cloudMap32_c[x] = (cloudMap32_a[x] * ((this->cloudAnimTimeStep - timer) / this->cloudAnimTimeStep)) + (cloudMap32_b[x] * (timer / this->cloudAnimTimeStep));
    118 
    119 }
    120 
    12195void CloudEffect::draw() const
    12296{
     97        /* TODO:
     98                -Load a texture, for now from an existing image, a cloud texture
     99                -Blend / Overlay this with a blue sky like in the tutorial
     100                -Make a skybox or whatever....
     101                -Animate it (for now move it along the sky)
     102        */
     103
    123104        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    124105
     106  this->material->setDiffuseMap("maps/lightning_bolt.png");
     107  this->material->select();
     108
    125109        glPushMatrix();
    126         glEnable(GL_TEXTURE_2D);
     110        glEnable(GL_TEXTURE_2D);
    127111
    128         glBindTexture(GL_TEXTURE_2D, texID[0]);
     112        glBindTexture(GL_TEXTURE_2D, texID[0]);
    129113
    130114        // FIXME : Bind this to the sky - how do I do this?
    131115        glBegin(GL_QUADS);
    132                 glTexCoord2f(0.0f, 0.0f); glVertex3f(20, 20,  60);      // Bottom Left Of The Texture and Quad
    133                 glTexCoord2f(1.0f, 0.0f); glVertex3f(60, 20,  60);      // Bottom Right Of The Texture and Quad
    134                 glTexCoord2f(1.0f, 1.0f); glVertex3f(60, 60,  60);      // Top Right Of The Texture and Quad
    135                 glTexCoord2f(0.0f, 1.0f); glVertex3f(20, 60,  60);      // Top Left Of The Texture and Quad
     116                glTexCoord2f(0.0f, 0.0f);
     117                glVertex3f(20, 20,  60);        // Bottom Left Of The Texture and Quad
     118
     119                glTexCoord2f(1.0f, 0.0f);
     120                glVertex3f(60, 20,  60);        // Bottom Right Of The Texture and Quad
     121
     122                glTexCoord2f(1.0f, 1.0f);
     123                glVertex3f(60, 60,  60);        // Top Right Of The Texture and Quad
     124
     125                glTexCoord2f(0.0f, 1.0f);
     126                glVertex3f(20, 60,  60);        // Top Left Of The Texture and Quad
    136127        glEnd();
    137128
     
    141132void CloudEffect::tick (float dt)
    142133{
    143         if (this->cloudAnimTimeStep > 0) {
    144                 if (timer >= this->cloudAnimTimeStep) {
    145                         timer -= this->cloudAnimTimeStep;
    146                         for (int i = 0; i < 32*32; i++)
    147                                 cloudMap32_a[i] = cloudMap32_b[i];
    148                         CloudEffect::genNoiseMap(cloudMap32_b);
    149                 }
    150 
    151                 //map32anim = (map32a * (10 - timer)) + (map32b * timer);
    152                 CloudEffect::calcAnimMap(timer);
    153                 //CloudEffect::overlapOctaves();
    154                 //CloudEffect::expFilter();
    155                 CloudEffect::genCloudTexture();
    156 
    157                 timer += dt;
    158         }
     134       
    159135}
    160 
    161 /*
    162         Random noise generator
    163 */
    164 float CloudEffect::noise(int x, int y, int random)
    165 {
    166                 int n = x + y * 57 + random * 131;
    167                 n = (n<<13) ^ n;
    168                 return (1.0f - ( (n * (n * n * 15731 + 789221) +
    169                                                 1376312589)&0x7fffffff)* 0.000000000931322574615478515625f);
    170 }
    171 
    172 /*
    173         Set noise for the 32*32 noise map:
    174 */
    175 void CloudEffect::genNoiseMap(float  *map)
    176 {
    177         float temp[34][34];
    178 
    179         int random = rand() % 5000;
    180 
    181         for (int y = 1; y < 33; y++)
    182                 for (int x = 1; x < 33; x++)
    183                         temp[x][y] = 128.0f + CloudEffect::noise(x,  y,  random) * 128.0f;
    184 
    185         // Seamless cloud
    186         for (int x=1; x<33; x++)
    187         {
    188                 temp[0][x] = temp[32][x];
    189                 temp[33][x] = temp[1][x];
    190                 temp[x][0] = temp[x][32];
    191                 temp[x][33] = temp[x][1];
    192         }
    193         temp[0][0] = temp[32][32];
    194         temp[33][33] = temp[1][1];
    195         temp[0][33] = temp[32][1];
    196         temp[33][0] = temp[1][32];
    197 
    198         // We mirror the side and corner elements so our final cloud will be seamless without any ugly borders showing.
    199         for (int y=1; y<33; y++)
    200                 for (int x=1; x<33; x++)
    201                 {
    202                         float center = temp[x][y]/4.0f;
    203                         float sides = (temp[x+1][y] + temp[x-1][y] + temp[x][y+1] + temp[x][y-1])/8.0f;
    204                         float corners = (temp[x+1][y+1] + temp[x+1][y-1] + temp[x-1][y+1] + temp[x-1][y-1])/16.0f;
    205 
    206                         map[((x-1)*32) + (y-1)] = center + sides + corners;
    207                 }
    208 }
    209 
    210 /*
    211         Interpolation - average the value of each pixel value with that of its neighbors' values.
    212 */
    213 float CloudEffect::interpolate(float x, float y, float  *map)
    214 {
    215         int Xint = (int)x;
    216         int Yint = (int)y;
    217 
    218         float Xfrac = x - Xint;
    219         float Yfrac = y - Yint;
    220 
    221         int X0 = Xint % 32;
    222         int Y0 = Yint % 32;
    223         int X1 = (Xint + 1) % 32;
    224         int Y1 = (Yint + 1) % 32;
    225 
    226         float bot = map[X0*32 + Y0] + Xfrac * (map[X1*32 + Y0] - map[X0*32 + Y0]);
    227         float top = map[X0*32 + Y1] + Xfrac * (map[X1*32 +  Y1] - map[X0*32 + Y1]);
    228 
    229         return (bot + Yfrac * (top - bot));
    230 }
    231 
    232 
    233 /*
    234         Octaves are overlapped together to give cloud more turbulence. We will use four octaves for our cloud.
    235 */
    236 void CloudEffect::overlapOctaves()
    237 {
    238         for (int x=0; x<256*256; x++)
    239         {
    240                 cloudMap256[x] = 0;
    241         }
    242 
    243         for (int octave=0; octave<4; octave++)
    244                 for (int x=0; x<256; x++)
    245                         for (int y=0; y<256; y++)
    246                         {
    247                                 float scale = 1 / pow(2.0f, (float) 3-octave);
    248                                 float noise = CloudEffect::interpolate(x*scale, y*scale , cloudMap32_c);
    249 
    250                                 //The octaves are added together with the proper weight factors.
    251                                 //You could replace pow(2, i) with 1<<i for faster computation
    252                                 cloudMap256[(y*256) + x] += noise / pow(2.0f, (float) octave);
    253                         }
    254 }
    255 
    256 
    257 /*
    258         Filter the noise with exponential function
    259 */
    260 void CloudEffect::expFilter()
    261 {
    262         float cover = 20.0f;
    263         float sharpness = 0.95f;
    264 
    265         for (int x=0; x<256*256; x++)
    266         {
    267                 float c = cloudMap256[x] - (255.0f-cover);
    268                 if (c<0)     c = 0;
    269                 cloudMap256[x] = 255.0f - ((float)(pow(sharpness, c))*255.0f);
    270         }
    271 }
    272 
    273 
  • branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h

    r7810 r8052  
    1313#include "sound_buffer.h"
    1414#include "sound_source.h"
     15
     16class Material;
     17
    1518
    1619class CloudEffect : public WeatherEffect
     
    5760                void                                                                                                            genCloudTexture();
    5861                void                                                                                                            calcAnimMap(float timer);
     62                Material*       material;
    5963
    6064};
  • branches/atmospheric_engine/src/lib/graphics/effects/rain_effect.cc

    r8023 r8052  
    3535CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
    3636
    37 // TODO: Dim Light with Rain, Finish preCaching, check out if multiple rain emitters work,  Think about what happens with building poss. to hang movewithcam off, sound volume, benchmark, possible to activate lightening, turn off visibility when in a building, variable emitter size depending on playable, also rain velocity
     37// TODO: Dim Light with Rain, Finish preCaching, check out if multiple rain emitters work,  Think about what happens with building poss. to hang movewithcam off, benchmark, possible to activate lightening, turn off visibility when in a building, variable emitter size depending on playable, also rain velocity
    3838
    3939RainEffect::RainEffect(const TiXmlElement* root)
     
    139139        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
    140140       
    141         this->soundSource.loop(this->rainBuffer);
     141        this->soundSource.loop(this->rainBuffer, 0.8f);
    142142        if (this->rainWindForce > 0)
    143                 this->soundSource.loop(this->windBuffer);
     143                this->soundSource.loop(this->windBuffer, 0.5f);
    144144}
    145145
  • branches/atmospheric_engine/src/lib/sound/sound_source.cc

    r7810 r8052  
    11/*
    2    orxonox - the future of 3D-vertical-scrollers
    3 
    4    Copyright (C) 2004 orx
    5 
    6    This program is free software; you can redistribute it and/or modify
    7    it under the terms of the GNU General Public License as published by
    8    the Free Software Foundation; either version 2, or (at your option)
    9    any later version.
    10 
    11    ### File Specific:
    12    main-programmer: Benjamin Grauer
    13    co-programmer: ...
     2        orxonox - the future of 3D-vertical-scrollers
     3
     4        Copyright (C) 2004 orx
     5
     6        This program is free software; you can redistribute it and/or modify
     7        it under the terms of the GNU General Public License as published by
     8        the Free Software Foundation; either version 2, or (at your option)
     9        any later version.
     10
     11        ### File Specific:
     12        main-programmer: Benjamin Grauer
     13        co-programmer: ...
    1414*/
    1515
     
    2424namespace OrxSound
    2525{
    26   /**
    27    * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
    28    */
    29   SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer)
    30   {
    31     this->setClassID(CL_SOUND_SOURCE, "SoundSource");
    32 
    33     // adding the Source to the SourcesList of the SoundEngine
    34     this->buffer = buffer;
    35     this->sourceNode = sourceNode;
    36     this->resident = false;
    37 
    38     this->sourceID = 0;
    39     this->bPlay = false;
    40   }
    41 
    42 
    43   /**
    44    * @brief construct a SoundSource out of the another soundSource
    45    * @param source the Source to create this source from.
    46    *
    47    * Copies the buffer from source to this Source.
    48    * Acquires a new SourceID if source is Playing.
    49    */
    50   SoundSource::SoundSource(const SoundSource& source)
    51   {
    52     this->setClassID(CL_SOUND_SOURCE, "SoundSource");
    53 
    54     // adding the Source to the SourcesList of the SoundEngine
    55     this->buffer = source.buffer;
    56     this->sourceNode = source.sourceNode;
    57     this->resident = source.resident;
    58 
    59     this->sourceID = 0;
    60     if (source.bPlay == true)
    61     {
    62       this->bPlay = true;
    63       SoundEngine::getInstance()->popALSource(this->sourceID);
    64     }
    65     else
    66       this->bPlay = false;
    67   }
    68 
    69 
    70   /**
    71    * @brief paste a copy of the source into this Source.
    72    * @param source the SoundSource to paste into this one.
    73    * @returns a Reference to this Source.
    74    *
    75    */
    76   SoundSource& SoundSource::operator=(const SoundSource& source)
    77   {
    78     this->buffer = source.buffer;
    79     this->sourceNode = sourceNode;
    80     this->resident = source.resident;
    81 
    82     if (source.bPlay)
    83       this->play();
    84     else
    85       this->stop();
    86   }
    87 
    88 
    89   /**
    90    * @brief compares two Sources with each other.
    91    * @param source the Source to compare against this One.
    92    * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
    93    * The alSource must not match, because no two Sources can have the same alSource.
    94    */
    95   bool SoundSource::operator==(const SoundSource& source)
    96   {
    97     return (this->buffer == source.buffer &&
    98             this->bPlay == source.bPlay &&
    99             this->sourceNode == source.sourceNode);
    100   }
    101 
    102 
    103   /**
    104    * @brief deletes a SoundSource
    105    */
    106   SoundSource::~SoundSource()
    107   {
    108     this->stop();
    109     if (this->sourceID != 0)
    110       SoundEngine::getInstance()->pushALSource(this->sourceID);
    111   }
    112 
    113 
    114   /**
    115    * @brief Plays back a SoundSource
    116    */
    117   void SoundSource::play()
    118   {
    119     if (this->buffer && this->retrieveSource())
    120     {
    121       if (this->bPlay)
    122         alSourceStop(this->sourceID);
    123       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
    124     alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE  );
    125     alSourcePlay(this->sourceID);
    126 
    127       if (DEBUG_LEVEL >= 3)
    128         SoundEngine::checkError("Play Source", __LINE__);
    129       this->bPlay = true;
    130     }
    131   }
    132 
    133 
    134   /**
    135    * @brief Plays back buffer on this Source
    136    * @param buffer the buffer to play back on this Source
    137    */
    138   void SoundSource::play(const SoundBuffer* buffer)
    139   {
    140     if (!this->retrieveSource())
    141     {
    142       PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
    143       return;
    144     }
    145 
    146   alSourceStop(this->sourceID);
    147   alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
    148   alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE  );
    149   alSourcePlay(this->sourceID);
    150 
    151     if (unlikely(this->buffer != NULL))
    152       alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
    153     this->bPlay = true;
    154 
    155     if (DEBUG_LEVEL >= 3)
    156       SoundEngine::checkError("Play Source", __LINE__);
    157   }
    158 
     26        /**
     27        * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
     28        */
     29        SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer)
     30        {
     31                this->setClassID(CL_SOUND_SOURCE, "SoundSource");
     32
     33                // adding the Source to the SourcesList of the SoundEngine
     34                this->buffer = buffer;
     35                this->sourceNode = sourceNode;
     36                this->resident = false;
     37
     38                this->sourceID = 0;
     39                this->bPlay = false;
     40        }
     41
     42
     43        /**
     44        * @brief construct a SoundSource out of the another soundSource
     45        * @param source the Source to create this source from.
     46        *
     47        * Copies the buffer from source to this Source.
     48        * Acquires a new SourceID if source is Playing.
     49        */
     50        SoundSource::SoundSource(const SoundSource& source)
     51        {
     52                this->setClassID(CL_SOUND_SOURCE, "SoundSource");
     53
     54                // adding the Source to the SourcesList of the SoundEngine
     55                this->buffer = source.buffer;
     56                this->sourceNode = source.sourceNode;
     57                this->resident = source.resident;
     58
     59                this->sourceID = 0;
     60                if (source.bPlay == true)
     61                {
     62                        this->bPlay = true;
     63                        SoundEngine::getInstance()->popALSource(this->sourceID);
     64                }
     65                else
     66                        this->bPlay = false;
     67        }
     68
     69
     70        /**
     71        * @brief paste a copy of the source into this Source.
     72        * @param source the SoundSource to paste into this one.
     73        * @returns a Reference to this Source.
     74        *
     75        */
     76        SoundSource& SoundSource::operator=(const SoundSource& source)
     77        {
     78                this->buffer = source.buffer;
     79                this->sourceNode = sourceNode;
     80                this->resident = source.resident;
     81
     82                if (source.bPlay)
     83                        this->play();
     84                else
     85                        this->stop();
     86        }
     87
     88
     89        /**
     90        * @brief compares two Sources with each other.
     91        * @param source the Source to compare against this One.
     92        * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
     93        * The alSource must not match, because no two Sources can have the same alSource.
     94        */
     95        bool SoundSource::operator==(const SoundSource& source)
     96        {
     97                return (this->buffer == source.buffer &&
     98                                                this->bPlay == source.bPlay &&
     99                                                this->sourceNode == source.sourceNode);
     100        }
     101
     102
     103        /**
     104        * @brief deletes a SoundSource
     105        */
     106        SoundSource::~SoundSource()
     107        {
     108                this->stop();
     109                if (this->sourceID != 0)
     110                        SoundEngine::getInstance()->pushALSource(this->sourceID);
     111        }
     112
     113
     114        /**
     115        * @brief Plays back a SoundSource
     116        */
     117        void SoundSource::play()
     118        {
     119                if (this->buffer && this->retrieveSource())
     120                {
     121                        if (this->bPlay)
     122                                alSourceStop(this->sourceID);
     123                       
     124                        alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     125                        alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
     126                        alSourcef (this->sourceID, AL_GAIN, 1);
     127                        alSourcePlay(this->sourceID);
     128
     129                        if (DEBUG_LEVEL >= 3)
     130                                SoundEngine::checkError("Play Source", __LINE__);
     131                        this->bPlay = true;
     132                }
     133        }
     134
     135
     136        /**
     137        * @brief Plays back buffer on this Source
     138        * @param buffer the buffer to play back on this Source
     139        */
     140        void SoundSource::play(const SoundBuffer* buffer)
     141        {
     142                if (!this->retrieveSource())
     143                {
     144                        PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
     145                        return;
     146                }
     147
     148                alSourceStop(this->sourceID);
     149                alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
     150                alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
     151                alSourcef (this->sourceID, AL_GAIN, 1);
     152
     153                alSourcePlay(this->sourceID);
     154
     155                if (unlikely(this->buffer != NULL))
     156                        alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     157                this->bPlay = true;
     158
     159                if (DEBUG_LEVEL >= 3)
     160                        SoundEngine::checkError("Play Source", __LINE__);
     161        }
     162
     163
     164        /**
     165        * @brief Plays back buffer on this Source with gain
     166        * @param buffer the buffer to play back on this Source
     167        */
     168        void SoundSource::play(const SoundBuffer* buffer, float gain)
     169        {
     170                if (!this->retrieveSource())
     171                {
     172                        PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
     173                        return;
     174                }
     175
     176                alSourceStop(this->sourceID);
     177                alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
     178                alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
     179                alSourcef (this->sourceID, AL_GAIN, gain);
     180
     181                alSourcePlay(this->sourceID);
     182
     183                if (unlikely(this->buffer != NULL))
     184                        alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     185                this->bPlay = true;
     186
     187                if (DEBUG_LEVEL >= 3)
     188                        SoundEngine::checkError("Play Source", __LINE__);
     189        }
     190
     191
     192        /**
     193        * @brief Plays and loops back a SoundSource
     194        */
     195        void SoundSource::loop()
     196        {
     197                if (this->buffer && this->retrieveSource())
     198                {
     199                        if (this->bPlay)
     200                                alSourceStop(this->sourceID);
     201                       
     202                        alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     203                        alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
     204                        alSourcef (this->sourceID, AL_GAIN, 1);
     205                        alSourcePlay(this->sourceID);
     206
     207                        if (DEBUG_LEVEL >= 3)
     208                                SoundEngine::checkError("Play Source", __LINE__);
     209                        this->bPlay = true;
     210                }
     211        }
    159212
    160213/**
    161  * @brief Plays and loops buffer on this Source
    162  * @param buffer the buffer to play back on this Source
    163  */
     214* @brief Plays and loops buffer on this Source
     215* @param buffer the buffer to play back on this Source
     216*/
    164217void SoundSource::loop(const SoundBuffer* buffer)
    165218{
    166   if (this->buffer && this->retrieveSource())
    167   {
    168     if (this->bPlay)
    169       alSourceStop(this->sourceID);
    170 
    171     alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
    172     alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE  );
    173     alSourcePlay(this->sourceID);
    174 
    175     if (DEBUG_LEVEL >= 3)
    176       SoundEngine::checkError("Play LoopSource", __LINE__);
    177     this->bPlay = true;
    178   }
     219        if (this->buffer && this->retrieveSource())
     220        {
     221                if (this->bPlay)
     222                        alSourceStop(this->sourceID);
     223
     224                alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     225                alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
     226                alSourcef (this->sourceID, AL_GAIN, 1);
     227
     228                alSourcePlay(this->sourceID);
     229
     230                if (DEBUG_LEVEL >= 3)
     231                        SoundEngine::checkError("Play LoopSource", __LINE__);
     232                this->bPlay = true;
     233        }
    179234}
    180235
    181236
    182237/**
    183  * @brief Stops playback of a SoundSource
    184  */
     238* @brief Plays and loops buffer on this Source with gain control
     239* @param buffer the buffer to play back on this Source
     240*/
     241void SoundSource::loop(const SoundBuffer* buffer, float gain)
     242{
     243        if (this->buffer && this->retrieveSource())
     244        {
     245                if (this->bPlay)
     246                        alSourceStop(this->sourceID);
     247
     248                alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     249                alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
     250                alSourcef (this->sourceID, AL_GAIN, gain);
     251
     252                alSourcePlay(this->sourceID);
     253
     254                if (DEBUG_LEVEL >= 3)
     255                        SoundEngine::checkError("Play LoopSource", __LINE__);
     256                this->bPlay = true;
     257        }
     258}
     259
     260
     261/**
     262* @brief Stops playback of a SoundSource
     263*/
    185264void SoundSource::stop()
    186265{
    187   this->bPlay = false;
    188   if (this->sourceID != 0)
    189   {
    190     this->bPlay = false;
    191     if (this->sourceID != 0)
    192     {
    193       alSourceStop(this->sourceID);
    194       if (DEBUG_LEVEL >= 3)
    195         SoundEngine::checkError("StopSource", __LINE__);
    196       alSourcei(this->sourceID, AL_BUFFER, 0);
    197       if (!this->resident)
    198         SoundEngine::getInstance()->pushALSource(this->sourceID);
    199       this->sourceID = 0;
    200     }
    201   }
     266        this->bPlay = false;
     267        if (this->sourceID != 0)
     268        {
     269                this->bPlay = false;
     270                if (this->sourceID != 0)
     271                {
     272                        alSourceStop(this->sourceID);
     273                        if (DEBUG_LEVEL >= 3)
     274                                SoundEngine::checkError("StopSource", __LINE__);
     275                        alSourcei(this->sourceID, AL_BUFFER, 0);
     276                        if (!this->resident)
     277                                SoundEngine::getInstance()->pushALSource(this->sourceID);
     278                        this->sourceID = 0;
     279                }
     280        }
    202281}
    203282
    204   /**
    205    * @brief Pauses Playback of a SoundSource
    206    */
    207   void SoundSource::pause()
    208   {
    209     alSourcePause(this->sourceID);
    210     if (DEBUG_LEVEL >= 3)
    211       SoundEngine::checkError("Pause Source", __LINE__);
    212   }
    213 
    214 
    215   /**
    216    * @brief Rewinds Playback of a SoundSource
    217    */
    218   void SoundSource::rewind()
    219   {
    220     alSourceRewind(this->sourceID);
    221 
    222     if (DEBUG_LEVEL >= 3)
    223       SoundEngine::checkError("Rewind Source", __LINE__);
    224   }
    225 
    226 
    227   /**
    228    * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
    229    * @param rolloffFactor The Factor described
    230    *
    231    * this tells openAL how fast the Sounds decay outward from the Source
    232    */
    233   void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
    234   {
    235     alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
    236 
    237     if (DEBUG_LEVEL >= 3)
    238       SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
    239   }
    240 
    241 
    242   /**
    243    * @brief sets the Positional this Source should be attached to.
    244    * @param sourceNode the Source this is attached to.
    245    * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
    246    */
    247   void SoundSource::setSourceNode(const PNode* sourceNode)
    248   {
    249     this->sourceNode = sourceNode;
    250   }
    251 
    252   /**
    253    * @brief retrieve a Source.
    254    */
    255   bool SoundSource::retrieveSource()
    256   {
    257     if (this->sourceID != 0)
    258       return true;
    259     else
    260     {
    261       SoundEngine::getInstance()->popALSource(this->sourceID);
    262       if (this->sourceID != 0)
    263       {
    264         if (unlikely(this->sourceNode == NULL))
    265           resetSource(this->sourceID);
    266         return true;
    267       }
    268     }
    269     return false;
    270   }
    271 
    272 
    273   /**
    274    * @brief reset an alSource to its default Values.
    275    */
    276   void SoundSource::resetSource(ALuint sourceID)
    277   {
    278     alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
    279     alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
    280     alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
    281     alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
    282     //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
    283     alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
    284   }
     283        /**
     284        * @brief Pauses Playback of a SoundSource
     285        */
     286        void SoundSource::pause()
     287        {
     288                alSourcePause(this->sourceID);
     289                if (DEBUG_LEVEL >= 3)
     290                        SoundEngine::checkError("Pause Source", __LINE__);
     291        }
     292
     293
     294        /**
     295        * @brief Rewinds Playback of a SoundSource
     296        */
     297        void SoundSource::rewind()
     298        {
     299                alSourceRewind(this->sourceID);
     300
     301                if (DEBUG_LEVEL >= 3)
     302                        SoundEngine::checkError("Rewind Source", __LINE__);
     303        }
     304
     305
     306        /**
     307        * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
     308        * @param rolloffFactor The Factor described
     309        *
     310        * this tells openAL how fast the Sounds decay outward from the Source
     311        */
     312        void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
     313        {
     314                alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
     315
     316                if (DEBUG_LEVEL >= 3)
     317                        SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
     318        }
     319
     320
     321        /**
     322        * @brief sets the Positional this Source should be attached to.
     323        * @param sourceNode the Source this is attached to.
     324        * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
     325        */
     326        void SoundSource::setSourceNode(const PNode* sourceNode)
     327        {
     328                this->sourceNode = sourceNode;
     329        }
     330
     331        /**
     332        * @brief retrieve a Source.
     333        */
     334        bool SoundSource::retrieveSource()
     335        {
     336                if (this->sourceID != 0)
     337                        return true;
     338                else
     339                {
     340                        SoundEngine::getInstance()->popALSource(this->sourceID);
     341                        if (this->sourceID != 0)
     342                        {
     343                                if (unlikely(this->sourceNode == NULL))
     344                                        resetSource(this->sourceID);
     345                                return true;
     346                        }
     347                }
     348                return false;
     349        }
     350
     351
     352        /**
     353        * @brief reset an alSource to its default Values.
     354        */
     355        void SoundSource::resetSource(ALuint sourceID)
     356        {
     357                alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
     358                alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
     359                alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
     360                alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
     361                //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
     362                alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
     363        }
    285364}
  • branches/atmospheric_engine/src/lib/sound/sound_source.h

    r7810 r8052  
    2929    void play();
    3030    void play(const SoundBuffer* buffer);
     31    void play(const SoundBuffer* buffer, float gain);
    3132    void loop();
    3233    void loop(const SoundBuffer* buffer);
     34    void loop(const SoundBuffer* buffer, float gain);
    3335    void stop();
    3436    void pause();
Note: See TracChangeset for help on using the changeset viewer.