1 ////////////////////////////////////////////////////////////
2 //
3 //	Shader UI Utility routines
4 //
5 
6 #ifndef SHADERUTIL_H
7 #define SHADERUTIL_H
8 
9 #include "ICompositeShader.h"
10 #include "expmtlControl.h"
11 
12 #define ALPHA_MIN	0.015f
13 #define ALPHA_MAX	0.5f
14 #define SPEC_MAX	0.5f
15 
16 #define ALPHA_SZ	(ALPHA_MAX - ALPHA_MIN)
17 
18 #define DEFAULT_GLOSS2	0.03f
19 #define DEFAULT_K_REFL	1.0f
20 
21 #define MIN_ORIENT		-999.99
22 #define MAX_ORIENT		999.99
23 
24 
25 class CombineComponentsCompShader : public Shader, public ISpecularCompositeShader,
26 								    public ExposureMaterialControl {
27 public:
CombineComponentsCompShader()28 	CombineComponentsCompShader() : useComposite(false) {}
29 
GetRequirements(int subMtlNum)30 	ULONG GetRequirements( int subMtlNum ){ return isNoExposure() | MTLREQ_PHONG | MTLREQ_PREPRO; }
31 	void CombineComponents( ShadeContext &sc, IllumParams& ip );
32 
33 	// [dl | 13march2003] Replaced this using statement by this inline function to
34 	// resolve compile errors.
35 	//using Shader::GetInterface;
GetInterface(ULONG id)36 	virtual void* GetInterface(ULONG id) { return Shader::GetInterface(id); }
37 
38 	virtual BaseInterface* GetInterface(Interface_ID id);
39 	virtual void ChooseSpecularMethod(TimeValue t, RenderGlobalContext* rgc);
40 
getUseComposite()41 	bool getUseComposite() { return useComposite; }
42 
43 private:
44 	bool	useComposite;
45 };
46 
47 
48 class CombineComponentsFPDesc : public ExposureMaterialControlDesc {
49 public:
CombineComponentsFPDesc(ClassDesc & cd)50 	CombineComponentsFPDesc(ClassDesc& cd) :
51 		ExposureMaterialControlDesc(
52 			cd,
53 			IDS_EXPOSURE_MATERIAL_CONTROL,
54 			IDS_NO_EXPOSURE,
55 			IDS_INVERTSELFILLUM,
56 			IDS_INVERTREFLECT,
57 			IDS_INVERTREFRACT
58 		) {}
59 };
60 
61 
62 void CombineComponentsAdd( IllumParams& ip );
63 
64 #define TRANSP_SUB		0
65 #define TRANSP_ADD		1
66 #define TRANSP_FILTER	2
67 
68 Color transpColor( ULONG flags, float opac, Color& filt, Color& diff );
69 
70 Color OrenNayarIllum( Point3& N, Point3& L, Point3& V, float rough, Color& rho, float* pDiffIntensOut, float NL = -2.0f );
71 
72 float GaussHighlight( float gloss, float aniso, float orient,
73 					  Point3& N, Point3& V, Point3& L, Point3& U, float* pNL );
74 
75 Point3 GetTangent( ShadeContext &sc, int uvChan );
76 
77 float GaussHiliteCurve2( float x, float y, float sLevel, float gloss, float aniso );
78 
79 BOOL IsButtonChecked(HWND hWnd,int id);
80 void CheckButton(HWND hWnd,int id, BOOL check);
81 
82 void SetupLockButton(HWND hWnd,int id, BOOL check);
83 void SetupPadLockButton(HWND hWnd,int id, BOOL check);
84 void LoadStdShaderResources();
85 
86 // 2D version
87 void DrawHilite(HDC hdc, Rect& rect, Shader* pShader );
88 LRESULT CALLBACK HiliteWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
89 
90 // anisotropic version, w layers: layer 0= ignore layers, 1=layer1, 2=layer2
91 void DrawHilite2(HDC hdc, Rect& rect, Shader* pShader, int layer=0 );
92 LRESULT CALLBACK Hilite2WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
93 LRESULT CALLBACK Hilite2Layer1WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
94 LRESULT CALLBACK Hilite2Layer2WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
95 void UpdateHilite( HWND hwHilite, Shader* pShader, int layer=0 );
96 void UpdateHilite2( HWND hwHilite, Shader* pShader, int layer=0 );
97 
98 
99 Color GetMtlColor( int i, Shader* pShader ) ;
100 TCHAR *GetColorName( int i );
101 //void SetMtlColor(int i, Color c, StdShader* pShader, ColorSwatch* cs);
102 void SetMtlColor(int i, Color c, Shader* pShader, IColorSwatch** cs, TimeValue t);
103 
PcToFrac(int pc)104 inline float PcToFrac(int pc) { return (float)pc/100.0f; }
105 
FracToPc(float f)106 inline int FracToPc(float f) {
107 	if (f<0.0) return (int)(100.0f*f - .5f);
108 	else return (int) (100.0f*f + .5f);
109 }
110 
111 // Soften polynomials
112 #define Soften	 SoftSpline2
113 
114 // Quadratic
SoftSpline2(float r)115 static inline float SoftSpline2(float r) {
116 	return r*(2.0f-r);
117 	}
118 
119 // Cubic
SoftSpline3(float r)120 static inline float SoftSpline3(float r) {
121 	return r*r*(3.0f-2.0f*r);
122 	}
123 
124 extern float const Pi;
125 extern float const Pi2;
126 
127 // General math inlines
Sqr(float x)128 inline float Sqr( float x ) { return x * x; }
Cube(float x)129 inline float Cube( float x ) { return x * x * x; }
Abs(float a)130 inline float Abs( float a ) { return (a < 0.0f) ? -a : a; }
MinMax(float & a,float & b)131 inline void  MinMax( float& a, float& b ) { if (a > b){ float tmp=a; a=b; b=tmp;} }
Dot(Point3 & a,Point3 & b)132 inline float Dot( Point3& a, Point3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Len2(Point3 & a)133 inline float Len2( Point3& a ) { return Dot(a,a); }
Len(Point3 & a)134 inline float Len( Point3& a ) { return float( sqrt( Len2(a))); }
Normalize(Point3 & a)135 inline Point3 Normalize( Point3& a ) { float d = Len(a); return d==0.0f ? Point3(0,0,1) : a*(1.0f/d); }
AngleBetween(Point3 & a,Point3 & b)136 inline float AngleBetween( Point3& a, Point3& b ) {
137 	return float( acos( Dot(a,b)/(Len(a)*Len(b)) ));
138 }
Min(float a,float b)139 inline float Min( float a, float b ) { return (a < b) ? a : b; }
Min(float a,float b,float c)140 inline float Min( float a, float b, float c ) { return (a < b) ? Min(a,c) : Min(b,c); }
Min(Color & c)141 inline float Min( Color& c ){ return Min( c.r, c.g, c.b ); }
Max(float a,float b)142 inline float Max( float a, float b ) { return (a < b) ? b : a; }
Max(float a,float b,float c)143 inline float Max( float a, float b, float c ) { return (a < b) ? Max( b, c ) : Max(a,c); }
Max(Color & c)144 inline float Max( Color& c ){ return Max( c.r, c.g, c.b ); }
145 
NormClr(Color & a)146 inline float NormClr( Color& a ){ float m = Max( a ); if(m !=0.0f) a /= m; return m; }
147 
Bound0_1(float x)148 inline float Bound0_1( float x ){ return x < 0.0f ? 0.0f : ( x > 1.0f ? 1.0f : x); }
149 inline float Bound( float x, float min = 0.0f, float max = 1.0f ){ return x < min? min:( x > max? max : x); }
150 inline float UBound( float x, float max = 1.0f ){ return x > max ? max : x; }
151 inline float LBound( float x, float min = 0.0f ){ return x < min ? min : x; }
Bound(Color & c)152 inline Color Bound( Color& c )
153 	{ return Color( Bound(c.r), Bound(c.g), Bound(c.b) ); }
154 
155 inline Color UBound( Color& c, float max = 1.0f )
156 	{ return Color( UBound(c.r,max), UBound(c.g,max), UBound(c.b,max) ); }
157 inline Color LBound( Color& c, float min = 0.0f )
158 	{ return Color( LBound(c.r, min), LBound(c.g, min), LBound(c.b, min) ); }
159 
160 //inline Point3 Cross( Point3 v0, Point3 v1 ){ return CrossProd( v0, v1 ); }
161 
162 
DegToRdn(float d)163 inline float DegToRdn( float d ){ return d * (1.0f/180.0f) * Pi; } // d/360*2*pi
RdnToDeg(float r)164 inline float RdnToDeg( float r ){ return r * 180.0f * (1.0f/Pi); } // r/2pi*360
165 
166 Point3 RotateVec( Point3& p, Point3& axis, float rdn );
167 
168 #endif