123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "gui/controls/guiMaterialCtrl.h"
- #include "materials/baseMatInstance.h"
- #include "materials/materialManager.h"
- #include "materials/sceneData.h"
- #include "core/util/safeDelete.h"
- #include "console/console.h"
- #include "console/consoleTypes.h"
- #include "console/engineAPI.h"
- #include "gfx/gfxDevice.h"
- #include "math/util/matrixSet.h"
- #include "scene/sceneRenderState.h"
- IMPLEMENT_CONOBJECT( GuiMaterialCtrl );
- ConsoleDocClass( GuiMaterialCtrl,
- "@brief Container for GuiMaterialPreview\n\n"
- "Editor use only.\n\n"
- "@internal"
- );
- GuiMaterialCtrl::GuiMaterialCtrl()
- : mMaterialInst( NULL )
- {
- INIT_ASSET(Material);
- }
- void GuiMaterialCtrl::initPersistFields()
- {
- addGroup( "Material" );
- INITPERSISTFIELD_MATERIALASSET(Material, GuiMaterialCtrl, "");
- addProtectedField( "materialName", TypeStringFilename, Offset( mMaterialName, GuiMaterialCtrl ), &GuiMaterialCtrl::_setMaterialData, &defaultProtectedGetFn, "", AbstractClassRep::FIELD_HideInInspectors );
- endGroup( "Material" );
- Parent::initPersistFields();
- }
- bool GuiMaterialCtrl::onWake()
- {
- if ( !Parent::onWake() )
- return false;
- setActive( true );
- setMaterial( getMaterial() );
- return true;
- }
- void GuiMaterialCtrl::onSleep()
- {
- SAFE_DELETE( mMaterialInst );
- Parent::onSleep();
- }
- bool GuiMaterialCtrl::_setMaterial( void *object, const char *index, const char *data )
- {
- static_cast<GuiMaterialCtrl *>( object )->setMaterial( data );
- // Return false to keep the caller from setting the field.
- return false;
- }
- bool GuiMaterialCtrl::setMaterial( const String &materialName )
- {
- SAFE_DELETE( mMaterialInst );
- _setMaterial(StringTable->insert(materialName.c_str()));
- if ( getMaterial() != StringTable->EmptyString() && isAwake() )
- mMaterialInst = MATMGR->createMatInstance( getMaterial(), getGFXVertexFormat<GFXVertexPCT>() );
- return true;
- }
- void GuiMaterialCtrl::inspectPostApply()
- {
- Parent::inspectPostApply();
- }
- void GuiMaterialCtrl::onRender( Point2I offset, const RectI &updateRect )
- {
- Parent::onRender( offset, updateRect );
- if ( !mMaterialInst )
- return;
- // Draw a quad with the material assigned
- GFXVertexBufferHandle<GFXVertexPCT> verts( GFX, 4, GFXBufferTypeVolatile );
- verts.lock();
- F32 screenLeft = updateRect.point.x;
- F32 screenRight = (updateRect.point.x + updateRect.extent.x);
- F32 screenTop = updateRect.point.y;
- F32 screenBottom = (updateRect.point.y + updateRect.extent.y);
- const F32 fillConv = GFX->getFillConventionOffset();
- verts[0].point.set( screenLeft - fillConv, screenTop - fillConv, 0.f );
- verts[1].point.set( screenRight - fillConv, screenTop - fillConv, 0.f );
- verts[2].point.set( screenLeft - fillConv, screenBottom - fillConv, 0.f );
- verts[3].point.set( screenRight - fillConv, screenBottom - fillConv, 0.f );
- verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI( 255, 255, 255, 255 );
- verts[0].texCoord.set( 0.0f, 0.0f );
- verts[1].texCoord.set( 1.0f, 0.0f );
- verts[2].texCoord.set( 0.0f, 1.0f );
- verts[3].texCoord.set( 1.0f, 1.0f );
- verts.unlock();
- GFX->setVertexBuffer( verts );
- MatrixSet matSet;
- matSet.setWorld(GFX->getWorldMatrix());
- matSet.setView(GFX->getViewMatrix());
- matSet.setProjection(GFX->getProjectionMatrix());
-
- MatrixF cameraMatrix( true );
- F32 left, right, top, bottom, nearPlane, farPlane;
- bool isOrtho;
- GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
- Frustum frust( isOrtho, left, right, top, bottom, nearPlane, farPlane, cameraMatrix );
- SceneRenderState state
- (
- gClientSceneGraph,
- SPT_Diffuse,
- SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
- gClientSceneGraph->getDefaultRenderPass(),
- false
- );
- SceneData sgData;
- sgData.init( &state );
- sgData.wireframe = false; // Don't wireframe this.
- while( mMaterialInst->setupPass( &state, sgData ) )
- {
- mMaterialInst->setSceneInfo( &state, sgData );
- mMaterialInst->setTransforms( matSet, &state );
- GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
- }
- // Clean up
- GFX->setShader( NULL );
- GFX->setTexture( 0, NULL );
- }
- DefineEngineMethod( GuiMaterialCtrl, setMaterial, bool, ( const char * materialName ), , "( string materialName )"
- "Set the material to be displayed in the control." )
- {
- return object->setMaterial( materialName );
- }
|