public void BindVBOs(OpenGL gl, LinesProgram program)
        {
            var attribPos = program.Attribs["Position"];
            gl.BindBuffer(_vboTarget, Position);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["ColorValue"];
            gl.BindBuffer(_vboTarget, ColorValue);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            if (IndicesCount > 0)
                gl.BindBuffer(_iboTarget, Ibo);
        }
        public void RenderWithStruct(SharpGL.OpenGL gl, SharpGL.SceneGraph.Core.RenderMode renderMode)
        {
            //if (!this.IsRenderable())
            //return;

            ShaderProgram shader = this.shader; //GetShader(gl, renderMode);

            shader.Bind(gl);

            // 用VAO+EBO进行渲染。
            //  Bind the out vertex array.
            //gl.BindVertexArray(vao[0]);
            gl.BindVertexArray(vertexArrayObject);
            //  Draw the square.
            //gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.triangleBufferObject);

            // 启用Primitive restart
            gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
            gl.PrimitiveRestartIndex(uint.MaxValue);// 截断图元(四边形带、三角形带等)的索引值。
            gl.DrawElements(this.primitiveMode, this.triangleIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);

            //  Unbind our vertex array and shader.
            gl.BindVertexArray(0);
            gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);

            shader.Unbind(gl);
        }
        private void DrawTrefoilBuffers(OpenGL gl)
        {
            //  Bind the vertex and index buffer.
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBuffer);
            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

            gl.EnableVertexAttribArray(attrPosition);
            gl.EnableVertexAttribArray(attrNormal);

            //  Draw the geometry, straight from the vertex buffer.
            gl.VertexAttribPointer(attrPosition, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Zero);
            int normalOffset = Marshal.SizeOf(typeof(Vertex));
            gl.VertexAttribPointer(attrNormal, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Add(new IntPtr(0), normalOffset));

            gl.DrawElements(OpenGL.GL_LINES, (int)trefoilKnot.IndexCount, OpenGL.GL_UNSIGNED_SHORT, IntPtr.Zero);

        }
