2
0

guiMaterialCtrl.cpp 5.7 KB

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