basicClouds.cpp 12 KB

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