示例#4
0
        public uint CreateVertexNormalBuffer(OpenGL gl)
        {
            Vertex[] verts = new Vertex[VertexCount * 2];
            int count = 0;

            float ds = 1.0f / Slices;
            float dt = 1.0f / Stacks;

            // The upper bounds in these loops are tweaked to reduce the
            // chance of precision error causing an incorrect # of iterations.

            for (float s = 0; s < 1 - ds / 2; s += ds)
            {
                for (float t = 0; t < 1 - dt / 2; t += dt)
                {
                    const float E = 0.01f;
                    Vertex p = EvaluateTrefoil(s, t);
                    Vertex u = EvaluateTrefoil(s + E, t) - p;
                    Vertex v = EvaluateTrefoil(s, t + E) - p;
                    Vertex n = u.VectorProduct(v);
                    n.Normalize();
                    verts[count++] = p;
                    verts[count++] = n;
                }
            }
            
            //  Pin the data.
            GCHandle vertsHandle = GCHandle.Alloc(verts, GCHandleType.Pinned);
            IntPtr vertsPtr = vertsHandle.AddrOfPinnedObject();
            var size = Marshal.SizeOf(typeof(Vertex)) * VertexCount;

            uint[] buffers = new uint[1];
            gl.GenBuffers(1, buffers);
            uint handle = buffers[0];
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, handle);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, size, vertsPtr, OpenGL.GL_STATIC_DRAW);

            //  Free the data.
            vertsHandle.Free();

            return handle;
        }
        /// <summary>
        /// Calls VertexBuffer.Bind(gl), IndexBuffer.Bind(gl) and Material.Bind(gl). 
        /// </summary>
        /// <param name="gl">The OpenGL</param>
        public void Bind(OpenGL gl)
        {
            //if (gl == null)
            //{
            //    throw new ArgumentNullException("OpenGL parameter cannot be null. Call 'GenerateGeometry(...)' before attempting to bind.");
            //}

            // Bind the vertex, normal and index buffers.
            if (VertexBuffer != null)
            {
                //Bind
                //VertexBuffer.BindBuffer(gl); //
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer.BufferId.Value);
                gl.VertexAttribPointer(VertexAttributes.Position, BufferStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(VertexAttributes.Position);
            }

            if (NormalBuffer != null)
            {
                //Bind
                //NormalBuffer.BindBuffer(gl); //
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBuffer.BufferId.Value);
                gl.VertexAttribPointer(VertexAttributes.Normal, BufferStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(VertexAttributes.Normal);
            }

            if (IndexBuffer != null)
            {
                //IndexBuffer.BindBuffer(gl); //
                gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, IndexBuffer.BufferId.Value);
            }
        }
        public void SetBufferData(OpenGL gl, IEnumerable<TransformationMatrix> transformations, OGLModelUsage usage = OGLModelUsage.StaticRead)
        {
            var transCount = transformations.Count();

            // Validation.
            if(transCount > Math.Pow(2, 24))
            {
                throw new OverflowException(
                    "This shader can't handle more than 2^24 transformations while "+
                    "using 24 bit colors.");
            }

            #region get indices
            var indices = new ushort[transCount];
            for (ushort i = 0; i < indices.Length; i++)
            {
                indices[i] = i; // Do all transformations once.
            }
            #endregion get indices

            #region get transformations array
            var stride = 16; // Transformation matrix is a 4x4 = 16.

            var transformationsArray = new float[transCount * stride];
            for (int i = 0; i < transCount; i++)
            {
                float[] transAsFloats = transformations.ElementAt(i).ResultMatrix.to_array();
                for (int j = 0; j < stride; j++)
                {
                    transformationsArray[i * stride + j] = transAsFloats[j];
                }
            }
            #endregion get transformations array

            #region get color array
            int colorStride = 3;
            var colorArray = new float[transCount * colorStride];

            for (int i = 0; i < transCount; i++)
            {
                ulong id = transformations.ElementAt(i).UniqueId;
                var color = new ColorF((uint) id);

                colorArray[i * stride] = color.R;
                colorArray[i * stride + 1] = color.G;
                colorArray[i * stride + 2] = color.B;
            }
            #endregion get color array

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _indicesBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, indices, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, transformationsArray, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, (uint)usage);
        }
        public void Bind(OpenGL gl)
        {
            // Bind the vertex, normal and index buffers.
            if (_transformationsBufferId != null)
            {
                var transStride = 16;

                //Bind
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value);
                gl.VertexAttribPointer(VertexAttributes.Position, transStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(VertexAttributes.Position);
            }

            if (_colorsBufferId != null)
            {
                var colStride = 3;
                //Bind
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value);
                gl.VertexAttribPointer(VertexAttributes.Normal, colStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(VertexAttributes.Normal);
            }

            if (_indicesBufferId != null)
            {
                gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, _indicesBufferId.Value);
            }
        }
