Exemplo n.º 1
0
 public mat4(vec4 a, vec4 b, vec4 c, vec4 d)
 {
     this.col0 = a;
     this.col1 = b;
     this.col2 = c;
     this.col3 = d;
 }
Exemplo n.º 2
0
 public vec4(vec4 v)
 {
     this.x = v.x;
     this.y = v.y;
     this.z = v.z;
     this.w = v.w;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="mat4"/> struct.
 /// The matrix is initialised with the <paramref name="cols"/>.
 /// </summary>
 /// <param name="cols">The colums of the matrix.</param>
 public mat4(vec4[] cols)
 {
     this.col0 = cols[0];
     this.col1 = cols[1];
     this.col2 = cols[2];
     this.col3 = cols[3];
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="mat4"/> struct.
 /// This matrix is the identity matrix scaled by <paramref name="scale"/>.
 /// </summary>
 /// <param name="scale">The scale.</param>
 public mat4(float scale)
 {
     this.col0 = new vec4(scale, 0, 0, 0);
     this.col1 = new vec4(0, scale, 0, 0);
     this.col2 = new vec4(0, 0, scale, 0);
     this.col3 = new vec4(0, 0, 0, scale);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Gets or sets the <see cref="vec4"/> column at the specified index.
        /// </summary>
        /// <value>
        /// The <see cref="vec4"/> column.
        /// </value>
        /// <param name="column">The column index.</param>
        /// <returns>The column at index <paramref name="column"/>.</returns>
        public vec4 this[int column]
        {
            get
            {
                if (column == 0) { return this.col0; }
                if (column == 1) { return this.col1; }
                if (column == 2) { return this.col2; }
                if (column == 3) { return this.col3; }

                throw new ArgumentOutOfRangeException();
            }
            set
            {
                if (column == 0) { this.col0 = value; }
                else if (column == 1) { this.col1 = value; }
                else if (column == 2) { this.col2 = value; }
                else if (column == 3) { this.col3 = value; }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Exemplo n.º 6
0
        protected override void DoRender(RenderEventArgs e)
        {
            if (start_ticks == 0)
            {
                start_ticks = TimerHelper.GetTickCount();
                last_ticks = TimerHelper.GetTickCount();
            }
            uint current_ticks = TimerHelper.GetTickCount();
            const float factor = 0xFFFFF;
            float time = ((start_ticks - current_ticks) & 0xFFFFF) / factor;// *1.0f / 0.075f;
            float delta_time = (float)(current_ticks - last_ticks) * 0.075f;

            IntPtr attractors = GL.MapBufferRange(GL.GL_UNIFORM_BUFFER,
                0, 32 * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT);
            unsafe
            {
                vec4* array = (vec4*)attractors.ToPointer();
                for (int i = 0; i < 32; i++)
                {
                    array[i] = new vec4(
                        (float)Math.Sin(time * (float)(i + 4) * 7.5f * 20.0f) * 50.0f,
                        (float)Math.Cos(time * (float)(i + 7) * 3.9f * 20.0f) * 50.0f,
                        (float)(Math.Sin(time * (float)(i + 3) * 5.3f * 20.0f) * Math.Cos(time * (float)(i + 5) * 9.1f) * 100.0f),
                        attractor_masses[i]
                        );
                }
            }
            GL.UnmapBuffer(GL.GL_UNIFORM_BUFFER);

            // If dt is too large, the system could explode, so cap it to
            // some maximum allowed value
            if (delta_time >= 2.0f)
            {
                delta_time = 2.0f;
            }

            // Activate the compute program and bind the position and velocity buffers
            GL.UseProgram(compute_prog);
            //GL.BindImageTexture(0, velocity_tbo, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
            GL.BindImageTexture(0, tbos[1], 0, false, 0, GL.GL_READ_WRITE, GL.GL_RGBA32F);
            //GL.BindImageTexture(1, position_tbo, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
            GL.BindImageTexture(1, tbos[0], 0, false, 0, GL.GL_READ_WRITE, GL.GL_RGBA32F);
            // Set delta time
            GL.Uniform1(dt_location, delta_time);
            // Dispatch
            GL.DispatchCompute(PARTICLE_GROUP_COUNT, 1, 1);

            GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrier);

            //vmath::mat4 mvp = vmath::perspective(45.0f, aspect_ratio, 0.1f, 1000.0f) *
            //vmath::translate(0.0f, 0.0f, -60.0f) *
            //vmath::rotate(time * 1000.0f, vmath::vec3(0.0f, 1.0f, 0.0f));

            //int[] viewport = new int[4];
            //GL.GetInteger(GetTarget.Viewport, viewport);
            //mat4 projection = glm.perspective((float)(45.0f * Math.PI / 180.0f), (float)viewport[2] / (float)viewport[3], 0.1f, 1000.0f);
            //mat4 view1 = glm.translate(mat4.identity(), new vec3(0.0f, 0.0f, -60.0f));
            //mat4 view2 = glm.rotate(mat4.identity(), time * 1000.0f, new vec3(0.0f, 1.0f, 0.0f));
            //mat4 mvp = projection * view1 * view2;

            mat4 mvp = e.Camera.GetProjectionMat4() * e.Camera.GetViewMat4();

            // Clear, select the rendering program and draw a full screen quad
            //GL.Disable(GL.GL_DEPTH_TEST);
            GL.UseProgram(render_prog);
            GL.UniformMatrix4(0, 1, false, mvp.to_array());
            GL.BindVertexArray(render_vao[0]);
            GL.Enable(GL.GL_BLEND);
            GL.BlendFunc(GL.GL_ONE, GL.GL_ONE);
            // GL.PointSize(2.0f);
            GL.DrawArrays(GL.GL_POINTS, 0, PARTICLE_COUNT);
            GL.Disable(GL.GL_BLEND);

            last_ticks = current_ticks;
        }
Exemplo n.º 7
0
        protected override void DoInitialize()
        {
            // Initialize our compute program
            compute_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(compute_prog, ShaderType.ComputerShader, compute_shader_source);
            GL.LinkProgram(compute_prog);
            dt_location = GL.GetUniformLocation(compute_prog, "dt");

            GL.GenVertexArrays(1, render_vao);
            GL.BindVertexArray(render_vao[0]);

            GL.GenBuffers(2, buffers);
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]);//position buffer
                UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(PARTICLE_COUNT);
                GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicCopy);
                tmp.Dispose();
                IntPtr positions = GL.MapBufferRange(GL.GL_ARRAY_BUFFER,
                    0, PARTICLE_COUNT * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT);
                unsafe
                {
                    vec4* array = (vec4*)positions.ToPointer();
                    for (int i = 0; i < PARTICLE_COUNT; i++)
                    {
                        array[i] = new vec4(Vec3Helper.GetRandomVec3(), (float)random.NextDouble());
                    }
                }
                GL.UnmapBuffer(BufferTarget.ArrayBuffer);
                GL.VertexAttribPointer(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(0);
            }
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[1]);// velocity buffer
                UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(PARTICLE_COUNT);
                GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicCopy);
                tmp.Dispose();
                IntPtr velocities = GL.MapBufferRange(GL.GL_ARRAY_BUFFER,
                    0, PARTICLE_COUNT * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT);
                unsafe
                {
                    vec4* array = (vec4*)velocities.ToPointer();
                    for (int i = 0; i < PARTICLE_COUNT; i++)
                    {
                        array[i] = new vec4(Vec3Helper.GetRandomVec3(), (float)random.NextDouble());
                    }
                }
                GL.UnmapBuffer(BufferTarget.ArrayBuffer);
            }
            {
                GL.GenTextures(2, tbos);
                for (int i = 0; i < 2; i++)
                {
                    GL.BindTexture(GL.GL_TEXTURE_BUFFER, tbos[i]);
                    GL.TexBuffer(GL.GL_TEXTURE_BUFFER, GL.GL_RGBA32F, buffers[i]);
                }
            }
            {
                GL.GenBuffers(1, attractor_buffer);
                GL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]);
                UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(32);
                GL.BufferData(BufferTarget.UniformBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();

                for (int i = 0; i < MAX_ATTRACTORS; i++)
                {
                    attractor_masses[i] = 0.5f + (float)random.NextDouble() * 0.5f;
                }

                GL.BindBufferBase(TransformFeedbackBufferTarget.UniformBuffer, 0, attractor_buffer[0]);
            }
            {
                render_prog = GL.CreateProgram();
                ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.VertexShader, render_vs);
                ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.FragmentShader, render_fs);

                GL.LinkProgram(render_prog);
            }
        }
