guiMaterialCtrl.cpp 5.5 KB

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