예제 #1
0
파일: Clipmap.cs 프로젝트: hach-que/SLSharp
        public Clipmap(SceneManager scene)
        {
            _scene = scene;
            _scene.QueueStarted += (sender, args) =>
            {
                if (args.RenderQueueId == _scene.GetRenderQueue().DefaultRenderGroup)
                    QueuePatches(_scene.GetRenderQueue());
                args.SkipInvocation = false;
            };

            using (var testMap = (Bitmap)Image.FromFile(@"height.jpg"))
            {
                _testMapWidth = testMap.Width;
                _testMapHeight = testMap.Height;
                _testMap = new float[_testMapWidth * _testMapHeight];
                var i = 0;
                for (var y = 0; y < testMap.Height; y++)
                    for (var x = 0; x < testMap.Width; x++)
                        _testMap[i++] = testMap.GetPixel(x, y).R / 255.0f;
            }

            var tu = _shader.Sampler(() => _shader.Heightmap);
            tu.DesiredFormat = PixelFormat.FLOAT32_RGB;
            tu.SetTextureFiltering(FilterOptions.Point, FilterOptions.Point, FilterOptions.None);
            tu.SetTextureAddressingMode(TextureAddressing.Wrap);
            tu.BindingType = TextureBindingType.Vertex;

            _shader.SetAuto(() => _shader.ModelViewProjectionMatrix, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix);
            //_shader.SetAuto(() => _shader.NormalMatrix, GpuProgramParameters.AutoConstantType.ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX);
            _shader.SetAuto(() => _shader.ScaleFactor, GpuProgramParameters.AutoConstantType.Custom, 0);
            _shader.SetAuto(() => _shader.FineBlockOrigin, GpuProgramParameters.AutoConstantType.Custom, 1);

            Position = new IntFloatVector2(new IntFloat(-_testMapWidth / 4), new IntFloat(-_testMapHeight / 4));

            Locations = new PatchLocations(H, M);

            var scale = Scale;
            var scaleInt = 1;

            for (var i = 0; i < Levels; i++)
            {
                _levels[i] = new ClipmapLevel(scale, scaleInt, this);
                var level = _levels[i];
                tu.SetTextureName(level.Heightmap.Name);
                var m = _shader.CloneMaterial("ClipmapLevel" + i);
                _levels[i].Material = m;

                scale *= 2.0f;
                scaleInt *= 2;
            }

            UpdatePosition();
            Reset();

            _initialized = true;
        }
예제 #2
0
        public Clipmap(SceneManager scene)
        {
            _scene = scene;
            _scene.QueueStarted += (sender, args) =>
            {
                if (args.RenderQueueId == _scene.GetRenderQueue().DefaultRenderGroup)
                {
                    QueuePatches(_scene.GetRenderQueue());
                }
                args.SkipInvocation = false;
            };

            using (var testMap = (Bitmap)Image.FromFile(@"height.jpg"))
            {
                _testMapWidth  = testMap.Width;
                _testMapHeight = testMap.Height;
                _testMap       = new float[_testMapWidth * _testMapHeight];
                var i = 0;
                for (var y = 0; y < testMap.Height; y++)
                {
                    for (var x = 0; x < testMap.Width; x++)
                    {
                        _testMap[i++] = testMap.GetPixel(x, y).R / 255.0f;
                    }
                }
            }


            var tu = _shader.Sampler(() => _shader.Heightmap);

            tu.DesiredFormat = PixelFormat.FLOAT32_RGB;
            tu.SetTextureFiltering(FilterOptions.Point, FilterOptions.Point, FilterOptions.None);
            tu.SetTextureAddressingMode(TextureAddressing.Wrap);
            tu.BindingType = TextureBindingType.Vertex;

            _shader.SetAuto(() => _shader.ModelViewProjectionMatrix, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix);
            //_shader.SetAuto(() => _shader.NormalMatrix, GpuProgramParameters.AutoConstantType.ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX);
            _shader.SetAuto(() => _shader.ScaleFactor, GpuProgramParameters.AutoConstantType.Custom, 0);
            _shader.SetAuto(() => _shader.FineBlockOrigin, GpuProgramParameters.AutoConstantType.Custom, 1);

            Position = new IntFloatVector2(new IntFloat(-_testMapWidth / 4), new IntFloat(-_testMapHeight / 4));

            Locations = new PatchLocations(H, M);

            var scale    = Scale;
            var scaleInt = 1;

            for (var i = 0; i < Levels; i++)
            {
                _levels[i] = new ClipmapLevel(scale, scaleInt, this);
                var level = _levels[i];
                tu.SetTextureName(level.Heightmap.Name);
                var m = _shader.CloneMaterial("ClipmapLevel" + i);
                _levels[i].Material = m;

                scale    *= 2.0f;
                scaleInt *= 2;
            }

            UpdatePosition();
            Reset();

            _initialized = true;
        }