Exemplo n.º 1
0
        //! On load window (once)
        protected override void OnLoad()
        {
            _version        = openGLFactory.NewVersionInformation(Console.WriteLine);
            _extensions     = openGLFactory.NewExtensionInformation();
            _debug_callback = openGLFactory.NewDebugCallback(Console.WriteLine);

            // Version strings
            _version.Retrieve();

            // Get OpenGL extensions
            _extensions.Retrieve();

            // Debug callback
            _debug_callback.Init();

            // create Vertex Array Object, Array Buffer Object and Element Array Buffer Object

            // ...

            // Create shader program

            string compute_shader =
                @"#version 460

            layout(local_size_x = 1, local_size_y = 1) in;
            layout(rgba32f, binding = 1) writeonly uniform image2D img_output;

            layout(location = 1) uniform vec4  u_color;
            layout(location = 2) uniform float u_margin;
            layout(location = 3) uniform float u_random; 

            layout(std430, binding = 1) buffer TCoord1
            {
                vec4 pos;
            } coord_inout;

            vec4[7] constants = vec4[7](
                vec4(0.0,  0.85,  0.2,   -0.15),  
                vec4(0.0,  0.04, -0.26,   0.28),  
                vec4(0.0, -0.04,  0.23,   0.26),  
                vec4(0.16, 0.85,  0.22,   0.24),  
                vec4(0.0,  0.0,   0.0,    0.0 ),  
                vec4(0.0,  1.6,   1.6,    0.44),  
                vec4(1.0, 86.0,  93.0,  100.0 ));  

            void main() {
  
                  vec2 dims = vec2(imageSize(img_output)); // fetch image dimensions

                  int i = 0; 
                  for (; i < 4; ++i)
                  {
                      if (u_random*100.0 < constants[6][i])
                          break;
                  }
                  i = min(i, 3);

                  vec2 fern = vec2(
                      constants[0][i] * coord_inout.pos.x + constants[1][i] * coord_inout.pos.y + constants[4][i],
                      constants[2][i] * coord_inout.pos.x + constants[3][i] * coord_inout.pos.y + constants[5][i]);
                  coord_inout.pos.xy = fern;
                  
                  vec2 pixel_coords = vec2(
                      (fern.x + 2.1820 + u_margin) * dims.x / (2.1820 + 2.6558 + 2.0 * u_margin),  
                      dims.y - (-fern.y + 9.9983 + u_margin) * dims.y / (9.9983 + 2.0 * u_margin));
                  
                  // output to a specific pixel in the image
                  imageStore(img_output, ivec2(pixel_coords), u_color);
            }";

            this._compute_prog = openGLFactory.ComputeShaderProgram(compute_shader);
            this._compute_prog.Generate();

            // Model view projection shader storage block objects and buffers
            _coord_ssbo = openGLFactory.NewStorageBuffer();
            TCoordinate coord_data = new TCoordinate(Vector2.Zero);

            this._coord_ssbo.Create(ref coord_data, IStorageBuffer.Usage.ReadWrite);
            this._coord_ssbo.Bind(1);

            // framebuffers

            _fbo = openGLFactory.NewFramebuffer();
            _fbo.Create(_image_cx, _image_cy, IFramebuffer.Kind.texture, IFramebuffer.Format.RGBA_F32, true, false);
            _fbo.Clear(new Color4(0.2f, 0.1f, 0.0f, 1.0f));

            // states

            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            base.OnLoad();
        }