Example #1
0
        private static SceneObject ProcessOBJ(ObjContext objContext)
        {
            if (objContext == null)
            {
                throw new ArgumentNullException("objContext");
            }

            SceneObjectGeometry sceneObject = new SceneObjectGeometry();

            // Program shader to all objects
            // sceneObject.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex", new ShaderCompilerContext("GLO_DEBUG_NORMAL"));
            sceneObject.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+PhongFragment");
            // sceneObject.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Standard+LambertVertex");

            foreach (ObjGroup objGroup in objContext.Groups)
            {
                foreach (ObjGeometry objGeometry in objGroup.MergeGeometries())
                {
                    VertexArrays     vertexArray = objGeometry.CreateArrays(objContext);
                    GraphicsStateSet objectState = new GraphicsStateSet();

                    objectState.DefineState(objGeometry.CreateMaterialState());

                    sceneObject.AddGeometry(vertexArray, objectState);
                }
            }

            return(sceneObject);
        }
Example #2
0
        internal override IEnumerable <SceneObjectBatch> GetGeometries(GraphicsContext ctx, SceneGraphContext ctxScene)
        {
            if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.BoundingVolumes) != 0)
            {
                GraphicsStateSet boundingVolumeState = ctxScene.GraphicsStateStack.Current.Push();

                // Transform
                TransformStateBase boundingVolumeModel = (TransformStateBase)boundingVolumeState[TransformStateBase.StateSetIndex];

                float lightVolumeDepth = 100.0f;
                float lightVolumeSize  = lightVolumeDepth * (float)Math.Tan(Angle.ToRadians(FalloffAngle));

                boundingVolumeModel.LocalModelViewProjection.Set(boundingVolumeModel.LocalModelViewProjection.Multiply(LightMatrix));
                boundingVolumeModel.LocalModelViewProjection.Translate(0.0f, 0.0f, lightVolumeDepth);
                boundingVolumeModel.LocalModelViewProjection.Scale(lightVolumeSize, lightVolumeSize, lightVolumeDepth);

                // Uniform color
                ShaderUniformState uniformState = new ShaderUniformState("UniformState");
                uniformState.SetUniformState("glo_UniformColor", new ColorRGBAF(1.0f, 1.0f, 0.0f, 0.5f));
                boundingVolumeState.DefineState(uniformState);
                // Alpha blending
                boundingVolumeState.DefineState(BlendState.AlphaBlending);

                yield return(new SceneObjectBatch(
                                 _BoundingVolumeArrays,
                                 boundingVolumeState,
                                 _BoundingVolumeProgram
                                 ));
            }

            yield break;
        }
        /// <summary>
        /// Get geometries related to this object.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="ctxScene"></param>
        /// <returns></returns>
        internal override IEnumerable <SceneObjectBatch> GetGeometries(GraphicsContext ctx, SceneGraphContext ctxScene)
        {
            List <SceneObjectBatch> geometries = new List <SceneObjectBatch>();

            GraphicsStateSet sceneGeometryState = ctxScene.GraphicsStateStack.Current.Push();
            TransformState   sceneGeometryModel = (TransformState)sceneGeometryState[TransformState.StateSetIndex];

            if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.CullingViewFrustum) != 0)
            {
                // View-frustum culling
                geometries.AddRange(GetGeometriesViewFrustum(ctxScene));
            }
            else
            {
                // All geometries
                geometries.AddRange(GetGeometries(sceneGeometryState));
            }

            // Bounding volumes
            if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.BoundingVolumes) != 0)
            {
                if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.CullingViewFrustum) != 0)
                {
                    // View-frustum culling
                    geometries.AddRange(GetBoundingVolumes(ctxScene));
                }
            }

            return(geometries);
        }
