Adding your own Renderer

Apr 28, 2016
Adding your own Renderer
  • Basic information
    ObjectRenderer relies on the data to be displayable, and each renderer does not check for the data to be valid, since this would delay the rendering process. It is the task of the buffer-builder to actually fix and modify the data, so the renderer does not have to make any more calculations based on object data.
    Alright, since this is clear: A renderer basically tries to display a mesh in the world. The location and other transformations are specified by the mesh, not by the renderer.
    Lets take a look at the renderer interface:
    Code (Text):
    public interface IRenderer
    {
        public void renderMesh(BaseMesh mesh);
    }
    As you can see, the renderer will not get notified when the mesh data changes. Notice that one renderer can be used for multiple meshes, thus a renderer should not store any data per mesh.

    How do we implement a renderer?

    As stated earlier, we dont know which bufferbuilder the mesh used to build the data. The renderer just gets the task to display it. If the renderer is not able to display the data because something it requires is missing(face data for instance), then it should not display anything at all.
    Alright, lets make a simple particle renderer:

    Code (Text):
    @Override
        public void renderMesh(BaseMesh mesh)
        {
            List<Vector> vertices = mesh.getObjectBuffer().getVertexBuffer();
            Location baseLocation = mesh.getBaseLocation();
            World world = baseLocation.getWorld();
         
            double originX = baseLocation.getX();
            double originY = baseLocation.getY();
            double originZ = baseLocation.getZ();
         
            Vector scale = mesh.getScale();
            if(scale == null)
            {
                for(Vector v : vertices)
                {
                    world.spawnParticle(
                            this.particle,
                            originX + v.getX(),
                            originY + v.getY(),
                            originZ + v.getZ(),
                            count,
                            offsetX,
                            offsetY,
                            offsetZ,
                            extra);
                }
            }
            else
            {
                for(Vector v : vertices)
                {
                    world.spawnParticle(
                            this.particle,
                            originX + (v.getX() * scale.getX()),
                            originY + (v.getY() * scale.getY()),
                            originZ + (v.getZ() * scale.getZ()),
                            count,
                            offsetX,
                            offsetY,
                            offsetZ,
                            extra);
                }
            }
        }
    As you can see, the particlerenderer needs a valid vertex-buffer and does not check for indices/faces at all, since the bufferbuilder already gave us the correct vertices in the correct order. Thus, the mesh needs to be build with a compatible bufferbuilder to display the data. The objectbuffer contains enough information to be compatible with each renderer, however not every buffer will contain every information possible:

    Code (Text):
    public class ObjectBuffer
    {
        private List<Vector> vertexBuffer;
        private List<Integer> indexBuffer;
       
        private List<Face> faces;
    }
    Always remember:
    Do not modify any mesh data in the renderer!
  • Loading...
  • Loading...