Ejemplo n.º 1
0
        /// <summary>
        /// Draw this SceneGraph.
        /// </summary>
        /// <param name="ctx">
        /// The <see cref="GraphicsContext"/> used for drawing.
        /// </param>
        /// <param name="programOverride">
        /// A <see cref="ShaderProgram"/> that overrides the default one used for rendering the batch. It can be null.
        /// </param>
        public void Draw(GraphicsContext ctx, ShaderProgram programOverride)
        {
            CheckCurrentContext(ctx);

            // View parameters
            SceneRoot.LocalProjection          = ProjectionMatrix;
            SceneRoot.LocalModel               = new ModelMatrix();
            SceneRoot.LocalModelView           = ViewMatrix;
            SceneRoot.LocalModelViewProjection = new ModelMatrix((Matrix4x4)ProjectionMatrix.Multiply(ViewMatrix));

            using (SceneGraphContext ctxScene = new SceneGraphContext(this)) {
                ObjectBatchContext objectBatchContext = new ObjectBatchContext();

                // Traverse the scene graph
                SceneRoot.TraverseDirect(ctx, ctxScene, _TraverseDrawContext, objectBatchContext);
                // Generate shadow maps
                if ((SceneFlags & SceneGraphFlags.Lighting) != 0 && (SceneFlags & SceneGraphFlags.ShadowMaps) != 0)
                {
                    _LightManager.GenerateShadowMaps(ctx, this);
                }

                // Sort geometries
                List <SceneObjectBatch> sceneObjects = objectBatchContext.Objects;
                if (((SceneFlags & SceneGraphFlags.StateSorting) != 0) && (_SorterRoot != null))
                {
                    sceneObjects = _SorterRoot.Sort(objectBatchContext.Objects);
                }

                // Draw all batches
                KhronosApi.LogComment("*** Draw Graph");
                foreach (SceneObjectBatch objectBatch in sceneObjects)
                {
                    objectBatch.Draw(ctx, programOverride);
                }

                // Debug: shadow maps
                if ((SceneFlags & SceneGraphFlags.Lighting) != 0 && (SceneFlags & SceneGraphFlags.ShadowMaps) != 0)
                {
                    DisplayShadowMaps(ctx);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update framebuffer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObjectsControl_Render(object sender, GlControlEventArgs e)
        {
            GlControl senderControl     = (GlControl)sender;
            float     senderAspectRatio = (float)senderControl.Width / senderControl.Height;

            KhronosApi.LogEnabled = false;
            KhronosApi.LogComment("--------------------------------------------------");
            KhronosApi.LogComment("*** Draw");

            // Clear
            Gl.Viewport(0, 0, senderControl.Width, senderControl.Height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            _CubeScene.CurrentView.ProjectionMatrix = new PerspectiveProjectionMatrix(45.0f, senderAspectRatio, 0.1f, 100.0f);
            _CubeScene.CurrentView.LocalModel.SetIdentity();
            _CubeScene.CurrentView.LocalModel.Translate(_ViewStrideLat, _ViewStrideAlt, 0.0f);
            _CubeScene.CurrentView.LocalModel.RotateY(_ViewAzimuth);
            _CubeScene.CurrentView.LocalModel.RotateX(_ViewElevation);
            _CubeScene.CurrentView.LocalModel.Translate(0.0f, 0.0f, _ViewLever);
            _CubeScene.UpdateViewMatrix();

            _CubeScene.Draw(_Context);

            KhronosApi.LogEnabled = false;

            return;

            // Overlay
            ProjectionMatrix overlayProjection = new OrthoProjectionMatrix(0.0f, ClientSize.Width, 0.0f, ClientSize.Height);
            ModelMatrix      overlayModel      = new ModelMatrix();

            overlayModel.Translate(0.375f, 0.375f);

            Gl.Clear(ClearBufferMask.DepthBufferBit);

            ColorRGBAF fpsColor = ColorRGBAF.ColorGreen;
            int        fps      = (int)Math.Floor(1000.0 / senderControl.FrameSwapTime.TotalMilliseconds);

            if (fps >= 59)
            {
                fpsColor = ColorRGBAF.ColorGreen;
            }
            else if (fps >= 29)
            {
                fpsColor = ColorRGBAF.ColorBlue;
            }
            else if (fps >= 24)
            {
                fpsColor = ColorRGBAF.ColorYellow;
            }
            else
            {
                fpsColor = ColorRGBAF.ColorRed;
            }

            fontPatch.DrawString(
                _Context, overlayProjection * overlayModel, fpsColor,
                String.Format("FPS: {0}", fps)
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 64 + 0.375f);
            fontTitle.DrawString(
                _Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                "Hello Objects example ~(^.^)~"
                );

            overlayModel.SetIdentity();
            overlayModel.Translate(0.375f, ClientSize.Height - 128 + 0.375f);
            fontTitleV.DrawString(_Context, overlayProjection * overlayModel, ColorRGBAF.ColorGreen,
                                  "Hello Objects example ~(^.^)~"
                                  );

            // Release resources
            _Context.DisposeResources();
        }