pixSpecularGLSL.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "shaderGen/GLSL/pixSpecularGLSL.h"
  24. #include "materials/processedMaterial.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "shaderGen/shaderOp.h"
  27. #include "shaderGen/shaderGenVars.h"
  28. #include "gfx/gfxStructs.h"
  29. PixelSpecularGLSL::PixelSpecularGLSL()
  30. : mDep( "shaders/common/gl/lighting.glsl" )
  31. {
  32. addDependency( &mDep );
  33. }
  34. void PixelSpecularGLSL::processVert( Vector<ShaderComponent*> &componentList,
  35. const MaterialFeatureData &fd )
  36. {
  37. /*
  38. AssertFatal( fd.features[MFT_RTLighting],
  39. "PixelSpecularHLSL requires RTLighting to be enabled!" );
  40. MultiLine *meta = new MultiLine;
  41. // Get the eye world position.
  42. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  43. if( !eyePos )
  44. {
  45. eyePos = new Var;
  46. eyePos->setType( "float3" );
  47. eyePos->setName( "eyePosWorld" );
  48. eyePos->uniform = true;
  49. eyePos->constSortPos = cspPass;
  50. }
  51. // Grab a register for passing the
  52. // world space view vector.
  53. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  54. Var *wsView = connectComp->getElement( RT_TEXCOORD );
  55. wsView->setName( "wsView" );
  56. wsView->setStructName( "OUT" );
  57. wsView->setType( "float3" );
  58. // Get the input position.
  59. Var *position = (Var*)LangElement::find( "inPosition" );
  60. if ( !position )
  61. position = (Var*)LangElement::find( "position" );
  62. // Get the object to world transform.
  63. Var *objTrans = (Var*) LangElement::find( "objTrans" );
  64. if ( !objTrans )
  65. {
  66. objTrans = new Var;
  67. objTrans->setType( "float4x4" );
  68. objTrans->setName( "objTrans" );
  69. objTrans->uniform = true;
  70. objTrans->constSortPos = cspPrimitive;
  71. }
  72. meta->addStatement( new GenOp( " @ = @ - mul( @, float4( @.xyz,1 ) ).xyz;\r\n",
  73. wsView, eyePos, objTrans, position ) );
  74. output = meta;
  75. */
  76. }
  77. void PixelSpecularGLSL::processPix( Vector<ShaderComponent*> &componentList,
  78. const MaterialFeatureData &fd )
  79. {
  80. /*
  81. AssertFatal( fd.features[MFT_RTLighting],
  82. "PixelSpecularHLSL requires RTLighting to be enabled!" );
  83. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  84. MultiLine *meta = new MultiLine;
  85. // Get the normal and light vectors from which the
  86. // RTLighting feature should have already setup.
  87. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  88. Var *inLightVec = (Var*)LangElement::find( "inLightVec" );
  89. // Grab the world space position to eye vector.
  90. Var *wsView = connectComp->getElement( RT_TEXCOORD );
  91. wsView->setName( "wsView" );
  92. wsView->setStructName( "IN" );
  93. wsView->setType( "float3" );
  94. // Get the specular power and color.
  95. Var *specPow = new Var( "specularPower", "float" );
  96. specPow->uniform = true;
  97. specPow->constSortPos = cspPass;
  98. Var *specCol = (Var*)LangElement::find("specularColor");
  99. if(specCol == NULL)
  100. {
  101. specCol = new Var( "specularColor", "vec4" );
  102. specCol->uniform = true;
  103. specCol->constSortPos = cspPass;
  104. }
  105. // Calcuate the specular factor.
  106. Var *specular = new Var( "specular", "float" );
  107. meta->addStatement( new GenOp( " @ = calcSpecular( -@, normalize( @ ), normalize( @ ), @ );\r\n",
  108. new DecOp( specular ), inLightVec, wsNormal, wsView, specPow ) );
  109. LangElement *specMul = new GenOp( "float4(@.rgb,0) * @", specCol, specular );
  110. LangElement *final = specMul;
  111. // mask out with lightmap if present
  112. if( fd.features[MFT_LightMap] )
  113. {
  114. LangElement *lmColor = NULL;
  115. // find lightmap color
  116. lmColor = LangElement::find( "lmColor" );
  117. if ( !lmColor )
  118. {
  119. LangElement * lightMap = LangElement::find( "lightMap" );
  120. LangElement * lmCoord = LangElement::find( "texCoord2" );
  121. lmColor = new GenOp( "tex2D(@, @)", lightMap, lmCoord );
  122. }
  123. final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
  124. }
  125. // We we have a normal map then mask the specular
  126. if ( !fd.features[MFT_SpecularMap] && fd.features[MFT_NormalMap] )
  127. {
  128. Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
  129. final = new GenOp( "@ * @.a", final, bumpColor );
  130. }
  131. // Add the specular to the final color.
  132. meta->addStatement( new GenOp( " @;\r\n", assignColor( final, Material::Add ) ) );
  133. output = meta;
  134. */
  135. }
  136. ShaderFeature::Resources PixelSpecularGLSL::getResources( const MaterialFeatureData &fd )
  137. {
  138. Resources res;
  139. res.numTexReg = 1;
  140. return res;
  141. }
  142. void SpecularMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  143. {
  144. // Get the texture coord.
  145. Var *texCoord = getInTexCoord( "out_texCoord", "vec2", true, componentList );
  146. // create texture var
  147. Var *specularMap = new Var;
  148. specularMap->setType( "sampler2D" );
  149. specularMap->setName( "specularMap" );
  150. specularMap->uniform = true;
  151. specularMap->sampler = true;
  152. specularMap->constNum = Var::getTexUnitNum();
  153. LangElement *texOp = new GenOp( "texture2D(@, @)", specularMap, texCoord );
  154. Var *specularColor = new Var( "specularColor", "vec4" );
  155. output = new GenOp( " @ = @;\r\n", new DecOp( specularColor ), texOp );
  156. }
  157. ShaderFeature::Resources SpecularMapGLSL::getResources( const MaterialFeatureData &fd )
  158. {
  159. Resources res;
  160. res.numTex = 1;
  161. return res;
  162. }
  163. void SpecularMapGLSL::setTexData( Material::StageData &stageDat,
  164. const MaterialFeatureData &fd,
  165. RenderPassData &passData,
  166. U32 &texIndex )
  167. {
  168. GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
  169. if ( tex )
  170. {
  171. passData.mTexType[ texIndex ] = Material::Standard;
  172. passData.mTexSlot[ texIndex++ ].texObject = tex;
  173. }
  174. }