private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.QuadStrip;
            GLColor[] colors = this.ColorPalette.Colors;
            float[]   coords = this.ColorPalette.Coords;
            this.vertexCount = coords.Length * 2;
            this.vao         = new uint[1];

            float coordLength = coords[coords.Length - 1] - coords[0];
            {
                GL.GenVertexArrays(1, vao);

                GL.BindVertexArray(vao[0]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(this.vertexCount);
                    positionArray[0] = new vec3(-0.5f, -0.5f, 0);
                    positionArray[1] = new vec3(-0.5f, 0.5f, 0);
                    for (int i = 1; i < coords.Length; i++)
                    {
                        float x = (coords[i] - coords[0]) / coordLength - 0.5f;
                        positionArray[i * 2 + 0] = new vec3(x, -0.5f, 0);
                        positionArray[i * 2 + 1] = new vec3(x, 0.5f, 0);
                    }

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_PositionLocation);

                    positionArray.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    var colorArray = new UnmanagedArray <vec4>(this.vertexCount);
                    for (int i = 0; i < colors.Length; i++)
                    {
                        GLColor color = colors[i];
                        colorArray[i * 2 + 0] = new vec4(color.R, color.G, color.B, color.A);
                        colorArray[i * 2 + 1] = new vec4(color.R, color.G, color.B, color.A);
                    }

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(in_ColorLocation, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_ColorLocation);

                    colorArray.Dispose();
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
示例#2
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            {
                // velocity
                var velocity_buffer = new uint[1];
                OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, velocity_buffer);
                OpenGL.BindBuffer(BufferTarget.ArrayBuffer, velocity_buffer[0]);
                var velocities = new UnmanagedArray <vec4>(SunshineParticleModel.particleCount);
                unsafe
                {
                    var random = new Random();
                    var array  = (vec4 *)velocities.Header.ToPointer();
                    for (int i = 0; i < SunshineParticleModel.particleCount; i++)
                    {
                        array[i] = new vec4(
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            0);
                    }
                }
                OpenGL.BufferData(BufferTarget.ArrayBuffer, velocities, BufferUsage.DynamicCopy);
                velocities.Dispose();
                //GL.GetDelegateFor<GL.glVertexAttribPointer>()(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                //GL.GetDelegateFor<GL.glEnableVertexAttribArray>()(0);
                //
                OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                this.VelocityBufferPtrId = velocity_buffer[0];
            }

            this.PositionBufferPtr = this.bufferable.GetProperty(SunshineParticleModel.strPosition, null);
            this.VertexArrayObject = this.vertexArrayObject;
        }
示例#3
0
        internal void LoadSoundInMemory()
        {
            if (PreloadedBuffer.Ptr != IntPtr.Zero)
            {
                return;
            }

            using (var soundStream = ContentManager.FileProvider.OpenStream(CompressedDataUrl, VirtualFileMode.Open, VirtualFileAccess.Read, VirtualFileShare.Read, StreamFlags.Seekable))
                using (var decoder = new Celt(SampleRate, CompressedSoundSource.SamplesPerFrame, Channels, true))
                {
                    var reader           = new BinarySerializationReader(soundStream);
                    var samplesPerPacket = CompressedSoundSource.SamplesPerFrame * Channels;

                    PreloadedBuffer = AudioLayer.BufferCreate(samplesPerPacket * NumberOfPackets * sizeof(short));

                    var memory = new UnmanagedArray <short>(samplesPerPacket * NumberOfPackets);

                    var offset       = 0;
                    var outputBuffer = new short[samplesPerPacket];
                    for (var i = 0; i < NumberOfPackets; i++)
                    {
                        var len = reader.ReadInt16();
                        var compressedBuffer = reader.ReadBytes(len);
                        var samplesDecoded   = decoder.Decode(compressedBuffer, len, outputBuffer);
                        memory.Write(outputBuffer, offset, 0, samplesDecoded * Channels);
                        offset += samplesDecoded * Channels * sizeof(short);
                    }

                    AudioLayer.BufferFill(PreloadedBuffer, memory.Pointer, memory.Length * sizeof(short), SampleRate, Channels == 1);
                    memory.Dispose();
                }
        }