Example #4
0
        private GraphicsStateSet CreateBBoxState(BoundingBox bbox)
        {
            GraphicsStateSet bboxState          = new GraphicsStateSet();
            TransformState   bboxTransformState = new TransformState();

            bboxTransformState.LocalModel.Translate(bbox.Position);
            bboxTransformState.LocalModel.Scale(bbox.Size);
            bboxState.DefineState(bboxTransformState);

            return(bboxState);
        }
        /// <summary>
        /// Get all geometries compositing this SceneObjectGeometry, filtering them using view-frustum.
        /// </summary>
        /// <param name="currentState">
        /// A <see cref="State.GraphicsStateSet"/> that specifies the current graphics state to be merged with
        /// each returned geometry.
        /// </param>
        /// <param name="clippingPlanes">
        ///
        /// </param>
        /// <param name="viewModel">
        ///
        /// </param>
        /// <returns>
        /// It returns a <see cref="IEnumerable{SceneObjectBatch}"/>.
        /// </returns>
        private IEnumerable <SceneObjectBatch> GetGeometriesViewFrustum(SceneGraphContext ctxScene)
        {
            GraphicsStateSet currentState       = ctxScene.GraphicsStateStack.Current.Push();
            TransformState   sceneGeometryModel = (TransformState)currentState[TransformState.StateSetIndex];
            Matrix4x4f       viewModel          = sceneGeometryModel.ModelView;

            if (_GeometryInstances.Count > 0)
            {
                foreach (Geometry sceneObjectBatch in _GeometryInstances)
                {
                    IBoundingVolume instanceVolume = sceneObjectBatch.BoundingVolume ?? _BoundingVolume;

                    if (instanceVolume != null && instanceVolume.IsClipped(ctxScene.ViewFrustumPlanes, viewModel))
                    {
                        continue;
                    }

                    GraphicsStateSet geometryState;

                    if (sceneObjectBatch.State != null)
                    {
                        geometryState = currentState.Push();
                        geometryState.Merge(sceneObjectBatch.State);
                    }
                    else
                    {
                        geometryState = currentState;
                    }

                    yield return(new SceneObjectBatch(
                                     sceneObjectBatch.VertexArray ?? VertexArray,
                                     geometryState,
                                     sceneObjectBatch.Program ?? Program
                                     ));
                }
            }
            else
            {
                if (_BoundingVolume != null && _BoundingVolume.IsClipped(ctxScene.ViewFrustumPlanes, viewModel))
                {
                    yield break;
                }

                if (VertexArray == null)
                {
                    yield break;
                }

                yield return(new SceneObjectBatch(VertexArray, currentState, Program));
            }
        }
Example #6
0
        /// <summary>
        /// Traverse delegate for creating resources.
        /// </summary>
        /// <param name="ctx">
        /// The <see cref="GraphicsContext"/> used for creating resources.
        /// </param>
        /// <param name="ctxScene"></param>
        /// <param name="sceneObject"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static bool CreateDelegate(GraphicsContext ctx, SceneGraphContext ctxScene, SceneObject sceneObject, object data)
        {
            SceneObjectGeometry sceneGeometry = sceneObject as SceneObjectGeometry;

            if (sceneGeometry != null)
            {
                GraphicsStateSet sceneGeometryState = ctxScene.GraphicsStateStack.Current;

                sceneGeometry.Create(ctx);
                sceneGeometryState.Create(ctx, sceneGeometry.Program);
            }

            return(true);
        }
Example #7
0
        private static void SetBoundingVolumeState(BoundingBox boundingVolume, GraphicsStateSet volumeState)
        {
            // Set transform state
            TransformStateBase transformState = (TransformStateBase)volumeState[TransformStateBase.StateSetIndex];

            if (transformState == null)
            {
                return;
            }

            Vertex3f min = boundingVolume.MinPosition, max = boundingVolume.MaxPosition;
            Vertex3f size = boundingVolume.Size;

            // Scale model matrix in order to represent the bounding box with the correct size and barycenter
            transformState.LocalModelViewProjection.Translate((max + min) / 2.0f);
            transformState.LocalModelViewProjection.Scale(size);
        }