示例#8
0
        private void PlaySound()
        {
            try
            {
                gl.GenVertexArrays(1, vao);
                gl.BindVertexArray(vao[0]);


                gl.GenBuffers(1, vbo);

                float[] vertices = new float[] {
                    -1.0f, 1.0f,
                    1.0f, 1.0f,
                    1.0f, -1.0f,
                    -1.0f, 1.0f,
                    -1.0f, -1.0f,
                    1.0f, -1.0f
                };
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vbo[0]);
                unsafe
                {
                    fixed(float *verts = vertices)
                    {
                        var ptr = new IntPtr(verts);

                        // fill the buffer to the currently binded buffer
                        gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices.Length * sizeof(float), ptr, OpenGL.GL_STATIC_DRAW);
                    }
                }
                //  Create the shader program.
                var vertexShaderSource   = ManifestResourceLoader.LoadTextFile("SoundShader.vert");
                var fragmentShaderSource = ManifestResourceLoader.LoadTextFile("SoundShader.frag");
                vertexShader = gl.CreateShader(OpenGL.GL_VERTEX_SHADER);
                gl.ShaderSource(vertexShader, vertexShaderSource);
                gl.CompileShader(vertexShader);

                // Create and compile the fragment shader
                fragmentShader = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER);
                gl.ShaderSource(fragmentShader, fragmentShaderSource);
                gl.CompileShader(fragmentShader);

                // Link the vertex and fragment shader into a shader program
                shaderProgram = gl.CreateProgram();
                gl.AttachShader(shaderProgram, vertexShader);
                gl.AttachShader(shaderProgram, fragmentShader);

                gl.BindFragDataLocation(shaderProgram, (uint)0, "gl_FragColor");

                gl.LinkProgram(shaderProgram);
                int[] infoLength = new int[] { 0 };
                int   bufSize    = infoLength[0];
                //  Get the compile info.
                StringBuilder il = new StringBuilder(bufSize);
                gl.GetProgramInfoLog(shaderProgram, bufSize, IntPtr.Zero, il);
                gl.UseProgram(shaderProgram);

                posAttrib = gl.GetAttribLocation(shaderProgram, "position");
                gl.EnableVertexAttribArray((uint)posAttrib);
                gl.VertexAttribPointer((uint)posAttrib, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));


                timeLoc   = gl.GetUniformLocation(shaderProgram, "iGlobalTime");
                sampleLoc = gl.GetUniformLocation(shaderProgram, "Spectrum");
                waveLoc   = gl.GetUniformLocation(shaderProgram, "Wavedata");
                int resLoc = gl.GetUniformLocation(shaderProgram, "iResolution");


                gl.Uniform3(resLoc, (float)this.openGLControl1.Width, (float)this.openGLControl1.Height, (float)(this.openGLControl1.Width * this.openGLControl1.Height));
                Thread thread = new Thread(() => PlaySoundT(((USound)selectedObject).SoundBuffer));
                thread.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace.ToString());
            }
        }
        /// <summary>
        /// 渲染基质
        /// </summary>
        /// <param name="gl"></param>
        /// <param name="renderMode"></param>
        private void DoRenderMatrix(OpenGL gl, RenderMode renderMode)
        {
            if (this.positionBuffer == null || this.colorBuffer == null) { return; }

            if (this.RenderGrid && this.matrixVertexArrayObject != null)
            {
                shaderProgram.SetUniform1(gl, "renderingWireframe", 0.0f);
                shaderProgram.SetUniform1(gl, "opacity", this.Opacity);

                gl.Enable(OpenGL.GL_POLYGON_OFFSET_FILL);
                gl.PolygonOffset(1.0f, 1.0f);

                gl.Enable(OpenGL.GL_BLEND);
                gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);

                gl.BindVertexArray(matrixVertexArrayObject[0]);

                switch (this.MatrixType)
                {
                    case MatrixFormat.Triangle:
                        gl.DrawArrays(this.matrixRenderMode, 0, this.MatrixVertexOrIndexCount);
                        break;
                    case MatrixFormat.Tetrahedron:
                        gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                        gl.PrimitiveRestartIndex(uint.MaxValue);

                        gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]);
                        gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                        gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);
                        break;
                    case MatrixFormat.TriangularPrism:
                        // 先渲染三棱柱的上下三角形
                        gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.MatrixVertexOrIndexCount);
                        // 再渲染三棱柱的三个侧面
                        gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                        gl.PrimitiveRestartIndex(uint.MaxValue);
                        gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]);
                        gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                        gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);
                        break;
                    default:
                        break;
                }
                gl.BindVertexArray(0);
                gl.Disable(OpenGL.GL_BLEND);

                gl.Disable(OpenGL.GL_POLYGON_OFFSET_FILL);
            }

            if (this.RenderGridWireframe && this.matrixVertexArrayObject != null)
            {
                shaderProgram.SetUniform1(gl, "renderingWireframe", 1.0f);
                shaderProgram.SetUniform1(gl, "opacity", this.Opacity);
                gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Lines);

                gl.Enable(OpenGL.GL_BLEND);
                gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);

                gl.BindVertexArray(matrixVertexArrayObject[0]);
                switch (this.MatrixType)
                {
                    case MatrixFormat.Triangle:
                        gl.DrawArrays(this.matrixRenderMode, 0, this.MatrixVertexOrIndexCount);
                        break;
                    case MatrixFormat.Tetrahedron:
                        gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                        gl.PrimitiveRestartIndex(uint.MaxValue);
                        gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]);
                        gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                        gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);
                        break;
                    case MatrixFormat.TriangularPrism:
                        // 先渲染三棱柱的上下三角形
                        gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, this.MatrixVertexOrIndexCount / 9 * 6);
                        // 再渲染三棱柱的三个侧面
                        gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                        gl.PrimitiveRestartIndex(uint.MaxValue);
                        gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, this.matrixIndexBuffer[0]);
                        gl.DrawElements(this.matrixRenderMode, this.MatrixVertexOrIndexCount, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                        gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);
                        break;
                    default:
                        break;
                }
                gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Filled);
                gl.BindVertexArray(0);

                gl.Disable(OpenGL.GL_BLEND);
            }
        }
