Exemplo n.º 1
0
unsigned int TextureLoader::CreateNoiseTexture(double alpha, double beta, int n, int side) {
	Perlin perlin;
	float* data = new float[side*side*side];
	double step = 1.0/(side-1.0);
	int index = 0;
	for (double x = 0; x <= 1; x += step) {
		for (double y = 0; y <= 1; y += step) {
			for(double z = 0; z <= 1; z += step) {
				float noise = static_cast<float>(perlin.PerlinNoise3D(x, y, z, alpha, beta, n));
				data[index] = noise;
				index++;
			}
		}
	}

	//create the OpenGL texture
	unsigned int gl_texture_object;
	glGenTextures(1, &gl_texture_object);
	glBindTexture(GL_TEXTURE_3D, gl_texture_object);

	//filtering
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	float maxAnisotropy;
	glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
	glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);

	//when we work with textures of sizes not divisible by 4 we have to use the line reader
	//which loads the textures in OpenGL so as it can work with a 1 alligned memory (default is 4)
	//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	//Generates texture
	glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, side, side, side, 0, GL_RED, GL_FLOAT, data);

	//eliminates the array from the RAM
	delete data;

	//creates the mipmap hierarchy
	glGenerateMipmap(GL_TEXTURE_3D);

	//returns the texture object
	return gl_texture_object;
}