⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpumathlib_textures.cpp

📁 这是在GPU上进行高速数字计算的数学库函数
💻 CPP
字号:
/*********************************************************************************** The header file for the gpumathlib class** The Jahshaka Project** Copyright (C) 2000-2004 The Jahshaka Project.** Released under the GNU General Public License*********************************************************************************/#include "gpumathlib.h"// build permuation/gradient table for noisevoid initVertexNoiseConstants(int table_size, int*& permutation_table, float4*& gradient_table, int gradient_size){    int 					i;    int 					random_index;    int						temp;    permutation_table = new int[gradient_size];			// permutation table    gradient_table = new float4[gradient_size];  			// gradient table    // initalize random gradients    for(i = 0; i < table_size; i++)    {        permutation_table[i] = i;        gradient_table[i].x = sfrand();        gradient_table[i].y = sfrand();        gradient_table[i].z = sfrand();        normalize3f(gradient_table[i].x, gradient_table[i].y, gradient_table[i].z);    }    // initialize permutation table (random shuffle)    for(i = 0; i < table_size; i++)    {        random_index = (rand() >> 4) % table_size;        temp = permutation_table[i];        permutation_table[i] = permutation_table[random_index];        permutation_table[random_index] = temp;        gradient_table[i].w = (float)permutation_table[i];        gradient_table[(2 * table_size) - 1 - i].x = gradient_table[i].x;        gradient_table[(2 * table_size) - 1 - i].y = gradient_table[i].y;        gradient_table[(2 * table_size) - 1 - i].z = gradient_table[i].z;        gradient_table[(2 * table_size) - 1 - i].w = gradient_table[i].w;    }}void create_random_texture( int width, int height, GLuint & random_texture ){    GLubyte* texels = new GLubyte[width * height * 4];    int 						random;    for( GLint y = 0; y < height; y++ )    {        for( GLint x = 0; x < width; x++ )        {            random = rand();            texels[ (y * width * 4) + (x * 4) + 0 ] = random % 256;		// red            texels[ (y * width * 4) + (x * 4) + 1 ] = random % 256;		// green            texels[ (y * width * 4) + (x * 4) + 2 ] = random % 256;		// blue            texels[ y*width*4 + x*4 + 3 ] = 255;						// alpha        }    }    random_texture = 0;    glGenTextures( 1, &random_texture );    glBindTexture( GL_TEXTURE_2D, random_texture );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels );    delete texels;}void create_contrast_texture( int width, int height, double density, double exponent,GLuint & contrast_texture ){    /* A white texture is generated. Afterwards black sprinkles are added.    * These are shifted towards the 0 value of the y axis to archieve sort of a gradient.    */    GLubyte* texels = new GLubyte[width * height * 4];    int 							x;    int 							y;    for( y = 0; y < height; y++ )    {        for( x = 0; x < width; x++ )        {            texels[ (y * width * 4) + (x * 4) + 0 ] = 255;				// red            texels[ (y * width * 4) + (x * 4) + 1 ] = 255;				// green            texels[ (y * width * 4) + (x * 4) + 2 ] = 255;				// blue            texels[ (y * width * 4) + (x * 4) + 3 ] = 255;				// alpha        }    }    for( int i = 0; i < (width * height); i++ )    {        x = (int)( ((double)rand()/(double)RAND_MAX) * (double)(width - 1.0) );        y = (int)( (pow((double)rand() / (double)RAND_MAX, fabs(exponent))) * (double)(height - 1.0) );        texels[ (y * width * 4) + (4 * x) + 0 ] = 0;        texels[ (y * width * 4) + (4 * x) + 1 ] = 0;        texels[ (y * width * 4) + (4 * x) + 2 ] = 0;    }    contrast_texture = 0;    glGenTextures( 1, &contrast_texture );    glBindTexture( GL_TEXTURE_2D, contrast_texture );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels );    delete texels;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -