📄 worleyshader.cpp
字号:
// Sh: A GPU metaprogramming language.//// Copyright 2003-2005 Serious Hack Inc.// // This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, // MA 02110-1301, USA//////////////////////////////////////////////////////////////////////////////#include <sh/sh.hpp>#include <sh/shutil.hpp>#include <iostream>#include "Shader.hpp"#include "Globals.hpp"using namespace SH;using namespace ShUtil;#include "util.hpp"class WorleyShader : public Shader {public: WorleyShader(std::string name, bool tex) : Shader(std::string("Worley: ") + (tex ? " Texture Hash: " : " Procedural: ") + name), useTexture(tex) {} virtual ~WorleyShader() {} ShProgram vertex() { return vsh;} ShProgram fragment() { return fsh;} // sets default vertex shader, phong fsh bool init(); // sets colours, specular, worley function specific to each subclass and uses algebra to // add on to the default phong shader virtual void initfsh() = 0; ShProgram vsh, fsh; // uniforms used by subclasses ShAttrib1f param, freq, exponent; /*static*/ ShAttrib4f coeff; ShColor3f color1, color2; //ShUtil::ShWorleyMetric metric; bool useTexture;};//ShAttrib4f WorleyShader::coeff;bool WorleyShader::init(){ std::cerr << "Initializing " << name() << std::endl; param.name("param"); param = .5; param.range(0.0f, 1.0f); freq.name("Worley frequency"); freq = 16.0; freq.range(0.1f, 128.0f); coeff.name("Worley coefficient"); coeff = ShConstAttrib4f(1.0, 0.0, 0.0, 0.0); coeff.range(-3.0f, 3.0f); exponent.name("specular exponent"); exponent = 35.0; exponent.range(10.0f, 500.0f); color1.name("color1"); color1.range(-2.0f, 2.0f); color2.name("color2"); color2.range(-2.0f, 2.0f); vsh = ShKernelLib::shVsh(Globals::mv, Globals::mvp, 1) << shExtract("lightPos") << Globals::lightPos; ShConstColor3f lightColor(1.0f, 1.0f, 1.0f); fsh = ShKernelSurface::phong<ShColor3f>() << shExtract("specExp") << exponent; fsh = fsh << shExtract("irrad") << lightColor; initfsh(); return true;}class GradientWorley: public WorleyShader {public: GradientWorley(bool useTexture): WorleyShader("Gradients", useTexture) {} void initfsh() { DefaultGenFactory<2, float> genFactory(useTexture); DistSqGradientPropFactory<2, float> propFactory; ShProgram worleysh = shWorley<4>(&genFactory, &propFactory); worleysh = worleysh << (shMul<ShTexCoord2f>("texcoord", "freq", "texcoord") << fillcast<2>(freq)); ShAttrib1f SH_DECL(bumpScale) = ShConstAttrib1f(1.0f); bumpScale.range(-10.0f, 10.0f); color1 = ShColor3f(1.0, 0.0, 0.0); color2 = ShColor3f(0.0, 1.0, 0.0); ShProgram color = SH_BEGIN_PROGRAM("gpu:fragment") { ShInputAttrib4f result[3]; // result[0] holds distances, [1], [2] hold the gradient ShInputPosition4f SH_DECL(posh); ShOutputColor3f SH_NAMEDECL(resultColor, "result"); ShAttrib2f gradient; gradient(0) = dot(result[1], coeff); gradient(1) = dot(result[2], coeff); resultColor = bumpScale * (color1 * gradient(0) + color2 * gradient(1)); } SH_END; fsh = color << worleysh; vsh = namedAlign(vsh, fsh); }};class OrganicWorley: public WorleyShader {public: OrganicWorley(bool useTexture): WorleyShader("Organic", useTexture) {} void initfsh() { DefaultGenFactory<2, float> genFactory(useTexture); DistSqGradientPropFactory<2, float> propFactory; ShProgram worleysh = shWorley<4>(&genFactory, &propFactory); worleysh = worleysh << (shMul<ShTexCoord2f>("texcoord", "freq", "texcoord") << fillcast<2>(freq)); ShAttrib1f SH_DECL(bumpScale) = ShConstAttrib1f(3.0f); bumpScale.range(-10.0f, 10.0f); color1 = ShColor3f(0.4, 0.0, 0.0); color2 = ShColor3f(0.2, 0.6, 0.2); ShConstAttrib1f ZERO(0.0f); ShConstAttrib1f ONE(1.0f); // TODO when bump mapper kernel properly supports transformation btw // coordinate spaces and handles all used auxillary vectors (hv, etc.) // remove most of this code. ShProgram colorAndBump = SH_BEGIN_PROGRAM() { ShInputAttrib4f result[3]; // 0 holds distances, 1-2 hold gradient ShInputVector3f SH_DECL(tangent) = normalize(tangent); ShInputVector3f SH_DECL(tangent2) = normalize(tangent2); ShInputVector3f SH_DECL(lightVec) = normalize(lightVec); ShInOutNormal3f SH_DECL(normal) = normalize(normal); ShOutputColor3f SH_DECL(kd); ShOutputColor3f SH_DECL(ks); ShOutputVector3f SH_DECL(halfVec); ShAttrib1f dist = dot(coeff, result[0]); ShAttrib2f grad; grad(0) = dot(coeff, result[1]); grad(1) = dot(coeff, result[2]); kd = ks = lerp(clamp(dist, ZERO, ONE), color1, color2); normal += bumpScale * (grad(0) * tangent + grad(1) * tangent2); normal = normalize(normal); halfVec = 0.5f * (normal + lightVec); } SH_END; fsh = namedConnect((colorAndBump << worleysh), fsh); vsh = namedAlign(vsh, fsh); }};class BlueOrbWorley: public WorleyShader {public: BlueOrbWorley(bool useTexture): WorleyShader("BlueOrb", useTexture) {} void initfsh() { DefaultGenFactory<2, float> genFactory(useTexture); Dist_InfGradientPropFactory<2, float> propFactory; Dist_1PropFactory<2, float> dist1Factory; coeff = ShConstAttrib4f(-1.0f, 1.0f, 0.0f, 0.0f); ShAttrib1f SH_DECL(frequency) = ShConstAttrib1f(16.0f); frequency.range(0.0f, 256.0f); ShProgram worleysh = shWorley<4>(&genFactory, &propFactory); worleysh = worleysh << (shMul<ShTexCoord2f>("texcoord", "freq", "texcoord") << fillcast<2>(freq)); ShAttrib4f SH_DECL(innerCoeff) = coeff; ShAttrib1f SH_DECL(innerFreq) = ShConstAttrib1f(32.0f); innerFreq.range(0.0f, 256.0f); ShProgram innersh = shWorley<4>(&genFactory, &dist1Factory); innersh = innersh << (shMul<ShTexCoord2f>("texcoord", "freq", "texcoord") << fillcast<2>(innerFreq)); worleysh = namedCombine(worleysh, innersh); ShAttrib1f SH_DECL(bumpScale) = ShConstAttrib1f(1.0f); bumpScale.range(-10.0f, 10.0f); ShColor3f SH_DECL(latticeColor) = ShColor3f(0.0f, 0.0f, 0.0f); ShColor3f SH_DECL(latticeSpecular) = ShColor3f(0.5f, 0.1f, 0.1f); color1 = ShColor3f(0.0f, 0.0f, 0.0f); color2 = ShColor3f(0.25f, 0.25f, 1.0f); ShColor3f SH_DECL(specular) = ShColor3f(0.3f, 0.3f, 0.3f); ShAttrib1f SH_NAMEDECL(threshold, "Less Than Threshold") = ShConstAttrib1f(0.2f); threshold.range(-1.0f, 1.0f); ShAttrib1f SH_NAMEDECL(threshold2, "Greater Than Threshold") = ShConstAttrib1f(1.0f); threshold2.range(-1.0f, 1.0f); ShConstAttrib1f ZERO(0.0f); ShConstAttrib1f ONE(1.0f); ShProgram colorAndBump = SH_BEGIN_PROGRAM() { ShInputAttrib4f result[3]; ShInputAttrib4f innerResult; ShInputVector3f SH_DECL(tangent) = normalize(tangent); ShInputVector3f SH_DECL(tangent2) = normalize(tangent2); ShInputVector3f SH_DECL(lightVec) = normalize(lightVec); ShInOutNormal3f SH_DECL(normal) = normalize(normal); ShOutputColor3f SH_DECL(kd); ShOutputColor3f SH_DECL(ks); ShOutputVector3f SH_DECL(halfVec); // determine if we're in one of the bumps in the outer lattice work ShAttrib1f dist = coeff | result[0]; ShAttrib1f inBump = (dist < threshold) + (dist > threshold2); // figure out inner colour and select kd/ks based on whether we're in the bumps ShColor3f innerColor = lerp(innerCoeff | innerResult, color1, color2); kd = lerp(inBump, latticeColor, innerColor); ks = lerp(inBump, latticeSpecular, specular); // do the bump mapping (TODO use bump function/nibble) ShAttrib2f grad; grad(0) = dot(coeff, result[1]); grad(1) = dot(coeff, result[2]); normal += inBump * bumpScale * (grad(0) * tangent + grad(1) * tangent2); normal = normalize(normal); halfVec = 0.5f * (normal + lightVec); } SH_END; fsh = namedConnect((colorAndBump << worleysh), fsh); vsh = namedAlign(vsh, fsh); }};class PolkaDotWorley: public WorleyShader {public: PolkaDotWorley(bool useTexture): WorleyShader("Polka Dot", useTexture) {} void initfsh() { ShProgram worleysh = shWorley<4, 2, float>(useTexture); worleysh = worleysh << (shMul<ShTexCoord2f>("texcoord", "freq", "texcoord") << fillcast<2>(freq)); worleysh = (shDot<ShAttrib4f>() << coeff) << worleysh; // make polkadots by clamping the scalar result from worley ShProgram polkash = SH_BEGIN_PROGRAM() { ShInOutAttrib1f SH_DECL(scalar) = clamp((scalar + param - 0.75f) * 30.0f, 0.0f, 1.0f); } SH_END; worleysh = polkash << worleysh; color1 = ShColor3f(0.27, 0.35, 0.45); color2 = ShColor3f(1.0, 0.7, 0.0); ShColor3f specularColor(0.5, 0.5, 0.5); ShProgram colorsh = shLerp<ShColor3f, ShAttrib1f>("kd") << color1 << color2; // kd is a lerp based on the worley scalar colorsh = colorsh & ( keep<ShColor3f>("ks") << specularColor); fsh = fsh << colorsh << worleysh; vsh = namedAlign(vsh, fsh); }};class LavaWorley: public WorleyShader {public: LavaWorley(bool useTexture): WorleyShader("Lava", useTexture) {} void initfsh() { coeff = ShConstAttrib4f(-1, 1.2, 0, 0); ShAttrib4f SH_NAMEDECL(coeff2, "Worley coefficient 2") = ShConstAttrib4f(0, 1, 1, 0); ShAttrib1f SH_NAMEDECL(freq2, "Worley frequency 2") = freq * 2.131313f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -