Beispiel #1
0
        protected override void OnLoad()
        {
            Span <VertexTexture> vertices = stackalloc VertexTexture[]
            {
                new VertexTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)),
                new VertexTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
                new VertexTexture(new Vector3(1, -1, 0), new Vector2(1, 1)),
                new VertexTexture(new Vector3(1, 1, 0), new Vector2(1, 0))
            };

            vertexBuffer = new VertexBuffer <VertexTexture>(graphicsDevice, vertices, BufferUsageARB.StaticDraw);

            fbo1 = new Framebuffer2D(graphicsDevice, SimulationWidth, SimulationHeight, DepthStencilFormat.None); //FramebufferObject.Create2D(ref tex1, graphicsDevice, SimulationWidth, SimulationHeight, DepthStencilFormat.None);
            fbo2 = new Framebuffer2D(graphicsDevice, SimulationWidth, SimulationHeight, DepthStencilFormat.None); //FramebufferObject.Create2D(ref tex2, graphicsDevice, SimulationWidth, SimulationHeight, DepthStencilFormat.None);

            fbo1.Texture.SetTextureFilters(TextureMinFilter.Nearest, TextureMagFilter.Nearest);
            fbo2.Texture.SetTextureFilters(TextureMinFilter.Nearest, TextureMagFilter.Nearest);
            fbo1.Texture.SetWrapModes(TextureWrapMode.Repeat, TextureWrapMode.Repeat);
            fbo2.Texture.SetWrapModes(TextureWrapMode.Repeat, TextureWrapMode.Repeat);

            r = new Random();

            simProgram  = ShaderProgram.FromFiles <VertexTexture>(graphicsDevice, "sim_vs.glsl", "sim_fs.glsl", new string[] { "vPosition", "vTexCoords" });
            drawProgram = SimpleShaderProgram.Create <VertexTexture>(graphicsDevice);

            simProgram.Uniforms["pixelDelta"].SetValueVec2(1f / SimulationWidth, 1f / SimulationHeight);
            simPrevUniform = simProgram.Uniforms["previous"];

            graphicsDevice.BlendingEnabled     = false;
            graphicsDevice.DepthTestingEnabled = false;

            OnKeyDown(null, Key.Home, 0);
            OnKeyDown(null, Key.R, 0);
        }
Beispiel #2
0
        public ShaderParam(int uniformSize, int[] staticUniforms)
        {
            uniforms = new ShaderUniform[uniformSize];

            for (int i = 0; i < uniforms.Length; i++)
            {
                uniforms[i].type = ShaderUniform.Type.Float;
            }

            foreach (var i in staticUniforms)
            {
                uniforms[i].type = ShaderUniform.Type.StaticUniform;
            }
        }
Beispiel #3
0
        protected override void OnLoad()
        {
            inputManager = new InputManager3D(InputContext);

            cubemap = TextureCubemapExtensions.FromFiles(
                graphicsDevice,
                "cubemap/back.png", "cubemap/front.png",
                "cubemap/bottom.png", "cubemap/top.png",
                "cubemap/left.png", "cubemap/right.png"
                );
            cubemap.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);

            shaderProgram = ShaderProgram.FromFiles <VertexPosition>(graphicsDevice, "vs.glsl", "fs.glsl", "vPosition");

            shaderProgram.Uniforms["cubemap"].SetValueTexture(cubemap);
            shaderProgram.Uniforms["World"].SetValueMat4(Matrix4x4.Identity);
            viewUniform = shaderProgram.Uniforms["View"];
            viewUniform.SetValueMat4(Matrix4x4.Identity);

            Span <VertexPosition> vertexData = stackalloc VertexPosition[]
            {
                new Vector3(-0.5f, -0.5f, -0.5f), //4
                new Vector3(-0.5f, -0.5f, 0.5f),  //3
                new Vector3(-0.5f, 0.5f, -0.5f),  //7
                new Vector3(-0.5f, 0.5f, 0.5f),   //8
                new Vector3(0.5f, 0.5f, 0.5f),    //5
                new Vector3(-0.5f, -0.5f, 0.5f),  //3
                new Vector3(0.5f, -0.5f, 0.5f),   //1
                new Vector3(-0.5f, -0.5f, -0.5f), //4
                new Vector3(0.5f, -0.5f, -0.5f),  //2
                new Vector3(-0.5f, 0.5f, -0.5f),  //7
                new Vector3(0.5f, 0.5f, -0.5f),   //6
                new Vector3(0.5f, 0.5f, 0.5f),    //5
                new Vector3(0.5f, -0.5f, -0.5f),  //2
                new Vector3(0.5f, -0.5f, 0.5f),   //1
            };

            vertexBuffer = new VertexBuffer <VertexPosition>(graphicsDevice, vertexData, BufferUsage.StaticDraw);

            graphicsDevice.DepthTestingEnabled = false;
            graphicsDevice.BlendingEnabled     = false;
        }
