예제 #1
0
        protected override void OnLoad(EventArgs e)
        {
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Enable(EnableCap.DepthTest);

            InitScene();

            CursorVisible = false;
            base.OnLoad(e);
        }
예제 #2
0
        public void Draw(double deltaTime)
        {
            Debug.Clear();
            _selectedBlocks.Clear();

            _chunkPartManager.CheckInvalidations(_world);

            _framerate = (float)(1 / deltaTime);
            GL.ClearColor(new Color4(0.6f, 0.8f, 0.85f, 1f));

            GL.ClearDepth(1);
            GL.DepthFunc(DepthFunction.Less);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.FrontFace(FrontFaceDirection.Ccw);
            GL.Enable(EnableCap.CullFace);

            GL.Disable(EnableCap.PolygonSmooth);
            GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Fastest);

            var cameraPostition = _player.Position + new Vector3(0, 1.7f, 0);

            GL.Disable(EnableCap.DepthTest);
            var skyboxView = Matrix4.CreateRotationY(_player.Rotations.Y) *
                             Matrix4.CreateRotationX(_player.Rotations.X);
            var skyboxColor = new Vector3(1, 1, 1);

            _skyboxShader.Bind();
            _skyboxShader.SetUniformMatrix4("proj", false, ref _proj);
            _skyboxShader.SetUniformMatrix4("view", false, ref skyboxView);
            _skyboxShader.SetUniform3("col", ref skyboxColor);
            _skybox.BindVao(_skyboxShader);
            _skybox.Draw(_skyTexture);
            GL.Enable(EnableCap.DepthTest);

            try
            {
                HitInfo hitInfo;
                if (_worldManager.CastRay(_world, cameraPostition, _player.EyeForward, 20f, out hitInfo))
                {
                    _selectedBlocks.AddAABB(new AABBObjects.AABBObject
                    {
                        Position  = new Vector3(hitInfo.X - 0.001f, hitInfo.Y - 0.001f, hitInfo.Z - 0.001f),
                        Position2 = new Vector3(hitInfo.X + 1.001f, hitInfo.Y + 1.001f, hitInfo.Z + 1.001f),
                        Color     = new Vector4(0, 0, 0, 0.4f),
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            var view = Matrix4.CreateTranslation(-cameraPostition) * Matrix4.CreateRotationY(_player.Rotations.Y) * Matrix4.CreateRotationX(_player.Rotations.X);

            var mv = (view * _proj);

            mv.Transpose();
            _frustum[0].Nums = mv.Row3 + mv.Row0;
            _frustum[1].Nums = mv.Row3 - mv.Row0;
            _frustum[2].Nums = mv.Row3 - mv.Row1;
            _frustum[3].Nums = mv.Row3 + mv.Row1;
            _frustum[4].Nums = mv.Row2;
            _frustum[5].Nums = mv.Row3 - mv.Row2;

            _baseShader.Bind();
            _baseShader.SetUniform3("cameraPosition", ref cameraPostition);
            _baseShader.SetUniformMatrix4("proj", false, ref _proj);
            _baseShader.SetUniformMatrix4("view", false, ref view);
            var chunks = _worldManager.GetVisibleChunks(_world, cameraPostition, _frustum);

            _visibleChunks = chunks.Count;
            TextureManager.Use(_terrainTexture);
            foreach (var chunk in chunks)
            {
                _chunkManager.Draw(chunk, _baseShader);
            }
            TextureManager.Disable();
            _baseShader.Unbind();

            GL.Enable(EnableCap.Blend);
            _aabbShader.Bind();
            _aabbShader.SetUniformMatrix4("proj", false, ref _proj);
            _aabbShader.SetUniformMatrix4("view", false, ref view);
            _selectedBlocks.UpdateData();
            _selectedBlocks.BindVao(_aabbShader);
            _selectedBlocks.Draw();
            _aabbShader.Unbind();
            GL.Disable(EnableCap.Blend);

            // START DEBUG DRAWING
            GL.Disable(EnableCap.DepthTest);

            _debugShader.Bind();
            _debugShader.SetUniformMatrix4("proj", false, ref _proj);
            _debugShader.SetUniformMatrix4("view", false, ref view);
            Debug.UpdateData();
            Debug.BindVao(_debugShader);
            Debug.Draw();
            _debugShader.Unbind();
            GL.Enable(EnableCap.DepthTest);

            // END DEBUG DRAWING

            _guiShader.Bind();
            GL.Disable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            _guiShader.SetUniformMatrix4("proj", false, ref _guiProj);
            view = Matrix4.CreateTranslation(new Vector3(_text.Position) - new Vector3(_width / 2, _height / 2, 0));
            _guiShader.SetUniformMatrix4("view", false, ref view);
            _text.Draw();

            TextureManager.Use(_iconsTexture);
            view = Matrix4.CreateTranslation(-new Vector3(_crosshair.Size.X / 2, _crosshair.Size.Y / 2, 0));
            _guiShader.SetUniformMatrix4("view", false, ref view);
            _crosshair.Draw();
            TextureManager.Disable();

            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.DepthTest);
            _guiShader.Unbind();
        }