示例#10
0
        public void BufferData(OpenGL gl,
            uint[] indices, float[] vertices, float[] colors)
        {
            if (indices != null)
            {
                gl.BindBuffer(_iboTarget, Ibo);
                gl.BufferData(_iboTarget, indices, _usage);
            }

            gl.BindBuffer(_vboTarget, Position);
            gl.BufferData(_vboTarget, vertices, _usage);

            gl.BindBuffer(_vboTarget, ColorValue);
            gl.BufferData(_vboTarget, colors, _usage);
        }
示例#11
0
        private void DrawTrefoilCelShaded(OpenGL gl)
        {
            //  Use the shader program.
            gl.UseProgram(shaderProgram.ProgramObject);

            //  Set the variables for the shader program.
            gl.Uniform3(toonUniforms.DiffuseMaterial, 0f, 0.75f, 0.75f);
            gl.Uniform3(toonUniforms.AmbientMaterial, 0.04f, 0.04f, 0.04f);
            gl.Uniform3(toonUniforms.SpecularMaterial, 0.5f, 0.5f, 0.5f);
            gl.Uniform1(toonUniforms.Shininess, 50f);

            //  Set the light position.
            gl.Uniform3(toonUniforms.LightPosition, 1, new float[4] { 0.25f, 0.25f, 1f, 0f });

            //  Set the matrices.
            gl.UniformMatrix4(toonUniforms.Projection, 1, false, projection.AsColumnMajorArrayFloat);
            gl.UniformMatrix4(toonUniforms.Modelview, 1, false, modelView.AsColumnMajorArrayFloat);
            gl.UniformMatrix3(toonUniforms.NormalMatrix, 1, false, normalMatrix.AsColumnMajorArrayFloat);

            //  Bind the vertex and index buffer.
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBuffer);
            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

            gl.EnableVertexAttribArray(attrPosition);
            gl.EnableVertexAttribArray(attrNormal);

            //  Draw the geometry, straight from the vertex buffer.
            gl.VertexAttribPointer(attrPosition, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Zero);
            int normalOffset = Marshal.SizeOf(typeof(Vertex));
            gl.VertexAttribPointer(attrNormal, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Add(new IntPtr(0), normalOffset));

            gl.DrawElements(OpenGL.GL_TRIANGLES, (int)trefoilKnot.IndexCount, OpenGL.GL_UNSIGNED_SHORT, IntPtr.Zero);
        }