Beispiel #4
0
        protected override void OnLoad()
        {
            Span <VertexPosition> vertexData = stackalloc VertexPosition[]
            {
                new Vector3(-1, -1, 0),
                new Vector3(-1, 1, 0),
                new Vector3(1, -1, 0),
                new Vector3(1, 1, 0),
            };

            vertexBuffer = new VertexBuffer <VertexPosition>(graphicsDevice, vertexData, BufferUsage.StaticDraw);

            shaderProgram = ShaderProgram.FromFiles <VertexPosition>(graphicsDevice, "vs.glsl", "fs.glsl", "vPosition");

            transformUniform = shaderProgram.Uniforms["Transform"];
            cUniform         = shaderProgram.Uniforms["c"];

            graphicsDevice.DepthTestingEnabled = false;
            graphicsDevice.BlendingEnabled     = false;

            stopwatch = Stopwatch.StartNew();

            OnKeyDown(null, Key.Home, 0);
        }
Beispiel #5
0
        protected override void OnLoad()
        {
            stopwatch.Restart();
            inputManager = new InputManager3D(InputContext);

            chunkManager = new ChunkManager(GeneratorSeed.Default, 12, 0, 0);

            Vector3 startCoords = new Vector3(TerrainGenerator.ChunkSize / 2f, 0f, TerrainGenerator.ChunkSize / 2f);

            startCoords.Y = Math.Max(NoiseGenerator.GenHeight(chunkManager.GeneratorSeed, new Vector2(startCoords.X, startCoords.Z)), 0) + 32f;
            inputManager.CameraPosition  = startCoords;
            inputManager.CameraMoveSpeed = 100;

            Span <VertexPosition> skyVertices = stackalloc VertexPosition[]
            {
                new Vector3(0, -1, 1),
                new Vector3(1, -1, -1),
                new Vector3(-1, -1, -1),
                new Vector3(0, 1, 0),
                new Vector3(0, -1, 1),
                new Vector3(1, -1, -1),
            };

            skyBuffer              = new VertexBuffer <VertexPosition>(graphicsDevice, skyVertices, BufferUsageARB.StaticDraw);
            skyProgram             = ShaderProgram.FromFiles <VertexPosition>(graphicsDevice, "data/skyVs.glsl", "data/skyFs.glsl", "vPosition");
            skySunDirectionUniform = skyProgram.Uniforms["sunDirection"];
            skySunColorUniform     = skyProgram.Uniforms["sunColor"];
            skyViewUniform         = skyProgram.Uniforms["View"];

            terrainProgram          = ShaderProgram.FromFiles <TerrainVertex>(graphicsDevice, "data/terrainVs.glsl", "data/terrainFs.glsl", "vPosition", "vNormal", "vColor", "vLightingConfig");
            terrainViewUniform      = terrainProgram.Uniforms["View"];
            terrainCameraPosUniform = terrainProgram.Uniforms["cameraPos"];

            waterDistortMap = Texture2DExtensions.FromFile(graphicsDevice, "data/distortMap.png");
            waterNormalsMap = Texture2DExtensions.FromFile(graphicsDevice, "data/normalMap.png");
            waterDistortMap.SetWrapModes(TextureWrapMode.Repeat, TextureWrapMode.Repeat);
            waterDistortMap.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);
            waterNormalsMap.SetWrapModes(TextureWrapMode.Repeat, TextureWrapMode.Repeat);
            waterNormalsMap.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);

            waterReflectFbo = new Framebuffer2D(graphicsDevice, (uint)Window.Size.Width, (uint)Window.Size.Height, PreferredDepthFormat);
            waterRefractFbo = new Framebuffer2D(graphicsDevice, (uint)Window.Size.Width, (uint)Window.Size.Height, PreferredDepthFormat, 0, TextureImageFormat.Color4b, true);
            waterReflectFbo.Texture.SetWrapModes(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);
            waterRefractFbo.Texture.SetWrapModes(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);

            waterProgram = ShaderProgram.FromFiles <VertexNormalColor>(graphicsDevice, "data/waterVs.glsl", "data/waterFs.glsl", "vPosition");
            waterProgram.Uniforms["distortMap"].SetValueTexture(waterDistortMap);
            waterProgram.Uniforms["normalsMap"].SetValueTexture(waterNormalsMap);
            waterProgram.Uniforms["reflectSamp"].SetValueTexture(waterReflectFbo);
            waterProgram.Uniforms["refractSamp"].SetValueTexture(waterRefractFbo);
            waterRefractFbo.Framebuffer.TryGetTextureAttachment(FramebufferAttachmentPoint.Depth, out FramebufferTextureAttachment rtdpt);
            ((Texture2D)rtdpt.Texture).SetWrapModes(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);
            waterProgram.Uniforms["depthSamp"].SetValueTexture(rtdpt.Texture);
            waterCameraPosUniform     = waterProgram.Uniforms["cameraPos"];
            waterDistortOffsetUniform = waterProgram.Uniforms["distortOffset"];
            waterSunlightColorUniform = waterProgram.Uniforms["sunlightColor"];
            waterSunlightDirUniform   = waterProgram.Uniforms["sunlightDir"];
            waterViewUniform          = waterProgram.Uniforms["View"];

            Span <VertexColor> lines = stackalloc VertexColor[]
            {
                new VertexColor(new Vector3(0, 0, 0), Color4b.Red),
                new VertexColor(new Vector3(1, 0, 0), Color4b.Red),
                new VertexColor(new Vector3(0, 0, 0), Color4b.Lime),
                new VertexColor(new Vector3(0, 1, 0), Color4b.Lime),
                new VertexColor(new Vector3(0, 0, 0), Color4b.Blue),
                new VertexColor(new Vector3(0, 0, 1), Color4b.Blue),
            };

            linesBuffer  = new VertexBuffer <VertexColor>(graphicsDevice, lines, BufferUsageARB.StaticDraw);
            linesProgram = SimpleShaderProgram.Create <VertexColor>(graphicsDevice);

            mainFramebuffer = new Framebuffer2D(graphicsDevice, (uint)Window.Size.Width, (uint)Window.Size.Height, PreferredDepthFormat, 0, TextureImageFormat.Color4b, true);
            mainFramebuffer.TryGetDepthTexture(out mainFramebufferDepthTexture);
            mainFramebuffer.Texture.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);
            mainFramebuffer.Texture.SetWrapModes(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);
            mainFramebufferDepthTexture.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);
            mainFramebufferDepthTexture.SetWrapModes(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);

            textureProgram = SimpleShaderProgram.Create <VertexColorTexture>(graphicsDevice, 0, 0, true);

            underwaterShader = ShaderProgram.FromFiles <VertexColorTexture>(graphicsDevice, "data/postprocessVs.glsl", "data/underwaterFs.glsl", "vPosition", "vColor", "vTexCoords");
            underwaterShaderTextureUniform = underwaterShader.Uniforms["textureSamp"];
            underwaterShaderDepthUniform   = underwaterShader.Uniforms["depthSamp"];
            underwaterViewDistanceUniform  = underwaterShader.Uniforms["maxDistance"];
            underwaterColorUniform         = underwaterShader.Uniforms["waterColor"];

            textureBatcher = new TextureBatcher(graphicsDevice);

            SetSun(new Vector3(-1, 0.6f, 0.5f), Color4b.LightGoldenrodYellow.ToVector3());

            graphicsDevice.BlendState = BlendState.NonPremultiplied;
            graphicsDevice.DepthState = new DepthState(true, DepthFunction.Lequal);
            graphicsDevice.ClearColor = Vector4.UnitW;
        }