guiMaterialCtrl.cpp 5.7 KB

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