示例#12
0
        void IRenderable.Render(OpenGL gl, RenderMode renderMode)
        {
            if (positionBuffer == null || colorBuffer == null) { return; }

            if (this.shaderProgram == null)
            {
                this.shaderProgram = InitShader(gl, renderMode);
            }
            if (this.vertexArrayObject == null)
            {
                CreateVertexArrayObject(gl, renderMode);
            }

            BeforeRendering(gl, renderMode);

            if (this.RenderGridWireframe && this.vertexArrayObject != null)
            {
                //if (wireframeIndexBuffer != null)
                if (positionBuffer != null && colorBuffer != null && indexBuffer != null)
                {
                    shaderProgram.SetUniform1(gl, "renderingWireframe", 1.0f);// shader一律上白色。

                    gl.Disable(OpenGL.GL_LINE_STIPPLE);
                    gl.Disable(OpenGL.GL_POLYGON_STIPPLE);
                    gl.Enable(OpenGL.GL_LINE_SMOOTH);
                    gl.Enable(OpenGL.GL_POLYGON_SMOOTH);
                    gl.ShadeModel(SharpGL.Enumerations.ShadeModel.Smooth);
                    gl.Hint(SharpGL.Enumerations.HintTarget.LineSmooth, SharpGL.Enumerations.HintMode.Nicest);
                    gl.Hint(SharpGL.Enumerations.HintTarget.PolygonSmooth, SharpGL.Enumerations.HintMode.Nicest);
                    gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Lines);

                    gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                    gl.PrimitiveRestartIndex(uint.MaxValue);

                    gl.Enable(OpenGL.GL_BLEND);
                    gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);

                    gl.BindVertexArray(this.vertexArrayObject[0]);
                    gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]);
                    gl.DrawElements(OpenGL.GL_QUAD_STRIP, this.indexBufferLength, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                    gl.BindVertexArray(0);

                    gl.Disable(OpenGL.GL_BLEND);

                    gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);

                    gl.PolygonMode(SharpGL.Enumerations.FaceMode.FrontAndBack, SharpGL.Enumerations.PolygonMode.Filled);
                    gl.Disable(OpenGL.GL_POLYGON_SMOOTH);
                }
            }

            if (this.RenderGrid && this.vertexArrayObject != null)
            {
                if (positionBuffer != null && colorBuffer != null && indexBuffer != null)
                {
                    shaderProgram.SetUniform1(gl, "renderingWireframe", 0.0f);// shader根据uv buffer来上色。

                    gl.Enable(OpenGL.GL_PRIMITIVE_RESTART);
                    gl.PrimitiveRestartIndex(uint.MaxValue);

                    gl.Enable(OpenGL.GL_BLEND);
                    gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);

                    gl.BindVertexArray(this.vertexArrayObject[0]);
                    gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]);
                    gl.DrawElements(OpenGL.GL_QUAD_STRIP, this.indexBufferLength, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero);
                    gl.BindVertexArray(0);

                    gl.Disable(OpenGL.GL_BLEND);

                    gl.Disable(OpenGL.GL_PRIMITIVE_RESTART);
                }
            }

            AfterRendering(gl, renderMode);
        }
示例#13
0
        private void CreateVertexArrayObject(OpenGL gl, RenderMode renderMode)
        {
            if (this.positionBuffer == null || this.colorBuffer == null) { return; }

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

            // prepare positions
            {
                int location = shaderProgram.GetAttributeLocation(gl, in_Position);
                ATTRIB_INDEX_POSITION = (uint)location;
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBuffer[0]);
                gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION);
            }
            // prepare colors
            {
                int location = shaderProgram.GetAttributeLocation(gl, in_uv);
                ATTRIB_INDEX_UV = (uint)location;
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, colorBuffer[0]);
                gl.VertexAttribPointer(ATTRIB_INDEX_UV, 1, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(ATTRIB_INDEX_UV);
            }

            gl.BindVertexArray(0);
        }
        /// <summary>
        /// Calls glBindBuffer.
        /// </summary>
        /// <param name="gl"></param>
        public virtual void BindBuffer(OpenGL gl, OGLBindBufferTarget target, VertexAttribPointer pointer = null)
        {
            gl.BindBuffer((uint)target, BufferId.Value);
            VertexAttribPointer pointerToUse = null;

            if (pointer != null)
                pointerToUse = pointer;
            else if (VertexAttribPointer != null)
                pointerToUse = VertexAttribPointer;

            if (pointerToUse != null)
            {
                pointerToUse.Invoke(gl);
                gl.EnableVertexAttribArray(pointerToUse.Index);
            }
        }
示例#15
0
        public void BufferData(OpenGL gl,
            uint[] indices, float[] vertices, float[] normals, 
            float[] ambs, float[] diffs, float[] specs, float[] shinis,
            float[] htColor)
        {
            gl.BindBuffer(_iboTarget, Ibo);
            gl.BufferData(_iboTarget, indices, _usage);

            gl.BindBuffer(_vboTarget, Position);
            gl.BufferData(_vboTarget, vertices, _usage);

            gl.BindBuffer(_vboTarget, Normal);
            gl.BufferData(_vboTarget, normals, _usage);

            gl.BindBuffer(_vboTarget, AmbientMaterial);
            gl.BufferData(_vboTarget, ambs, _usage);

            gl.BindBuffer(_vboTarget, DiffuseMaterial);
            gl.BufferData(_vboTarget, diffs, _usage);

            gl.BindBuffer(_vboTarget, SpecularMaterial);
            gl.BufferData(_vboTarget, specs, _usage);

            gl.BindBuffer(_vboTarget, ShininessValue);
            gl.BufferData(_vboTarget, shinis, _usage);

            gl.BindBuffer(_vboTarget, HTColorId);
            gl.BufferData(_vboTarget, htColor, _usage);
        }