示例#4
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(Boolean disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                //TODO: Managed cleanup code here, while managed refs still valid
            }
            //TODO: Unmanaged cleanup code here
            if (PositionArray != null)
            {
                PositionArray.Dispose();
            }
            if (ColorArray != null)
            {
                ColorArray.Dispose();
            }
            if (RadiusArray != null)
            {
                RadiusArray.Dispose();
            }
            if (VisibleArray != null)
            {
                VisibleArray.Dispose();
            }

            disposed = true;
        }
        private void InitVAO()
        {
            this.mode        = DrawMode.Quads;
            this.vertexCount = 4;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray <vec3> in_Position = new UnmanagedArray <vec3>(this.vertexCount);
            UnmanagedArray <vec2> in_TexCoord = new UnmanagedArray <vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;

            float factor = (float)this.ttfTexture.BigBitmap.Width / (float)this.ttfTexture.BigBitmap.Height;
            float x1     = -factor;
            float x2     = factor;
            float y1     = -1;
            float y2     = 1;

            in_Position[0] = new vec3(x1, y1, 0);
            in_Position[1] = new vec3(x2, y1, 0);
            in_Position[2] = new vec3(x2, y2, 0);
            in_Position[3] = new vec3(x1, y2, 0);

            in_TexCoord[0] = new vec2(0, 0);
            in_TexCoord[1] = new vec2(1, 0);
            in_TexCoord[2] = new vec2(1, 1);
            in_TexCoord[3] = new vec2(0, 1);

            if (vao[0] != 0)
            {
                GL.DeleteBuffers(1, vao);
            }
            if (vbo[0] != 0)
            {
                GL.DeleteBuffers(vbo.Length, vbo);
            }

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

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
示例#6
0
        public override void Serialize(ref Sound obj, ArchiveMode mode, SerializationStream stream)
        {
            if (mode == ArchiveMode.Deserialize)
            {
                var services    = stream.Context.Tags.Get(ServiceRegistry.ServiceRegistryKey);
                var audioEngine = services.GetServiceAs <IAudioEngineProvider>()?.AudioEngine;

                obj.CompressedDataUrl = stream.ReadString();
                obj.SampleRate        = stream.ReadInt32();
                obj.Channels          = stream.ReadByte();
                obj.StreamFromDisk    = stream.ReadBoolean();
                obj.Spatialized       = stream.ReadBoolean();
                obj.NumberOfPackets   = stream.ReadInt16();
                obj.MaxPacketLength   = stream.ReadInt16();

                if (!obj.StreamFromDisk && audioEngine != null && audioEngine.State != AudioEngineState.Invalidated && audioEngine.State != AudioEngineState.Disposed) //immediatelly preload all the data and decode
                {
                    using (var soundStream = ContentManager.FileProvider.OpenStream(obj.CompressedDataUrl, VirtualFileMode.Open, VirtualFileAccess.Read, VirtualFileShare.Read, StreamFlags.Seekable))
                        using (var decoder = new Celt(obj.SampleRate, CompressedSoundSource.SamplesPerFrame, obj.Channels, true))
                        {
                            var reader           = new BinarySerializationReader(soundStream);
                            var samplesPerPacket = CompressedSoundSource.SamplesPerFrame * obj.Channels;

                            obj.PreloadedBuffer = AudioLayer.BufferCreate(samplesPerPacket * obj.NumberOfPackets * sizeof(short));

                            var memory = new UnmanagedArray <short>(samplesPerPacket * obj.NumberOfPackets);

                            var offset       = 0;
                            var outputBuffer = new short[samplesPerPacket];
                            for (var i = 0; i < obj.NumberOfPackets; i++)
                            {
                                var len = reader.ReadInt16();
                                var compressedBuffer = reader.ReadBytes(len);
                                var samplesDecoded   = decoder.Decode(compressedBuffer, len, outputBuffer);
                                memory.Write(outputBuffer, offset, 0, samplesDecoded * obj.Channels);
                                offset += samplesDecoded * obj.Channels * sizeof(short);
                            }

                            AudioLayer.BufferFill(obj.PreloadedBuffer, memory.Pointer, memory.Length * sizeof(short), obj.SampleRate, obj.Channels == 1);
                            memory.Dispose();
                        }
                }

                if (audioEngine != null)
                {
                    obj.Attach(audioEngine);
                }
            }
            else
            {
                stream.Write(obj.CompressedDataUrl);
                stream.Write(obj.SampleRate);
                stream.Write((byte)obj.Channels);
                stream.Write(obj.StreamFromDisk);
                stream.Write(obj.Spatialized);
                stream.Write((short)obj.NumberOfPackets);
                stream.Write((short)obj.MaxPacketLength);
            }
        }
示例#7
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.LineLoop;
            this.axisVertexCount   = 4;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(4);
                positionArray[0] = new vec3(-0.5f, -0.5f, 0);
                positionArray[1] = new vec3(0.5f, -0.5f, 0);
                positionArray[2] = new vec3(0.5f, 0.5f, 0);
                positionArray[3] = new vec3(-0.5f, 0.5f, 0);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(4);
                vec3 color = this.rectColor;
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = color;
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#8
0
        //protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        protected void InitializeVAO()
        {
            this.primitiveMode = DrawMode.Triangles;
            this.vertexCount   = positions.Length;

            this.vao = new uint[1];
            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                //uint[] ids = new uint[1];
                this.positionBufferObject = new uint[1];
                GL.GenBuffers(1, this.positionBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.positionBufferObject[0]);
                UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                //uint[] ids = new uint[1];
                this.colorBufferObject = new uint[1];
                GL.GenBuffers(1, this.colorBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBufferObject[0]);
                UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(positions.Length);
                for (int i = 0; i < colors.Length; i++)
                {
                    colorArray[i] = colors[i];
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
        private void InitVAO()
        {
            this.mode = DrawMode.Quads;
            this.vertexCount = 4;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray<vec3> in_Position = new UnmanagedArray<vec3>(this.vertexCount);
            UnmanagedArray<vec2> in_TexCoord = new UnmanagedArray<vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;

            float factor = (float)this.ttfTexture.BigBitmap.Width / (float)this.ttfTexture.BigBitmap.Height;
            float x1 = -factor;
            float x2 = factor;
            float y1 = -1;
            float y2 = 1;

            in_Position[0] = new vec3(x1, y1, 0);
            in_Position[1] = new vec3(x2, y1, 0);
            in_Position[2] = new vec3(x2, y2, 0);
            in_Position[3] = new vec3(x1, y2, 0);

            in_TexCoord[0] = new vec2(0, 0);
            in_TexCoord[1] = new vec2(1, 0);
            in_TexCoord[2] = new vec2(1, 1);
            in_TexCoord[3] = new vec2(0, 1);

            if (vao[0] != 0)
            { GL.DeleteBuffers(1, vao); }
            if (vbo[0] != 0)
            { GL.DeleteBuffers(vbo.Length, vbo); }

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

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
示例#10
0
    public override void Cleanup()
    {
        _trianglePrimitive?.Dispose();

        _trianglePrimitiveTransforms.Dispose();
        _trianglePrimitivePerSecondDelta.Dispose();

        _constantBuffer?.Dispose();
        _uploadBuffer?.Dispose();
        _vertexBuffer?.Dispose();

        base.Cleanup();
    }
示例#11
0
        private void DoRender(object sender, PaintEventArgs e)
        {
            // Compute the MVP (Model View Projection matrix)
            {
                GL.BindBuffer(BufferTarget.UniformBuffer, BufferName[1]);//BufferName[TRANSFORM]
                var mvpPointer = GL.MapBufferRange(GL.GL_UNIFORM_BUFFER, 0, 64, (uint)(GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT));

                var  tmp        = new UnmanagedArray <mat4>(1);
                mat4 projection = this.camera.GetProjectionMat4();
                mat4 view       = this.camera.GetViewMat4();
                //tmp[0] = projection * view;
                //tmp.CopyTo(Pointer);
                unsafe
                {
                    mat4 *array = (mat4 *)mvpPointer.ToPointer();
                    array[0] = projection * view;
                }

                GL.UnmapBuffer(GL.GL_UNIFORM_BUFFER);

                tmp.Dispose();
            }

            // Clear color buffer
            //GL.ClearBufferfv(GL_COLOR, 0, &GL.m.vec4(0.0f, 0.0f, 0.0f, 1.0f)[0]);
            GL.ClearBuffer(GL.GL_COLOR, 0, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });

            // First draw, capture the attributes
            // Disable rasterisation, vertices processing only!
            GL.Enable(GL.GL_RASTERIZER_DISCARD);

            transformProgram.Bind();

            GL.BindVertexArray(transformArray[0]);
            //GL.BindBufferBase(GL.GL_UNIFORM_BUFFER, semantic.uniform.TRANSFORM0, BufferName[buffer.TRANSFORM]);
            GL.BindBufferBase(GL.GL_UNIFORM_BUFFER, 1, BufferName[1]);

            GL.BindTransformFeedback(GL.GL_TRANSFORM_FEEDBACK, feedbackObj[0]);
            GL.BeginTransformFeedback(GL.GL_TRIANGLES);
            GL.DrawArraysInstanced(GL.GL_TRIANGLES, 0, 6, 1);//VertexCount: 6
            GL.EndTransformFeedback();
            GL.BindTransformFeedback(GL.GL_TRANSFORM_FEEDBACK, 0);

            GL.Disable(GL.GL_RASTERIZER_DISCARD);

            // Second draw, reuse the captured attributes
            feedbackProgram.Bind();

            GL.BindVertexArray(feedbackArray[0]);
            GL.DrawTransformFeedback(GL.GL_TRIANGLES, feedbackObj[0]);
        }
示例#12
0
        protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        {
            primitiveMode = DrawMode.Quads;
            vertexCount   = positions.Length;

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

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                var positionArray = new UnmanagedArray <vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }
            //  Create a vertex buffer for the uv data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                var uvArray = new UnmanagedArray <vec2>(uvs.Length);
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvArray[i] = uvs[i];
                }

                uint uvLocation = shaderProgram.GetAttributeLocation(strin_uv);

                GL.BufferData(BufferTarget.ArrayBuffer, uvArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(uvLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(uvLocation);

                uvArray.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#13
0
        protected void InitializeVAO(OpenGL gl, out uint[] vao, out BeginMode primitiveMode, out int vertexCount)
        {
            primitiveMode = BeginMode.Quads;
            vertexCount   = positions.Length;

            vao = new uint[1];
            gl.GenVertexArrays(1, vao);
            gl.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);
                var positionArray = new UnmanagedArray <vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_Position);

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, positionArray.ByteLength, positionArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(positionLocation, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }
            //  Create a vertex buffer for the uv data.
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);
                var uvArray = new UnmanagedArray <vec2>(uvs.Length);
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvArray[i] = uvs[i];
                }

                uint uvLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_uv);

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, uvArray.ByteLength, uvArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(uvLocation, 2, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(uvLocation);

                uvArray.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            gl.BindVertexArray(0);
        }
        private void initVol3DTex(string filename, int width, int height, int depth)
        {
            var data = new UnmanagedArray <byte>(width * height * depth);

            unsafe
            {
                int   index     = 0;
                int   readCount = 0;
                byte *array     = (byte *)data.Header.ToPointer();
                using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                    using (var br = new BinaryReader(fs))
                    {
                        int       unReadCount = (int)fs.Length;
                        const int cacheSize   = 1024 * 1024;
                        do
                        {
                            int min   = Math.Min(cacheSize, unReadCount);
                            var cache = new byte[min];
                            readCount = br.Read(cache, 0, min);
                            if (readCount != min)
                            {
                                throw new Exception();
                            }

                            for (int i = 0; i < readCount; i++)
                            {
                                array[index++] = cache[i];
                            }
                            unReadCount -= readCount;
                        } while (readCount > 0);
                    }
            }

            OpenGL.GenTextures(1, vol3DTexObj);
            // bind 3D texture target
            OpenGL.BindTexture(OpenGL.GL_TEXTURE_3D, vol3DTexObj[0]);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_MAG_FILTER, (int)OpenGL.GL_LINEAR);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_MIN_FILTER, (int)OpenGL.GL_LINEAR);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_S, (int)OpenGL.GL_REPEAT);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_T, (int)OpenGL.GL_REPEAT);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_R, (int)OpenGL.GL_REPEAT);
            // pixel transfer happens here from client to OpenGL server
            OpenGL.PixelStorei(OpenGL.GL_UNPACK_ALIGNMENT, 1);
            OpenGL.TexImage3D(OpenGL.GL_TEXTURE_3D, 0, (int)OpenGL.GL_INTENSITY,
                              width, height, depth, 0,
                              OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, data.Header);
            data.Dispose();
            OpenGL.BindTexture(OpenGL.GL_TEXTURE_3D, 0);
        }
示例#15
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;
        }
