• Render Component (Components\RenderComponents\RenderComponent.cs)
    • The RenderComponent is used to give an Entity a model from which they can be rendered. The RenderComponent is responsible for letting the Graphics system know there is a model to be rendered for its Entity. The Graphics system will query each Entity in a Scene for RenderChunks, if the Entity has a RenderComponent, when it is queried it will give the Renderer this information so it can render the model. (See end of this section for extra reading about querying for render chunks, and how it works). Chunks are automatically created by the RenderComponent based on the model and Entity information. You shouldn’t need to change anything here unless you plan on adding features to the engine. 
    • Loading a model from file
      • Loading a model for your Entity is very simple. Do you remember earlier in the tutorial we learned how to load an Entity into the world, but we didn’t have a model to actually see it in the world? This is how we load that model.
      • If you want to dynamically create a RenderComponent, it would be done something like this: RenderComponent newRenderComponent = new RenderComponent(newEntity, "Models/ship", "Material/SimpleColored");
      • This will create a RenderComponent, attach it to the Entity ‘newEntity’ (in this case), give it the model defined by the file “Models/ship”, and it will give it the Material defined by the file “Material/SimpleColored”. Remember, all content references like "Models/ship" are within the "Content" directory within the sample project (or your own project, should you make one).
      • If newEntity has been added to the SceneManager, you should now be able to see it in the world (at whatever position you specified), with your camera.
      • If you're loading a RenderComponent from a definition XML file, then you just specify the model (Models/ship)and material (Material/SimpleColored) within that file. To see how it's done in the demo, look at the definition XML file, located at 'Content/Entities/ComponentDefinitions/Render/SpaceshipRender.xml' within the QuickStartSampleGameContent project.
    • Loading a model from a GeometricPrimitive
      • You can also load RenderComponents from primitive shape descriptions. There is a folder in the main engine called 'GeometricPrimitives' that contains all the code for this system.
      • The GeometricPrimitive code was pulled from an XNA Education Sample, made by Microsoft, and then adapted for this engine.
      • Dynamically loading from a primitive is very simple, here's the line of code required to do it:
        RenderComponent newRenderComponent = new RenderComponent(newEntity, "Material/SimpleColored", GeometricPrimitiveType.Cube);
      • The first parameter is the BaseEntity, the second parameter is the material, and the third parameter is the type of geometric primitive. The available types are: Cube, Sphere, Cylinder, Torus, and Teapot.
      • If you want to specify a GeometricPrimitive from your definition XML file, leave the 'ModelPath' empty, and in the 'PrimitiveType' section put the type of shape you want. The different types are defined at the top of GeometricPrimitive.cs, in the GeometricPrimitives folder within the QuickStart_Windows project. You can see an example of this in the definition XML file located at 'Content/Entities/ComponentDefinitions/Render/YellowSphereRender.xml'.
    • Setting Shadowing properties
      • If you want your entity to be able to cast shadows, or have shadows cast upon it, you’ll need to set shadow properties on your RenderComponent. You can see how to do this on the line below:
      • You can do this by directly accessing the RenderComponent.
      • To setup shadowing properties within your definition XML file, just specify true or false for the 'CanCreateShadows' or 'CanReceiveShadows' properties within the file.
    • Setting Model Color
      • A model’s default color is white. If you’d like to change a model’s color, do this:
      • Model color can be specified within the definition XML through the 'MeshColor' parameter. Please note that this parameter in the XML has values between 0.0 and 1.0 for R, G, B, and A. So if your color is yellow, which is (255, 140, 0, 255), then to convert to a scale from 0.0 to 1.0 you divide each component by 255 to get (1.0, 0.55, 0.0, 1.0).
    • Setting a Model’s transparency
      • There are a few ways to set transparency on a model.
        • The first way is simply to alter the alpha value of the model’s color, so we could simply include an alpha value when loading a model’s color. The following creates a red model, with 60% opacity:
        • You can change the opacity of a model anytime. If you have direct access to the render component, you can access the Opacity member variable, and set it to a value between 0 and 1. The following creates a red model, with 60% opacity:
          newRenderComponent.ModelColor = new Color(1.0f, 0.0f, 0.0f, 0.60f);
        • Of course this can also be done through the definition XML file, by setting the 'Opacity' parameter.
      • Note: Known Bug (v0.22 and higher), Depth-sorting for transparent meshes
        • The engine does not yet sort transparent objects by depth when rendering them, so there is the chance that when two or more transparent objects occupy the same space on the screen, that you will not see a blend of all of them. An example of this is that semi-transparent objects always render on top of particles, even if they're behind them.
        • This also affects when shadows of transparent and opaque objects overlap, the shadow will not always take the darkness of the opaque entity.
    • How Querying for Render Chunks Works (Optional Extra Reading)
      • The Graphics system calls DrawFrame() each frame. This is done in GraphicsSystem.cs, in the DrawFrame() function. It asks all registered providers of RenderChunks (which is an abstract way of saying “stuff that can be rendered or used in rendering”),  which chunks it has available to be rendered.
      • The SceneManager is a registered provider of RenderChunks. So it is queried for which chunks it has. You can see this occur in SceneManager.cs, in the QueryForRenderChunks() function. This function will then in turn call QueryForRenderChunks on every entity in the scene. The entity will check if it has a RenderComponent, and if it does then it will return its RenderChunk to the SceneManager, which returns it to the Renderer in the Graphics system.
      • If you’d like to see how the RenderComponent creates the chunk, you can find it in RenderComponent.cs in the QueryForRenderChunks() function. A chunk of information about the model, including its material, and world transform, is created.
    • Using render ordering (Optional Extra Reading)
      • You can set render ordering with each render chunk by setting the RenderOrder variable. The variable is of the type PreferredRenderOrder.
      • The default for all render chunks is PreferredRenderOrder.RenderNormal, if you need something to render before that, set it to PreferredRenderOrder.RenderFirst. If you need something to render after the default, use PreferredRenderOrder.RenderLast.
      • The general rule is to render opaque entities first, then the sky, then entities that may have transparency (which, eventually, we will have the engine sort in order of depth from the camera for a more specific render order).
      • Render order can be set within the definition XML file through the 'RenderOrder' parameter.

Last edited Jan 5, 2012 at 5:22 AM by LordIkon, version 1

Comments

No comments yet.