guiTerrPreviewCtrl.cpp 11 KB

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