00001 /* -*- C++ -*- */ 00002 00003 /* COPYRIGHT 00004 * 00005 * This file is part of the Visual Effects Engine - VEE 00006 * 00007 * Read the "VEE-LICENSE" file for the license. 00008 * 00009 * Authors & Copyright: 00010 * 00011 * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 00012 * Janne Kontkanen 2001-2004 00013 * 00014 * Additional copyrights: Tekes 2003-2004 00015 * 00016 * firstname.lastname@hut.fi 00017 * 00018 */ 00019 00020 #ifndef VEE_RANDOM_H 00021 #define VEE_RANDOM_H 00022 00023 #include <vee_vector3.h> 00024 00029 class VEE_Random 00030 { 00031 public: 00032 VEE_Random() {} 00033 virtual ~VEE_Random() {} 00034 00036 virtual float rand() = 0; 00037 virtual VEE_Random * clone() = 0; 00038 }; 00039 00042 00047 class VEE_RandomUniform : public VEE_Random 00048 { 00049 public: 00050 VEE_RandomUniform(); 00051 virtual ~VEE_RandomUniform(); 00052 00053 virtual float rand(); 00054 virtual VEE_Random * clone(); 00055 00056 inline float rand01() 00057 { 00058 unsigned tmp = m_val * m_randMul + 1; 00059 m_val = tmp; 00060 return (float) tmp * (1.0 / (float) ((unsigned) 0xffffffff)); 00061 } 00062 00063 inline float rand0X(float x) 00064 { 00065 unsigned tmp = m_val * m_randMul + 1; 00066 m_val = tmp; 00067 return (float) tmp * (x / (float) ((unsigned) 0xffffffff)); 00068 } 00069 00071 inline float rand11() 00072 { 00073 unsigned tmp = m_val * m_randMul + 1; 00074 m_val = tmp; 00075 return (float) tmp * (2.0f / (float) ((unsigned) 0xffffffff)) - 1.0f; 00076 } 00077 00078 00079 VEE_Vector3 randomVector11() 00080 { 00081 float xr = rand11(); 00082 float yr = rand11(); 00083 float zr = rand11(); 00084 return VEE_Vector3(xr, yr, zr); 00085 } 00086 00087 VEE_Vector3 randomVectorInSphere() 00088 { 00089 VEE_Vector3 tmp; 00090 00091 do { 00092 tmp = randomVector11(); 00093 } while(tmp.lengthSqr() > 1.0f); 00094 00095 return tmp; 00096 } 00097 VEE_Vector3 randomVectorOnSphere() 00098 { 00099 VEE_Vector3 tmp = randomVectorInSphere(); 00100 tmp.normalize(); 00101 return tmp; 00102 } 00103 00104 void setSeed(unsigned seed) { m_val = seed; } 00105 00106 protected: 00108 unsigned m_val; 00109 static const unsigned m_randMul = 134695621; 00110 }; 00111 00112 extern VEE_RandomUniform VEE_rand; 00113 00114 #endif