示例#16
0
        protected void InitializeVAO()
        {
            this.primitiveMode = DrawMode.Points;
            const int axisCount = 3;
            const int count     = axisCount * axisCount * axisCount;

            this.vertexCount = count;

            this.vao = new uint[1];

            GL.GenVertexArrays(4, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(count);
                for (int i = 0, index = 0; i < axisCount; i++)
                {
                    for (int j = 0; j < axisCount; j++)
                    {
                        for (int k = 0; k < axisCount; k++)
                        {
                            //positionArray[index++] = 10 * new vec3(i - axisCount / 2, j - axisCount / 2, k - axisCount / 2);
                            positionArray[index++] = 10 * new vec3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
                        }
                    }
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#17
0
        private void Dispose(bool disposing)
        {
            if (this.disposedValue == false)
            {
                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unmanaged resources.
                UnmanagedArray <T> array = this.array;
                this.array = null;
                if (array != null)
                {
                    array.Dispose();
                }
            }

            this.disposedValue = true;
        }
示例#18
0
        /// <summary>
        ///
        /// </summary>
        public override void Fill()
        {
            var data = new UnmanagedArray <byte>(width * height * depth);

            unsafe
            {
                int   index     = 0;
                int   readCount = 0;
                byte *array     = (byte *)data.Header.ToPointer();
                using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                    using (var br = new BinaryReader(fs))
                    {
                        int       unReadCount = (int)fs.Length;
                        const int cacheSize   = 1024 * 1024;
                        do
                        {
                            int min   = Math.Min(cacheSize, unReadCount);
                            var cache = new byte[min];
                            readCount = br.Read(cache, 0, min);
                            if (readCount != min)
                            {
                                throw new Exception();
                            }

                            for (int i = 0; i < readCount; i++)
                            {
                                array[index++] = cache[i];
                            }
                            unReadCount -= readCount;
                        } while (readCount > 0);
                    }
            }

            OpenGL.PixelStorei(OpenGL.GL_UNPACK_ALIGNMENT, 1);
            OpenGL.TexImage3D(OpenGL.GL_TEXTURE_3D, 0, (int)OpenGL.GL_INTENSITY,
                              width, height, depth, 0,
                              OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, data.Header);
            data.Dispose();
        }
示例#19
0
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            long filesize = f.Length;

            byte[] data = new byte[filesize];
            UnmanagedArray<byte> raw_data;
            f.Read(data, 0, (int)filesize);
            f.Seek(0, SeekOrigin.Begin);

            VBM_HEADER header = br.ReadStruct<VBM_HEADER>();
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct<VBM_ATTRIB_HEADER>();
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            VBM_FRAME_HEADER frame_header = br.ReadStruct<VBM_FRAME_HEADER>();

            uint total_data_size = 0;

            this.m_header = header;

            {
                long offset = header.size;// +header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_attrib = new VBM_ATTRIB_HEADER[header.num_attribs];
            for (int i = 0; i < header.num_attribs; i++)
            {
                this.m_attrib[i] = br.ReadStruct<VBM_ATTRIB_HEADER>();
            }

            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));// +header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_frame = new VBM_FRAME_HEADER[header.num_frames];
            for (int i = 0; i < header.num_frames; i++)
            {
                this.m_frame[i] = br.ReadStruct<VBM_FRAME_HEADER>();
            }

            GL.GenVertexArrays(1, m_vao);
            GL.BindVertexArray(m_vao[0]);
            GL.GenBuffers(1, m_attribute_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_attribute_buffer[0]);

            //uint i;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                raw_data = new UnmanagedArray<byte>((int)total_data_size);
                for (int i = 0; i < raw_data.Length; i++)
                {
                    raw_data[i] = data[offset + i];
                }
            }
            //GL.BufferData(GL_ARRAY_BUFFER, total_data_size, raw_data, GL_STATIC_DRAW);
            GL.BufferData(BufferTarget.ArrayBuffer, raw_data, BufferUsage.StaticDraw);

            total_data_size = 0;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                uint attribIndex = i;

                if (attribIndex == 0)
                    attribIndex = (uint)vertexIndex;
                else if (attribIndex == 1)
                    attribIndex = (uint)normalIndex;
                else if (attribIndex == 2)
                    attribIndex = (uint)texCoord0Index;

                GL.VertexAttribPointer(attribIndex, (int)m_attrib[i].components, m_attrib[i].type, false, 0, new IntPtr(total_data_size));
                GL.EnableVertexAttribArray(attribIndex);
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }

            if (header.num_indices > 0)
            {
                GL.GenBuffers(1, m_index_buffer);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_index_buffer[0]);
                uint element_size;
                if (header.indexype == 0x1403)
                {
                    element_size = sizeof(ushort);
                }
                else
                {
                    element_size = sizeof(uint);
                }
                //GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, header.num_indices * element_size, raw_data + total_data_size, GL_STATIC_DRAW);
                UnmanagedArray<byte> tmp = new UnmanagedArray<byte>((int)(header.num_indices * element_size));
                for (int t = 0; t < tmp.Length; t++)
                {
                    tmp[t] = raw_data[(int)(t + total_data_size)];
                }
                GL.BufferData(BufferTarget.ElementArrayBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();
            }

            GL.BindVertexArray(0);

            raw_data.Dispose();

            return true;
        }
示例#20
0
        private void DoRender(object sender, PaintEventArgs e)
        {
            // Compute the MVP (Model View Projection matrix)
            {
                GL.BindBuffer(BufferTarget.UniformBuffer, BufferName[1]);//BufferName[TRANSFORM]
                var mvpPointer = GL.MapBufferRange(GL.GL_UNIFORM_BUFFER, 0, 64, (uint)(GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT));

                var tmp = new UnmanagedArray<mat4>(1);
                mat4 projection = this.camera.GetProjectionMat4();
                mat4 view = this.camera.GetViewMat4();
                //tmp[0] = projection * view;
                //tmp.CopyTo(Pointer);
                unsafe
                {
                    mat4* array = (mat4*)mvpPointer.ToPointer();
                    array[0] = projection * view;
                }

                GL.UnmapBuffer(GL.GL_UNIFORM_BUFFER);

                tmp.Dispose();
            }

            // Clear color buffer
            //GL.ClearBufferfv(GL_COLOR, 0, &GL.m.vec4(0.0f, 0.0f, 0.0f, 1.0f)[0]);
            GL.ClearBuffer(GL.GL_COLOR, 0, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });

            // First draw, capture the attributes
            // Disable rasterisation, vertices processing only!
            GL.Enable(GL.GL_RASTERIZER_DISCARD);

            transformProgram.Bind();

            GL.BindVertexArray(transformArray[0]);
            //GL.BindBufferBase(GL.GL_UNIFORM_BUFFER, semantic.uniform.TRANSFORM0, BufferName[buffer.TRANSFORM]);
            GL.BindBufferBase(GL.GL_UNIFORM_BUFFER, 1, BufferName[1]);

            GL.BindTransformFeedback(GL.GL_TRANSFORM_FEEDBACK, feedbackObj[0]);
            GL.BeginTransformFeedback(GL.GL_TRIANGLES);
            GL.DrawArraysInstanced(GL.GL_TRIANGLES, 0, 6, 1);//VertexCount: 6
            GL.EndTransformFeedback();
            GL.BindTransformFeedback(GL.GL_TRANSFORM_FEEDBACK, 0);

            GL.Disable(GL.GL_RASTERIZER_DISCARD);

            // Second draw, reuse the captured attributes
            feedbackProgram.Bind();

            GL.BindVertexArray(feedbackArray[0]);
            GL.DrawTransformFeedback(GL.GL_TRIANGLES, feedbackObj[0]);

        }
