Eigene Dateien/FlowVis/src/VWhiteNoise.cpp

Go to the documentation of this file.
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);        // random seed
00010 
00011         // choose one of the random number generators:
00012         mRandomNumberGenerator = TRandomCombined<CRandomMother, CRandomMersenne>(seed);           // make instance of random number generator
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 /* = 20 */ )
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         //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mWidth, mHeight, GL_RGBA, GL_FLOAT, &(tmp[0]));
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         //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mWidth, mHeight, GL_RGBA, GL_FLOAT, &(tmp[0]));
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         /*float x = 0.0f;
00157         for (int i = 0; i < m_Steps; ++i)
00158         {
00159                 float u = mRandomNumberGenerator.Random();
00160                 x += u;
00161         }               
00162         
00163         x -= (float) m_Steps / 2.0f;
00164         x *= sqrt(12.0f / (float)m_Steps);
00165         x = x / (float)m_Steps + 0.5f;
00166         */
00167         //float s = 100.0f;
00168         //float v1;
00169         //float v2;
00170 
00171         //while ( s >= 1.0f)
00172         //{
00173         //      float u1 = mRandomNumberGenerator.Random();
00174         //      float u2 = mRandomNumberGenerator.Random();
00175         //      v1 = 2.0f * u1 - 1.0f;
00176         //      v2 = 2.0f * u2 - 1.0f;
00177         //      s = v1 * v1 + v2 *  v2;
00178         //}
00179         //float X = sqrt(-2.0f * log(s) / s) * v1;
00180         //float Y = sqrt(-2.0f * log(s) / s) * v2;
00181 
00182         /*const static int q = 15;
00183         const static float c1 = (1 << q) - 1;
00184         const static float c2 = ((int)(c1 / 3)) + 1;
00185         const static float c3 = 1.f / c1;
00186 
00187         float random = mRandomNumberGenerator.Random();
00188         float x = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3;
00189         */
00190         float x = mRandomNumberGenerator.Random();
00191         return x;
00192         //float desc = mRandomNumberGenerator.Random();
00193 
00194         //if(desc > 0.5f)
00195         //{
00196         //      return X;
00197         //}
00198         //else
00199         //{
00200         //      return Y;
00201         //}
00202 }

Generated on Mon Jan 21 01:15:16 2008 for FlowVis by  doxygen 1.5.4