basicClouds.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "platform/profiler.h"
  24. #include "console/consoleTypes.h"
  25. #include "basicClouds.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "gfx/gfxTextureManager.h"
  28. #include "core/stream/fileStream.h"
  29. #include "core/stream/bitStream.h"
  30. #include "scene/sceneRenderState.h"
  31. #include "renderInstance/renderPassManager.h"
  32. #include "materials/shaderData.h"
  33. #include "math/mathIO.h"
  34. ConsoleDocClass( BasicClouds,
  35. "@brief Renders up to three layers of scrolling cloud-cover textures overhead.\n\n"
  36. "%BasicClouds always renders overhead, following the camera. It is intended "
  37. "as part of the background of your level, rendering in front of Sky/Sun "
  38. "type objects and behind everything else.\n\n"
  39. "The parameters controlling the rendering of each texture are refered to "
  40. "and grouped as 'layers'. They are rendered in sequential order, so, layer 1 "
  41. "obscures layer 0, and so on.\n\n"
  42. "BasicClouds is not affected by scene lighting and is therefore not appropriate "
  43. "for scenes in which lighting radically changes, such as day/night.\n\n"
  44. "@ingroup Atmosphere"
  45. );
  46. U32 BasicClouds::smVertStride = 50;
  47. U32 BasicClouds::smStrideMinusOne = 49;
  48. U32 BasicClouds::smVertCount = 50 * 50;
  49. U32 BasicClouds::smTriangleCount = smStrideMinusOne * smStrideMinusOne * 2;
  50. BasicClouds::BasicClouds()
  51. {
  52. mTypeMask |= EnvironmentObjectType | StaticObjectType;
  53. mNetFlags.set(Ghostable | ScopeAlways);
  54. mTimeSC = NULL;
  55. mModelViewProjSC = NULL;
  56. mTexScaleSC = NULL;
  57. mTexDirectionSC = NULL;
  58. mTexOffsetSC = NULL;
  59. mDiffuseMapSC = NULL;
  60. mLayerEnabled[0] = true;
  61. mLayerEnabled[1] = true;
  62. mLayerEnabled[2] = true;
  63. // Default textures are assigned by the ObjectBuilderGui.
  64. //mTexName[0] = "art/skies/clouds/cloud1";
  65. //mTexName[1] = "art/skies/clouds/cloud2";
  66. //mTexName[2] = "art/skies/clouds/cloud3";
  67. mHeight[0] = 4.0f;
  68. mHeight[1] = 3.0f;
  69. mHeight[2] = 2.0f;
  70. mTexSpeed[0] = 0.0005f;
  71. mTexSpeed[1] = 0.001f;
  72. mTexSpeed[2] = 0.0003f;
  73. mTexScale[0] = 1.0;
  74. mTexScale[1] = 1.0;
  75. mTexScale[2] = 1.0;
  76. mTexDirection[0].set( 1.0f, 0.0f );
  77. mTexDirection[1].set( 1.0f, 0.0f );
  78. mTexDirection[2].set( 1.0f, 0.0f );
  79. mTexOffset[0].set( 0.5f, 0.5f );
  80. mTexOffset[1].set( 0.5f, 0.5f );
  81. mTexOffset[2].set( 0.5f, 0.5f );
  82. }
  83. IMPLEMENT_CO_NETOBJECT_V1( BasicClouds );
  84. // ConsoleObject...
  85. bool BasicClouds::onAdd()
  86. {
  87. if ( !Parent::onAdd() )
  88. return false;
  89. setGlobalBounds();
  90. resetWorldBox();
  91. addToScene();
  92. if ( isClientObject() )
  93. {
  94. _initTexture();
  95. _initBuffers();
  96. // Find ShaderData
  97. ShaderData *shaderData;
  98. mShader = Sim::findObject( "BasicCloudsShader", shaderData ) ? shaderData->getShader() : NULL;
  99. if ( !mShader )
  100. {
  101. Con::errorf( "BasicClouds::onAdd - could not find BasicCloudsShader" );
  102. return false;
  103. }
  104. // Create ShaderConstBuffer and Handles
  105. mShaderConsts = mShader->allocConstBuffer();
  106. mModelViewProjSC = mShader->getShaderConstHandle( "$modelView" );
  107. mTimeSC = mShader->getShaderConstHandle( "$accumTime" );
  108. mTexScaleSC = mShader->getShaderConstHandle( "$texScale" );
  109. mTexDirectionSC = mShader->getShaderConstHandle( "$texDirection" );
  110. mTexOffsetSC = mShader->getShaderConstHandle( "$texOffset" );
  111. mDiffuseMapSC = mShader->getShaderConstHandle( "$diffuseMap" );
  112. // Create StateBlocks
  113. GFXStateBlockDesc desc;
  114. desc.setCullMode( GFXCullNone );
  115. desc.setBlend( true );
  116. desc.setZReadWrite( true, false );
  117. desc.samplersDefined = true;
  118. desc.samplers[0].addressModeU = GFXAddressWrap;
  119. desc.samplers[0].addressModeV = GFXAddressWrap;
  120. desc.samplers[0].addressModeW = GFXAddressWrap;
  121. desc.samplers[0].magFilter = GFXTextureFilterLinear;
  122. desc.samplers[0].minFilter = GFXTextureFilterLinear;
  123. desc.samplers[0].mipFilter = GFXTextureFilterLinear;
  124. desc.samplers[0].textureColorOp = GFXTOPModulate;
  125. mStateblock = GFX->createStateBlock( desc );
  126. }
  127. return true;
  128. }
  129. void BasicClouds::onRemove()
  130. {
  131. removeFromScene();
  132. Parent::onRemove();
  133. }
  134. void BasicClouds::initPersistFields()
  135. {
  136. addGroup( "BasicClouds" );
  137. addArray( "Layers", TEX_COUNT );
  138. addField( "layerEnabled", TypeBool, Offset( mLayerEnabled, BasicClouds ), TEX_COUNT,
  139. "Enable or disable rendering of this layer." );
  140. addField( "texture", TypeImageFilename, Offset( mTexName, BasicClouds ), TEX_COUNT,
  141. "Texture for this layer." );
  142. addField( "texScale", TypeF32, Offset( mTexScale, BasicClouds ), TEX_COUNT,
  143. "Texture repeat for this layer." );
  144. addField( "texDirection", TypePoint2F, Offset( mTexDirection, BasicClouds ), TEX_COUNT,
  145. "Texture scroll direction for this layer, relative to the world axis." );
  146. addField( "texSpeed", TypeF32, Offset( mTexSpeed, BasicClouds ), TEX_COUNT,
  147. "Texture scroll speed for this layer." );
  148. addField( "texOffset", TypePoint2F, Offset( mTexOffset, BasicClouds ), TEX_COUNT,
  149. "UV offset for this layer." );
  150. addField( "height", TypeF32, Offset( mHeight, BasicClouds ), TEX_COUNT,
  151. "Abstract number which controls the curvature and height of the dome mesh" );
  152. endArray( "Layers" );
  153. endGroup( "BasicClouds" );
  154. Parent::initPersistFields();
  155. }
  156. void BasicClouds::inspectPostApply()
  157. {
  158. Parent::inspectPostApply();
  159. setMaskBits( BasicCloudsMask );
  160. }
  161. // NetObject...
  162. U32 BasicClouds::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  163. {
  164. U32 retMask = Parent::packUpdate( conn, mask, stream );
  165. for ( U32 i = 0; i < TEX_COUNT; i++ )
  166. {
  167. stream->writeFlag( mLayerEnabled[i] );
  168. stream->write( mTexName[i] );
  169. stream->write( mTexScale[i] );
  170. mathWrite( *stream, mTexDirection[i] );
  171. stream->write( mTexSpeed[i] );
  172. mathWrite( *stream, mTexOffset[i] );
  173. stream->write( mHeight[i] );
  174. }
  175. return retMask;
  176. }
  177. void BasicClouds::unpackUpdate( NetConnection *conn, BitStream *stream )
  178. {
  179. Parent::unpackUpdate( conn, stream );
  180. for ( U32 i = 0; i < TEX_COUNT; i++ )
  181. {
  182. mLayerEnabled[i] = stream->readFlag();
  183. stream->read( &mTexName[i] );
  184. stream->read( &mTexScale[i] );
  185. mathRead( *stream, &mTexDirection[i] );
  186. stream->read( &mTexSpeed[i] );
  187. mathRead( *stream, &mTexOffset[i] );
  188. stream->read( &mHeight[i] );
  189. }
  190. if ( isProperlyAdded() )
  191. {
  192. // We could check if the height or texture have actually changed.
  193. _initBuffers();
  194. _initTexture();
  195. }
  196. }
  197. // SceneObject...
  198. void BasicClouds::prepRenderImage( SceneRenderState *state )
  199. {
  200. PROFILE_SCOPE( BasicClouds_prepRenderImage );
  201. bool isEnabled = false;
  202. for ( U32 i = 0; i < TEX_COUNT; i++ )
  203. {
  204. if ( mLayerEnabled[i] )
  205. {
  206. isEnabled = true;
  207. break;
  208. }
  209. }
  210. if ( !isEnabled )
  211. return;
  212. // This should be sufficient for most objects that don't manage zones, and
  213. // don't need to return a specialized RenderImage...
  214. ObjectRenderInst *ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
  215. ri->renderDelegate.bind( this, &BasicClouds::renderObject );
  216. ri->type = RenderPassManager::RIT_Sky;
  217. ri->defaultKey = 0;
  218. ri->defaultKey2 = 0;
  219. state->getRenderPass()->addInst( ri );
  220. }
  221. void BasicClouds::renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mi )
  222. {
  223. GFXTransformSaver saver;
  224. Point3F camPos = state->getCameraPosition();
  225. MatrixF xfm(true);
  226. xfm.setPosition(camPos);
  227. GFX->multWorld(xfm);
  228. if ( state->isReflectPass() )
  229. GFX->setProjectionMatrix( state->getSceneManager()->getNonClipProjection() );
  230. GFX->setShader( mShader );
  231. GFX->setShaderConstBuffer( mShaderConsts );
  232. GFX->setStateBlock( mStateblock );
  233. MatrixF xform(GFX->getProjectionMatrix());
  234. xform *= GFX->getViewMatrix();
  235. xform *= GFX->getWorldMatrix();
  236. mShaderConsts->setSafe( mModelViewProjSC, xform );
  237. mShaderConsts->setSafe( mTimeSC, (F32)Sim::getCurrentTime() / 1000.0f );
  238. GFX->setPrimitiveBuffer( mPB );
  239. for ( U32 i = 0; i < TEX_COUNT; i++ )
  240. {
  241. if ( !mLayerEnabled[i] )
  242. continue;
  243. mShaderConsts->setSafe( mTexScaleSC, mTexScale[i] );
  244. mShaderConsts->setSafe( mTexDirectionSC, mTexDirection[i] * mTexSpeed[i] );
  245. mShaderConsts->setSafe( mTexOffsetSC, mTexOffset[i] );
  246. GFX->setTexture( mDiffuseMapSC->getSamplerRegister(), mTexture[i] );
  247. GFX->setVertexBuffer( mVB[i] );
  248. GFX->drawIndexedPrimitive( GFXTriangleList, 0, 0, smVertCount, 0, smTriangleCount );
  249. }
  250. }
  251. // BasicClouds Internal Methods....
  252. void BasicClouds::_initTexture()
  253. {
  254. for ( U32 i = 0; i < TEX_COUNT; i++ )
  255. {
  256. if ( !mLayerEnabled[i] )
  257. {
  258. mTexture[i] = NULL;
  259. continue;
  260. }
  261. if ( mTexName[i].isNotEmpty() )
  262. mTexture[i].set( mTexName[i], &GFXStaticTextureSRGBProfile, "BasicClouds" );
  263. if ( mTexture[i].isNull() )
  264. mTexture[i].set( GFXTextureManager::getWarningTexturePath(), &GFXStaticTextureSRGBProfile, "BasicClouds" );
  265. }
  266. }
  267. void BasicClouds::_initBuffers()
  268. {
  269. // Primitive Buffer... Is shared for all Layers.
  270. mPB.set( GFX, smTriangleCount * 3, smTriangleCount, GFXBufferTypeStatic );
  271. U16 *pIdx = NULL;
  272. mPB.lock(&pIdx);
  273. U32 curIdx = 0;
  274. for ( U32 y = 0; y < smStrideMinusOne; y++ )
  275. {
  276. for ( U32 x = 0; x < smStrideMinusOne; x++ )
  277. {
  278. U32 offset = x + y * smVertStride;
  279. pIdx[curIdx] = offset;
  280. curIdx++;
  281. pIdx[curIdx] = offset + 1;
  282. curIdx++;
  283. pIdx[curIdx] = offset + smVertStride + 1;
  284. curIdx++;
  285. pIdx[curIdx] = offset;
  286. curIdx++;
  287. pIdx[curIdx] = offset + smVertStride + 1;
  288. curIdx++;
  289. pIdx[curIdx] = offset + smVertStride;
  290. curIdx++;
  291. }
  292. }
  293. mPB.unlock();
  294. // Vertex Buffer...
  295. // Each layer has their own so they can be at different heights.
  296. for ( U32 i = 0; i < TEX_COUNT; i++ )
  297. {
  298. Point3F vertScale( 16.0f, 16.0f, mHeight[i] );
  299. F32 zOffset = -( mCos( mSqrt( 1.0f ) ) + 0.01f );
  300. mVB[i].set( GFX, smVertCount, GFXBufferTypeStatic );
  301. GFXVertexPT *pVert = mVB[i].lock();
  302. for ( U32 y = 0; y < smVertStride; y++ )
  303. {
  304. F32 v = ( (F32)y / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  305. for ( U32 x = 0; x < smVertStride; x++ )
  306. {
  307. F32 u = ( (F32)x / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  308. F32 sx = u;
  309. F32 sy = v;
  310. F32 sz = mCos( mSqrt( sx*sx + sy*sy ) ) + zOffset;
  311. pVert->point.set( sx, sy, sz );
  312. pVert->point *= vertScale;
  313. pVert->texCoord.set( u, v );
  314. pVert++;
  315. }
  316. }
  317. mVB[i].unlock();
  318. }
  319. }