Ejemplo n.º 1
0
        private static IContainer RegisterDependencies()
        {
            var builder = new ContainerBuilder();

            var logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .MinimumLevel.Verbose()
                         .Enrich.FromLogContext()
                         .CreateLogger();

            Log.Logger = logger;

            var context = new OpenGlContext(640, 480, GraphicsMode.Default, "Dummy Context", logger); //this is a hack to create a context before any opengl calls

            builder.Register(c => context).As <OpenGlContext>().SingleInstance();
            builder.Register(c => logger).As <ILogger>().SingleInstance();
            builder.RegisterType <Game>().As <IGame>().OnRelease(game => game.Dispose()).InstancePerLifetimeScope();
            builder.RegisterType <AppConfig>().As <IAppConfig>().SingleInstance();
            builder.Register(c => new Camera(new Vector3(-1, 10, -1), 90, 0)).As <ICamera>().SingleInstance();
            builder.RegisterType <GroundGenerator>().As <IGroundGenerator>().SingleInstance();
            builder.RegisterType <BuildingGenerator>().As <IBuildingGenerator>().SingleInstance();
            builder.RegisterType <World>().As <IWorld>().SingleInstance();
            builder.RegisterType <Renderer.Renderer>().As <IRenderer>().InstancePerDependency();
            builder.RegisterType <ProceduralSkybox>().As <ISkybox>().SingleInstance();
            builder.RegisterType <BillboardTextureGenerator>().As <IBillboardTextureGenerator>().SingleInstance();
            builder.RegisterType <BillboardBuilder>().As <IBillboardBuilder>().SingleInstance();

            return(builder.Build());
        }
Ejemplo n.º 2
0
        public Game(
            IAppConfig config,
            ILogger logger,
            ICamera camera,
            IWorld world,
            OpenGlContext context,
            IRenderer renderer,
            IRenderer ndcRenderer,
            IRenderer skyboxRenderer,
            ISkybox skybox)
        {
            _camera = camera;
            _logger = logger;
            _config = config;
            _title  = config.WindowTitle;

            _context = context;
            ConfigureContext();

            _skybox = skybox;
            _world  = world;

            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.ClearColor(Color4.Green);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

            _renderer       = renderer;
            _ndcRenderer    = ndcRenderer;
            _skyboxRenderer = skyboxRenderer;

            _skyboxRenderer.BeforeRender = () =>
            {
                GL.DepthFunc(DepthFunction.Lequal);
                GL.CullFace(CullFaceMode.Front);
            };
            _skyboxRenderer.AfterRender = () =>
            {
                GL.CullFace(CullFaceMode.Back);
            };
            _skyboxRenderer.AddToScene(skybox);

            _traffic = _world.Traffic;
            _renderer.AddToScene(_traffic);
            _renderer.AddToScene(_world.Renderables);

            _backbufferTexture = new Texture(_config.ResolutionWidth, config.ResolutionHeight);
            _worldRenderer     = new BackBufferRenderer(
                _logger,
                _backbufferTexture,
                _config.ResolutionWidth,
                _config.ResolutionHeight,
                useDepthBuffer: true);

            _postprocessTexture  = new Texture(_config.ResolutionWidth, _config.ResolutionHeight);
            _postprocessPipeline = new PostprocessPipeline(_logger, _config, _worldRenderer.Texture, _postprocessTexture);

            _fullscreenShader = new Shader("vs.vert", "fs.frag");
            _fullscreenShader.SetUniformValue("tex", new IntUniform
            {
                Value = 0
            });

            _ndcTexture = _postprocessTexture;
            var fullScreenQuad = new FullScreenQuad(new[] { _ndcTexture }, _fullscreenShader);

            _ndcRenderer.AddToScene(fullScreenQuad);
        }