// Render Scene with Material (0x4f0a3a3a,0x62554f4f)
// Renders a scene using the same material for all objects in the scene.
// - Rendertarget View.
// - Render Options.
// - Material.
#include "CKAll.h"
#include "RTView.h"
#define RENDERSCENEWITHMATERIAL_GUID CKGUID(0x4f0a3a3a,0x62554f4f)
//-----------------------------------------------------------------------------
enum
{
PARAM_VIEW = 0,
PARAM_RENDEROPTIONS,
PARAM_BGCOLOR,
PARAM_MATERIAL
};
//-----------------------------------------------------------------------------
int RenderSceneWithMaterial(const CKBehaviorContext& behcontext)
{
CKBehavior* beh = behcontext.Behavior;
CKParameterIn* pin = beh->GetInputParameter(PARAM_VIEW);
if (!pin)
return CKBR_PARAMETERERROR;
if (!pin->GetRealSource())
return CKBR_PARAMETERERROR;
// Read the View
RTView rtview(pin->GetRealSource());
// Reads background color
VxColor bgcolor(0, 0, 0, 0);
beh->GetInputParameterValue(PARAM_BGCOLOR, &bgcolor);
CKMaterial* mat = (CKMaterial*)beh->GetInputParameterObject(PARAM_MATERIAL);
if (!mat)
return CKBR_PARAMETERERROR;
// On
if (beh->IsInputActive(0))
{
CKRenderContext* rc = behcontext.CurrentRenderContext;
rtview.tex->EnsureVideoMemory(rc);
//--- Check Rect size
if(rtview.rect.right <=0 && rtview.rect.bottom <=0 ){
if(rtview.tex){
rtview.rect.right = (float)rtview.tex->GetWidth();
rtview.rect.bottom = (float)rtview.tex->GetHeight();
}else{
rtview.rect.right = (float)rc->GetWidth();
rtview.rect.bottom = (float)rc->GetHeight();
}
}
CKDWORD ropt = rc->GetCurrentRenderOptions();
beh->GetInputParameterValue(PARAM_RENDEROPTIONS, &ropt);
// Remove present command
ropt &= ~CK_RENDER_DOBACKTOFRONT;
VxRect oldrect;
rc->GetViewRect(oldrect);
//--- Set Render Target
rc->SetRenderTarget(rtview.tex);
//--- Set Render View
rc->SetViewRect(rtview.rect);
VxColor oldbg = rc->GetBackgroundMaterial()->GetDiffuse();
rc->GetBackgroundMaterial()->SetDiffuse(bgcolor);
rc->SetOverriddenRendering(CK_OVERRIDE_MATERIAL, mat);
rc->Render((CK_RENDER_FLAGS)ropt);
rc->SetOverriddenRendering(CK_OVERRIDE_NONE);
rc->GetBackgroundMaterial()->SetDiffuse(oldbg);
if (!rtview.tex)
rc->ChangeCurrentRenderOptions(CK_RENDER_RESTORENEXTFRAME | CK_RENDER_SKIPDRAWSCENE | CK_RENDER_DOBACKTOFRONT, CK_RENDER_CLEARBACK);
// Restore old rendertarget
rc->SetRenderTarget(NULL);
rc->SetViewRect(oldrect);
}
beh->ActivateOutput(0);
return CKBR_OK;
}
//-----------------------------------------------------------------------------
CKERROR CreateRenderSceneWithMaterialProto(CKBehaviorPrototype **pproto)
{
CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("Render Scene Using Material");
if(!proto) return CKERR_OUTOFMEMORY;
proto->DeclareInput("In");
proto->DeclareOutput("Out");
proto->DeclareInParameter("Target View", CKPGUID_RTVIEW);
proto->DeclareInParameter("Render Options", CKPGUID_RENDEROPTIONS);
proto->DeclareInParameter("Background Color", CKPGUID_COLOR);
proto->DeclareInParameter("Material", CKPGUID_MATERIAL);
proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
proto->SetFunction(RenderSceneWithMaterial);
*pproto = proto;
return CK_OK;
}
//-----------------------------------------------------------------------------
CKObjectDeclaration *FillBehaviorRenderSceneWithMaterial()
{
CKObjectDeclaration *od = CreateCKObjectDeclaration("Render Scene Using Material");
od->SetDescription("Renders the scene into a RenderTarget View with all meshes using a single material.");
/* rem:
In: triggers the process
Out: is activated when the process is completed.
View: The rendertarget view.
Render Options: Render flags.
Background Color: The background color used for clear.
Material: The material to use to render.
*/
od->SetCategory("Shaders/Rendering");
od->SetType(CKDLL_BEHAVIORPROTOTYPE);
od->SetGuid(RENDERSCENEWITHMATERIAL_GUID);
od->SetAuthorGuid(VIRTOOLS_GUID);
od->SetAuthorName("Virtools");
od->SetVersion(0x00010000);
od->SetCreationFunction(CreateRenderSceneWithMaterialProto);
od->SetCompatibleClassId(CKCID_BEOBJECT);
return od;
}