blobShadow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "lighting/common/blobShadow.h"
  24. #include "gfx/primBuilder.h"
  25. #include "gfx/gfxTextureManager.h"
  26. #include "gfx/bitmap/gBitmap.h"
  27. #include "math/mathUtils.h"
  28. #include "lighting/lightInfo.h"
  29. #include "lighting/lightingInterfaces.h"
  30. #include "T3D/shapeBase.h"
  31. #include "scene/sceneManager.h"
  32. #include "lighting/lightManager.h"
  33. #include "ts/tsMesh.h"
  34. DepthSortList BlobShadow::smDepthSortList;
  35. GFXTexHandle BlobShadow::smGenericShadowTexture = NULL;
  36. S32 BlobShadow::smGenericShadowDim = 32;
  37. U32 BlobShadow::smShadowMask = TerrainObjectType;
  38. F32 BlobShadow::smGenericRadiusSkew = 0.4f; // shrink radius of shape when it always uses generic shadow...
  39. Box3F gBlobShadowBox;
  40. SphereF gBlobShadowSphere;
  41. Point3F gBlobShadowPoly[4];
  42. //--------------------------------------------------------------
  43. BlobShadow::BlobShadow(SceneObject* parentObject, LightInfo* light, TSShapeInstance* shapeInstance)
  44. {
  45. mParentObject = parentObject;
  46. mShapeBase = dynamic_cast<ShapeBase*>(parentObject);
  47. mParentLight = light;
  48. mShapeInstance = shapeInstance;
  49. mRadius = 0.0f;
  50. mLastRenderTime = 0;
  51. mDepthBias = -0.0002f;
  52. generateGenericShadowBitmap(smGenericShadowDim);
  53. setupStateBlocks();
  54. }
  55. void BlobShadow::setupStateBlocks()
  56. {
  57. GFXStateBlockDesc sh;
  58. sh.cullDefined = true;
  59. sh.cullMode = GFXCullNone;
  60. sh.zDefined = true;
  61. sh.zEnable = true;
  62. sh.zWriteEnable = false;
  63. sh.zBias = mDepthBias;
  64. sh.blendDefined = true;
  65. sh.blendEnable = true;
  66. sh.blendSrc = GFXBlendSrcAlpha;
  67. sh.blendDest = GFXBlendInvSrcAlpha;
  68. sh.alphaDefined = true;
  69. sh.alphaTestEnable = true;
  70. sh.alphaTestFunc = GFXCmpGreater;
  71. sh.alphaTestRef = 0;
  72. sh.samplersDefined = true;
  73. sh.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  74. mShadowSB = GFX->createStateBlock(sh);
  75. }
  76. BlobShadow::~BlobShadow()
  77. {
  78. mShadowBuffer = NULL;
  79. }
  80. bool BlobShadow::shouldRender(F32 camDist)
  81. {
  82. Point3F lightDir;
  83. if (mShapeBase && mShapeBase->getFadeVal() < TSMesh::VISIBILITY_EPSILON)
  84. return false;
  85. F32 shadowLen = 10.0f * mShapeInstance->getShape()->radius;
  86. Point3F pos = mShapeInstance->getShape()->center;
  87. // this is a bit of a hack...move generic shadows towards feet/base of shape
  88. pos *= 0.5f;
  89. pos.convolve(mParentObject->getScale());
  90. mParentObject->getRenderTransform().mulP(pos);
  91. if(mParentLight->getType() == LightInfo::Vector)
  92. {
  93. lightDir = mParentLight->getDirection();
  94. }
  95. else
  96. {
  97. lightDir = pos - mParentLight->getPosition();
  98. lightDir.normalize();
  99. }
  100. // pos is where shadow will be centered (in world space)
  101. setRadius(mShapeInstance, mParentObject->getScale());
  102. bool render = prepare(pos, lightDir, shadowLen);
  103. return render;
  104. }
  105. void BlobShadow::generateGenericShadowBitmap(S32 dim)
  106. {
  107. if(smGenericShadowTexture)
  108. return;
  109. GBitmap * bitmap = new GBitmap(dim,dim,false,GFXFormatR8G8B8A8);
  110. U8 * bits = bitmap->getWritableBits();
  111. dMemset(bits, 0, dim*dim*4);
  112. S32 center = dim >> 1;
  113. F32 invRadiusSq = 1.0f / (F32)(center*center);
  114. F32 tmpF;
  115. for (S32 i=0; i<dim; i++)
  116. {
  117. for (S32 j=0; j<dim; j++)
  118. {
  119. tmpF = (F32)((i-center)*(i-center)+(j-center)*(j-center)) * invRadiusSq;
  120. U8 val = tmpF>0.99f ? 0 : (U8)(180.0f*(1.0f-tmpF)); // 180 out of 255 max
  121. bits[(i*dim*4)+(j*4)+3] = val;
  122. }
  123. }
  124. smGenericShadowTexture.set( bitmap, &GFXDefaultStaticDiffuseProfile, true, "BlobShadow" );
  125. }
  126. //--------------------------------------------------------------
  127. void BlobShadow::setLightMatrices(const Point3F & lightDir, const Point3F & pos)
  128. {
  129. AssertFatal(mDot(lightDir,lightDir)>0.0001f,"BlobShadow::setLightDir: light direction must be a non-zero vector.");
  130. // construct light matrix
  131. Point3F x,z;
  132. if (mFabs(lightDir.z)>0.001f)
  133. {
  134. // mCross(Point3F(1,0,0),lightDir,&z);
  135. z.x = 0.0f;
  136. z.y = lightDir.z;
  137. z.z = -lightDir.y;
  138. z.normalize();
  139. mCross(lightDir,z,&x);
  140. }
  141. else
  142. {
  143. mCross(lightDir,Point3F(0,0,1),&x);
  144. x.normalize();
  145. mCross(x,lightDir,&z);
  146. }
  147. mLightToWorld.identity();
  148. mLightToWorld.setColumn(0,x);
  149. mLightToWorld.setColumn(1,lightDir);
  150. mLightToWorld.setColumn(2,z);
  151. mLightToWorld.setColumn(3,pos);
  152. mWorldToLight = mLightToWorld;
  153. mWorldToLight.inverse();
  154. }
  155. void BlobShadow::setRadius(F32 radius)
  156. {
  157. mRadius = radius;
  158. }
  159. void BlobShadow::setRadius(TSShapeInstance * shapeInstance, const Point3F & scale)
  160. {
  161. const Box3F & bounds = shapeInstance->getShape()->bounds;
  162. F32 dx = 0.5f * (bounds.maxExtents.x-bounds.minExtents.x) * scale.x;
  163. F32 dy = 0.5f * (bounds.maxExtents.y-bounds.minExtents.y) * scale.y;
  164. F32 dz = 0.5f * (bounds.maxExtents.z-bounds.minExtents.z) * scale.z;
  165. mRadius = mSqrt(dx*dx+dy*dy+dz*dz);
  166. }
  167. //--------------------------------------------------------------
  168. bool BlobShadow::prepare(const Point3F & pos, Point3F lightDir, F32 shadowLen)
  169. {
  170. if (mPartition.empty())
  171. {
  172. // --------------------------------------
  173. // 1.
  174. F32 dirMult = (1.0f) * (1.0f);
  175. if (dirMult < 0.99f)
  176. {
  177. lightDir.z *= dirMult;
  178. lightDir.z -= 1.0f - dirMult;
  179. }
  180. lightDir.normalize();
  181. shadowLen *= (1.0f) * (1.0f);
  182. // --------------------------------------
  183. // 2. get polys
  184. F32 radius = mRadius;
  185. radius *= smGenericRadiusSkew;
  186. buildPartition(pos,lightDir,radius,shadowLen);
  187. }
  188. if (mPartition.empty())
  189. // no need to draw shadow if nothing to cast it onto
  190. return false;
  191. return true;
  192. }
  193. //--------------------------------------------------------------
  194. void BlobShadow::buildPartition(const Point3F & p, const Point3F & lightDir, F32 radius, F32 shadowLen)
  195. {
  196. setLightMatrices(lightDir,p);
  197. Point3F extent(2.0f*radius,shadowLen,2.0f*radius);
  198. smDepthSortList.clear();
  199. smDepthSortList.set(mWorldToLight,extent);
  200. smDepthSortList.setInterestNormal(lightDir);
  201. if (shadowLen<1.0f)
  202. // no point in even this short of a shadow...
  203. shadowLen = 1.0f;
  204. mInvShadowDistance = 1.0f / shadowLen;
  205. // build world space box and sphere around shadow
  206. Point3F x,y,z;
  207. mLightToWorld.getColumn(0,&x);
  208. mLightToWorld.getColumn(1,&y);
  209. mLightToWorld.getColumn(2,&z);
  210. x *= radius;
  211. y *= shadowLen;
  212. z *= radius;
  213. gBlobShadowBox.maxExtents.set(mFabs(x.x)+mFabs(y.x)+mFabs(z.x),
  214. mFabs(x.y)+mFabs(y.y)+mFabs(z.y),
  215. mFabs(x.z)+mFabs(y.z)+mFabs(z.z));
  216. y *= 0.5f;
  217. gBlobShadowSphere.radius = gBlobShadowBox.maxExtents.len();
  218. gBlobShadowSphere.center = p + y;
  219. gBlobShadowBox.minExtents = y + p - gBlobShadowBox.maxExtents;
  220. gBlobShadowBox.maxExtents += y + p;
  221. // get polys
  222. gClientContainer.findObjects(STATIC_COLLISION_TYPEMASK, BlobShadow::collisionCallback, this);
  223. // setup partition list
  224. gBlobShadowPoly[0].set(-radius,0,-radius);
  225. gBlobShadowPoly[1].set(-radius,0, radius);
  226. gBlobShadowPoly[2].set( radius,0, radius);
  227. gBlobShadowPoly[3].set( radius,0,-radius);
  228. mPartition.clear();
  229. mPartitionVerts.clear();
  230. smDepthSortList.depthPartition(gBlobShadowPoly,4,mPartition,mPartitionVerts);
  231. if(mPartitionVerts.empty())
  232. return;
  233. // Find the rough distance of the shadow verts
  234. // from the object position and use that to scale
  235. // the visibleAlpha so that the shadow fades out
  236. // the further away from you it gets
  237. F32 dist = 0.0f;
  238. // Calculate the center of the partition verts
  239. Point3F shadowCenter(0.0f, 0.0f, 0.0f);
  240. for (U32 i = 0; i < mPartitionVerts.size(); i++)
  241. shadowCenter += mPartitionVerts[i];
  242. shadowCenter /= mPartitionVerts.size();
  243. mLightToWorld.mulP(shadowCenter);
  244. dist = (p - shadowCenter).len();
  245. // now set up tverts & colors
  246. mShadowBuffer.set(GFX, mPartitionVerts.size(), GFXBufferTypeVolatile);
  247. mShadowBuffer.lock();
  248. F32 visibleAlpha = 255.0f;
  249. if (mShapeBase && mShapeBase->getFadeVal())
  250. visibleAlpha = mClampF(255.0f * mShapeBase->getFadeVal(), 0, 255);
  251. visibleAlpha *= 1.0f - (dist / gBlobShadowSphere.radius);
  252. F32 invRadius = 1.0f / radius;
  253. for (S32 i=0; i<mPartitionVerts.size(); i++)
  254. {
  255. Point3F vert = mPartitionVerts[i];
  256. mShadowBuffer[i].point.set(vert);
  257. mShadowBuffer[i].color.set(255, 255, 255, visibleAlpha);
  258. mShadowBuffer[i].texCoord.set(0.5f + 0.5f * mPartitionVerts[i].x * invRadius, 0.5f + 0.5f * mPartitionVerts[i].z * invRadius);
  259. };
  260. mShadowBuffer.unlock();
  261. }
  262. //--------------------------------------------------------------
  263. void BlobShadow::collisionCallback(SceneObject * obj, void* thisPtr)
  264. {
  265. if (obj->getWorldBox().isOverlapped(gBlobShadowBox))
  266. {
  267. // only interiors clip...
  268. ClippedPolyList::allowClipping = (obj->getTypeMask() & LIGHTMGR->getSceneLightingInterface()->mClippingMask) != 0;
  269. obj->buildPolyList(PLC_Collision,&smDepthSortList,gBlobShadowBox,gBlobShadowSphere);
  270. ClippedPolyList::allowClipping = true;
  271. }
  272. }
  273. //--------------------------------------------------------------
  274. void BlobShadow::render( F32 camDist, const TSRenderState &rdata )
  275. {
  276. mLastRenderTime = Platform::getRealMilliseconds();
  277. GFX->pushWorldMatrix();
  278. MatrixF world = GFX->getWorldMatrix();
  279. world.mul(mLightToWorld);
  280. GFX->setWorldMatrix(world);
  281. GFX->setupGenericShaders(GFXDevice::GSModColorTexture);
  282. GFX->setStateBlock(mShadowSB);
  283. GFX->setTexture(0, smGenericShadowTexture);
  284. GFX->setVertexBuffer(mShadowBuffer);
  285. for(U32 p=0; p<mPartition.size(); p++)
  286. GFX->drawPrimitive(GFXTriangleFan, mPartition[p].vertexStart, (mPartition[p].vertexCount - 2));
  287. // This is a bad nasty hack which forces the shadow to reconstruct itself every frame.
  288. mPartition.clear();
  289. GFX->popWorldMatrix();
  290. }
  291. void BlobShadow::deleteGenericShadowBitmap()
  292. {
  293. smGenericShadowTexture = NULL;
  294. }