guiTerrPreviewCtrl.cpp 11 KB

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