guiMaterialCtrl.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "gui/controls/guiMaterialCtrl.h"
  24. #include "materials/baseMatInstance.h"
  25. #include "materials/materialManager.h"
  26. #include "materials/sceneData.h"
  27. #include "core/util/safeDelete.h"
  28. #include "console/console.h"
  29. #include "console/consoleTypes.h"
  30. #include "gfx/gfxDevice.h"
  31. #include "math/util/matrixSet.h"
  32. #include "scene/sceneRenderState.h"
  33. IMPLEMENT_CONOBJECT( GuiMaterialCtrl );
  34. ConsoleDocClass( GuiMaterialCtrl,
  35. "@brief Container for GuiMaterialPreview\n\n"
  36. "Editor use only.\n\n"
  37. "@internal"
  38. );
  39. GuiMaterialCtrl::GuiMaterialCtrl()
  40. : mMaterialInst( NULL )
  41. {
  42. }
  43. void GuiMaterialCtrl::initPersistFields()
  44. {
  45. addGroup( "Material" );
  46. addProtectedField( "materialName", TypeStringFilename, Offset( mMaterialName, GuiMaterialCtrl ), &GuiMaterialCtrl::_setMaterial, &defaultProtectedGetFn, "" );
  47. endGroup( "Material" );
  48. Parent::initPersistFields();
  49. }
  50. bool GuiMaterialCtrl::onWake()
  51. {
  52. if ( !Parent::onWake() )
  53. return false;
  54. setActive( true );
  55. setMaterial( mMaterialName );
  56. return true;
  57. }
  58. void GuiMaterialCtrl::onSleep()
  59. {
  60. SAFE_DELETE( mMaterialInst );
  61. Parent::onSleep();
  62. }
  63. bool GuiMaterialCtrl::_setMaterial( void *object, const char *index, const char *data )
  64. {
  65. static_cast<GuiMaterialCtrl *>( object )->setMaterial( data );
  66. // Return false to keep the caller from setting the field.
  67. return false;
  68. }
  69. bool GuiMaterialCtrl::setMaterial( const String &materialName )
  70. {
  71. SAFE_DELETE( mMaterialInst );
  72. mMaterialName = materialName;
  73. if ( mMaterialName.isNotEmpty() && isAwake() )
  74. mMaterialInst = MATMGR->createMatInstance( mMaterialName, getGFXVertexFormat<GFXVertexPCT>() );
  75. return true;
  76. }
  77. void GuiMaterialCtrl::inspectPostApply()
  78. {
  79. Parent::inspectPostApply();
  80. }
  81. void GuiMaterialCtrl::onRender( Point2I offset, const RectI &updateRect )
  82. {
  83. Parent::onRender( offset, updateRect );
  84. if ( !mMaterialInst )
  85. return;
  86. // Draw a quad with the material assigned
  87. GFXVertexBufferHandle<GFXVertexPCT> verts( GFX, 4, GFXBufferTypeVolatile );
  88. verts.lock();
  89. F32 screenLeft = updateRect.point.x;
  90. F32 screenRight = (updateRect.point.x + updateRect.extent.x);
  91. F32 screenTop = updateRect.point.y;
  92. F32 screenBottom = (updateRect.point.y + updateRect.extent.y);
  93. const F32 fillConv = GFX->getFillConventionOffset();
  94. verts[0].point.set( screenLeft - fillConv, screenTop - fillConv, 0.f );
  95. verts[1].point.set( screenRight - fillConv, screenTop - fillConv, 0.f );
  96. verts[2].point.set( screenLeft - fillConv, screenBottom - fillConv, 0.f );
  97. verts[3].point.set( screenRight - fillConv, screenBottom - fillConv, 0.f );
  98. verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI( 255, 255, 255, 255 );
  99. verts[0].texCoord.set( 0.0f, 0.0f );
  100. verts[1].texCoord.set( 1.0f, 0.0f );
  101. verts[2].texCoord.set( 0.0f, 1.0f );
  102. verts[3].texCoord.set( 1.0f, 1.0f );
  103. verts.unlock();
  104. GFX->setVertexBuffer( verts );
  105. MatrixSet matSet;
  106. matSet.setWorld(GFX->getWorldMatrix());
  107. matSet.setView(GFX->getViewMatrix());
  108. matSet.setProjection(GFX->getProjectionMatrix());
  109. MatrixF cameraMatrix( true );
  110. F32 left, right, top, bottom, nearPlane, farPlane;
  111. bool isOrtho;
  112. GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
  113. Frustum frust( isOrtho, left, right, top, bottom, nearPlane, farPlane, cameraMatrix );
  114. SceneRenderState state
  115. (
  116. gClientSceneGraph,
  117. SPT_Diffuse,
  118. SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
  119. gClientSceneGraph->getDefaultRenderPass(),
  120. false
  121. );
  122. SceneData sgData;
  123. sgData.init( &state );
  124. sgData.wireframe = false; // Don't wireframe this.
  125. while( mMaterialInst->setupPass( &state, sgData ) )
  126. {
  127. mMaterialInst->setSceneInfo( &state, sgData );
  128. mMaterialInst->setTransforms( matSet, &state );
  129. GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
  130. }
  131. // Clean up
  132. GFX->setShader( NULL );
  133. GFX->setTexture( 0, NULL );
  134. }
  135. ConsoleMethod( GuiMaterialCtrl, setMaterial, bool, 3, 3, "( string materialName )"
  136. "Set the material to be displayed in the control." )
  137. {
  138. return object->setMaterial( (const char*)argv[2] );
  139. }