示例#21
0
        protected void InitializeVAO()
        {
            this.axisPrimitiveMode = DrawMode.QuadStrip; //PrimitiveModes.QuadStrip;
            this.axisVertexCount = faceCount * 2;
            this.vao = new uint[4];

            GL.GenVertexArrays(4, vao);

            vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1) };
            // 计算三个坐标轴
            for (int axisIndex = 0; axisIndex < 3; axisIndex++)
            {
                GL.BindVertexArray(vao[axisIndex]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(faceCount * 2);
                    for (int i = 0; i < faceCount * 2; i++)
                    {
                        int face = i / 2;
                        float[] components = new float[]{
                            i % 2 == 1 ? 0 : this.axisLength,
                            (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                            (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))};
                        positionArray[i] = new vec3(
                            components[(0 + axisIndex) % 3], components[(2 + axisIndex) % 3], components[(4 + axisIndex) % 3]);
                    }

                    uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(positionLocation);
                    positionArray.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(faceCount * 2);
                    for (int i = 0; i < colorArray.Length; i++)
                    {
                        colorArray[i] = colors[axisIndex];
                    }

                    uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(colorLocation);
                    colorArray.Dispose();
                }
                {
                    UnmanagedArray<uint> cylinderIndex = new UnmanagedArray<uint>(faceCount * 2 + 2);
                    for (int i = 0; i < cylinderIndex.Length - 2; i++)
                    {
                        cylinderIndex[i] = (uint)i;
                    }
                    cylinderIndex[cylinderIndex.Length - 2] = 0;
                    cylinderIndex[cylinderIndex.Length - 1] = 1;

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, cylinderIndex, BufferUsage.StaticDraw);
                    cylinderIndex.Dispose();
                }
                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
            // 计算XZ平面
            {
                this.planPrimitveMode = DrawMode.LineLoop;
                var lanVertexCount = 4;

                GL.BindVertexArray(vao[3]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray<vec3> plan = new UnmanagedArray<vec3>(lanVertexCount);
                    float length = this.axisLength;
                    plan[0] = new vec3(-length, 0, -length);
                    plan[1] = new vec3(-length, 0, length);
                    plan[2] = new vec3(length, 0, length);
                    plan[3] = new vec3(length, 0, -length);

                    uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, plan, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(positionLocation);

                    plan.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(4);
                    for (int i = 0; i < colorArray.Length; i++)
                    {
                        colorArray[i] = this.planColor;
                    }

                    uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(colorLocation);

                    colorArray.Dispose();
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
示例#22
0
        protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        {
            primitiveMode = DrawMode.Quads;
            vertexCount = positions.Length;

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

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                var positionArray = new UnmanagedArray<vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }
            //  Create a vertex buffer for the uv data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                var uvArray = new UnmanagedArray<vec2>(uvs.Length);
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvArray[i] = uvs[i];
                }

                uint uvLocation = shaderProgram.GetAttributeLocation(strin_uv);

                GL.BufferData(BufferTarget.ArrayBuffer, uvArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(uvLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(uvLocation);

                uvArray.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#23
0
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            //FILE * f = NULL;
            FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            //f = fopen(filename, "rb");
            //if(f == NULL)
            //return false;

            //fseek(f, 0, SEEK_END);
            //size_t filesize = ftell(f);
            //fseek(f, 0, SEEK_SET);
            long filesize = f.Length;
            //f.Seek(0, SeekOrigin.End);
            //f.Seek(0, SeekOrigin.Begin);

            byte[] data = new byte[filesize];
            UnmanagedArray<byte> raw_data;
            f.Read(data, 0, (int)filesize);
            //f.Close();
            f.Seek(0, SeekOrigin.Begin);

            //VBM_HEADER * header = (VBM_HEADER *)data;
            VBM_HEADER header = br.ReadStruct<VBM_HEADER>();
            //raw_data = data + header.size + header->num_attribs * sizeof(VBM_ATTRIB_HEADER) + header->num_frames * sizeof(VBM_FRAME_HEADER);
            //{
            //    long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER)); 
            //    raw_data = new UnmanagedArray<byte>((int)(data.Length - offset));
            //    for (int i = 0; i < raw_data.Length; i++)
            //    {
            //        raw_data[i] = data[offset+i];
            //    }
            //}
            //VBM_ATTRIB_HEADER * attrib_header = (VBM_ATTRIB_HEADER *)(data + header.size);
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct<VBM_ATTRIB_HEADER>();
            //VBM_FRAME_HEADER * frame_header = (VBM_FRAME_HEADER *)(data + header.size + header.num_attribs * sizeof(VBM_ATTRIB_HEADER));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            VBM_FRAME_HEADER frame_header = br.ReadStruct<VBM_FRAME_HEADER>();

            uint total_data_size = 0;

            //memcpy(&m_header, header, header.size < Marshal.SizeOf(typeof(VBM_HEADER)) ? header.size : Marshal.SizeOf(typeof(VBM_HEADER)));
            this.m_header = header;

            //memcpy(m_attrib, attrib_header, header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)));
            {
                long offset = header.size;// +header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_attrib = new VBM_ATTRIB_HEADER[header.num_attribs];
            for (int i = 0; i < header.num_attribs; i++)
            {
                this.m_attrib[i] = br.ReadStruct<VBM_ATTRIB_HEADER>();
            }

            //memcpy(m_frame, frame_header, header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER)));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));// +header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_frame = new VBM_FRAME_HEADER[header.num_frames];
            for (int i = 0; i < header.num_frames; i++)
            {
                this.m_frame[i] = br.ReadStruct<VBM_FRAME_HEADER>();
            }

            GL.GenVertexArrays(1, m_vao);
            GL.BindVertexArray(m_vao[0]);
            GL.GenBuffers(1, m_attribute_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_attribute_buffer[0]);

            //uint i;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                raw_data = new UnmanagedArray<byte>((int)total_data_size);
                for (int i = 0; i < raw_data.Length; i++)
                {
                    raw_data[i] = data[offset + i];
                }
            }
            //GL.BufferData(GL_ARRAY_BUFFER, total_data_size, raw_data, GL_STATIC_DRAW);
            GL.BufferData(BufferTarget.ArrayBuffer, raw_data, BufferUsage.StaticDraw);

            total_data_size = 0;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                uint attribIndex = i;

                if (attribIndex == 0)
                    attribIndex = (uint)vertexIndex;
                else if (attribIndex == 1)
                    attribIndex = (uint)normalIndex;
                else if (attribIndex == 2)
                    attribIndex = (uint)texCoord0Index;

                GL.VertexAttribPointer(attribIndex, (int)m_attrib[i].components, m_attrib[i].type, false, 0, new IntPtr(total_data_size));
                GL.EnableVertexAttribArray(attribIndex);
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }

            if (header.num_indices > 0)
            {
                GL.GenBuffers(1, m_index_buffer);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_index_buffer[0]);
                uint element_size;
                if (header.indexype == 0x1403)
                {
                    element_size = sizeof(ushort);
                }
                else
                {
                    element_size = sizeof(uint);
                }
                //GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, header.num_indices * element_size, raw_data + total_data_size, GL_STATIC_DRAW);
                UnmanagedArray<byte> tmp = new UnmanagedArray<byte>((int)(header.num_indices * element_size));
                for (int t = 0; t < tmp.Length; t++)
                {
                    tmp[t] = raw_data[(int)(t + total_data_size)];
                }
                GL.BufferData(BufferTarget.ElementArrayBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();
            }

            GL.BindVertexArray(0);

            if (m_header.num_materials != 0)
            {
                m_material = new VBM_MATERIAL[m_header.num_materials];
                //memcpy(m_material, raw_data + total_data_size, m_header.num_materials * sizeof(VBM_MATERIAL));
                {
                    var offset = header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material[t] = br.ReadStruct<VBM_MATERIAL>();
                }
                total_data_size += (uint)(m_header.num_materials * Marshal.SizeOf(typeof(VBM_MATERIAL)));
                m_material_textures = new material_texture[m_header.num_materials];
                //memset(m_material_textures, 0, m_header.num_materials * sizeof(*m_material_textures));
                {
                    var offset = 0;
                    f.Seek(0, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material_textures[t] = br.ReadStruct<material_texture>();
                }
            }

            if (m_header.num_chunks != 0)
            {
                m_chunks = new VBM_RENDER_CHUNK[m_header.num_chunks];
                //memcpy(m_chunks, raw_data + total_data_size, m_header.num_chunks * sizeof(VBM_RENDER_CHUNK));
                {
                    var offset = m_header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_chunks; t++)
                {
                    m_chunks[t] = br.ReadStruct<VBM_RENDER_CHUNK>();
                }
                //total_data_size += m_header.num_chunks * sizeof(VBM_RENDER_CHUNK);
            }

            raw_data.Dispose();

            return true;
        }
示例#24
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.LineLoop;
            this.axisVertexCount = 4;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(4);
                positionArray[0] = new vec3(-0.5f, -0.5f, 0);
                positionArray[1] = new vec3(0.5f, -0.5f, 0);
                positionArray[2] = new vec3(0.5f, 0.5f, 0);
                positionArray[3] = new vec3(-0.5f, 0.5f, 0);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(4);
                vec3 color = this.rectColor;
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = color;
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#25
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.Lines;
            this.axisVertexCount   = 8 * 3;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(8 * 3);
                const float           halfLength    = 0.5f;
                // x axis
                positionArray[0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[1] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[3] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[4] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[6] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[7] = new vec3(halfLength, halfLength, -halfLength);
                // y axis
                positionArray[8 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[8 + 1] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[8 + 2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[8 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[8 + 4] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[8 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[8 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[8 + 7] = new vec3(halfLength, halfLength, -halfLength);
                // z axis
                positionArray[16 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[16 + 1] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[16 + 2] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[16 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[16 + 4] = new vec3(halfLength, halfLength, -halfLength);
                positionArray[16 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[16 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[16 + 7] = new vec3(halfLength, -halfLength, halfLength);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(8 * 3);
                vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1), };
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = colors[i / 8];
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#26
0
        protected void InitializeVAO()
        {
            this.axisPrimitiveMode = DrawMode.QuadStrip; //PrimitiveModes.QuadStrip;
            this.axisVertexCount   = faceCount * 2;
            this.vao = new uint[4];

            GL.GenVertexArrays(4, vao);

            vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1) };
            // 计算三个坐标轴
            for (int axisIndex = 0; axisIndex < 3; axisIndex++)
            {
                GL.BindVertexArray(vao[axisIndex]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray <vec3> positionArray = new UnmanagedArray <vec3>(faceCount * 2);
                    for (int i = 0; i < faceCount * 2; i++)
                    {
                        int     face       = i / 2;
                        float[] components = new float[] {
                            i % 2 == 1 ? 0 : this.axisLength,
                            (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                            (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))
                        };
                        positionArray[i] = new vec3(
                            components[(0 + axisIndex) % 3], components[(2 + axisIndex) % 3], components[(4 + axisIndex) % 3]);
                    }

                    uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(positionLocation);
                    positionArray.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(faceCount * 2);
                    for (int i = 0; i < colorArray.Length; i++)
                    {
                        colorArray[i] = colors[axisIndex];
                    }

                    uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(colorLocation);
                    colorArray.Dispose();
                }
                {
                    UnmanagedArray <uint> cylinderIndex = new UnmanagedArray <uint>(faceCount * 2 + 2);
                    for (int i = 0; i < cylinderIndex.Length - 2; i++)
                    {
                        cylinderIndex[i] = (uint)i;
                    }
                    cylinderIndex[cylinderIndex.Length - 2] = 0;
                    cylinderIndex[cylinderIndex.Length - 1] = 1;

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, cylinderIndex, BufferUsage.StaticDraw);
                    cylinderIndex.Dispose();
                }
                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
            // 计算XZ平面
            {
                this.planPrimitveMode = DrawMode.LineLoop;
                var lanVertexCount = 4;

                GL.BindVertexArray(vao[3]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray <vec3> plan = new UnmanagedArray <vec3>(lanVertexCount);
                    float length = this.axisLength;
                    plan[0] = new vec3(-length, 0, -length);
                    plan[1] = new vec3(-length, 0, length);
                    plan[2] = new vec3(length, 0, length);
                    plan[3] = new vec3(length, 0, -length);

                    uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, plan, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(positionLocation);

                    plan.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(4);
                    for (int i = 0; i < colorArray.Length; i++)
                    {
                        colorArray[i] = this.planColor;
                    }

                    uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(colorLocation);

                    colorArray.Dispose();
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(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);
        }
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.QuadStrip;
            GLColor[] colors = this.ColorPalette.Colors;
            float[] coords = this.ColorPalette.Coords;
            this.numbers = new PointSpriteStringElement[coords.Length];
            this.vertexCount = coords.Length * 2;
            this.vao = new uint[1];

            float coordLength = coords[coords.Length - 1] - coords[0];
            {
                GL.GenVertexArrays(1, vao);

                GL.BindVertexArray(vao[0]);

                //  Create a vertex buffer for the vertex data.
                {
                    UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(this.vertexCount);
                    positionArray[0] = new vec3(-0.5f, -0.5f, 0);
                    positionArray[1] = new vec3(-0.5f, 0.5f, 0);
                    for (int i = 1; i < coords.Length; i++)
                    {
                        float x = (coords[i] - coords[0]) / coordLength - 0.5f;
                        positionArray[i * 2 + 0] = new vec3(x, -0.5f, 0);
                        positionArray[i * 2 + 1] = new vec3(x, 0.5f, 0);
                    }

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_PositionLocation);

                    positionArray.Dispose();
                }

                //  Now do the same for the colour data.
                {
                    var colorArray = new UnmanagedArray<vec4>(this.vertexCount);
                    for (int i = 0; i < colors.Length; i++)
                    {
                        GLColor color = colors[i];
                        colorArray[i * 2 + 0] = new vec4(color.R, color.G, color.B, color.A);
                        colorArray[i * 2 + 1] = new vec4(color.R, color.G, color.B, color.A);
                    }

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(in_ColorLocation, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_ColorLocation);

                    colorArray.Dispose();
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }

            // prepare numbers
            {
                const float numberPosY = -0.6f;
                this.numbers[0] = new PointSpriteStringElement(
                    this.Min.ToShortString(), new vec3(-0.5f, numberPosY, 0));
                this.numbers[0].Initialize();
                for (int i = 1; i < coords.Length; i++)
                {
                    float x = (coords[i] - coords[0]) / coordLength - 0.5f;
                    if (i + 1 == coords.Length)
                    {
                        this.numbers[i] = new PointSpriteStringElement(
                            (this.Min + i * this.Step).ToShortString(), new vec3(x, numberPosY, 0));
                    }
                    else
                    {
                        this.numbers[i] = new PointSpriteStringElement(
                            this.Max.ToShortString(), new vec3(x, numberPosY, 0));
                    }
                    this.numbers[i].Initialize();
                }
            }
        }
示例#29
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.Lines;
            this.axisVertexCount = 8 * 3;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(8 * 3);
                const float halfLength = 0.5f;
                // x axis
                positionArray[0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[1] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[3] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[4] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[6] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[7] = new vec3(halfLength, halfLength, -halfLength);
                // y axis
                positionArray[8 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[8 + 1] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[8 + 2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[8 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[8 + 4] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[8 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[8 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[8 + 7] = new vec3(halfLength, halfLength, -halfLength);
                // z axis
                positionArray[16 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[16 + 1] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[16 + 2] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[16 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[16 + 4] = new vec3(halfLength, halfLength, -halfLength);
                positionArray[16 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[16 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[16 + 7] = new vec3(halfLength, -halfLength, halfLength);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(8 * 3);
                vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1), };
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = colors[i / 8];
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            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);
            }

        }
示例#31
0
        internal void LoadSoundInMemory()
        {
            if (PreloadedBuffer.Ptr != IntPtr.Zero) return;

            using (var soundStream = ContentManager.FileProvider.OpenStream(CompressedDataUrl, VirtualFileMode.Open, VirtualFileAccess.Read, VirtualFileShare.Read, StreamFlags.Seekable))
            using (var decoder = new Celt(SampleRate, CompressedSoundSource.SamplesPerFrame, Channels, true))
            {
                var reader = new BinarySerializationReader(soundStream);
                var samplesPerPacket = CompressedSoundSource.SamplesPerFrame * Channels;

                PreloadedBuffer = AudioLayer.BufferCreate(samplesPerPacket * NumberOfPackets * sizeof(short));

                var memory = new UnmanagedArray<short>(samplesPerPacket * NumberOfPackets);

                var offset = 0;
                var outputBuffer = new short[samplesPerPacket];
                for (var i = 0; i < NumberOfPackets; i++)
                {
                    var len = reader.ReadInt16();
                    var compressedBuffer = reader.ReadBytes(len);
                    var samplesDecoded = decoder.Decode(compressedBuffer, len, outputBuffer);
                    memory.Write(outputBuffer, offset, 0, samplesDecoded * Channels);
                    offset += samplesDecoded * Channels * sizeof(short);
                }

                AudioLayer.BufferFill(PreloadedBuffer, memory.Pointer, memory.Length * sizeof(short), SampleRate, Channels == 1);
                memory.Dispose();
            }
        }
        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);

        }
示例#33
0
        private void InitVAO(string value)
        {
            if (value == null) { value = string.Empty; }

            this.mode = DrawMode.Quads;
            this.vertexCount = 4 * value.Length;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray<vec3> in_Position = new UnmanagedArray<vec3>(this.vertexCount);
            UnmanagedArray<vec2> in_TexCoord = new UnmanagedArray<vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;
            // step 1: set width for each glyph
            vec3[] tmpPositions = new vec3[this.vertexCount];
            float totalLength = 0;
            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];
                CharacterInfo cInfo;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    float glyphWidth = (float)cInfo.width / (float)this.ttfTexture.FontHeight;
                    if (i == 0)
                    {
                        tmpPositions[i * 4 + 0] = new vec3(0, 0, 0);
                        tmpPositions[i * 4 + 1] = new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 2] = new vec3(glyphWidth, 1, 0);
                        tmpPositions[i * 4 + 3] = new vec3(0, 1, 0);
                    }
                    else
                    {
                        tmpPositions[i * 4 + 0] = tmpPositions[i * 4 + 0 - 4 + 1];
                        tmpPositions[i * 4 + 1] = tmpPositions[i * 4 + 0] + new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 3] = tmpPositions[i * 4 + 3 - 4 - 1];
                        tmpPositions[i * 4 + 2] = tmpPositions[i * 4 + 3] + new vec3(glyphWidth, 0, 0);
                    }
                    totalLength += glyphWidth;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
            }
            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];
                CharacterInfo cInfo;
                float x1 = 0;
                float x2 = 1;
                float y1 = 0;
                float y2 = 1;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    x1 = (float)cInfo.xoffset / (float)bigBitmap.Width;
                    x2 = (float)(cInfo.xoffset + cInfo.width) / (float)bigBitmap.Width;
                    y1 = (float)cInfo.yoffset / (float)bigBitmap.Height;
                    y2 = (float)(cInfo.yoffset + this.ttfTexture.FontHeight) / (float)bigBitmap.Height;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
                in_Position[i * 4 + 0] = tmpPositions[i * 4 + 0] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 1] = tmpPositions[i * 4 + 1] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 2] = tmpPositions[i * 4 + 2] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 3] = tmpPositions[i * 4 + 3] - new vec3(totalLength / 2, 0, 0);

                //in_TexCoord[i * 4 + 0] = new vec2(x1, y1);
                //in_TexCoord[i * 4 + 1] = new vec2(x2, y1);
                //in_TexCoord[i * 4 + 2] = new vec2(x2, y2);
                //in_TexCoord[i * 4 + 3] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 0] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 1] = new vec2(x2, y2);
                in_TexCoord[i * 4 + 2] = new vec2(x2, y1);
                in_TexCoord[i * 4 + 3] = new vec2(x1, y1);
            }

            if (vao[0] != 0)
            { GL.DeleteBuffers(1, vao); }
            if (vbo[0] != 0)
            { GL.DeleteBuffers(vbo.Length, vbo); }

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

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
示例#34
0
        private void InitVAO()
        {
            // reserve a vertex array object(VAO) 预约一个VAO
            this.vertexArrayObject = new uint[1];
            GL.GenVertexArrays(1, this.vertexArrayObject);

            // prepare vertex buffer object(VBO) for vertexes' positions 为顶点位置准备VBO
            uint[] positionBufferObject = new uint[1];
            {
                // specify position array
                var positionArray = new UnmanagedArray <vec3>(vertexCount);
                positionArray[0]  = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[1]  = new vec3(-1.0f, -1.0f, 1.0f);
                positionArray[2]  = new vec3(1.0f, -1.0f, 1.0f);
                positionArray[3]  = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[4]  = new vec3(1.0f, -1.0f, 1.0f);
                positionArray[5]  = new vec3(1.0f, -1.0f, -1.0f);
                positionArray[6]  = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[7]  = new vec3(1.0f, -1.0f, -1.0f);
                positionArray[8]  = new vec3(-1.0f, -1.0f, -1.0f);
                positionArray[9]  = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[10] = new vec3(-1.0f, -1.0f, -1.0f);
                positionArray[11] = new vec3(-1.0f, -1.0f, 1.0f);

                // put positions into VBO
                GL.GenBuffers(1, positionBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferObject[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);

                positionArray.Dispose();
            }

            // prepare vertex buffer object(VBO) for vertexes' colors
            uint[] colorBufferObject = new uint[1];
            {
                // specify color array
                UnmanagedArray <vec3> colorArray = new UnmanagedArray <vec3>(vertexCount);
                colorArray[0]  = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[1]  = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[2]  = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[3]  = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[4]  = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[5]  = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[6]  = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[7]  = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[8]  = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[9]  = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[10] = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[11] = new vec3(0.0f, 1.0f, 0.0f);

                // put colors into VBO
                GL.GenBuffers(1, colorBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferObject[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);

                colorArray.Dispose();
            }

            uint positionLocation = shaderProgram.GetAttributeLocation("in_Position");
            uint colorLocation    = shaderProgram.GetAttributeLocation("in_Color");

            {
                // bind the vertex array object(VAO), we are going to specify data for it.
                GL.BindVertexArray(vertexArrayObject[0]);

                // specify vertexes' positions
                GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferObject[0]);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                // specify vertexes' colors
                GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferObject[0]);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                //  Unbind the vertex array object(VAO), we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
示例#35
0
        private void InitVAO(string value)
        {
            if (value == null)
            {
                value = string.Empty;
            }

            this.mode        = DrawMode.Quads;
            this.vertexCount = 4 * value.Length;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray <vec3> in_Position = new UnmanagedArray <vec3>(this.vertexCount);
            UnmanagedArray <vec2> in_TexCoord = new UnmanagedArray <vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;

            // step 1: set width for each glyph
            vec3[] tmpPositions = new vec3[this.vertexCount];
            float  totalLength  = 0;

            for (int i = 0; i < value.Length; i++)
            {
                char          c = value[i];
                CharacterInfo cInfo;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    float glyphWidth = (float)cInfo.width / (float)this.ttfTexture.FontHeight;
                    if (i == 0)
                    {
                        tmpPositions[i * 4 + 0] = new vec3(0, 0, 0);
                        tmpPositions[i * 4 + 1] = new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 2] = new vec3(glyphWidth, 1, 0);
                        tmpPositions[i * 4 + 3] = new vec3(0, 1, 0);
                    }
                    else
                    {
                        tmpPositions[i * 4 + 0] = tmpPositions[i * 4 + 0 - 4 + 1];
                        tmpPositions[i * 4 + 1] = tmpPositions[i * 4 + 0] + new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 3] = tmpPositions[i * 4 + 3 - 4 - 1];
                        tmpPositions[i * 4 + 2] = tmpPositions[i * 4 + 3] + new vec3(glyphWidth, 0, 0);
                    }
                    totalLength += glyphWidth;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
            }
            for (int i = 0; i < value.Length; i++)
            {
                char          c = value[i];
                CharacterInfo cInfo;
                float         x1 = 0;
                float         x2 = 1;
                float         y1 = 0;
                float         y2 = 1;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    x1 = (float)cInfo.xoffset / (float)bigBitmap.Width;
                    x2 = (float)(cInfo.xoffset + cInfo.width) / (float)bigBitmap.Width;
                    y1 = (float)cInfo.yoffset / (float)bigBitmap.Height;
                    y2 = (float)(cInfo.yoffset + this.ttfTexture.FontHeight) / (float)bigBitmap.Height;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
                in_Position[i * 4 + 0] = tmpPositions[i * 4 + 0] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 1] = tmpPositions[i * 4 + 1] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 2] = tmpPositions[i * 4 + 2] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 3] = tmpPositions[i * 4 + 3] - new vec3(totalLength / 2, 0, 0);

                //in_TexCoord[i * 4 + 0] = new vec2(x1, y1);
                //in_TexCoord[i * 4 + 1] = new vec2(x2, y1);
                //in_TexCoord[i * 4 + 2] = new vec2(x2, y2);
                //in_TexCoord[i * 4 + 3] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 0] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 1] = new vec2(x2, y2);
                in_TexCoord[i * 4 + 2] = new vec2(x2, y1);
                in_TexCoord[i * 4 + 3] = new vec2(x1, y1);
            }

            if (vao[0] != 0)
            {
                GL.DeleteBuffers(1, vao);
            }
            if (vbo[0] != 0)
            {
                GL.DeleteBuffers(vbo.Length, vbo);
            }

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

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
示例#36
0
        private void scientificVisual3DControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'r')
            {
                // 随机显示某些hexahedron
                foreach (var item in this.scientificVisual3DControl.ModelContainer.Children)
                {
                    {
                        HexahedronGridderElement element = item as HexahedronGridderElement;
                        if (element != null)
                        {
                            YieldingGeometryModel.Builder.HexahedronGridderElementHelper.RandomVisibility(element, this.scientificVisual3DControl.OpenGL, 0.2);
                        }
                    }
                    {
                        PointSpriteGridderElement element = item as PointSpriteGridderElement;
                        if (element != null)
                        {
                            YieldingGeometryModel.Builder.PointSpriteGridderElementHelper.RandomVisibility(element, this.scientificVisual3DControl.OpenGL, 0.2);
                        }
                    }
                }

                this.scientificVisual3DControl.Invalidate();
            }


            if (e.KeyChar == 'c')
            {
                OpenGL gl = this.scientificVisual3DControl.OpenGL;

                List <HexahedronGridderElement> elements = this.scientificVisual3DControl.ModelContainer.Traverse <HexahedronGridderElement>().ToList <HexahedronGridderElement>();
                if (elements.Count > 0)
                {
                    HexahedronGridderElement gridder  = elements[0];
                    HexahedronGridderSource  source   = gridder.Source;
                    UnmanagedArray <float>   visibles = HexahedronGridderHelper.GridVisibleFromActive(source);

                    //随机生成不完整网格的属性。
                    int propCount = source.DimenSize / 2;
                    if (propCount <= 0)
                    {
                        return;
                    }

                    int     minValue = 5000;
                    int     maxValue = 10000;
                    int[]   gridIndexes;
                    float[] gridValues;
                    HexahedronGridderHelper.RandomValue(propCount, minValue, maxValue, out gridIndexes, out gridValues);
                    float step = (maxValue - minValue) / 10.0f;
                    this.scientificVisual3DControl.SetColorIndicator(minValue, maxValue, step);

                    ColorF[] colors = new ColorF[propCount];
                    for (int i = 0; i < colors.Length; i++)
                    {
                        colors[i] = (ColorF)this.scientificVisual3DControl.MapToColor(gridValues[i]);
                    }

                    UnmanagedArray <ColorF> colorArray = HexahedronGridderHelper.FromColors(source, gridIndexes, colors, visibles);
                    gridder.UpdateColorBuffer(gl, colorArray, visibles);
                    colorArray.Dispose();
                    visibles.Dispose();
                    this.scientificVisual3DControl.Invalidate();
                }
            }
        }