示例#16
0
        public void BindNMVBOs(OpenGL gl, NormalMaterialProgram program)
        {
            var attribPos = program.Attribs["Position"];
            gl.BindBuffer(_vboTarget, Position);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["Normal"];
            gl.BindBuffer(_vboTarget, Normal);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["AmbientMaterial"];
            gl.BindBuffer(_vboTarget, AmbientMaterial);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["DiffuseMaterial"];
            gl.BindBuffer(_vboTarget, DiffuseMaterial);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["SpecularMaterial"];
            gl.BindBuffer(_vboTarget, SpecularMaterial);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["ShininessValue"];
            gl.BindBuffer(_vboTarget, ShininessValue);
            gl.VertexAttribPointer(attribPos, 1, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            gl.BindBuffer(_iboTarget, Ibo);
        }
示例#17
0
        public void BindHTVBOs(OpenGL gl, HitTestProgram program)
        {
            var attribPos = program.Attribs["Position"];
            gl.BindBuffer(_vboTarget, Position);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(attribPos);

            attribPos = program.Attribs["HTColorId"];
            gl.BindBuffer(_vboTarget, HTColorId);
            gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.VertexAttribDivisor(attribPos, 1);
            gl.EnableVertexAttribArray(attribPos);

            gl.BindBuffer(_iboTarget, Ibo);
        }
示例#18
0
        private uint CreateIndexBuffer(OpenGL gl)
        {
            ushort[] inds = new ushort[IndexCount];
            int count = 0;

            ushort n = 0;
            for (ushort i = 0; i < Slices; i++)
            {
                for (ushort j = 0; j < Stacks; j++)
                {
                    inds[count++] = (ushort)(n + j);
                    inds[count++] = (ushort)(n + (j + 1) % Stacks);
                    inds[count++] = (ushort)((n + j + Stacks) % VertexCount);

                    inds[count++] = (ushort)((n + j + Stacks) % VertexCount);
                    inds[count++] = (ushort)((n + (j + 1) % Stacks) % VertexCount);
                    inds[count++] = (ushort)((n + (j + 1) % Stacks + Stacks) % VertexCount);
                }

                n += (ushort)Stacks;
            }
            
            //  Pin the data.
            GCHandle indsHandle = GCHandle.Alloc(inds, GCHandleType.Pinned);
            IntPtr indsPtr = indsHandle.AddrOfPinnedObject();
            var size = Marshal.SizeOf(typeof(ushort)) * VertexCount;

            uint[] buffers = new uint[1];
            gl.GenBuffers(1, buffers);
            uint handle = buffers[0];
            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, handle);
            gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, (int)size, indsPtr, OpenGL.GL_STATIC_DRAW);

            //  Free the data.
            indsHandle.Free();

            return handle;
        }
示例#19
-29
 /// <summary>
 /// Function to draw the model
 /// </summary>
 private void drawModel(OpenGL gl) 
 {
     if(l_vboId != null)
     {
         gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
         gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);
         // itering over each list of points
         for(int k = 0; k < l_vboId.Count; k++)
         {
             gl.PushMatrix();
                 //transformations
                 gl.Scale(1.0f / f_scale, 1.0f / f_scale, 1.0f / f_scale);
                 gl.Translate(-v_center.X, -v_center.Y, -v_center.Z);
                 //vertexes
                 gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[k][0]);
                 gl.VertexPointer(3, OpenGL.GL_FLOAT, 0, BUFFER_OFFSET_ZERO);
                 //color
                 gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[k][1]);
                 gl.ColorPointer(3, OpenGL.GL_FLOAT, 0, BUFFER_OFFSET_ZERO);
                 //draw l_sizes[k] points
                 gl.DrawArrays(OpenGL.GL_POINTS, 0, l_sizes[k]);
             gl.PopMatrix();
         }
         gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
         gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);
         gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
     }
 }