Example #8
0
        /// <summary>
        /// Traverse delegate for collecting geometries to be drawn.
        /// </summary>
        /// <param name="ctx">
        /// The <see cref="GraphicsContext"/> used for drawing.
        /// </param>
        /// <param name="ctxScene"></param>
        /// <param name="sceneObject"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static bool GraphDrawDelegate(GraphicsContext ctx, SceneGraphContext ctxScene, SceneObject sceneObject, object data)
        {
            ObjectBatchContext objectBatchContext = (ObjectBatchContext)data;

            if (sceneObject.ObjectType == SceneObjectGeometry.ClassObjectType)
            {
                SceneObjectGeometry sceneGeometry      = (SceneObjectGeometry)sceneObject;
                GraphicsStateSet    sceneGeometryState = ctxScene.GraphicsStateStack.Current;
                TransformStateBase  sceneGeometryModel = (TransformStateBase)sceneGeometryState[TransformStateBase.StateSetIndex];

                IEnumerable <SceneObjectBatch> geometries;

                if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.CullingViewFrustum) != 0)
                {
                    // View-frustum culling
                    geometries = sceneGeometry.GetGeometries(sceneGeometryState, objectBatchContext.ViewFrustumPlanes, sceneGeometryModel.ModelView);
                }
                else
                {
                    // All geometries
                    geometries = sceneGeometry.GetGeometries(sceneGeometryState);
                }

                objectBatchContext.Objects.AddRange(geometries);

                // Bounding volumes
                if ((ctxScene.Scene.SceneFlags & SceneGraphFlags.BoundingVolumes) != 0)
                {
                }
            }
            else if (sceneObject.ObjectType == SceneObjectLightZone.ClassObjectType)
            {
                SceneObjectLightZone sceneObjectLightZone = (SceneObjectLightZone)sceneObject;

                // TODO: Push instead of Clear to support stacked zones
                objectBatchContext.Lights.Clear();
                objectBatchContext.LightZone = sceneObjectLightZone;
            }
            else if (sceneObject.ObjectType == SceneObjectLight.ClassObjectType)
            {
                objectBatchContext.Lights.Add((SceneObjectLight)sceneObject);
            }

            return(true);
        }
        internal IEnumerable <SceneObjectBatch> GetBoundingVolumes(SceneGraphContext ctxScene)
        {
            GraphicsStateSet currentState       = ctxScene.GraphicsStateStack.Current.Push();
            TransformState   sceneGeometryModel = (TransformState)currentState[TransformState.StateSetIndex];
            Matrix4x4f       viewModel          = sceneGeometryModel.ModelView;

            if (_GeometryInstances.Count > 0)
            {
                foreach (Geometry sceneObjectBatch in _GeometryInstances)
                {
                    IBoundingVolume instanceVolume = sceneObjectBatch.BoundingVolume ?? _BoundingVolume;

                    if (instanceVolume != null && instanceVolume.IsClipped(ctxScene.ViewFrustumPlanes, viewModel))
                    {
                        continue;
                    }

                    GraphicsStateSet volumeState = currentState.Push();

                    SetBoundingVolumeState(instanceVolume, volumeState);

                    yield return(new SceneObjectBatch(
                                     _BoundingVolumeArray,
                                     volumeState,
                                     _BoundingVolumeProgram
                                     ));
                }
            }
            else
            {
                if (_BoundingVolume == null || _BoundingVolume.IsClipped(ctxScene.ViewFrustumPlanes, viewModel))
                {
                    yield break;
                }

                GraphicsStateSet volumeState = currentState.Push();

                SetBoundingVolumeState(_BoundingVolume, volumeState);

                yield return(new SceneObjectBatch(_BoundingVolumeArray, volumeState, _BoundingVolumeProgram));
            }
        }
Example #10
0
        private static void SetBoundingVolumeState(BoundingBox boundingVolume, GraphicsStateSet volumeState)
        {
            // Set transform state
            TransformState transformState = (TransformState)volumeState[TransformState.StateSetIndex];

            if (transformState == null)
            {
                return;
            }

            Vertex3f min = boundingVolume.MinPosition, max = boundingVolume.MaxPosition;
            Vertex3f size = boundingVolume.Size;
            Vertex3f avg  = (max + min) / 2.0f;

            // Scale model matrix in order to represent the bounding box with the correct size and barycenter
            transformState.ModelViewProjection =
                transformState.ModelViewProjection *
                Matrix4x4f.Translated(avg.x, avg.y, avg.z) *
                Matrix4x4f.Scaled(size.x, size.y, size.z);
        }
Example #11
0
        private static void SetBoundingVolumeState(IBoundingVolume boundingVolume, GraphicsStateSet volumeState)
        {
            if (boundingVolume == null)
            {
                throw new ArgumentNullException("boundingVolume");
            }
            if (volumeState == null)
            {
                throw new ArgumentNullException("volumeState");
            }

            if (boundingVolume.GetType() == typeof(BoundingBox))
            {
                SetBoundingVolumeState((BoundingBox)boundingVolume, volumeState);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Example #12
0
        /// <summary>
        /// Get all geometries compositing this SceneObjectGeometry.
        /// </summary>
        /// <param name="currentState">
        /// A <see cref="State.GraphicsStateSet"/> that specifies the current graphics state to be merged with
        /// each returned geometry.
        /// </param>
        /// <returns>
        /// It returns a <see cref="IEnumerable{SceneObjectBatch}"/>.
        /// </returns>
        private IEnumerable <SceneObjectBatch> GetGeometries(GraphicsStateSet currentState)
        {
            if (currentState == null)
            {
                throw new ArgumentNullException("currentState");
            }

            if (_GeometryInstances.Count > 0)
            {
                foreach (Geometry sceneObjectBatch in _GeometryInstances)
                {
                    GraphicsStateSet geometryState;

                    if (sceneObjectBatch.State != null)
                    {
                        geometryState = currentState.Push();
                        geometryState.Merge(sceneObjectBatch.State);
                    }
                    else
                    {
                        geometryState = currentState;
                    }

                    yield return(new SceneObjectBatch(
                                     sceneObjectBatch.VertexArray ?? VertexArray,
                                     geometryState,
                                     sceneObjectBatch.Program ?? Program
                                     ));
                }
            }
            else
            {
                if (VertexArray == null)
                {
                    yield break;
                }

                yield return(new SceneObjectBatch(VertexArray, currentState, Program));
            }
        }