示例#37
0
        internal static void TypicalScene()
        {
            // 数组较小时可按此方法使用UnmanagedArray,数组较大时请参考UnmanagedArrayHelper。
            const int count = 100;

            // 测试float类型
            {
                var floatArray = new UnmanagedArray<float>(count);
                for (int i = 0; i < count; i++)
                {
                    floatArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = floatArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
            }

            // 测试decimal类型
            {
                var decimalArray = new UnmanagedArray<decimal>(count);
                for (int i = 0; i < count; i++)
                {
                    decimalArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = decimalArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
            }

            // 测试int类型
            {
                var intArray = new UnmanagedArray<int>(count);
                for (int i = 0; i < count; i++)
                {
                    intArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = intArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
            }

            // 测试bool类型
            {
                var boolArray = new UnmanagedArray<bool>(count);
                for (int i = 0; i < count; i++)
                {
                    boolArray[i] = i % 2 == 0;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = boolArray[i];
                    if (item != (i % 2 == 0))
                    { throw new Exception(); }
                }
            }

            // 测试vec3类型
            {
                var vec3Array = new UnmanagedArray<vec3>(count);
                for (int i = 0; i < count; i++)
                {
                    vec3Array[i] = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                }
                for (int i = 0; i < count; i++)
                {
                    var item = vec3Array[i];
                    var old = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                    if (item.x != old.x || item.y != old.y || item.z != old.z)
                    { throw new Exception(); }
                }

                // 释放此数组占用的内存,这之后就不能再使用vec3Array了。
                vec3Array.Dispose();
            }

            // 速度较慢,不再使用。
            //// 测试foreach
            //foreach (var item in vec3Array.Elements())
            //{
            //    Console.WriteLine(item);
            //}

            // 立即释放所有非托管数组占用的内存,任何之前创建的UnmanagedBase数组都不再可用了。
            UnmanagedArray<int>.FreeAll();
        }
示例#38
0
        private void InitVAO()
        {
            int size = this.size;

            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            // prepare positions
            {
                var positionArray = new UnmanagedArray <vec3>(size * size * size * 14);
                int index         = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                                         + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_PositionLocation);

                positionArray.Dispose();
            }
            // prepare colors
            {
                var    colorArray = new UnmanagedArray <vec3>(size * size * size * 14);
                Random random     = new Random();
                int    index      = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint in_ColorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_ColorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);

            // prepare firsts and counts
            {
                // todo: rename 'size'
                this.firsts = new int[size * size * size];
                for (int i = 0; i < this.firsts.Length; i++)
                {
                    this.firsts[i] = i * 14;
                }

                this.counts = new int[size * size * size];
                for (int i = 0; i < this.counts.Length; i++)
                {
                    this.counts[i] = 14;
                }
            }
        }
        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);
            }
        }
