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

📄 bump.cpp

📁 Ray tracing on PS3, using the acceleration of PPU, No SPE acceleration is used. The code must be com
💻 CPP
字号:
/* Copyright (c) 2007 Massachusetts Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//** * bump.cpp - Implementation for bumpy material routines. * @author Russel Ryan * Never compile by itself... let material.cpp include it. */#include <lerp_vec4.h>#define lerp_vec4 _lerp_vec4#include <noise3_v.h>#define noise3_v _noise3_v#include <fractalsum3_v.h>#define fractalsum3_v _fractalsum3_v//#include <sin18_v.h>//#define sin18_v _sin18_vvoid material_bumpNormal(material* materials, HitPacket &hp, const vector float p_x, const vector float p_y, const vector float p_z, const vector unsigned int shadeBits) {    union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;  vector float px, py, pz;  vector float nx, ny, nz;  vector float f0, fx, fy, fz;  vector float density, ampl;  vector float epsilon;  vector float minf, maxf;  minf  = (vector float){90.0f, 90.0f, 90.0f, 90.0f};  maxf = (vector float){100.0f, 100.0f, 100.0f, 100.0f};    density = (vector float){materials[id1].arg1_f, materials[id2].arg1_f, materials[id3].arg1_f, materials[id4].arg1_f};  ampl = (vector float){materials[id1].arg2_f, materials[id2].arg2_f, materials[id3].arg2_f, materials[id4].arg2_f};    px = spu_mul(p_x, density);  py = spu_mul(p_y, density);  pz = spu_mul(p_z, density);  epsilon = (vector float){0.0001f, 0.0001f, 0.0001f, 0.0001f};    f0 = fractalsum3_v(px, py, pz, minf, maxf);  fx = fractalsum3_v(spu_add(px, epsilon), py, pz, minf, maxf);  fy = fractalsum3_v(px, spu_add(py, epsilon), pz, minf, maxf);    fz = fractalsum3_v(px, py, spu_add(pz, epsilon), minf, maxf);    f0 = spu_mul(f0, ampl);  fx = spu_mul(fx, ampl);  fy = spu_mul(fy, ampl);  fz = spu_mul(fz, ampl);    nx = spu_sub(hp.nx, divf4(spu_sub(fx, f0), epsilon));  ny = spu_sub(hp.ny, divf4(spu_sub(fy, f0), epsilon));  nz = spu_sub(hp.nz, divf4(spu_sub(fz, f0), epsilon));    normalize3_v(&nx, &ny, &nz, nx, ny, nz);    vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);    //newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  hp.nx = spu_sel(hp.nx, nx, valid);  hp.ny = spu_sel(hp.ny, ny, valid);  hp.nz = spu_sel(hp.nz, nz, valid);  }MATERIAL_SHADER(material_BumpShade) {  dprintf("material_BumpShade\r\n");    union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;  //vector float px = spu_add(rp.x0, spu_mul(rp.dx, hp.t));  //vector float py = spu_add(rp.y0, spu_mul(rp.dy, hp.t));  //vector float pz = spu_add(rp.z0, spu_mul(rp.dz, hp.t));  vector float px = spu_madd(rp.dx, hp.t, rp.x0);  vector float py = spu_madd(rp.dy, hp.t, rp.y0);  vector float pz = spu_madd(rp.dz, hp.t, rp.z0);    vector float nx, ny, nz;  union {    vector float f0;    struct {      float f0_e[4];    };  };  union {    vector float fx;    struct {      float fx_e[4];    };  }; union {    vector float fy;    struct {      float fy_e[4];    };  }; union {    vector float fz;    struct {      float fz_e[4];    };  };  vector float density, ampl;  vector float epsilon;  vector float minf, maxf;  minf  = (vector float){90.0f, 90.0f, 90.0f, 90.0f};  maxf = (vector float){100.0f, 100.0f, 100.0f, 100.0f};    density = (vector float){materials[id1].arg1_f, materials[id2].arg1_f, materials[id3].arg1_f, materials[id4].arg1_f};  ampl = (vector float){materials[id1].arg2_f, materials[id2].arg2_f, materials[id3].arg2_f, materials[id4].arg2_f};    px = spu_mul(px, density);  py = spu_mul(py, density);  pz = spu_mul(pz, density);  epsilon = (vector float){0.0001f, 0.0001f, 0.0001f, 0.0001f};  f0 = fractalsum3_v(px, py, pz, minf, maxf);  fx = fractalsum3_v(spu_add(px, epsilon), py, pz, minf, maxf);  fy = fractalsum3_v(px, spu_add(py, epsilon), pz, minf, maxf);    fz = fractalsum3_v(px, py, spu_add(pz, epsilon), minf, maxf);    f0 = spu_mul(f0, ampl);  fx = spu_mul(fx, ampl);  fy = spu_mul(fy, ampl);  fz = spu_mul(fz, ampl);    nx = spu_sub(hp.nx, divf4(spu_sub(fx, f0), epsilon));  ny = spu_sub(hp.ny, divf4(spu_sub(fy, f0), epsilon));  nz = spu_sub(hp.nz, divf4(spu_sub(fz, f0), epsilon));    normalize3_v(&nx, &ny, &nz, nx, ny, nz);    vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);  vector unsigned int mats = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i};    HitPacket newhp;  newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  newhp.nx = nx;  newhp.ny = ny;  newhp.nz = nz;  newhp.t = hp.t;  materialShade(materials, rgbp, rp, newhp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, valid);  }MATERIAL_SHADER_DIFFUSE(material_getBumpDiffuseColor) {  dprintf("material_getBumpDiffuseColor\r\n");    union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;  vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);  vector unsigned int mats = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i};    HitPacket newhp;  newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  newhp.nx = hp.nx;  newhp.ny = hp.ny;  newhp.nz = hp.nz;  newhp.t = hp.t;    material_getDiffuseColor(materials, rgbp, newhp, p_x, p_y, p_z, valid);  }MATERIAL_SHADER_REFLECTIVE(material_getBumpReflectiveColor) {  dprintf("material_getBumpReflectiveColor\r\n");      union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;  vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);  vector unsigned int mats = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i};    HitPacket newhp;  newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  newhp.nx = hp.nx;  newhp.ny = hp.ny;  newhp.nz = hp.nz;  newhp.t = hp.t;    material_getReflectiveColor(materials, rgbp, newhp, p_x, p_y, p_z, valid);  }MATERIAL_SHADER_TRANSPARENT(material_getBumpTransparentColor) {  dprintf("material_getBumpTransparentColor\r\n");    union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;  vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);  vector unsigned int mats = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i};    HitPacket newhp;  newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  newhp.nx = hp.nx;  newhp.ny = hp.ny;  newhp.nz = hp.nz;  newhp.t = hp.t;    material_getTransparentColor(materials, rgbp, newhp, p_x, p_y, p_z, valid);  }MATERIAL_SHADER_REFRACTIVE(material_getBumpRefractiveIndex) {  dprintf("material_getBumpRefractiveIndex\r\n");    union {    vector unsigned int ids;    struct {      unsigned int id1, id2, id3, id4;    };  };  ids = hp.materialID;    vector unsigned int type_eq_bump = spu_cmpeq(spu_splats((unsigned int)MATERIAL_BUMP), (vector unsigned int){(unsigned int)materials[id1].materialType, (unsigned int)materials[id2].materialType, (unsigned int)materials[id3].materialType, (unsigned int)materials[id4].materialType});    vector unsigned int valid = spu_and(type_eq_bump, shadeBits);  vector unsigned int mats = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i};    HitPacket newhp;  newhp.materialID = spu_sel(spu_splats((unsigned int)0), mats, valid);  newhp.nx = hp.nx;  newhp.ny = hp.ny;  newhp.nz = hp.nz;  newhp.t = hp.t;  material_getRefractiveIndex(materials, rindices, newhp, p_x, p_y, p_z, valid);  }

⌨️ 快捷键说明

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