Beispiel #1
0
// copied from the orange book
GlTexture* NoiseGenerator::make3Dnoise(int size, float ampStart, float ampDiv, int startFrequency)
{

    int f, i, j, k, inc;
    //int startFrequency = 4;
    int numOctaves = NUM_OCT; //4;
    double ni[3];
    double inci, incj, inck;
    int frequency = startFrequency;
    double amp = ampStart;

    double minn = 100.0, maxn = -100.0;

// 	uint bufsize = noise3DTexSize * noise3DTexSize * noise3DTexSize * 4;
// 	if ((noise3DTexPtr = (GLubyte *) malloc(bufsize)) == nullptr)
// 	{
// 		printf("ERROR: Could not allocate 3D noise texture - %d bytes\n", bufsize);
// 		return nullptr;
// 	}
    Space3D<Vec4b> noise3DTexPtr(size, size, size);

    byte *ptr = noise3DTexPtr.ptr()->v;

    for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= ampDiv)
    {
        setNoiseFrequency(frequency);
        ni[0] = ni[1] = ni[2] = 0;

        inci = 1.0 / (size / frequency);
        for (i = 0; i < size; ++i, ni[0] += inci)
        {
            incj = 1.0 / (size / frequency);
            for (j = 0; j < size; ++j, ni[1] += incj)
            {
                inck = 1.0 / (size / frequency);
                for (k = 0; k < size; ++k, ni[2] += inck, ptr+= 4)
                {
                    double ns = noise3(ni) * N_FACT;
                    *(ptr+inc) = (GLubyte)(((ns+1.0) * amp)*255.0);
                    if (ns > maxn)
                        maxn = ns;
                    if (ns < minn)
                        minn = ns;
                }
            }
        }
    }

    //GlTexture *tex = new GlTexture();
    //tex->init(GL_TEXTURE_3D, QSize(size, size), size, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 
    //		 noise3DTexPtr, GL_LINEAR, GL_LINEAR, GL_REPEAT); //GL_NEAREST

    printf("%lf - %lf = %lf\n", maxn, minn, maxn - minn);
    Vec2i sz2d;
    uchar* buf2d = saveto2D(noise3DTexPtr, size, size/4, 8, &sz2d);

    GlTexture *tex = new GlTexture();

    tex->init(GL_TEXTURE_2D, Vec2i(sz2d.x, sz2d.y), 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, buf2d, GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
    
/*	
    QImage img(buf2d, sz2d.x, sz2d.y, QImage::Format_ARGB32);
    img.save("c:/temp/cubeTex.jpg");
*/
    delete[] buf2d;

    return tex;

}