示例#40
0
        /// <summary>
        /// 用随机颜色更新当前的颜色。
        /// </summary>
        public void UpdateColorBuffer()
        {
            var colorArray = new UnmanagedArray<vec3>(size * size * size * 8);
            Random random = new Random();
            int index = 0;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    for (int k = 0; k < size; k++)
                    {
                        //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                        for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                        {
                            vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            colorArray[index++] = color;
                        }
                    }
                }
            }

            // update buffer object.
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0]);

            IntPtr destColors = GL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadWrite);

            colorArray.CopyTo(destColors);

            GL.UnmapBuffer(BufferTarget.ArrayBuffer);

            // This do the same thing: update buffer object
            using (var mappingBuffer = new MappingBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0], MapBufferAccess.ReadWrite))
            {
                colorArray.CopyTo(mappingBuffer.BufferPointer);
            }

            colorArray.Dispose();
        }
示例#41
0
        internal static void TypicalScene()
        {
            // 数组较小时可按此方法使用UnmanagedArray,数组较大时请参考UnmanagedArrayHelper。
            const int count = 100;

            // 测试float类型
            {
                var floatArray = new UnmanagedArray <float>(count);
                for (int i = 0; i < count; i++)
                {
                    floatArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = floatArray[i];
                    if (item != i)
                    {
                        throw new Exception();
                    }
                }
            }

            // 测试decimal类型
            {
                var decimalArray = new UnmanagedArray <decimal>(count);
                for (int i = 0; i < count; i++)
                {
                    decimalArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = decimalArray[i];
                    if (item != i)
                    {
                        throw new Exception();
                    }
                }
            }

            // 测试int类型
            {
                var intArray = new UnmanagedArray <int>(count);
                for (int i = 0; i < count; i++)
                {
                    intArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = intArray[i];
                    if (item != i)
                    {
                        throw new Exception();
                    }
                }
            }

            // 测试bool类型
            {
                var boolArray = new UnmanagedArray <bool>(count);
                for (int i = 0; i < count; i++)
                {
                    boolArray[i] = i % 2 == 0;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = boolArray[i];
                    if (item != (i % 2 == 0))
                    {
                        throw new Exception();
                    }
                }
            }

            // 测试vec3类型
            {
                var vec3Array = new UnmanagedArray <vec3>(count);
                for (int i = 0; i < count; i++)
                {
                    vec3Array[i] = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                }
                for (int i = 0; i < count; i++)
                {
                    var item = vec3Array[i];
                    var old  = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                    if (item.x != old.x || item.y != old.y || item.z != old.z)
                    {
                        throw new Exception();
                    }
                }

                // 释放此数组占用的内存,这之后就不能再使用vec3Array了。
                vec3Array.Dispose();
            }

            // 速度较慢,不再使用。
            //// 测试foreach
            //foreach (var item in vec3Array.Elements())
            //{
            //    Console.WriteLine(item);
            //}

            // 立即释放所有非托管数组占用的内存,任何之前创建的UnmanagedBase数组都不再可用了。
            UnmanagedArray <int> .FreeAll();
        }
