From: <re...@us...> - 2010-11-30 13:38:55
|
Revision: 35346 http://crystal.svn.sourceforge.net/crystal/?rev=35346&view=rev Author: res2002 Date: 2010-11-30 13:38:48 +0000 (Tue, 30 Nov 2010) Log Message: ----------- Reinstantiate target cycling in simplept Modified Paths: -------------- CS/trunk/apps/tutorial/simplept/simplept.cpp CS/trunk/apps/tutorial/simplept/simplept.h Removed Paths: ------------- CS/trunk/apps/tutorial/simplept/sproctex.cpp Modified: CS/trunk/apps/tutorial/simplept/simplept.cpp =================================================================== --- CS/trunk/apps/tutorial/simplept/simplept.cpp 2010-11-30 04:21:52 UTC (rev 35345) +++ CS/trunk/apps/tutorial/simplept/simplept.cpp 2010-11-30 13:38:48 UTC (rev 35346) @@ -159,11 +159,88 @@ factstate->Invalidate (); } +struct TargetToUse +{ + csRenderTargetAttachment attachment; + const char* format; +}; +static const TargetToUse targetsToUse[] = { + {rtaColor0, "rgb8"}, + {rtaColor0, "rgb16_f"}, + //{rtaDepth, "d32"} // FIXME: Requires attachment support in iRenderManagerTargets +}; + +static const char* AttachmentToStr (csRenderTargetAttachment a) +{ + switch(a) + { + case rtaColor0: return "color0"; + case rtaDepth: return "depth"; + default: return 0; + } +} + +void Simple::CreateTextures () +{ + for (size_t n = 0; n < sizeof(targetsToUse)/sizeof(targetsToUse[0]); n++) + { + if (!g3d->CanSetRenderTarget (targetsToUse[n].format, + targetsToUse[n].attachment)) + { + csReport (object_reg, CS_REPORTER_SEVERITY_ERROR, + "crystalspace.application.simplept", + "Format unsupported: %s(%s)", targetsToUse[n].format, + AttachmentToStr (targetsToUse[n].attachment)); + continue; + } + + csRef<iTextureHandle> texHandle = + g3d->GetTextureManager()->CreateTexture (256, 256, csimg2D, + targetsToUse[n].format, CS_TEXTURE_3D); + if (!texHandle) continue; + + Target target; + target.texh = texHandle; + target.format = targetsToUse[n].format; + target.attachment = targetsToUse[n].attachment; + targetTextures.Push (target); + + availableFormatsStr.AppendFmt ("%s ", target.format); + + if (!targetTex) + { + targetTex = texHandle; + currentTargetStr.Format ("%s(%s)", AttachmentToStr (target.attachment), + target.format); + } + } +} + +void Simple::CycleTarget() +{ + csRef<iRenderManagerTargets> targets = + scfQueryInterface<iRenderManagerTargets> (rm); + if (!targets) return; + + targets->UnregisterRenderTarget (targetTex); + + currentTarget = (currentTarget + 1) % targetTextures.GetSize(); + const Target& target = targetTextures[currentTarget]; + currentTargetStr.Format ("%s(%s)", AttachmentToStr (target.attachment), + target.format); + + targetTex = target.texh; + targets->RegisterRenderTarget (targetTex, targetView); + + // Also need to set new texture handle on material + targetMat->GetMaterial()->GetVariableAdd (svTexDiffuse)->SetValue (targetTex); +} + //----------------------------------------------------------------------------- static const char appID[] = "CrystalSpace.SimplePT"; -Simple::Simple () +Simple::Simple () : currentTarget (0) { SetApplicationName (appID); } @@ -206,20 +283,18 @@ // Tell the camera to render into the frame buffer. rm->RenderView (view); -#if 0 g3d->BeginDraw(CSDRAW_2DGRAPHICS); int fontHeight = font->GetTextHeight(); int y = g3d->GetDriver2D()->GetHeight() - fontHeight; int white = g3d->GetDriver2D()->FindRGB (255, 255, 255); g3d->GetDriver2D()->Write (font, 0, y, white, -1, csString().Format ("SPACE to cycle formats: %s", - ProcTexture->GetAvailableFormats())); + availableFormatsStr.GetData())); y -= fontHeight; g3d->GetDriver2D()->Write (font, 0, y, white, -1, csString().Format ("current target: %s", - ProcTexture->GetCurrentTarget())); + currentTargetStr.GetData())); g3d->FinishDraw (); -#endif } bool Simple::OnKeyboard (iEvent& ev) @@ -235,7 +310,7 @@ else if ((ev.Name == csevKeyboardUp(object_reg)) && (csKeyEventHelper::GetCookedCode (&ev) == CSKEY_SPACE)) { - ProcTexture->CycleTarget (); + CycleTarget (); return true; } @@ -358,6 +433,12 @@ // We use the full window to draw the world. view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ()); + // Grab string ID for "tex diffuse" SV name + csRef<iShaderVarStringSet> svstrings = + csQueryRegistryTagInterface<iShaderVarStringSet> ( + GetObjectRegistry (), "crystalspace.shader.variablenameset"); + svTexDiffuse = svstrings->Request ("tex diffuse"); + // Here we create our world. CreateRoom (); @@ -376,7 +457,7 @@ // pushing our work into the 3D engine and rendering it // to the screen. printer.AttachNew (new FramePrinter (GetObjectRegistry ())); - + return true; } @@ -400,14 +481,10 @@ bool Success = (loader->LoadMapFile ("world", false)); VFS->PopDir (); + targetMat = engine->CreateMaterial ("rendertarget", nullptr); + CreateTextures (); + targetMat->GetMaterial()->GetVariableAdd (svTexDiffuse)->SetValue (targetTex); { - csRef<iTextureHandle> texHandle = - g3d->GetTextureManager()->CreateTexture (256, 256, csimg2D, "rgb8", - CS_TEXTURE_3D); - targetTexture = engine->GetTextureList()->NewTexture (texHandle); - } - csRef<iMaterialWrapper> targetMat = engine->CreateMaterial ("rendertarget", targetTexture); - { iSector *room = engine->GetSectors ()->FindByName ("room"); targetView = csPtr<iView> (new csView (engine, g3d)); targetView->GetCamera ()->SetViewportSize (256, 256); @@ -420,7 +497,7 @@ csRef<iRenderManagerTargets> targets = scfQueryInterface<iRenderManagerTargets> (rm); if (targets) - targets->RegisterRenderTarget (targetTexture->GetTextureHandle(), targetView); + targets->RegisterRenderTarget (targetTex, targetView); } // Load the texture from the standard library. This is located in Modified: CS/trunk/apps/tutorial/simplept/simplept.h =================================================================== --- CS/trunk/apps/tutorial/simplept/simplept.h 2010-11-30 04:21:52 UTC (rev 35345) +++ CS/trunk/apps/tutorial/simplept/simplept.h 2010-11-30 13:38:48 UTC (rev 35346) @@ -21,39 +21,6 @@ #include <crystalspace.h> -class csEngineProcTex : public csProcTexture -{ -private: - csRef<iEngine> Engine; - csRef<iView> View; - - struct Target - { - csRef<iTextureHandle> texh; - csRenderTargetAttachment attachment; - const char* format; - }; - csArray<Target> targets; - size_t currentTarget; - - csString currentTargetStr; - csString availableFormatsStr; - bool renderTargetState; -public: - csEngineProcTex (); - ~csEngineProcTex (); - - bool LoadLevel (); - iTextureWrapper* CreateTexture (iObjectRegistry* object_reg); - virtual bool PrepareAnim (); - virtual void Animate (csTicks current_time); - - const char* GetCurrentTarget () const { return currentTargetStr; } - const char* GetAvailableFormats() const { return availableFormatsStr; } - bool GetRenderTargetState() const { return renderTargetState; } - void CycleTarget(); -}; - class Simple : public csApplicationFramework, public csBaseEventHandler { private: @@ -65,13 +32,14 @@ csRef<iView> view; csRef<iRenderManager> rm; csRef<iVirtualClock> vc; - csEngineProcTex* ProcTexture; csRef<iMeshWrapper> genmesh; csRef<iGeneralFactoryState> factstate; csRef<iFont> font; csRef<FramePrinter> printer; - csRef<iTextureWrapper> targetTexture; + CS::ShaderVarStringID svTexDiffuse; + csRef<iTextureHandle> targetTex; + csRef<iMaterialWrapper> targetMat; csRef<iView> targetView; void CreatePolygon (iGeneralFactoryState *th, int v1, int v2, int v3, int v4); @@ -84,6 +52,21 @@ bool CreateGenMesh (iMaterialWrapper* mat); void AnimateGenMesh (csTicks elapsed); + struct Target + { + csRef<iTextureHandle> texh; + csRenderTargetAttachment attachment; + const char* format; + }; + csArray<Target> targetTextures; + size_t currentTarget; + + csString currentTargetStr; + csString availableFormatsStr; + bool renderTargetState; + + void CreateTextures (); + void CycleTarget(); public: bool SetupModules (); Deleted: CS/trunk/apps/tutorial/simplept/sproctex.cpp =================================================================== --- CS/trunk/apps/tutorial/simplept/sproctex.cpp 2010-11-30 04:21:52 UTC (rev 35345) +++ CS/trunk/apps/tutorial/simplept/sproctex.cpp 2010-11-30 13:38:48 UTC (rev 35346) @@ -1,164 +0,0 @@ -/* - Copyright (C) 2001 by Jorrit Tyberghein - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with this library; if not, write to the Free - Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include "simplept.h" - -csEngineProcTex::csEngineProcTex() : csProcTexture () -{ - mat_w = 256; - mat_h = 256; - - texFlags = CS_TEXTURE_3D; -} - -csEngineProcTex::~csEngineProcTex () -{ -} - -bool csEngineProcTex::LoadLevel () -{ - Engine = csQueryRegistry<iEngine> (object_reg); - csRef<iVFS> vfs = csQueryRegistry<iVFS> (object_reg); - csRef<iLoader> loader = csQueryRegistry<iLoader> (object_reg); - // load a map file to display - vfs->PushDir (); - vfs->ChDir ("/lev/partsys/"); - bool Success = (loader->LoadMapFile ("world", false)); - vfs->PopDir (); - if (!Success) return false; - return true; -} - -struct TargetToUse -{ - csRenderTargetAttachment attachment; - const char* format; -}; -static const TargetToUse targetsToUse[] = { - {rtaColor0, "rgb8"}, - {rtaColor0, "rgb16_f"}, - {rtaDepth, "d32"} -}; - -static const char* AttachmentToStr (csRenderTargetAttachment a) -{ - switch(a) - { - case rtaColor0: return "color0"; - case rtaDepth: return "depth"; - default: return 0; - } -} - -iTextureWrapper* csEngineProcTex::CreateTexture (iObjectRegistry* object_reg) -{ - iTextureWrapper* tex = 0; - - csRef<iEngine> engine (csQueryRegistry<iEngine> (object_reg)); - - for (size_t n = 0; n < sizeof(targetsToUse)/sizeof(targetsToUse[0]); n++) - { - if (!g3d->CanSetRenderTarget (targetsToUse[n].format, - targetsToUse[n].attachment)) - { - csReport (object_reg, CS_REPORTER_SEVERITY_ERROR, - "crystalspace.application.simplept", - "Format unsupported: %s(%s)", targetsToUse[n].format, - AttachmentToStr (targetsToUse[n].attachment)); - continue; - } - - csRef<iTextureHandle> texHandle = - g3d->GetTextureManager()->CreateTexture (mat_w, mat_h, csimg2D, - targetsToUse[n].format, CS_TEXTURE_3D | texFlags); - if (!texHandle) continue; - - Target target; - target.texh = texHandle; - target.format = targetsToUse[n].format; - target.attachment = targetsToUse[n].attachment; - targets.Push (target); - - availableFormatsStr.AppendFmt ("%s ", target.format); - - if (!tex) - { - tex = engine->GetTextureList()->NewTexture (texHandle); - currentTargetStr.Format ("%s(%s)", AttachmentToStr (target.attachment), - target.format); - } - } - - return tex; -} - -bool csEngineProcTex::PrepareAnim () -{ - if (anim_prepared) return true; - if (!csProcTexture::PrepareAnim ()) return false; - - return true; -} - -void csEngineProcTex::Animate (csTicks CurrentTime) -{ - if (!(renderTargetState = g3d->SetRenderTarget (targets[currentTarget].texh, - false, 0, targets[currentTarget].attachment))) - return; - if (!(renderTargetState = g3d->ValidateRenderTargets ())) - return; - - if (!View.IsValid()) - { - // @@@ Here to get the render target size, not screen size - iSector *room = Engine->GetSectors ()->FindByName ("room"); - View = csPtr<iView> (new csView (Engine, g3d)); - View->GetCamera ()->GetTransform ().SetOrigin (csVector3 (-0.5,0,0)); - View->GetCamera ()->SetSector (room); - View->SetRectangle (0, 0, 256, 256); - View->GetCamera ()->SetPerspectiveCenter (128, 128); - View->GetCamera ()->SetFOVAngle (View->GetCamera ()->GetFOVAngle(), 256); - } - - // move the camera - csVector3 Position (-0.5, 0, 3 + sin (CurrentTime / (10*1000.0))*3); - View->GetCamera ()->Move (Position - View->GetCamera () - ->GetTransform ().GetOrigin ()); - - // Switch to the context of the procedural texture. - iTextureHandle *oldContext = Engine->GetContext (); - Engine->SetContext (tex->GetTextureHandle ()); - - // Draw the engine view. - g3d->BeginDraw (CSDRAW_3DGRAPHICS); - //g3d->GetDriver2D()->Clear (g3d->GetDriver2D()->FindRGB (0, 255, 0)); - View->Draw (); - g3d->FinishDraw (); - - // switch back to the old context - Engine->SetContext (oldContext); -} - -void csEngineProcTex::CycleTarget() -{ - currentTarget = (currentTarget + 1) % targets.GetSize(); - const Target& target = targets[currentTarget]; - currentTargetStr.Format ("%s(%s)", AttachmentToStr (target.attachment), - target.format); - tex->SetTextureHandle (targets[currentTarget].texh); -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |