internal void Clean() { if (_dirty) { int maximumArrayIndex = 0; for (int i = 0; i < _attributes.Length; ++i) { VertexBufferAttribute attribute = _attributes[i].VertexBufferAttribute; if (_attributes[i].Dirty) { if (attribute != null) { Attach(i); } else { Detach(i); } _attributes[i].Dirty = false; } if (attribute != null) { maximumArrayIndex = Math.Max(NumberOfVertices(attribute) - 1, maximumArrayIndex); } } _dirty = false; _maximumArrayIndex = maximumArrayIndex; } }
private void CreateVertexArray(Context context) { // TODO: Hint per buffer? One hint? _positionBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, _billboards.Count * SizeInBytes <Vector3F> .Value); _colorBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, _billboards.Count * SizeInBytes <BlittableRGBA> .Value); _originBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, _billboards.Count); _pixelOffsetBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, _billboards.Count * SizeInBytes <Vector2H> .Value); _textureCoordinatesBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, _billboards.Count * SizeInBytes <Vector4H> .Value); VertexBufferAttribute positionAttribute = new VertexBufferAttribute( _positionBuffer, ComponentDatatype.Float, 3); VertexBufferAttribute colorAttribute = new VertexBufferAttribute( _colorBuffer, ComponentDatatype.UnsignedByte, 4, true, 0, 0); VertexBufferAttribute originAttribute = new VertexBufferAttribute( _originBuffer, ComponentDatatype.UnsignedByte, 1); VertexBufferAttribute pixelOffsetAttribute = new VertexBufferAttribute( _pixelOffsetBuffer, ComponentDatatype.HalfFloat, 2); VertexBufferAttribute textureCoordinatesAttribute = new VertexBufferAttribute( _textureCoordinatesBuffer, ComponentDatatype.HalfFloat, 4); ShaderProgram sp = _drawState.ShaderProgram; VertexArray va = context.CreateVertexArray(); va.Attributes[sp.VertexAttributes["position"].Location] = positionAttribute; va.Attributes[sp.VertexAttributes["textureCoordinates"].Location] = textureCoordinatesAttribute; va.Attributes[sp.VertexAttributes["color"].Location] = colorAttribute; va.Attributes[sp.VertexAttributes["origin"].Location] = originAttribute; va.Attributes[sp.VertexAttributes["pixelOffset"].Location] = pixelOffsetAttribute; _drawState.VertexArray = va; }
private void CreateVertexArray(Context context) { // TODO: Buffer hint. _positionBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, SizeInBytes <Vector2F> .Value); VertexBufferAttribute positionAttribute = new VertexBufferAttribute( _positionBuffer, ComponentDatatype.Float, 2); _drawState.VertexArray = context.CreateVertexArray(); _drawState.VertexArray.Attributes[_drawState.ShaderProgram.VertexAttributes["position"].Location] = positionAttribute; }
private void Attach(int index) { GL.EnableVertexAttribArray(index); VertexBufferAttribute attribute = _attributes[index].VertexBufferAttribute; VertexBufferGL3x bufferObjectGL = (VertexBufferGL3x)attribute.VertexBuffer; bufferObjectGL.Bind(); GL.VertexAttribPointer(index, attribute.NumberOfComponents, TypeConverterGL3x.To(attribute.ComponentDatatype), attribute.Normalize, attribute.StrideInBytes, attribute.OffsetInBytes); }
internal void Update(Context context, ShaderProgram sp) { if (_va == null) { VertexBufferAttribute positionAttribute = new VertexBufferAttribute( _positionBuffer, ComponentDatatype.Float, 2); VertexBufferAttribute textureCoordinatesAttribute = new VertexBufferAttribute( _textureCoordinatesBuffer, ComponentDatatype.HalfFloat, 2); _va = context.CreateVertexArray(); _va.Attributes[sp.VertexAttributes["position"].Location] = positionAttribute; _va.Attributes[sp.VertexAttributes["textureCoordinates"].Location] = textureCoordinatesAttribute; } if (_viewport != context.Viewport) { // // Bottom and top swapped: MS -> OpenGL // float left = context.Viewport.Left; float bottom = context.Viewport.Top; float right = context.Viewport.Right; float top = context.Viewport.Bottom; Vector2F[] positions = new Vector2F[] { new Vector2F(left, bottom), new Vector2F(right, bottom), new Vector2F(left, top), new Vector2F(right, top) }; _positionBuffer.CopyFromSystemMemory(positions); Vector2H[] textureCoordinates = new Vector2H[] { new Vector2H(0, 0), new Vector2H(1, 0), new Vector2H(0, 1), new Vector2H(1, 1) }; _textureCoordinatesBuffer.CopyFromSystemMemory(textureCoordinates); _viewport = context.Viewport; } }
private static int NumberOfVertices(VertexBufferAttribute attribute) { return(attribute.VertexBuffer.SizeInBytes / attribute.StrideInBytes); }
internal void Update(Context context, ShaderProgram sp) { if (_va == null) { VertexBufferAttribute positionAttribute = new VertexBufferAttribute( _positionBuffer, ComponentDatatype.Float, 2); VertexBufferAttribute textureCoordinatesAttribute = new VertexBufferAttribute( _textureCoordinatesBuffer, ComponentDatatype.HalfFloat, 2); _va = context.CreateVertexArray(); _va.Attributes[sp.VertexAttributes["position"].Location] = positionAttribute; _va.Attributes[sp.VertexAttributes["textureCoordinates"].Location] = textureCoordinatesAttribute; _va.IndexBuffer = _indexBuffer; } // 检测当前视口变换并重新计算视口矩形 if (_viewport != context.Viewport) { // // Bottom and top swapped: MS -> OpenGL // float left = context.Viewport.Left; float bottom = context.Viewport.Top; float right = context.Viewport.Right; float top = context.Viewport.Bottom; Vector2F[] positions = new Vector2F[] { // left eye verts new Vector2F(left, bottom), // -1, -1 new Vector2F((left + right) / 2.0f, bottom), // 0, -1 new Vector2F(left, top), // -1, 1 new Vector2F((left + right) / 2.0f, top), // 0, 1 // right eye verts new Vector2F((left + right) / 2.0f, bottom), // 0, -1 new Vector2F(right, bottom), // 1, -1 new Vector2F((left + right) / 2.0f, top), // 0, 1 new Vector2F(right, top) // 1, 1 }; _positionBuffer.CopyFromSystemMemory(positions); Vector2H[] textureCoordinates = new Vector2H[] { // left eye tex coord new Vector2H(0, 1), new Vector2H(1, 1), new Vector2H(0, 0), new Vector2H(1, 0), // right eye tex coord new Vector2H(0, 1), new Vector2H(1, 1), new Vector2H(0, 0), new Vector2H(1, 0), }; _textureCoordinatesBuffer.CopyFromSystemMemory(textureCoordinates); ushort[] index = new ushort[] { 0, 1, 3, 0, 3, 2, 4, 5, 7, 4, 7, 6 }; _indexBuffer.CopyFromSystemMemory(index); _viewport = context.Viewport; } }