示例#42
0
        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);

        }
示例#43
0
        protected override void DoInitialize()
        {
            // Now create a simple program to visualize the result
            basicShaderProgram = new ShaderProgram();
            basicShaderProgram.Create(basicVertexShader, basicFragmentShader, null);
            basicShaderProgram.AssertValid();
            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;
        }
示例#44
0
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            FileStream   f  = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            long filesize = f.Length;

            byte[] data = new byte[filesize];
            UnmanagedArray <byte> raw_data;

            f.Read(data, 0, (int)filesize);
            f.Seek(0, SeekOrigin.Begin);

            VBM_HEADER        header        = br.ReadStruct <VBM_HEADER>();
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct <VBM_ATTRIB_HEADER>();
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            VBM_FRAME_HEADER frame_header = br.ReadStruct <VBM_FRAME_HEADER>();

            uint total_data_size = 0;

            this.m_header = header;

            {
                long offset = header.size;// +header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_attrib = new VBM_ATTRIB_HEADER[header.num_attribs];
            for (int i = 0; i < header.num_attribs; i++)
            {
                this.m_attrib[i] = br.ReadStruct <VBM_ATTRIB_HEADER>();
            }

            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));// +header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_frame = new VBM_FRAME_HEADER[header.num_frames];
            for (int i = 0; i < header.num_frames; i++)
            {
                this.m_frame[i] = br.ReadStruct <VBM_FRAME_HEADER>();
            }

            GL.GenVertexArrays(1, m_vao);
            GL.BindVertexArray(m_vao[0]);
            GL.GenBuffers(1, m_attribute_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_attribute_buffer[0]);

            //uint i;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                raw_data = new UnmanagedArray <byte>((int)total_data_size);
                for (int i = 0; i < raw_data.Length; i++)
                {
                    raw_data[i] = data[offset + i];
                }
            }
            //GL.BufferData(GL_ARRAY_BUFFER, total_data_size, raw_data, GL_STATIC_DRAW);
            GL.BufferData(BufferTarget.ArrayBuffer, raw_data, BufferUsage.StaticDraw);

            total_data_size = 0;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                uint attribIndex = i;

                if (attribIndex == 0)
                {
                    attribIndex = (uint)vertexIndex;
                }
                else if (attribIndex == 1)
                {
                    attribIndex = (uint)normalIndex;
                }
                else if (attribIndex == 2)
                {
                    attribIndex = (uint)texCoord0Index;
                }

                GL.VertexAttribPointer(attribIndex, (int)m_attrib[i].components, m_attrib[i].type, false, 0, new IntPtr(total_data_size));
                GL.EnableVertexAttribArray(attribIndex);
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }

            if (header.num_indices > 0)
            {
                GL.GenBuffers(1, m_index_buffer);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_index_buffer[0]);
                uint element_size;
                if (header.indexype == 0x1403)
                {
                    element_size = sizeof(ushort);
                }
                else
                {
                    element_size = sizeof(uint);
                }
                //GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, header.num_indices * element_size, raw_data + total_data_size, GL_STATIC_DRAW);
                UnmanagedArray <byte> tmp = new UnmanagedArray <byte>((int)(header.num_indices * element_size));
                for (int t = 0; t < tmp.Length; t++)
                {
                    tmp[t] = raw_data[(int)(t + total_data_size)];
                }
                GL.BufferData(BufferTarget.ElementArrayBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();
            }

            GL.BindVertexArray(0);

            raw_data.Dispose();

            return(true);
        }
示例#45
0
        private void InitVAO()
        {
            // reserve a vertex array object(VAO) 预约一个VAO
            this.vertexArrayObject = new uint[1];
            GL.GenVertexArrays(1, this.vertexArrayObject);

            // prepare vertex buffer object(VBO) for vertexes' positions 为顶点位置准备VBO
            uint[] positionBufferObject = new uint[1];
            {
                // specify position array
                var positionArray = new UnmanagedArray<vec3>(vertexCount);
                positionArray[0] = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[1] = new vec3(-1.0f, -1.0f, 1.0f);
                positionArray[2] = new vec3(1.0f, -1.0f, 1.0f);
                positionArray[3] = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[4] = new vec3(1.0f, -1.0f, 1.0f);
                positionArray[5] = new vec3(1.0f, -1.0f, -1.0f);
                positionArray[6] = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[7] = new vec3(1.0f, -1.0f, -1.0f);
                positionArray[8] = new vec3(-1.0f, -1.0f, -1.0f);
                positionArray[9] = new vec3(0.0f, 1.0f, 0.0f);
                positionArray[10] = new vec3(-1.0f, -1.0f, -1.0f);
                positionArray[11] = new vec3(-1.0f, -1.0f, 1.0f);

                // put positions into VBO
                GL.GenBuffers(1, positionBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferObject[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);

                positionArray.Dispose();
            }

            // prepare vertex buffer object(VBO) for vertexes' colors
            uint[] colorBufferObject = new uint[1];
            {
                // specify color array
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(vertexCount);
                colorArray[0] = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[1] = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[2] = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[3] = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[4] = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[5] = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[6] = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[7] = new vec3(0.0f, 1.0f, 0.0f);
                colorArray[8] = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[9] = new vec3(1.0f, 0.0f, 0.0f);
                colorArray[10] = new vec3(0.0f, 0.0f, 1.0f);
                colorArray[11] = new vec3(0.0f, 1.0f, 0.0f);

                // put colors into VBO
                GL.GenBuffers(1, colorBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferObject[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);

                colorArray.Dispose();
            }

            uint positionLocation = shaderProgram.GetAttributeLocation("in_Position");
            uint colorLocation = shaderProgram.GetAttributeLocation("in_Color");

            {
                // bind the vertex array object(VAO), we are going to specify data for it.
                GL.BindVertexArray(vertexArrayObject[0]);

                // specify vertexes' positions
                GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferObject[0]);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                // specify vertexes' colors
                GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferObject[0]);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                //  Unbind the vertex array object(VAO), we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
示例#46
0
        protected void InitializeVAO(OpenGL gl, out uint[] vao, out BeginMode primitiveMode, out int vertexCount)
        {
            primitiveMode = BeginMode.QuadStrip;
            vertexCount   = (faceCount * 2 + 2) * (this.pipe.Count - 1);

            UnmanagedArray <Vertex> positionArray = new UnmanagedArray <Vertex>(vertexCount);
            {
                int index = 0;
                for (int i = 1; i < this.pipe.Count; i++)
                {
                    Vertex p1     = this.pipe[i - 1];
                    Vertex p2     = this.pipe[i];
                    Vertex vector = p2 - p1;// 从p1到p2的向量
                    // 找到互相垂直的三个向量:vector, orthogontalVector1和orthogontalVector2
                    Vertex orthogontalVector1 = new Vertex(vector.Y - vector.Z, vector.Z - vector.X, vector.X - vector.Y);
                    Vertex orthogontalVector2 = vector.VectorProduct(orthogontalVector1);
                    orthogontalVector1.Normalize();
                    orthogontalVector2.Normalize();
                    orthogontalVector1 *= (float)Math.Sqrt(this.radius);
                    orthogontalVector2 *= (float)Math.Sqrt(this.radius);
                    for (int faceIndex = 0; faceIndex < faceCount + 1; faceIndex++)
                    {
                        double angle = (Math.PI * 2 * faceIndex) / faceCount;
                        Vertex point = orthogontalVector1 * (float)Math.Cos(angle) + orthogontalVector2 * (float)Math.Sin(angle);
                        positionArray[index++] = p2 + point;
                        positionArray[index++] = p1 + point;
                    }

                    //positionArray[index++] = new vec3();//用于分割圆柱体
                }
            }

            UnmanagedArray <Vertex> colorArray = new UnmanagedArray <Vertex>(vertexCount);
            {
                Vertex vColor = new Vertex(this.color.R, this.color.G, this.color.B);
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = vColor;
                    // 测试时用此代码区分各个圆柱
                    //if ((i / (faceCount * 2 + 2)) % 3 == 0)
                    //{
                    //    colorArray[i] = new vec3(1, 0, 0);
                    //}
                    //else if ((i / (faceCount * 2 + 2)) % 3 == 1)
                    //{
                    //    colorArray[i] = new vec3(0, 1, 0);
                    //}
                    //else
                    //{
                    //    colorArray[i] = new vec3(0, 0, 1);
                    //}
                }
            }

            UnmanagedArray <uint> indexArray = new UnmanagedArray <uint>(vertexCount + (this.pipe.Count - 1));

            {
                uint positionIndex = 0;
                for (int i = 0; i < indexArray.Length; i++)
                {
                    if (i % (faceCount * 2 + 2 + 1) == (faceCount * 2 + 2))
                    {
                        indexArray[i] = uint.MaxValue;//分割各个圆柱体
                    }
                    else
                    {
                        indexArray[i] = positionIndex++;
                    }
                }
            }

            vao = new uint[1];
            gl.GenVertexArrays(1, vao);
            gl.BindVertexArray(vao[0]);
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);

                int location = gl.GetAttribLocation(shaderProgram.ShaderProgramObject, strin_Position);
                if (location < 0)
                {
                    throw new Exception();
                }
                uint positionLocation = (uint)location;

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, positionArray.ByteLength, positionArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(positionLocation, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
                this.positionBufferObject = ids[0];
            }
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);

                int location = gl.GetAttribLocation(shaderProgram.ShaderProgramObject, strin_Color);
                if (location < 0)
                {
                    throw new Exception();
                }
                uint colorLocation = (uint)location;

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray.ByteLength, colorArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(colorLocation, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
                this.colorBufferObject = ids[0];
            }
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ids[0]);
                gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexArray.ByteLength, indexArray.Header, OpenGL.GL_STATIC_DRAW);

                indexArray.Dispose();
                this.indexBufferObject = ids[0];
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            gl.BindVertexArray(0);
        }
        private void InitVAO()
        {
            int size = this.size;

            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            // prepare positions
            {
                var positionArray = new UnmanagedArray<vec3>(size * size * size * 14);
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                    + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_PositionLocation);

                positionArray.Dispose();
            }
            // prepare colors
            {
                var colorArray = new UnmanagedArray<vec3>(size * size * size * 14);
                Random random = new Random();
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint in_ColorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_ColorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);

            // prepare firsts and counts
            {
                // todo: rename 'size'
                this.firsts = new int[size * size * size];
                for (int i = 0; i < this.firsts.Length; i++)
                {
                    this.firsts[i] = i * 14;
                }

                this.counts = new int[size * size * size];
                for (int i = 0; i < this.counts.Length; i++)
                {
                    this.counts[i] = 14;
                }
            }
        }