Exemplo n.º 8
0
 public static vec4 normalize(vec4 v)
 {
     float sqr = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
     return v * (1.0f / (float)Math.Sqrt(sqr));
 }
Exemplo n.º 9
0
 public static float dot(vec4 x, vec4 y)
 {
     vec4 tmp = new vec4(x * y);
     return (tmp.x + tmp.y) + (tmp.z + tmp.w);
 }
Exemplo n.º 10
0
 public vec3(vec4 v)
 {
     this.x = v.x;
     this.y = v.y;
     this.z = v.z;
 }
Exemplo n.º 11
0
        private void InitBuffer()
        {
            BufferName = new uint[2];
            GL.GenBuffers(BufferName.Length, BufferName);

            int[] UniformBufferOffset = new int[1];
            GL.GetInteger(GetTarget.UniformBufferOffsetAlignment, UniformBufferOffset);
            int mat4Size = Marshal.SizeOf(typeof(mat4));
            int UniformBlockSize = Math.Max(mat4Size, UniformBufferOffset[0]);

            GL.BindBuffer(BufferTarget.UniformBuffer, BufferName[1]);
            var buffer = new UnmanagedArray<byte>(UniformBlockSize);
            GL.BufferData(BufferTarget.UniformBuffer, buffer, BufferUsage.DynamicDraw);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);

            transformBuffer = new uint[1];
            GL.GenBuffers(transformBuffer.Length, transformBuffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, transformBuffer[0]);
            UnmanagedArray<vec4> positionData = new UnmanagedArray<vec4>(6);
            positionData[0] = new vec4(-1.0f, -1.0f, 0.0f, 1.0f);
            positionData[1] = new vec4(1.0f, -1.0f, 0.0f, 1.0f);
            positionData[2] = new vec4(1.0f, 1.0f, 0.0f, 1.0f);
            positionData[3] = new vec4(1.0f, 1.0f, 0.0f, 1.0f);
            positionData[4] = new vec4(-1.0f, 1.0f, 0.0f, 1.0f);
            positionData[5] = new vec4(-1.0f, -1.0f, 0.0f, 1.0f);
            GL.BufferData(BufferTarget.ArrayBuffer, positionData, BufferUsage.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            feedBackBuffer = new uint[1];
            GL.GenBuffers(feedBackBuffer.Length, feedBackBuffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, feedBackBuffer[0]);
            UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(2 * 6);
            GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
Exemplo n.º 12
0
 public override string ToString()
 {
     var builder = new System.Text.StringBuilder();
     var cols = new vec4[] { col0, col1, col2, col3 };
     for (int i = 0; i < cols.Length; i++)
     {
         builder.Append(cols[i]);
         builder.Append(" + ");
     }
     return builder.ToString();
 }
Exemplo n.º 13
0
        protected override void DoInitialize()
        {
            render_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.VertexShader, render_vs);
            ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.FragmentShader, render_fs);

            GL.LinkProgram(render_prog);
            GL.UseProgram(render_prog);

            view_matrix_loc = GL.GetUniformLocation(render_prog, "view_matrix");
            projection_matrix_loc = GL.GetUniformLocation(render_prog, "projection_matrix");

            vboObject.LoadFromVBM(@"media\armadillo_low.vbm", 0, 1, 2);

            // Bind its vertex array object so that we can append the instanced attributes
            vboObject.BindVertexArray();

            // Get the locations of the vertex attributes in 'prog', which is the
            // (linked) program object that we're going to be rendering with. Note
            // that this isn't really necessary because we specified locations for
            // all the attributes in our vertex shader. This code could be made
            // more concise by assuming the vertex attributes are where we asked
            // the compiler to put them.
            int position_loc = GL.GetAttribLocation(render_prog, "position");
            int normal_loc = GL.GetAttribLocation(render_prog, "normal");
            int color_loc = GL.GetAttribLocation(render_prog, "color");
            int matrix_loc = GL.GetAttribLocation(render_prog, "model_matrix");
            // Generate the colors of the objects
            var colors = new UnmanagedArray<vec4>(INSTANCE_COUNT);

            for (int n = 0; n < INSTANCE_COUNT; n++)
            {
                float a = (float)(n) / 4.0f;
                float b = (float)(n) / 5.0f;
                float c = (float)(n) / 6.0f;

                colors[n] = new vec4(
                    (float)(0.5f + 0.25f * (Math.Sin(a + 1.0f) + 1.0f)),
                    (float)(0.5f + 0.25f * (Math.Sin(b + 2.0f) + 1.0f)),
                    (float)(0.5f + 0.25f * (Math.Sin(c + 3.0f) + 1.0f)),
                    (float)(1.0f)
                    );
            }

            GL.GenBuffers(1, color_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, colors, BufferUsage.DynamicDraw);
            colors.Dispose();

            // Now we set up the color array. We want each instance of our geometry
            // to assume a different color, so we'll just pack colors into a buffer
            // object and make an instanced vertex attribute out of it.
            GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer[0]);
            GL.VertexAttribPointer((uint)color_loc, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray((uint)color_loc);
            // This is the important bit... set the divisor for the color array to
            // 1 to get OpenGL to give us a new value of 'color' per-instance
            // rather than per-vertex.
            GL.VertexAttribDivisor((uint)color_loc, 1);

            // Likewise, we can do the same with the model matrix. Note that a
            // matrix input to the vertex shader consumes N consecutive input
            // locations, where N is the number of columns in the matrix. So...
            // we have four vertex attributes to set up.
            UnmanagedArray<mat4> tmp = new UnmanagedArray<mat4>(INSTANCE_COUNT);
            GL.GenBuffers(1, model_matrix_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, model_matrix_buffer[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicDraw);
            tmp.Dispose();

            // Loop over each column of the matrix...
            for (int i = 0; i < 4; i++)
            {
                // Set up the vertex attribute
                GL.VertexAttribPointer((uint)(matrix_loc + i),              // Location
                                      4, GL.GL_FLOAT, false,       // vec4
                                      Marshal.SizeOf(typeof(mat4)),                // Stride
                                      new IntPtr(Marshal.SizeOf(typeof(vec4)) * i)); // Start offset
                // Enable it
                GL.EnableVertexAttribArray((uint)(matrix_loc + i));
                // Make it instanced
                GL.VertexAttribDivisor((uint)(matrix_loc + i), 1);
            }

            // Done (unbind the object's VAO)
            GL.BindVertexArray(0);
        }