Пример #1
0
        protected override void DoInitialize()
        {
            // Now create a simple program to visualize the result
            basicShaderProgram = new ShaderProgram();
            basicShaderProgram.Create(basicVertexShader, basicFragmentShader, null);

            base_model_matrix_pos      = GL.GetUniformLocation(basicShaderProgram.ShaderProgramObject, "model_matrix");
            base_projection_matrix_pos = GL.GetUniformLocation(basicShaderProgram.ShaderProgramObject, "projection_matrix");

            fur_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(fur_prog, ShaderType.VertexShader, furVertexShader);
            ShaderHelper.vglAttachShaderSource(fur_prog, ShaderType.GeometryShader, furGeometryShader);
            ShaderHelper.vglAttachShaderSource(fur_prog, ShaderType.FragmentShader, furFragmentShader);
            GL.LinkProgram(fur_prog);
            GL.UseProgram(fur_prog);
            fur_model_matrix_pos      = GL.GetUniformLocation(fur_prog, "model_matrix");
            fur_projection_matrix_pos = GL.GetUniformLocation(fur_prog, "projection_matrix");

            GL.GenTextures(1, fur_texture);
            UnmanagedArray <byte> tex = new UnmanagedArray <byte>(1024 * 1024 * 4);
            Random random             = new Random();

            for (int n = 0; n < 256; n++)
            {
                for (int m = 0; m < 1270; m++)
                {
                    int x = random.Next() & 0x3FF;
                    int y = random.Next() & 0x3FF;
                    tex[(y * 1024 + x) * 4 + 0] = (byte)((random.Next() & 0x3F) + 0xC0);
                    tex[(y * 1024 + x) * 4 + 1] = (byte)((random.Next() & 0x3F) + 0xC0);
                    tex[(y * 1024 + x) * 4 + 2] = (byte)((random.Next() & 0x3F) + 0xC0);
                    tex[(y * 1024 + x) * 4 + 3] = (byte)(n);
                    //tex[(y * 1024 + x) * 4 + 0] = (byte)(random.Next());
                    //tex[(y * 1024 + x) * 4 + 1] = (byte)(random.Next());
                    //tex[(y * 1024 + x) * 4 + 2] = (byte)(random.Next());
                    //tex[(y * 1024 + x) * 4 + 3] = (byte)(random.Next());
                }
            }
            GL.BindTexture(GL.GL_TEXTURE_2D, fur_texture[0]);
            GL.TexImage2D(TexImage2DTargets.Texture2D, 0, TexImage2DFormats.RGBA, 1024, 1024, 0, TexImage2DFormats.RGBA, TexImage2DTypes.UnsignedByte, tex.Header);
            GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, (int)GL.GL_LINEAR);
            GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, (int)GL.GL_LINEAR);
            tex.Dispose();

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

            base.BeforeRendering += LightingExample_BeforeRendering;
            base.AfterRendering  += LightingExample_AfterRendering;
        }
Пример #2
0
        protected override void DoInitialize()
        {
            base_prog = GL.CreateProgram();

            ShaderHelper.vglAttachShaderSource(base_prog, ShaderType.VertexShader, quad_shader_vs);
            ShaderHelper.vglAttachShaderSource(base_prog, ShaderType.FragmentShader, quad_shader_fs);

            GL.GenBuffers(1, quad_vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, quad_vbo[0]);

            var quad_data = new UnmanagedArray <vec2>(8);

            quad_data[0] = new vec2(1.0f, -1.0f);
            quad_data[1] = new vec2(-1.0f, -1.0f);
            quad_data[2] = new vec2(-1.0f, 1.0f);
            quad_data[3] = new vec2(1.0f, 1.0f);
            quad_data[4] = new vec2(0.0f, 0.0f);
            quad_data[5] = new vec2(1.0f, 0.0f);
            quad_data[6] = new vec2(1.0f, 1.0f);
            quad_data[7] = new vec2(0.0f, 1.0f);

            GL.BufferData(BufferTarget.ArrayBuffer, quad_data, BufferUsage.StaticDraw);

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

            GL.VertexAttribPointer(0, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.VertexAttribPointer(1, 2, GL.GL_FLOAT, false, 0, new IntPtr(8 * sizeof(float)));

            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);

            GL.LinkProgram(base_prog);

            StringBuilder buf = new StringBuilder(1024);

            GL.GetProgramInfoLog(base_prog, 1024, IntPtr.Zero, buf);

            vglImageData image = new vglImageData();

            tex = vgl.vglLoadTexture(@"media\test.dds", 0, ref image);

            GL.TexParameteri(image.target, GL.GL_TEXTURE_MIN_FILTER, (int)GL.GL_LINEAR_MIPMAP_LINEAR);

            vgl.vglUnloadImage(ref image);
        }
