00001 #include "VWhiteNoise.h"
00002 #include "glew.h"
00003 #include <time.h>
00004 #include <iostream>
00005 #include <cmath>
00006
00007 VWhiteNoise::VWhiteNoise() : mTextureHandle(0), mWidth(0), mHeight(0), mRandomNumberGenerator(1024), mFrolicHandle(0), mFWidth(0), mFHeight(0)
00008 {
00009 int32 seed = (int32)time(0);
00010
00011
00012 mRandomNumberGenerator = TRandomCombined<CRandomMother, CRandomMersenne>(seed);
00013 }
00014
00015 VWhiteNoise::~VWhiteNoise()
00016 {
00017 if(mTextureHandle != 0)
00018 {
00019 glDeleteTextures(1, &mTextureHandle);
00020 mTextureHandle = 0;
00021 }
00022 mNoise.clear();
00023
00024 if(mFrolicHandle != 0)
00025 {
00026 glDeleteTextures(1, &mFrolicHandle);
00027 mFrolicHandle = 0;
00028 }
00029 mFrolic.clear();
00030 }
00031
00032 void VWhiteNoise::generate( int m_Width, int m_Height, int m_Steps )
00033 {
00034 mWidth = m_Width;
00035 mHeight = m_Height;
00036
00037 std::cout << "Generating White Noise of size " << m_Height << " x " << m_Height << std::endl;
00038 mNoise.clear();
00039
00040 for(int i = 0; i < (m_Width * m_Height); ++i)
00041 {
00042 mNoise.push_back(generateRandom(m_Steps));
00043 }
00044
00045 bindOpenGlTexture();
00046 }
00047
00048 void VWhiteNoise::generateFrolicTexture ( int m_Width, int m_Height)
00049 {
00050 mFWidth = m_Width;
00051 mFHeight = m_Height;
00052 mFrolic.clear();
00053
00054 mFrolic.resize(mFWidth * mFHeight);
00055
00056 for(int i = 0; i < (int)((float)(mFWidth * mFHeight) / 4.0f); ++i)
00057 {
00058 float x = generateRandom(20);
00059 float y = generateRandom(20);
00060
00061 int xind = (int)(x * (float)mFWidth);
00062 int yind = (int)(y * (float)mFHeight);
00063
00064 mFrolic[yind * mFWidth + xind] = 1.0f;
00065 }
00066
00067
00068 bindOpengGlFrolicTexture();
00069 }
00070
00071 void VWhiteNoise::bindOpenGlTexture()
00072 {
00073 std::vector<float> tmp;
00074
00075 for(int i = 0; i < mWidth * mHeight; ++i)
00076 {
00077 tmp.push_back(mNoise[i]);
00078 tmp.push_back(mNoise[i]);
00079 tmp.push_back(mNoise[i]);
00080 tmp.push_back(1.0f);
00081 }
00082
00083 if(mTextureHandle != 0)
00084 {
00085 glDeleteTextures(1, &mTextureHandle);
00086 }
00087
00088 glEnable(GL_TEXTURE_2D);
00089 glGenTextures(1, &mTextureHandle);
00090
00091 glBindTexture(GL_TEXTURE_2D, mTextureHandle);
00092
00093 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00094 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00095 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00096 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00097
00098 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA16F_ARB, mWidth, mHeight, GL_RGBA, GL_FLOAT, &(tmp[0]));
00099
00100
00101 glBindTexture(GL_TEXTURE_2D, 0);
00102 glDisable(GL_TEXTURE_2D);
00103
00104 const GLenum glError = glGetError();
00105
00106 if (glError != GL_NO_ERROR)
00107 std::cout << "Error Generating White Noise " << gluErrorString(glError) << std::endl;
00108 else
00109 std::cout << "Finished Generating White Noise." << std::endl;
00110 }
00111
00112 void VWhiteNoise::bindOpengGlFrolicTexture()
00113 {
00114 std::vector<float> tmp;
00115
00116 for(int i = 0; i < mFWidth * mFHeight; ++i)
00117 {
00118 tmp.push_back(mFrolic[i]);
00119 tmp.push_back(mFrolic[i]);
00120 tmp.push_back(mFrolic[i]);
00121 tmp.push_back(1.0f);
00122 }
00123
00124 if(mFrolicHandle != 0)
00125 {
00126 glDeleteTextures(1, &mFrolicHandle);
00127 }
00128
00129 glEnable(GL_TEXTURE_2D);
00130 glGenTextures(1, &mFrolicHandle);
00131
00132 glBindTexture(GL_TEXTURE_2D, mFrolicHandle);
00133
00134 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00135 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00138
00139 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA16F_ARB, mFWidth, mFHeight, GL_RGBA, GL_FLOAT, &(tmp[0]));
00140
00141
00142 glBindTexture(GL_TEXTURE_2D, 0);
00143 glDisable(GL_TEXTURE_2D);
00144
00145 const GLenum glError = glGetError();
00146
00147 if (glError != GL_NO_ERROR)
00148 std::cout << "Error Generating White Noise " << gluErrorString(glError) << std::endl;
00149 else
00150 std::cout << "Finished Generating White Noise." << std::endl;
00151 }
00152
00153
00154 float VWhiteNoise::generateRandom( int m_Steps )
00155 {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 float x = mRandomNumberGenerator.Random();
00191 return x;
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 }