guiTerrPreviewCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "console/console.h"
  23. #include "console/engineAPI.h"
  24. #include "console/consoleTypes.h"
  25. #include "terrain/terrData.h"
  26. #include "gui/worldEditor/guiTerrPreviewCtrl.h"
  27. #include "gfx/primBuilder.h"
  28. #include "T3D/gameFunctions.h"
  29. IMPLEMENT_CONOBJECT(GuiTerrPreviewCtrl);
  30. ConsoleDocClass( GuiTerrPreviewCtrl,
  31. "@brief Very old GUI used for terrain preview\n\n"
  32. "Deprecated\n\n"
  33. "@internal"
  34. );
  35. GuiTerrPreviewCtrl::GuiTerrPreviewCtrl(void) : mTerrainSize(2048.0f), mTerrainEditor(NULL)
  36. {
  37. mRoot.set( 0, 0 );
  38. mOrigin.set( 0, 0 );
  39. mWorldScreenCenter.set( mTerrainSize*0.5f, mTerrainSize*0.5f );
  40. mControlsStateBlock = NULL;
  41. mTerrainBitmapStateBlock = NULL;
  42. }
  43. bool GuiTerrPreviewCtrl::onAdd()
  44. {
  45. if(Parent::onAdd() == false)
  46. {
  47. return false;
  48. }
  49. SimObject* inTerrEditor = Sim::findObject("ETerrainEditor");
  50. if(!inTerrEditor)
  51. {
  52. Con::errorf(ConsoleLogEntry::General, "TerrainEditor::onAdd: failed to load Terrain Editor");
  53. return false;
  54. }
  55. mTerrainEditor = dynamic_cast<TerrainEditor*>(inTerrEditor);
  56. GFXStateBlockDesc desc;
  57. desc.setBlend(false, GFXBlendOne, GFXBlendZero);
  58. desc.samplersDefined = true;
  59. desc.samplers[0].addressModeU = GFXAddressWrap;
  60. desc.samplers[0].addressModeV = GFXAddressWrap;
  61. desc.setCullMode(GFXCullNone);
  62. desc.setZReadWrite(false);
  63. mTerrainBitmapStateBlock = GFX->createStateBlock(desc);
  64. mControlsStateBlock = GFX->createStateBlock(desc);
  65. return true;
  66. }
  67. void GuiTerrPreviewCtrl::initPersistFields()
  68. {
  69. docsURL;
  70. Parent::initPersistFields();
  71. }
  72. DefineEngineMethod( GuiTerrPreviewCtrl, reset, void, (), , "Reset the view of the terrain.")
  73. {
  74. object->reset();
  75. }
  76. DefineEngineMethod( GuiTerrPreviewCtrl, setRoot, void, (), , "Add the origin to the root and reset the origin.")
  77. {
  78. object->setRoot();
  79. }
  80. DefineEngineMethod( GuiTerrPreviewCtrl, getRoot, Point2F, (), , "Return a Point2F representing the position of the root.")
  81. {
  82. return object->getRoot();
  83. }
  84. DefineEngineMethod( GuiTerrPreviewCtrl, setOrigin, void, (Point2F pos), , "(float x, float y)"
  85. "Set the origin of the view.")
  86. {
  87. object->setOrigin( pos );
  88. }
  89. DefineEngineMethod( GuiTerrPreviewCtrl, getOrigin, Point2F, (), , "Return a Point2F containing the position of the origin.")
  90. {
  91. return object->getOrigin();
  92. }
  93. DefineEngineMethod( GuiTerrPreviewCtrl, getValue, const char*, (), , "Returns a 4-tuple containing: root_x root_y origin_x origin_y")
  94. {
  95. Point2F r = object->getRoot();
  96. Point2F o = object->getOrigin();
  97. static char valuebuf[64];
  98. dSprintf(valuebuf,sizeof(valuebuf),"%g %g %g %g", r.x, -r.y, o.x, -o.y);
  99. return valuebuf;
  100. }
  101. DefineEngineMethod( GuiTerrPreviewCtrl, setValue, void, (const char * tuple), , "Accepts a 4-tuple in the same form as getValue returns.\n\n"
  102. "@see GuiTerrPreviewCtrl::getValue()")
  103. {
  104. Point2F r,o;
  105. dSscanf(tuple, "%g %g %g %g", &r.x, &r.y, &o.x, &o.y);
  106. r.y = -r.y;
  107. o.y = -o.y;
  108. object->reset();
  109. object->setRoot(r);
  110. object->setOrigin(o);
  111. }
  112. bool GuiTerrPreviewCtrl::onWake()
  113. {
  114. if (! Parent::onWake())
  115. return false;
  116. return true;
  117. }
  118. void GuiTerrPreviewCtrl::onSleep()
  119. {
  120. Parent::onSleep();
  121. }
  122. void GuiTerrPreviewCtrl::setBitmap(const GFXTexHandle &handle)
  123. {
  124. mTextureHandle = handle;
  125. }
  126. void GuiTerrPreviewCtrl::reset()
  127. {
  128. mRoot.set(0,0);
  129. mOrigin.set(0,0);
  130. }
  131. void GuiTerrPreviewCtrl::setRoot()
  132. {
  133. mRoot += mOrigin;
  134. mOrigin.set(0,0);
  135. }
  136. void GuiTerrPreviewCtrl::setRoot(const Point2F &p)
  137. {
  138. mRoot = p;
  139. }
  140. void GuiTerrPreviewCtrl::setOrigin(const Point2F &p)
  141. {
  142. mOrigin = p;
  143. }
  144. Point2F& GuiTerrPreviewCtrl::wrap(const Point2F &p)
  145. {
  146. static Point2F result;
  147. result = p;
  148. while (result.x < 0.0f)
  149. result.x += mTerrainSize;
  150. while (result.x > mTerrainSize)
  151. result.x -= mTerrainSize;
  152. while (result.y < 0.0f)
  153. result.y += mTerrainSize;
  154. while (result.y > mTerrainSize)
  155. result.y -= mTerrainSize;
  156. return result;
  157. }
  158. Point2F& GuiTerrPreviewCtrl::worldToTexture(const Point2F &p)
  159. {
  160. static Point2F result;
  161. result = wrap( p + mRoot ) / mTerrainSize;
  162. return result;
  163. }
  164. Point2F& GuiTerrPreviewCtrl::worldToCtrl(const Point2F &p)
  165. {
  166. static Point2F result;
  167. result = wrap( p - mCamera - mWorldScreenCenter );
  168. result *= getWidth() / mTerrainSize;
  169. return result;
  170. }
  171. void GuiTerrPreviewCtrl::onPreRender()
  172. {
  173. setUpdate();
  174. }
  175. void GuiTerrPreviewCtrl::onRender(Point2I offset, const RectI &updateRect)
  176. {
  177. CameraQuery query;
  178. GameProcessCameraQuery(&query);
  179. Point3F cameraRot;
  180. TerrainBlock *terrBlock = NULL;
  181. MatrixF matrix = query.cameraMatrix;
  182. matrix.getColumn(3,&cameraRot); // get Camera translation
  183. mCamera.set(cameraRot.x, -cameraRot.y);
  184. matrix.getRow(1,&cameraRot); // get camera rotation
  185. if (mTerrainEditor != NULL)
  186. terrBlock = mTerrainEditor->getActiveTerrain();
  187. if (!terrBlock)
  188. return;
  189. for(U32 i = 0; i < GFX->getNumSamplers(); i++)
  190. GFX->setTexture(i, NULL);
  191. GFX->setupGenericShaders(GFXDevice::GSModColorTexture);
  192. Point2F terrPos(terrBlock->getPosition().x, terrBlock->getPosition().y);
  193. mTerrainSize = terrBlock->getWorldBlockSize();
  194. //----------------------------------------- RENDER the Terrain Bitmap
  195. if (mTextureHandle)
  196. {
  197. GFXTextureObject *texture = (GFXTextureObject*)mTextureHandle;
  198. if (texture)
  199. {
  200. //GFX->setLightingEnable(false);
  201. GFX->setStateBlock(mTerrainBitmapStateBlock);
  202. GFX->setTexture(0, texture);
  203. Point2F screenP1(offset.x - 0.5f, offset.y + 0.5f);
  204. Point2F screenP2(offset.x + getWidth() - 0.5f, offset.y + getWidth() + 0.5f);
  205. Point2F textureP1( worldToTexture( mCamera - terrPos ) - Point2F(0.5f, 0.5f));
  206. Point2F textureP2(textureP1 + Point2F(1.0f, 1.0f));
  207. // the texture if flipped horz to reflect how the terrain is really drawn
  208. PrimBuild::color3f(1.0f, 1.0f, 1.0f);
  209. PrimBuild::begin(GFXTriangleStrip, 4);
  210. PrimBuild::texCoord2f(textureP1.x, textureP1.y);
  211. PrimBuild::vertex2f(screenP1.x, screenP1.y); // left top
  212. PrimBuild::texCoord2f(textureP2.x, textureP1.y);
  213. PrimBuild::vertex2f(screenP2.x, screenP1.y); // right top
  214. PrimBuild::texCoord2f(textureP1.x, textureP2.y);
  215. PrimBuild::vertex2f(screenP1.x, screenP2.y); // left bottom
  216. PrimBuild::texCoord2f(textureP2.x, textureP2.y);
  217. PrimBuild::vertex2f(screenP2.x, screenP2.y); // right bottom
  218. PrimBuild::end();
  219. }
  220. }
  221. //Draw blank texture
  222. else
  223. {
  224. RectI rect(offset.x, offset.y, getWidth(), getHeight());
  225. GFX->getDrawUtil()->drawRect(rect, ColorI(0,0,0));
  226. }
  227. GFX->setStateBlock(mControlsStateBlock);
  228. //----------------------------------------- RENDER the '+' at the center of the Block
  229. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 1.0f);
  230. Point2F center( worldToCtrl(terrPos + Point2F(mTerrainSize * 0.5f, mTerrainSize * 0.5f)) );
  231. S32 y;
  232. for (y=-1; y<=1; y++)
  233. {
  234. F32 yoffset = offset.y + y*256.0f;
  235. for (S32 x=-1; x<=1; x++)
  236. {
  237. F32 xoffset = offset.x + x*256.0f;
  238. PrimBuild::begin(GFXLineList, 4);
  239. PrimBuild::vertex2f(xoffset + center.x, yoffset + center.y-5);
  240. PrimBuild::vertex2f(xoffset + center.x, yoffset + center.y+6);
  241. PrimBuild::vertex2f(xoffset + center.x-5, yoffset + center.y);
  242. PrimBuild::vertex2f(xoffset + center.x+6, yoffset + center.y);
  243. PrimBuild::end();
  244. }
  245. }
  246. //----------------------------------------- RENDER the Block Corners
  247. Point2F cornerf( worldToCtrl(terrPos) + Point2F(0.125f, 0.125f));
  248. Point2I corner=Point2I((S32)cornerf.x,(S32)cornerf.y);
  249. for (y=-1; y<=1; y++)
  250. {
  251. S32 yoffset = offset.y + y*256;
  252. for (S32 x=-1; x<=1; x++)
  253. {
  254. S32 xoffset = offset.x + x*256;
  255. PrimBuild::begin(GFXLineStrip, 3);
  256. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
  257. PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y-128);
  258. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.7f);
  259. PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y);
  260. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
  261. PrimBuild::vertex2i(xoffset + corner.x+128, yoffset + corner.y);
  262. PrimBuild::end();
  263. PrimBuild::begin(GFXLineStrip, 3);
  264. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
  265. PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y+128);
  266. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.7f);
  267. PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y);
  268. PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
  269. PrimBuild::vertex2i(xoffset + corner.x-128, yoffset + corner.y);
  270. PrimBuild::end();
  271. }
  272. }
  273. //----------------------------------------- RENDER the Viewcone
  274. Point2F pointA(cameraRot.x * -40, cameraRot.y * -40);
  275. Point2F pointB(-pointA.y, pointA.x);
  276. F32 tann = mTan(0.5f);
  277. Point2F point1( pointA + pointB * tann );
  278. Point2F point2( pointA - pointB * tann );
  279. center.set((F32)(offset.x + getWidth() / 2), (F32)(offset.y + getHeight() / 2 ));
  280. PrimBuild::begin(GFXLineStrip, 3);
  281. PrimBuild::color4f(1.0f, 0.0f, 0.0f, 0.7f);
  282. PrimBuild::vertex2i((S32)(center.x + point1.x), (S32)(center.y + point1.y));
  283. PrimBuild::color4f(1.0f, 0.0f, 0.0f, 1.0f);
  284. PrimBuild::vertex2i((S32)center.x,(S32)center.y);
  285. PrimBuild::color4f(1.0f, 0.0f, 0.0f, 0.7f);
  286. PrimBuild::vertex2i((S32)(center.x + point2.x), (S32)(center.y + point2.y));
  287. PrimBuild::end();
  288. renderChildControls(offset, updateRect);
  289. }