History: MOGREIntermediateTutorial7Source
Source of version: 2 (current)
Copy to clipboard
This is the C# source for ((MOGRE Intermediate Tutorial 7)).
{CODE(wrap="1",colors="c++")}
namespace Mogre.Demo.RenderToTexure
{
class Program
{
static void Main(string[] args)
{
try { new RTTTutorial().Go(); }
catch
{
if (OgreException.IsThrown) ExampleApplication.Example.ShowOgreException();
else throw;
}
}
}
class RTTTutorial : ExampleApplication.Example
{
Plane mPlane;
Entity mPlaneEnt;
SceneNode mPlaneNode;
public override void CreateScene()
{
sceneMgr.AmbientLight = new ColourValue(0.2f, 0.2f, 0.2f);
Light light = sceneMgr.CreateLight("MainLight");
light.Position = new Vector3(20, 80, 50);
camera.Position = new Vector3(60, 200, 70);
camera.LookAt(0, 0, 0);
//First of all, we need to create a texture.
MaterialPtr mat = MaterialManager.Singleton.Create("PlaneMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
TextureUnitState tuisTexture = mat.GetTechnique(0).GetPass(0).CreateTextureUnitState("grass_1024.jpg");
mPlane = new Plane();
mPlane.d = 0;
mPlane.normal = Vector3.UNIT_Y;
MeshManager.Singleton.CreatePlane("PlaneMesh", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, mPlane, 120, 120, 1, 1, true, 1, 1, 1, Vector3.UNIT_Z);
mPlaneEnt = sceneMgr.CreateEntity("PlaneEntity", "PlaneMesh");
mPlaneEnt.SetMaterialName("PlaneMat");
mPlaneNode = sceneMgr.RootSceneNode.CreateChildSceneNode();
mPlaneNode.AttachObject(mPlaneEnt);
//-------------
TexturePtr rtt_texture = TextureManager.Singleton.CreateManual("RttTex", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, window.Width, window.Height, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET);
RenderTexture renderTexture = rtt_texture.GetBuffer().GetRenderTarget();
renderTexture.AddViewport(camera);
renderTexture.GetViewport(0).SetClearEveryFrame(true);
renderTexture.GetViewport(0).BackgroundColour = ColourValue.Black;
renderTexture.GetViewport(0).OverlaysEnabled = false;
// Either this way
renderTexture.IsAutoUpdated = true;
//or this way
//renderTexture.Update();
// Now save the contents
//renderTexture.WriteContentsToFile("start.png");
//-------------
mMiniScreen = new Rectangle2D(true);
mMiniScreen.SetCorners(0f, 0f, 1.0f, -1.0f);
mMiniScreen.BoundingBox = new AxisAlignedBox(-100000.0f * Vector3.UNIT_SCALE, 100000.0f * Vector3.UNIT_SCALE);
SceneNode miniScreenNode = sceneMgr.RootSceneNode.CreateChildSceneNode("MiniScreenNode");
miniScreenNode.AttachObject(mMiniScreen);
//--------------
//MaterialPtr renderMaterial = MaterialManager.Singleton.Create("RttMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
//renderMaterial.CreateTechnique().CreatePass();
//Pass pass = renderMaterial.GetTechnique(0).GetPass(0);
//pass.LightingEnabled = false;
//pass.CreateTextureUnitState("RttTex");
//mMiniScreen.SetMaterial("RttMat");
//--------------
MaterialPtr renderMaterial = MaterialManager.Singleton.GetByName("Sepia");
renderMaterial.GetTechnique(0).GetPass(0).GetTextureUnitState(0).SetTextureName("RttTex");
mMiniScreen.SetMaterial("Sepia");
renderTexture.PreRenderTargetUpdate += renderTexture_PreRenderTargetUpdate;
renderTexture.PostRenderTargetUpdate += renderTexture_PostRenderTargetUpdate;
root.FrameRenderingQueued += frameRenderingQueued;
}
private Rectangle2D mMiniScreen;
void renderTexture_PreRenderTargetUpdate(RenderTargetEvent_NativePtr evt)
{
mMiniScreen.Visible = false;
}
void renderTexture_PostRenderTargetUpdate(RenderTargetEvent_NativePtr evt)
{
mMiniScreen.Visible = true;
}
bool frameRenderingQueued(FrameEvent evt)
{
mPlaneNode.Yaw(new Radian(evt.timeSinceLastFrame));
return true;
}
}
}
{CODE}