private IEnumerable <Vector3> _GetVolumePoints()
        {
            foreach (var obj in _objects)
            {
                IBoundable bound = obj as IBoundable;
                if (bound == null)
                {
                    continue;
                }

                var boundingBox    = bound.BoundingBox;
                var boundingSphere = bound.BoundingSphere;
                if (boundingBox != null)
                {
                    BoxBuilder box = new BoxBuilder(2, 2, 2);

                    for (int i = 0; i < box.Vertices.Length; i++)
                    {
                        var mat = Matrix.Scale(boundingBox.Extends) * boundingBox.GlobalPose;
                        yield return(Vector3.Transform(box.Vertices[i].Position, mat));
                    }
                }
                else if (boundingSphere.Radius > 0)
                {
                    SphereBuilder sphere = new SphereBuilder(16, 16, boundingSphere.Radius);
                    for (int i = 0; i < sphere.Vertices.Length; i++)
                    {
                        var mat = Matrix.Translate(boundingSphere.Center);
                        yield return(Vector3.Transform(sphere.Vertices[i].Position, mat));
                    }
                }
            }
        }
Exemple #2
0
        public Model Finish()
        {
            GraphicsBuffer buffer      = new GraphicsBuffer();
            SphereBuilder  modelBounds = new SphereBuilder();

            foreach (MeshInfo meshInfo in meshes)
            {
                meshInfo.AddCentrePoints(ref modelBounds, positionChannel, indices);
            }
            modelBounds.CalculateCentre();
            foreach (MeshInfo meshInfo in meshes)
            {
                meshInfo.AddRadiusPoints(ref modelBounds, positionChannel, indices);
            }

            Model model = new Model(buffer, ElementType.UInt32, modelBounds.Sphere);

            // Put the bones in the model.
            foreach (var mesh in meshes)
            {
                if (mesh.Bone != null && mesh.Bone.model == null)
                {
                    model.Bones.Add(mesh.Bone);
                }
            }

            // Create the meshes and put them in the model.
            foreach (MeshInfo meshInfo in meshes)
            {
                SphereBuilder meshSphere = new SphereBuilder();

                meshInfo.AddCentrePoints(ref meshSphere, positionChannel, indices);
                meshSphere.CalculateCentre();
                meshInfo.AddRadiusPoints(ref meshSphere, positionChannel, indices);

                ModelMesh mesh = new ModelMesh(meshSphere.Sphere, meshInfo.Bone, meshInfo.Parts);
                model.Meshes.Add(mesh);
            }

            // Calculate the total size of the buffer.
            int bufferSize = indexCount * 4;

            foreach (var channel in Channels)
            {
                channel.CheckSize(ref bufferSize);
            }

            // Write the data into the buffer.
            byte[] bufferData   = new byte[bufferSize];
            int    bufferOffset = indexCount * 4;

            indices.CopyTo(0, indexCount, bufferData, 0);
            foreach (var channel in Channels)
            {
                channel.Write(bufferData, ref bufferOffset, model, buffer);
            }
            buffer.Data(bufferData);

            return(model);
        }
        public override void Draw(RenderContext renderContext, GameTime gameTime)
        {
            if (renderContext.layerPass == RenderContext.LayerPass.MAIN_PASS && !Game1.DEFERRED_RENDERING)
            {
                var effect = this.GetDefaultEffect(renderContext.graphicsDevice);

                camera.ApplyMatrices(effect);
                float distance = (float)(9 * Math.Pow(0.5, camera.cameraZoom)); // TODO: this is hacky
                effect.View = CameraMatrixManager.GetWorldRelativeView(distance);

                effect.TextureEnabled = true;
                foreach (var rootSector in ZCoords.GetSectorManager().GetTopmostOSMSectors())
                {
                    SectorBounds       bounds = GetSectorBounds(renderContext.graphicsDevice, rootSector);
                    VertexIndiceBuffer sphere = SphereBuilder.MakeSphereSegExplicit(renderContext.graphicsDevice, rootSector, 2, bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, camera);
                    effect.Texture = renderTargets[rootSector];
                    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        renderContext.graphicsDevice.Indices = sphere.indices;
                        renderContext.graphicsDevice.SetVertexBuffer(sphere.vertices);
                        renderContext.graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, sphere.indices.IndexCount / 3);
                    }
                    sphere.vertices.Dispose();
                    sphere.indices.Dispose();
                }
            }
            if (renderContext.layerPass == RenderContext.LayerPass.MAIN_PASS && Game1.DEFERRED_RENDERING)
            {
                var    effect   = GlobalContent.DeferredBasicNormalTextureShader;
                float  distance = (float)(9 * Math.Pow(0.5, camera.cameraZoom)); // TODO: this is hacky
                Matrix view     = CameraMatrixManager.GetWorldRelativeView(distance);
                effect.Parameters["WVP"].SetValue(camera.world * view * camera.projection);
                foreach (var rootSector in ZCoords.GetSectorManager().GetTopmostOSMSectors())
                {
                    SectorBounds       bounds = GetSectorBounds(renderContext.graphicsDevice, rootSector);
                    VertexIndiceBuffer sphere = SphereBuilder.MakeSphereSegExplicit(renderContext.graphicsDevice, rootSector, 2, bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, camera);
                    effect.Parameters["Texture"].SetValue(renderTargets[rootSector]);
                    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        renderContext.graphicsDevice.Indices = sphere.indices;
                        renderContext.graphicsDevice.SetVertexBuffer(sphere.vertices);
                        renderContext.graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, sphere.indices.IndexCount / 3);
                    }
                    sphere.vertices.Dispose();
                    sphere.indices.Dispose();
                }
            }
            foreach (var rootSector in ZCoords.GetSectorManager().GetTopmostOSMSectors())
            {
                Draw3D(renderContext.graphicsDevice, allBounds[rootSector], rootSector, renderContext.layerPass);
            }
        }
Exemple #4
0
            public void AddRadiusPoints(ref SphereBuilder sphereBuilder, Channel <BVector3> positionChannel, int[] indices)
            {
                foreach (var part in Parts)
                {
                    for (int index = part.Offset / 4, count = part.Count + index; index < count; index++)
                    {
                        int element = indices[index];
                        if (element == -1)
                        {
                            continue;
                        }
                        Vector3f point = positionChannel[element];

                        sphereBuilder.AddRadiusPoint(ref point);
                    }
                }
            }
        public static Mesh CreateSphere(int stacks, int slices, float radius)
        {
            SphereBuilder sphere = new SphereBuilder(stacks, slices, radius);

            return(CreateMesh(sphere.Vertices, sphere.Indices));
        }