FS2_Open
Open source remastering of the Freespace 2 engine
ds3d.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "cmdline/cmdline.h"
13 #include "globalincs/pstypes.h"
14 #include "object/object.h"
15 #include "sound/channel.h"
16 #include "sound/ds.h"
17 #include "sound/ds3d.h"
18 #include "sound/openal.h"
19 #include "sound/sound.h"
20 
21 
22 // ---------------------------------------------------------------------------------------
23 // ds3d_update_buffer()
24 //
25 // parameters: channel => identifies the 3D sound to update
26 // min => the distance at which sound doesn't get any louder
27 // max => the distance at which sound doesn't attenuate any further
28 // pos => world position of sound
29 // vel => velocity of the objects producing the sound
30 //
31 // returns: 0 => success
32 // -1 => failure
33 //
34 //
35 int ds3d_update_buffer(int channel_id, float min, float max, vec3d *pos, vec3d *vel)
36 {
37  if (Cmdline_no_3d_sound) {
38  nprintf(("Sound", "Aborting ds3d_update_buffer due to Cmdline_no_3d_sound..."));
39  return -1;
40  }
41 
42  if (channel_id < 0) {
43  return 0;
44  }
45 
46  ALuint source_id = Channels[channel_id].source_id;
47  ALfloat rolloff = 1.0f;
48 
49  if (pos) {
50  OpenAL_ErrorPrint( alSource3f(source_id, AL_POSITION, pos->xyz.x, pos->xyz.y, -pos->xyz.z) );
51  }
52 
53  if (vel) {
54  OpenAL_ErrorPrint( alSource3f(source_id, AL_VELOCITY, vel->xyz.x, vel->xyz.y, vel->xyz.z) );
55  //OpenAL_ErrorPrint( alDopplerFactor(1.0f) );
56  } else {
57  OpenAL_ErrorPrint( alSource3f(source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f) );
58  OpenAL_ErrorPrint( alDopplerFactor(0.0f) );
59  }
60 
61  if (max <= min) {
62  rolloff = 0.0f;
63  } else {
64  #define MIN_GAIN 0.05f
65 
66  rolloff = (min / (min + (max - min))) / MIN_GAIN;
67 
68  if (rolloff < 0.0f) {
69  rolloff = 0.0f;
70  }
71  }
72 
73  OpenAL_ErrorPrint( alSourcef(source_id, AL_ROLLOFF_FACTOR, rolloff) );
74 
75  OpenAL_ErrorPrint( alSourcef(source_id, AL_REFERENCE_DISTANCE, min) );
76  OpenAL_ErrorPrint( alSourcef(source_id, AL_MAX_DISTANCE, max) );
77 
78  return 0;
79 }
80 
81 
82 // ---------------------------------------------------------------------------------------
83 // ds3d_update_listener()
84 //
85 // returns: 0 => success
86 // -1 => failure
87 //
89 {
90  if (Cmdline_no_3d_sound) {
91  nprintf(("Sound", "Aborting ds3d_update_listener due to Cmdline_no_3d_sound..."));
92  return -1;
93  }
94 
95  if (pos) {
96  OpenAL_ErrorPrint( alListener3f(AL_POSITION, pos->xyz.x, pos->xyz.y, -pos->xyz.z) );
97  }
98 
99  if (vel) {
100  OpenAL_ErrorPrint( alListener3f(AL_VELOCITY, vel->xyz.x, vel->xyz.y, vel->xyz.z) );
101  }
102 
103  if (orient) {
104  ALfloat alOrient[6];
105 
106  alOrient[0] = orient->vec.fvec.xyz.x;
107  alOrient[1] = orient->vec.fvec.xyz.y;
108  alOrient[2] = -orient->vec.fvec.xyz.z;
109 
110  alOrient[3] = orient->vec.uvec.xyz.x;
111  alOrient[4] = orient->vec.uvec.xyz.y;
112  alOrient[5] = -orient->vec.uvec.xyz.z;
113 
114  OpenAL_ErrorPrint( alListenerfv(AL_ORIENTATION, alOrient) );
115  }
116 
117  return 0;
118 }
119 
Definition: pstypes.h:88
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
ALuint source_id
Definition: channel.h:20
#define OpenAL_ErrorPrint(x)
Definition: openal.h:34
hull_check orient
Definition: lua.cpp:5049
int ds3d_update_buffer(int channel_id, float min, float max, vec3d *pos, vec3d *vel)
Definition: ds3d.cpp:35
struct matrix::@228::@230 vec
#define nprintf(args)
Definition: pstypes.h:239
int ds3d_update_listener(vec3d *pos, vec3d *vel, matrix *orient)
Definition: ds3d.cpp:88
#define MIN_GAIN
ALint *typedef ALfloat
Definition: ds.cpp:145
channel * Channels
Definition: ds.cpp:46
hull_check pos
Definition: lua.cpp:5050
int Cmdline_no_3d_sound
Definition: cmdline.cpp:446