📄 blend.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. *//** * blend.cpp - Includes all code related to blending shaders * @author Russel Ryan * Never compile on its own, just 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_v//blend.cpp holds all the blend related code// never compile this on its own, it's included from material.cpp just to make things neater// you have to ask yourself, 'But Will it Blend?'MATERIAL_SHADER(material_BlenderShade) { dprintf("material_BlenderShade\r\n"); vector float _zero = spu_splats(0.0f); union { vector unsigned int ids; struct { unsigned int id1, id2, id3, id4; }; }; ids = hp.materialID; 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 pw = _zero; vector float px_temp, py_temp, pz_temp, pw_temp; vector float px_res, py_res, pz_res; px_res = py_res = pz_res = _zero; vector unsigned int thisID; vector float matrix[16]; //Do material1's matrix splat_matrix4x4((vector float*)&matrix, &materials[id1].colorDSRT_r); xform_vec3_v(&px_temp, &py_temp, &pz_temp, &pw_temp, px, py, pz, (vector float*)&matrix); thisID = spu_cmpeq(hp.materialID, spu_splats(id1)); px_res = spu_sel(px_res, px_temp, thisID); py_res = spu_sel(py_res, py_temp, thisID); pz_res = spu_sel(pz_res, pz_temp, thisID); //pw_res = spu_sel(pw_res, pw_temp, thisID); //Do material2's matrix splat_matrix4x4((vector float*)&matrix, &materials[id2].colorDSRT_r); xform_vec3_v(&px_temp, &py_temp, &pz_temp, &pw_temp, px, py, pz, (vector float*)&matrix); thisID = spu_cmpeq(hp.materialID, spu_splats(id2)); px_res = spu_sel(px_res, px_temp, thisID); py_res = spu_sel(py_res, py_temp, thisID); pz_res = spu_sel(pz_res, pz_temp, thisID); //pw_res = spu_sel(pw_res, pw_temp, thisID); //Do material3's matrix splat_matrix4x4((vector float*)&matrix, &materials[id3].colorDSRT_r); xform_vec3_v(&px_temp, &py_temp, &pz_temp, &pw_temp, px, py, pz, (vector float*)&matrix); thisID = spu_cmpeq(hp.materialID, spu_splats(id3)); px_res = spu_sel(px_res, px_temp, thisID); py_res = spu_sel(py_res, py_temp, thisID); pz_res = spu_sel(pz_res, pz_temp, thisID); //pw_res = spu_sel(pw_res, pw_temp, thisID); //Do material4's matrix splat_matrix4x4((vector float*)&matrix, &materials[id4].colorDSRT_r); xform_vec3_v(&px_temp, &py_temp, &pz_temp, &pw_temp, px, py, pz, (vector float*)&matrix); thisID = spu_cmpeq(hp.materialID, spu_splats(id4)); px_res = spu_sel(px_res, px_temp, thisID); py_res = spu_sel(py_res, py_temp, thisID); pz_res = spu_sel(pz_res, pz_temp, thisID); //pw_res = spu_sel(pw_res, pw_temp, thisID); union { vector unsigned int matTypes; struct { unsigned int mat1_type; unsigned int mat2_type; unsigned int mat3_type; unsigned int mat4_type; }; }; matTypes = (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 type_eq_blender = spu_or(spu_cmpeq(spu_splats((unsigned int)MATERIAL_WOOD), matTypes), spu_cmpeq(spu_splats((unsigned int)MATERIAL_MARBLE), matTypes)); vector unsigned int valid = spu_and(shadeBits, type_eq_blender); union { vector unsigned int functions; struct { material_shader_blendfactor f1; material_shader_blendfactor f2; material_shader_blendfactor f3; material_shader_blendfactor f4; }; }; functions = (vector unsigned int){ (unsigned int)fp_shader[mat1_type].blendfactor, (unsigned int)fp_shader[mat2_type].blendfactor, (unsigned int)fp_shader[mat3_type].blendfactor, (unsigned int)fp_shader[mat4_type].blendfactor}; vector unsigned int zero_i = spu_splats((unsigned int)0); vector unsigned int ones_i = spu_splats((unsigned int)1); union { vector float blendFactors_v; struct { float blendFactor[4]; }; }; union { vector unsigned int materials_v; struct { material *material1; material *material2; material *material3; material *material4; }; }; material1 = &materials[id1]; material2 = &materials[id2]; material3 = &materials[id3]; material4 = &materials[id4]; blendFactors_v = _zero; thisID = spu_cmpeq(matTypes, spu_splats(mat1_type)); (*f1)(materials_v, blendFactors_v, px_res, py_res, pz_res, thisID); functions = spu_sel(functions, spu_splats((unsigned int)material_getBlendFactorDummy), thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat2_type)); (*f2)(materials_v, blendFactors_v, px_res, py_res, pz_res, thisID); functions = spu_sel(functions, spu_splats((unsigned int)material_getBlendFactorDummy), thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat3_type)); (*f3)(materials_v, blendFactors_v, px_res, py_res, pz_res, thisID); functions = spu_sel(functions, spu_splats((unsigned int)material_getBlendFactorDummy), thisID); thisID = spu_cmpeq(matTypes, spu_splats(mat4_type)); (*f4)(materials_v, blendFactors_v, px_res, py_res, pz_res, thisID); //functions = spu_sel(functions, spu_splats((unsigned int)material_getBlendFactorDummy), thisID); vector unsigned int mat1s = (vector unsigned int){materials[id1].arg1_i, materials[id2].arg1_i, materials[id3].arg1_i, materials[id4].arg1_i}; vector unsigned int mat2s = (vector unsigned int){materials[id1].arg2_i, materials[id2].arg2_i, materials[id3].arg2_i, materials[id4].arg2_i}; RGBPacket shade1, shade2; shade1.r = shade2.r = rgbp.r; shade1.g = shade2.g = rgbp.g; shade1.b = shade2.b = rgbp.b; HitPacket newhp; newhp.nx = hp.nx; newhp.ny = hp.ny; newhp.nz = hp.nz; newhp.t = hp.t; newhp.materialID = mat1s; newhp.materialID = spu_sel(spu_splats((unsigned int)0), newhp.materialID, valid); materialShade(materials, shade1, rp, newhp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, valid); newhp.materialID = mat2s; newhp.materialID = spu_sel(spu_splats((unsigned int)0), newhp.materialID, valid); materialShade(materials, shade2, rp, newhp, dirToLight_x, dirToLight_y, dirToLight_z, lightColor_r, lightColor_g, lightColor_b, valid); // result = (1-t) * v1 + t * v2 lerp_vec3_v(&shade1.r, &shade1.g, &shade1.b, shade2.r, shade2.g, shade2.b, shade1.r, shade1.g, shade1.b, blendFactors_v); rgbp.r = spu_sel(rgbp.r, shade1.r, valid); rgbp.g = spu_sel(rgbp.g, shade1.g, valid); rgbp.b = spu_sel(rgbp.b, shade1.b, valid); return;}MATERIAL_SHADER_BLENDFACTOR(material_getWoodBlendFactor) { union { vector unsigned int materials_v; struct { material* material1; material* material2; material* material3; material* material4; }; }; //n = PerlinNoise::octave_noise(pt, m_octaves) //m = sin(m_frequency * sqrt(pt[0]*pt[0] + pt[1]*pt[1]) + m_amplitude *n) //m = (m+1.0f)/2.0f; //shift from [-1 1] to [0, 1] materials_v = materials; vector float freq = (vector float){material1->arg1_f, material2->arg1_f, material3->arg1_f, material4->arg1_f}; vector float ampl = (vector float){material1->arg2_f, material2->arg2_f, material3->arg2_f, material4->arg2_f}; vector float noise = noise3_v(p_x, p_y, p_z); //vector float noise = fractalsum3_v(p_x, p_y, p_z, freq, ampl); ampl = spu_mul(ampl, noise); vector float m = sqrtf4(spu_add(spu_mul(p_x, p_x), spu_mul(p_y, p_y))); freq = spu_mul(freq, m); // m = sin18_v(spu_add(freq, ampl)); m = sinf4(spu_add(freq, ampl)); //shift from [-1 1] -> [0 1] m = spu_add(m, spu_splats(1.0f)); m = divf4(m, spu_splats(2.0f)); blendFactors = spu_sel(blendFactors, m, shadeBits); return;}MATERIAL_SHADER_BLENDFACTOR(material_getMarbleBlendFactor) { union { vector unsigned int materials_v; struct { material* material1; material* material2; material* material3; material* material4; }; }; //n = PerlinNoise::octave_noise(pt, m_octaves) //m = sin(m_frequency * sqrt(pt[0]*pt[0] + pt[1]*pt[1]) + m_amplitude *n) //m = (m+1.0f)/2.0f; //shift from [-1 1] to [0, 1] materials_v = materials; vector float noise, freq, ampl; freq = (vector float){0.5f, 0.5f, 0.5f, 0.5f}; ampl = (vector float){3.0f, 3.0f, 3.0f, 3.0f}; // TODO: changed noise = fractalsum3_v(p_x, p_y, p_z, freq, ampl); //noise = noise3_v(p_x, p_y, p_z); freq = (vector float){material1->arg1_f, material2->arg1_f, material3->arg1_f, material4->arg1_f}; ampl = (vector float){material1->arg2_f, material2->arg2_f, material3->arg2_f, material4->arg2_f}; ampl = spu_mul(ampl, noise); freq = spu_mul(freq, p_x); //vector float m = sin18_v(spu_add(freq, ampl)); vector float m = sinf4(spu_add(freq, ampl)); //shift from [-1 1] -> [0 1] m = spu_add(m, spu_splats(1.0f)); m = divf4(m, spu_splats(2.0f)); blendFactors = spu_sel(blendFactors, m, shadeBits); return;}MATERIAL_SHADER_DIFFUSE(material_getBlenderDiffuseColor) { dprintf("material_getBlenderDiffuseColor\r\n"); vector float _zero = spu_splats(0.0f); union { vector unsigned int ids; struct { unsigned int id1, id2, id3, id4; }; }; ids = hp.materialID; vector float px_temp, py_temp, pz_temp, pw_temp; vector float px_res, py_res, pz_res; px_res = py_res = pz_res = _zero; vector unsigned int thisID; vector float matrix[16]; //Do material1's matrix splat_matrix4x4((vector float*)&matrix, &materials[id1].colorDSRT_r);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -