private void LoadBufferData(IDrawableShape shape)
 {
     shape.GetArraysforVbo(out _primitiveType, out _vertexT2fN3fV3Fs, out _indices);
     _vertexBuffer.PositionData  = shape.Vertices;
     _vertexBuffer.NormalsData   = shape.Normals;
     _vertexBuffer.IndicesData   = shape.Indices;
     _vertexBuffer.PrimitiveType = _primitiveType;
 }
        public void GenerateVertexBuffer(string name, IDrawableShape shape, int program)
        {
            LoadBufferData(shape);

            CalculateBufferSize();

            // Generate Vertex Buffer Object and bind it so it is current.
            GenerateVertexBuffer();

            SendVertexBufferData();

            // Generate Vertex Array Object and bind it so it is current.
            GenerateVertexArray();

            GetVertexArrayPointers(program);

            // IMPORTANT: vertex array needs unbinding here to avoid rendering incorrectly
            UnbindVertexArrayBuffer();

            _vertexBuffers.Add(name, _vertexBuffer);
        }
Beispiel #3
0
 public void Remove(IDrawableShape shape)
 {
 }
Beispiel #4
0
 public void Add(IDrawableShape shape)
 {
     shapes.Add(shape);
 }
        public void AddBufferObject(string name, IDrawableShape shape, int program)
        {
            BufferObject bufferObject = new BufferObject();

            //bufferObject.PositionData = new Vector3d[1];
            //bufferObject.NormalsData = new Vector3d[1];
            VertexT2fN3fV3f[] vertexData;
            uint[]            indices;
            PrimitiveType     type;

            shape.GetArraysforVBO(out type, out vertexData, out indices);

            bufferObject.PositionData  = shape.Vertices;
            bufferObject.NormalsData   = shape.Normals;
            bufferObject.IndicesData   = shape.Indices;
            bufferObject.PrimitiveType = type;

            int bufferHandle;

            #region Get sizes of buffer stores
            int sizeOfPositionData = Vector3.SizeInBytes * bufferObject.PositionData.Length;
            int sizeOfNormalsData  = Vector3.SizeInBytes * bufferObject.NormalsData.Length;
            //int sizeOfColorData = Marshal.SizeOf(new Color4()) * bufferObject.ColorData.Length;
            IntPtr bufferSize = new IntPtr(sizeOfPositionData + sizeOfNormalsData);
            IntPtr noOffset   = new IntPtr(0);
            #endregion

            // Generate Vertex Buffer Object and bind it so it is current.
            GL.GenBuffers(1, out bufferHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);

            #region Save pointers generated by OpenGL here so i dont forget.
            bufferObject.VboID = bufferHandle;
            #endregion

            #region Send all data to the Vertex Buffer
            // Initialise storage space for the Vertex Buffer.
            GL.BufferData(BufferTarget.ArrayBuffer, bufferSize, IntPtr.Zero, BufferUsageHint.StaticDraw);
            // Send Position data.
            GL.BufferSubData <Vector3>(
                BufferTarget.ArrayBuffer, noOffset, new IntPtr(sizeOfPositionData), bufferObject.PositionData);
            // Send Normals data, offset by size of Position data.
            GL.BufferSubData <Vector3>(
                BufferTarget.ArrayBuffer, new IntPtr(sizeOfPositionData), new IntPtr(sizeOfNormalsData), bufferObject.NormalsData);

            GL.GenBuffers(1, out bufferHandle);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferHandle);
            GL.BufferData(
                BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(uint) * bufferObject.IndicesData.Length), bufferObject.IndicesData, BufferUsageHint.StaticDraw);

            bufferObject.IboID = bufferHandle;

            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferObject.VboID);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferObject.IboID);
            #endregion

            // GL3 allows us to store the vertex layout in a "vertex array object" (VAO).
            // This means we do not have to re-issue VertexAttribPointer calls
            // every time we try to use a different vertex layout - these calls are
            // stored in the VAO so we simply need to bind the correct VAO.

            // Generate Vertex Array Object and bind it so it is current.
            GL.GenVertexArrays(1, out bufferHandle);
            GL.BindVertexArray(bufferHandle);

            bufferObject.VaoID = bufferHandle;

            bufferHandle = GL.GetAttribLocation(program, "in_position");
            GL.EnableVertexAttribArray(bufferHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferObject.VboID);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
            GL.BindAttribLocation(program, bufferHandle, "in_position");

            bufferHandle = GL.GetAttribLocation(program, "in_normal");
            GL.EnableVertexAttribArray(bufferHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferObject.VboID);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, sizeOfPositionData);
            GL.BindAttribLocation(program, bufferHandle, "in_normal");

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferObject.IboID);

            // IMPORTANT: vertex array needs unbinding here to avoid rendering incorrectly
            GL.BindVertexArray(0);

            m_bufferStore.Add(name, bufferObject);
        }