示例#48
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);
        }
示例#49
0
        //protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        protected void InitializeVAO()
        {
            this.primitiveMode = DrawMode.Triangles;
            this.vertexCount = positions.Length;

            this.vao = new uint[1];
            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                //uint[] ids = new uint[1];
                this.positionBufferObject = new uint[1];
                GL.GenBuffers(1, this.positionBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.positionBufferObject[0]);
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                //uint[] ids = new uint[1];
                this.colorBufferObject = new uint[1];
                GL.GenBuffers(1, this.colorBufferObject);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBufferObject[0]);
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(positions.Length);
                for (int i = 0; i < colors.Length; i++)
                {
                    colorArray[i] = colors[i];
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
        protected void InitializeVAO()
        {
            this.primitiveMode = DrawMode.Points;
            const int axisCount = 3;
            const int count = axisCount * axisCount * axisCount;
            this.vertexCount = count;

            this.vao = new uint[1];

            GL.GenVertexArrays(4, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(count);
                for (int i = 0, index = 0; i < axisCount; i++)
                {
                    for (int j = 0; j < axisCount; j++)
                    {
                        for (int k = 0; k < axisCount; k++)
                        {
                            //positionArray[index++] = 10 * new vec3(i - axisCount / 2, j - axisCount / 2, k - axisCount / 2);
                            positionArray[index++] = 10 * new vec3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
                        }
                    }
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#51
0
        private void InitVAO()
        {
            int size = this.size;

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            // prepare positions
            {
                var positionArray = new UnmanagedArray<vec3>(size * size * size * 8);
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                    + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_PositionLocation);

                positionArray.Dispose();
            }
            // prepare colors
            {
                var colorArray = new UnmanagedArray<vec3>(size * size * size * 8);
                Random random = new Random();
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint in_ColorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.GenBuffers(1, this.colorBuffer);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_ColorLocation);

                colorArray.Dispose();
            }
            // prepare index
            {
                var indexArray = new UnmanagedArray<uint>(size * size * size * (14 + 1));
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                long posIndex = unitCubeIndex[cubeIndex] + (i * size * size + j * size + k) * 8;
                                indexArray[index++] = (uint)posIndex;
                            }

                            indexArray[index++] = uint.MaxValue;
                        }
                    }
                }

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ElementArrayBuffer, indexArray, BufferUsage.StaticDraw);

                indexArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#52
0
        protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        {
            primitiveMode = DrawMode.QuadStrip;// PrimitiveModes.QuadStrip;
            vertexCount = faceCount * 2;

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

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(faceCount * 2);
                for (int i = 0; i < faceCount * 2; i++)
                {
                    int face = i / 2;
                    positionArray[i] = new vec3(
                        (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                        (i % 2 == 1 ? -1 : 1) * this.height / 2,
                        (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))
                        );
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);
                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(faceCount * 2);
                for (int i = 0; i < colorArray.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        colorArray[i] = new vec3(1, 0, 0); //new vec3((i % 3) / 3.0f, (i + 1) % 3 / 3.0f, (i + 2) % 3 / 3.0f);
                    }
                    else
                    {
                        colorArray[i] = new vec3(0, 1, 0); //new vec3((i % 3) / 3.0f, (i + 1) % 3 / 3.0f, (i + 2) % 3 / 3.0f);
                    }
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);
                colorArray.Dispose();
            }
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                UnmanagedArray<uint> cylinderIndex = new UnmanagedArray<uint>(faceCount * 2 + 2);
                for (int i = 0; i < cylinderIndex.Length - 2; i++)
                {
                    cylinderIndex[i] = (uint)i;
                }
                cylinderIndex[cylinderIndex.Length - 2] = 0;
                cylinderIndex[cylinderIndex.Length - 1] = 1;
                GL.BufferData(BufferTarget.ElementArrayBuffer, cylinderIndex, BufferUsage.StaticDraw);
                cylinderIndex.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
示例#53
0
        private void InitVAO()
        {
            int size = this.size;

            // prepare positions
            {
                var positionArray = new UnmanagedArray <vec3>(size * size * size * 8);
                int index         = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                                         + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);

                positionArray.Dispose();
                positionBuffer = ids[0];
            }
            // prepare colors
            {
                var    colorArray = new UnmanagedArray <vec3>(size * size * size * 8);
                Random random     = new Random();
                int    index      = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);

                colorArray.Dispose();
                colorBuffer = ids[0];
            }

            // prepare index
            {
                var indexArray = new UnmanagedArray <uint>(size * size * size * (14 + 1));
                int index      = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                long posIndex = unitCubeIndex[cubeIndex] + (i * size * size + j * size + k) * 8;
                                indexArray[index++] = (uint)posIndex;
                            }

                            indexArray[index++] = uint.MaxValue;
                        }
                    }
                }

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ElementArrayBuffer, indexArray, BufferUsage.StaticDraw);

                indexArray.Dispose();
                indexBuffer = ids[0];
            }

            // create vao
            {
                this.vao = new uint[1];
                GL.GenVertexArrays(1, vao);
                GL.BindVertexArray(vao[0]);

                // prepare positions
                {
                    GL.BindBuffer(BufferTarget.ArrayBuffer, positionBuffer);
                    GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_PositionLocation);
                }
                // prepare colors
                {
                    GL.BindBuffer(BufferTarget.ArrayBuffer, colorBuffer);
                    GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(in_ColorLocation);
                }
                // prepare index
                {
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
        private void initVol3DTex(string filename, int width, int height, int depth)
        {
            var data = new UnmanagedArray<byte>(width * height * depth);
            unsafe
            {
                int index = 0;
                int readCount = 0;
                byte* array = (byte*)data.Header.ToPointer();
                using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                using (var br = new BinaryReader(fs))
                {
                    int unReadCount = (int)fs.Length;
                    const int cacheSize = 1024 * 1024;
                    do
                    {
                        int min = Math.Min(cacheSize, unReadCount);
                        var cache = new byte[min];
                        readCount = br.Read(cache, 0, min);
                        if (readCount != min)
                        { throw new Exception(); }

                        for (int i = 0; i < readCount; i++)
                        {
                            array[index++] = cache[i];
                        }
                        unReadCount -= readCount;
                    } while (readCount > 0);
                }
            }

            OpenGL.GenTextures(1, vol3DTexObj);
            // bind 3D texture target
            OpenGL.BindTexture(OpenGL.GL_TEXTURE_3D, vol3DTexObj[0]);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_MAG_FILTER, (int)OpenGL.GL_LINEAR);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_MIN_FILTER, (int)OpenGL.GL_LINEAR);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_S, (int)OpenGL.GL_REPEAT);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_T, (int)OpenGL.GL_REPEAT);
            OpenGL.TexParameteri(OpenGL.GL_TEXTURE_3D, OpenGL.GL_TEXTURE_WRAP_R, (int)OpenGL.GL_REPEAT);
            // pixel transfer happens here from client to OpenGL server
            OpenGL.PixelStorei(OpenGL.GL_UNPACK_ALIGNMENT, 1);
            OpenGL.TexImage3D(OpenGL.GL_TEXTURE_3D, 0, (int)OpenGL.GL_INTENSITY,
                width, height, depth, 0,
                OpenGL.GL_LUMINANCE, OpenGL.GL_UNSIGNED_BYTE, data.Header);
            data.Dispose();
            OpenGL.BindTexture(OpenGL.GL_TEXTURE_3D, 0);
        }