Пример #3
0
        } // end sub

        #endregion

        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);
        }
        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);
            }
        }
Пример #5
0
        } // end sub

        #endregion

        protected override void DoInitialize()
        {
            skybox_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.VertexShader, skybox_shader_vs);
            ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.FragmentShader, skybox_shader_fs);
            GL.LinkProgram(skybox_prog);

            object_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.VertexShader, object_shader_vs);
            ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.FragmentShader, object_shader_fs);
            GL.LinkProgram(object_prog);

            GL.GenBuffers(1, cube_vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, cube_vbo[0]);
            var cube_vertices = new UnmanagedArray <vec3>(8);

            cube_vertices[0] = new vec3(-1.0f, -1.0f, -1.0f);
            cube_vertices[1] = new vec3(-1.0f, -1.0f, 1.0f);
            cube_vertices[2] = new vec3(-1.0f, 1.0f, -1.0f);
            cube_vertices[3] = new vec3(-1.0f, 1.0f, 1.0f);
            cube_vertices[4] = new vec3(1.0f, -1.0f, -1.0f);
            cube_vertices[5] = new vec3(1.0f, -1.0f, 1.0f);
            cube_vertices[6] = new vec3(1.0f, 1.0f, -1.0f);
            cube_vertices[7] = new vec3(1.0f, 1.0f, 1.0f);

            var cube_indices = new UnmanagedArray <ushort>(16);

            // First strip
            cube_indices[0] = 0;
            cube_indices[1] = 1;
            cube_indices[2] = 2;
            cube_indices[3] = 3;
            cube_indices[4] = 6;
            cube_indices[5] = 7;
            cube_indices[6] = 4;
            cube_indices[7] = 5;
            // Second strip
            cube_indices[8]  = 2;
            cube_indices[9]  = 6;
            cube_indices[10] = 0;
            cube_indices[11] = 4;
            cube_indices[12] = 1;
            cube_indices[13] = 5;
            cube_indices[14] = 3;
            cube_indices[15] = 7;

            GL.BufferData(BufferTarget.ArrayBuffer, cube_vertices, BufferUsage.StaticDraw);
            cube_vertices.Dispose();

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);
            GL.VertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(0);

            GL.GenBuffers(1, cube_element_buffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, cube_element_buffer[0]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, cube_indices, BufferUsage.StaticDraw);
            cube_indices.Dispose();

            skybox_rotate_loc  = GL.GetUniformLocation(skybox_prog, "tc_rotate");
            object_mat_mvp_loc = GL.GetUniformLocation(object_prog, "mat_mvp");
            object_mat_mv_loc  = GL.GetUniformLocation(object_prog, "mat_mv");
            skyboxTexLocation  = GL.GetUniformLocation(skybox_prog, "tex");
            objectTexLocation  = GL.GetUniformLocation(object_prog, "tex");



            //tex = new Texture2D();
            //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"media\TantolundenCube.png");
            //tex.Initialize(bmp);
            vglImageData data = new vglImageData();

            tex = vgl.vglLoadTexture(@"media\TantolundenCube.dds", 0, ref data);

            uint e = GL.GetError();

            vgl.vglUnloadImage(ref data);

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