Пример #1
0
        void Update()
        {
            System.Diagnostics.Debug.WriteLine("Draw..");
            renderer.Begin();
            renderer.Clear();

            // render normal triangle
            renderer.UseShader(shaderProgram);
            renderer.UseVertexBuffer(vb1);
            renderer.DrawVertexBuffer(PrimitiveType.Triangles, 0, 3);

            // render textured triangle
            renderer.UseShader(shaderProgramTextured);
            renderer.UseVertexBuffer(vb2.VertexBuffer);
            //renderer.UseTexture(brickTexture); // Either use only one texture
            renderer.UseTextures(new ITexture2D[] { brickTexture, mossTexture }); // or multiple textures
            shaderProgramTextured.SetUniform(sampler1, 0);                        // the order of the textures is determined by setting the uniform to the right index of the texture
            shaderProgramTextured.SetUniform(sampler2, 1);
            renderer.DrawVertexBufferIndexed(PrimitiveType.Triangles, 6);

            // animate color
            uniformValue = uniformValue + 0.01f;
            uniformValue = uniformValue > 1.0f ? 1.0f : uniformValue;
            shaderProgramColored.SetUniform(multiplicator, uniformValue);

            // render colored triangle
            renderer.UseShader(shaderProgramColored);
            renderer.UseVertexBuffer(vb3.VertexBuffer);
            renderer.DrawVertexBufferIndexed(PrimitiveType.Triangles, 3);

            renderer.SwapBuffers();

            // end probably useless.. don't know if possible failures are due to wpf
            //renderer.End();
        }
Пример #2
0
        /// <summary>
        /// Initializes the graphics engine, the renderer, a plain shader program, vertex buffer and the viewport for a given handle.
        /// </summary>
        void InitializeSurface(IntPtr handle)
        {
            System.Diagnostics.Debug.WriteLine("Initialize Engine..");
            // initialize graphics and a renderer using a window handle
            graphics = Engine.initializeGraphics(RenderingAPI.OpenGL);
            renderer = graphics.CreateRenderer(handle);
            renderer.UseSRGBFramebuffer();

            // Create shader program by using shader sources
            ShaderSource[] sources =
            {
                new ShaderSource(ShaderType.VertexShader, Properties.Resources.VertexShader),
                new ShaderSource(ShaderType.PixelShader,  Properties.Resources.FragmentShader)
            };
            shaderProgram = RendererHelper.createShaderProgram(graphics, sources).Value;

            // create shader program manually by creating shaders
            using (IShader vertexShader = graphics.CreateShader(ShaderType.VertexShader, Properties.Resources.VertexShaderColored).Value,
                   pixelShader = graphics.CreateShader(ShaderType.PixelShader, Properties.Resources.FragmentShaderColoredUniform).Value)
            {
                shaderProgramColored = graphics.CreateShaderProgram(new IShader[] { vertexShader, pixelShader }).Value;
            }

            // create textured shader
            ShaderSource[] sourcesTexture =
            {
                new ShaderSource(ShaderType.VertexShader, Properties.Resources.VertexShaderTex),
                new ShaderSource(ShaderType.PixelShader,  Properties.Resources.FragmentShaderTex)
            };
            shaderProgramTextured = RendererHelper.createShaderProgram(graphics, sourcesTexture).Value;

            // Diffuse textures mostly srgb, normals mostly normal rgb
            // create texture
            var settings = new TextureSettings2D(TextureTargetFormat.SRGB8, TextureOriginalFormat.BGR, TextureWrapMode.MirroredRepeat,
                                                 TextureWrapMode.MirroredRepeat, TextureMinifyingFilter.Linear, TextureMagnifyingFilter.Linear, Maybe <Color <float> > .Nothing);

            brickTexture = graphics.CreateTexture2D(settings, @"Resources\BrickGroutless0095_2_XL.jpg").Value;
            mossTexture  = graphics.CreateTexture2D(settings, @"Resources\Moss0177_1_XL.jpg").Value;
            sampler1     = shaderProgramTextured.GetUniform <int>("tex");
            sampler2     = shaderProgramTextured.GetUniform <int>("tex2");

            // use uniform values for shaders
            multiplicator = shaderProgramColored.GetUniform <float>("multiplicator");
            uniformValue  = 0.0f;
            shaderProgramColored.SetUniform(multiplicator, uniformValue);

            CreateVertexBuffer1();

            CreateVertexBuffer2();

            CreateVertexBuffer3();

            renderer.ClearColor(new Color <float>(0.2f, 0.1f, 0.2f, 0.1f));
            renderer.Viewport(new Vec2 <short>(0, 0), new Size <short>((short)Width, (short)Height));
        }