#include "../framework/framework.hpp"
#include "../framework/object/objects/cube_object.hpp"
class noisemod3f{
public:
noisemod3f(int mod) {
m_mod = mod;
m_and_mod = mod-1;
if((m_and_mod & (m_mod-1) )+1 != m_mod) g_log.error << "Unable to use " << mod << " as modulo in noisemod3f" << endl;
p = new int[mod*2];
int *permutation = new int[mod];
for (int i=0; i < mod; ++i) permutation[i] = i;
for (int i=0; i < mod; ++i) {
int temp = (int)((float)mod*( 0.5f*(random(i)+1.0)));
int pertemp = permutation[ temp ];
permutation[ temp ] = permutation[ i ];
permutation[ i ] = pertemp;
}
for (int i=0; i < mod; ++i) p[mod+i] = p[i] = permutation[i];
delete[] permutation;
}
~noisemod3f() {
delete p;
}
float random( int x) {
x = (x<<13) ^ x;
return ( 1.0 - (float)( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
}
float noise(float x, float y, float z) {
int X = (int)floor(x) & m_and_mod,
Y = (int)floor(y) & m_and_mod,
Z = (int)floor(z) & m_and_mod;
x -= floor(x);
y -= floor(y);
z -= floor(z);
float u = fade(x),
v = fade(y),
w = fade(z);
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z,
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z;
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1),
grad(p[BA+1], x-1, y , z-1)),
lerp(u, grad(p[AB+1], x , y-1, z-1),
grad(p[BB+1], x-1, y-1, z-1))));
}
float noise( float x, float y, float z, float persistence, int octaves) {
float total = 0;
float normalize = 0;
float frequency = 1.0f;
float amplitude = persistence;
for(int i=0; i<octaves; ++i) {
total += noise( x*frequency, y*frequency, z*frequency) * amplitude;
normalize += amplitude;
frequency *= 2;
amplitude *= persistence;
}
return total / normalize;
}
float fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
float lerp(float t, float a, float b) { return a + t * (b - a); }
float grad(int hash, float x, float y, float z) {
int h = hash & 15;
float u = h<8||h==12||h==13 ? x : y,
v = h<4||h==12||h==13 ? y : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
int *p;
int m_mod, m_and_mod;
};
class material_perlin : public material_base
{
public:
material_perlin( kernel* n_kernel, int n_width, int n_height, texture_format n_format ) {
m_kernel = n_kernel;
m_width = n_width;
m_height = n_height;
m_tex = m_kernel->g_texture_manager->create_texture( n_width, n_height, n_format );
m_tex->set_use_clamp( true );
m_tex_normalmap = m_kernel->g_texture_manager->create_texture( n_width, n_height, n_format );
m_tex_normalmap->set_use_clamp( true );
m_bpp_t = m_tex->get_bpp()/8;
m_wavelength = 12.0f;
m_amplitude = 0.5f;
m_function = 1;
m_tex_data = new unsigned char[ m_width*m_height*m_bpp_t ];
m_normalmap_data = new unsigned char[ m_width*m_height*m_bpp_t ];
m_tex_noise = new unsigned char[ m_width*m_height ];
m_normalmap_noise = new unsigned char[ m_width*m_height ];
m_tex->set_data( m_tex_data );
m_tex_normalmap->set_data( m_normalmap_data );
m_noise_buffer = new float[ n_width * n_height ];
m_noise = new noisemod3f*[8];
m_noise[7] = new noisemod3f(256);
m_noise[6] = new noisemod3f(128);
m_noise[5] = new noisemod3f( 64);
m_noise[4] = new noisemod3f( 32);
m_noise[3] = new noisemod3f( 16);
m_noise[2] = new noisemod3f( 8);
m_noise[1] = new noisemod3f( 4);
m_noise[0] = new noisemod3f( 2);
create_palette(white, green);
update_noise_buffer();
}
~material_perlin() {
delete m_noise;
delete[] m_tex_data;
delete[] m_normalmap_data;
delete[] m_tex_noise;
delete[] m_normalmap_noise;
}
float noise( int mod, float x, float y, float z, float persistence, int octaves) {
float total = 0;
float normalize = 0;
float frequency = 1.0f;
float amplitude = persistence;
for(int i=0; i<octaves; ++i) {
total += m_noise[mod]->noise( x*frequency, y*frequency, z*frequency) * amplitude;
mod++;
normalize += amplitude;
frequency *= 2;
amplitude *= persistence;
}
return total / normalize;
}
void set_amplitude( float n_amplitude ) {
if(n_amplitude != m_amplitude) {
m_amplitude = n_amplitude;
update_textures();
}
}
void set_wavelength( float n_wavelength ) {
if(n_wavelength != m_wavelength) {
m_wavelength = n_wavelength;
update_textures();
}
}
void set_function( int n_function ) {
if(n_function != m_function) {
m_function = n_function;
update_textures();
}
}
void update_palette(color color1, color color2) {
if(color1 != m_color1 || color2 != m_color2) {
m_color1 = color1;
m_color2 = color2;
create_palette( color1, color2 );
update_texture();
}
}
void create_palette(color color1, color color2) {
color temp_color;
for(int i=0; i<256; ++i) {
temp_color = color1 + ((float)i/255.0f)*(color2 - color1);
m_palette[ i * 3 + 0 ] = (unsigned char)(255.0f * temp_color.r);
m_palette[ i * 3 + 1 ] = (unsigned char)(255.0f * temp_color.g);
m_palette[ i * 3 + 2 ] = (unsigned char)(255.0f * temp_color.b);
}
}
float get_wavelength() { return m_wavelength; }
void update_noise_buffer() {
for(int x=0; x<m_width; x++)
for(int y=0; y<m_height; y++) {
m_noise_buffer[x + y*m_width] = noise(1,(float)x/64.0f, (float)y/64.0f, 0, 0.95f, 4);
}
/* noise buffer updated, update texture */
update_textures();
}
void update_textures() {
int value;
float temp;
float width_div_wavelength = m_width/m_wavelength;
int offset1 = 0;
int i, x, y;
#define loop(f) for(x=0; x<256; x++) \
for(y=0; y<256; y++) { \
temp = m_noise_buffer[offset1]*m_amplitude; \
f \
value = (unsigned char) (255.0f*temp); \
m_tex_noise [ offset1 ] = value; \
m_normalmap_noise [ offset1 ] = value; \
offset1++; \
}
#define loop_tex(f) offset1=0; \
for(x=0; x<256; x++) \
for(y=0; y<256; y++) { \
temp = m_noise_buffer[offset
perlin_material.zip_opengl 纹理生成_perlin open_visual c
版权申诉
115 浏览量
2022-09-21
04:06:50
上传
评论
收藏 468KB ZIP 举报
JonSco
- 粉丝: 66
- 资源: 1万+
最新资源
- 信呼OA系统2.1.7版源码
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
- 发那科工业机器人保养大全
- Sphere.h
- REMD固有时间尺度分解信号分量可视化(Matlab完整源码和数据)
- 嵌入式系统双单片机STC89C52+STC15W104多功能学习板电路图可扩展 适用于单片机初学者和教学
- 基于STM32蓝牙控制小车系统设计(硬件+源代码+论文)大赛作品
- XILINXFPGA源码基于Spartan3火龙刀系列FPGA开发板VGA测试例程
- Java聊天室的设计与实现【尚学堂·百战程序员】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