protected override void DoInitialize() { base.DoInitialize(); this.positionBuffer = this.DataSource.GetVertexAttributeBuffer(QuadStripModel.position, null); this.texCoordBuffer = this.DataSource.GetVertexAttributeBuffer(QuadStripModel.texCoord, null); }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { int length = maxCharCount; VertexBuffer buffer = VertexBuffer.Create(typeof(GlyphPosition), length, VBOConfig.Vec2, varNameInShader, BufferUsage.DynamicDraw); this.positionBuffer = buffer; } return this.positionBuffer; } else if (bufferName == strUV) { if (this.uvBuffer == null) { int length = maxCharCount; VertexBuffer buffer = VertexBuffer.Create(typeof(GlyphTexCoord), length, VBOConfig.Vec2, varNameInShader, BufferUsage.DynamicDraw); this.uvBuffer = buffer; } return this.uvBuffer; } else { throw new ArgumentException(); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { //int length = model.positions.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < model.positions.Length; i++) // { // array[i] = model.positions[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; // another way to do this: this.positionBuffer = this.model.positions.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.positionBuffer; } else if (bufferName == strTexCoord) { if (this.uvBuffer == null) { //int length = model.texCoords.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec2), length, VBOConfig.Vec2, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec2*)pointer; // for (int i = 0; i < model.texCoords.Length; i++) // { // array[i] = model.texCoords[i]; // } // buffer.UnmapBuffer(); //} //this.uvBuffer = buffer; // another way to do this: this.uvBuffer = this.model.texCoords.GenVertexBuffer(VBOConfig.Vec2, varNameInShader, BufferUsage.StaticDraw); } return this.uvBuffer; } else { return null; } }
/// <summary> /// 生成顶点属性Buffer。描述顶点的位置或颜色或UV等各种属性。 /// <para>每个<see cref="VertexBuffer"/>仅描述其中一个属性。</para> /// <para>Vertex Buffer Object that describes vertex' property(position, color, uv coordinate, etc.).</para> /// <para>Each <see cref="VertexBuffer"/> describes only 1 property.</para> /// </summary> /// <param name="array"></param> /// <param name="config">This <paramref name="config"/> decides parameters' values in glVertexAttribPointer(attributeLocation, size, type, false, 0, IntPtr.Zero);</param> /// <param name="varNameInVertexShader">此顶点属性VBO对应于vertex shader中的哪个in变量?<para>Mapping variable's name in vertex shader.</para></param> /// <param name="usage"></param> /// <param name="instancedDivisor">0: not instanced. 1: instanced divisor is 1.</param> /// <param name="patchVertexes">How many vertexes makes a patch? No patch if <paramref name="patchVertexes"/> is 0.</param> /// <returns></returns> public static VertexBuffer GenVertexBuffer(this UnmanagedArrayBase array, VBOConfig config, string varNameInVertexShader, BufferUsage usage, uint instancedDivisor = 0, int patchVertexes = 0) { if (glGenBuffers == null) { InitFunctions(); } uint[] buffers = new uint[1]; glGenBuffers(1, buffers); const uint target = OpenGL.GL_ARRAY_BUFFER; glBindBuffer(target, buffers[0]); glBufferData(target, array.ByteLength, array.Header, (uint)usage); glBindBuffer(target, 0); var buffer = new VertexBuffer( buffers[0], config, varNameInVertexShader, array.Length, array.ByteLength, instancedDivisor, patchVertexes); return buffer; }
/// <summary> /// Creates a <see cref="VertexBuffer"/> object(actually an array) directly in server side(GPU) without initializing its value. /// </summary> /// <param name="elementType">element's type of this 'array'.</param> /// <param name="length">How many elements are there?</param> /// <param name="config">mapping to vertex shader's 'in' type.</param> /// <param name="varNameInVertexShader">mapping to vertex shader's 'in' name.</param> /// <param name="usage"></param> /// <param name="instanceDivisor"></param> /// <param name="patchVertexes"></param> /// <returns></returns> public static VertexBuffer Create(Type elementType, int length, VBOConfig config, string varNameInVertexShader, BufferUsage usage, uint instanceDivisor = 0, int patchVertexes = 0) { if (!elementType.IsValueType) { throw new ArgumentException(string.Format("{0} must be a value type!", elementType)); } if (glGenBuffers == null) { glGenBuffers = OpenGL.GetDelegateFor<OpenGL.glGenBuffers>(); } if (glBindBuffer == null) { glBindBuffer = OpenGL.GetDelegateFor<OpenGL.glBindBuffer>(); } if (glBufferData == null) { glBufferData = OpenGL.GetDelegateFor<OpenGL.glBufferData>(); } int byteLength = Marshal.SizeOf(elementType) * length; uint[] buffers = new uint[1]; glGenBuffers(1, buffers); const uint target = OpenGL.GL_ARRAY_BUFFER; glBindBuffer(target, buffers[0]); glBufferData(target, byteLength, IntPtr.Zero, (uint)usage); glBindBuffer(target, 0); var buffer = new VertexBuffer( buffers[0], config, varNameInVertexShader, length, byteLength, instanceDivisor, patchVertexes); return buffer; }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public IEnumerable <VertexBuffer> GetVertexAttribute(string bufferName) { if (bufferName == strPosition) { if (this.positionBuffer == null) { //int length = model.positions.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < model.positions.Length; i++) // { // array[i] = model.positions[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; // another way to do this: this.positionBuffer = this.model.positions.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw); } yield return(this.positionBuffer); } else if (bufferName == strNormal) { if (this.normalBuffer == null) { //int length = model.normals.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < model.normals.Length; i++) // { // array[i] = model.normals[i]; // } // buffer.UnmapBuffer(); //} //this.normalBuffer = buffer; // another way to do this: this.normalBuffer = this.model.normals.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw); } yield return(this.normalBuffer); } else if (bufferName == strColor) { if (this.colorBuffer == null) { //int length = model.colors.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < model.colors.Length; i++) // { // array[i] = model.colors[i]; // } // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; // another way to do this: this.colorBuffer = this.model.colors.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw); } yield return(this.colorBuffer); } else if (bufferName == strUV) { if (this.uvBuffer == null) { //int length = model.uv.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec2), length, VBOConfig.Vec2, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec2*)pointer; // for (int i = 0; i < model.uv.Length; i++) // { // array[i] = model.uv[i]; // } // buffer.UnmapBuffer(); //} //this.uvBuffer = buffer; // another way to do this: this.uvBuffer = model.uv.GenVertexBuffer(VBOConfig.Vec2, BufferUsage.StaticDraw); } yield return(this.uvBuffer); } else { throw new ArgumentException(); } }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> /// <param name="totalWidth"></param> /// <param name="totalHeight"></param> unsafe private void PositionPass(string text, GlyphServer server, out float totalWidth, out float totalHeight) { int textureWidth = server.TextureWidth; int textureHeight = server.TextureHeight; VertexBuffer buffer = this.positionBuffer; var positionArray = (QuadPositionStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite); const float height = 2.0f; // let's say height is 2.0f. totalWidth = 0; totalHeight = height; int index = 0; foreach (var c in text) { if (index >= this.labelModel.Capacity) { break; } GlyphInfo glyphInfo; float wByH = 0; if (server.GetGlyphInfo(c, out glyphInfo)) { float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth; float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight; wByH = height * w / h; } else { // put an empty glyph(square) here. wByH = height * 1.0f / 1.0f; } var leftTop = new vec2(totalWidth, height / 2); var leftBottom = new vec2(totalWidth, -height / 2); var rightBottom = new vec2(totalWidth + wByH, -height / 2); var rightTop = new vec2(totalWidth + wByH, height / 2); positionArray[index++] = new QuadPositionStruct(leftTop, leftBottom, rightBottom, rightTop); totalWidth += wByH; } // move to center. const float scale = 1f; for (int i = 0; i < text.Length; i++) { if (i >= this.labelModel.Capacity) { break; } QuadPositionStruct quad = positionArray[i]; var newPos = new QuadPositionStruct( // y is already in [-1, 1], so just shrink x to [-1, 1] new vec2(quad.leftTop.x / totalWidth * 2.0f - 1f, quad.leftTop.y) * scale, new vec2(quad.leftBottom.x / totalWidth * 2.0f - 1f, quad.leftBottom.y) * scale, new vec2(quad.rightBottom.x / totalWidth * 2.0f - 1f, quad.rightBottom.y) * scale, new vec2(quad.rightTop.x / totalWidth * 2.0f - 1f, quad.rightTop.y) * scale ); positionArray[i] = newPos; } buffer.UnmapBuffer(); }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { //int length = TetrahedronModel.position.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.position.Length; i++) // { // array[i] = TetrahedronModel.position[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; this.positionBuffer = TetrahedronModel.position.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return(this.positionBuffer); } else if (bufferName == strColor) { if (this.colorBuffer == null) { //int length = TetrahedronModel.color.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.color.Length; i++) // { // array[i] = TetrahedronModel.color[i]; // } // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; this.colorBuffer = TetrahedronModel.color.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return(this.colorBuffer); } else if (bufferName == strNormal) { if (this.normalBuffer == null) { // int length = TetrahedronModel.normal.Length; // VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); // unsafe // { // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.normal.Length; i++) // { // array[i] = TetrahedronModel.normal[i]; // } // buffer.UnmapBuffer(); // } // this.normalBuffer = buffer; this.normalBuffer = TetrahedronModel.normal.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return(this.normalBuffer); } else { return(null); } }
protected override void DoInitialize() { base.DoInitialize(); this.positionBuffer = this.DataSource.GetVertexAttributeBuffer(QuadStripColoredModel.position, null); this.colorBuffer = this.DataSource.GetVertexAttributeBuffer(QuadStripColoredModel.color, null); }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { //int length = TetrahedronModel.position.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.position.Length; i++) // { // array[i] = TetrahedronModel.position[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; this.positionBuffer = TetrahedronModel.position.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.positionBuffer; } else if (bufferName == strColor) { if (this.colorBuffer == null) { //int length = TetrahedronModel.color.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.color.Length; i++) // { // array[i] = TetrahedronModel.color[i]; // } // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; this.colorBuffer = TetrahedronModel.color.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.colorBuffer; } else if (bufferName == strNormal) { if (this.normalBuffer == null) { // int length = TetrahedronModel.normal.Length; // VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); // unsafe // { // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer; // for (int i = 0; i < TetrahedronModel.normal.Length; i++) // { // array[i] = TetrahedronModel.normal[i]; // } // buffer.UnmapBuffer(); // } // this.normalBuffer = buffer; this.normalBuffer = TetrahedronModel.normal.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.normalBuffer; } else { return null; } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { int length = 1; VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubePosition), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.ReadWrite); { var array = (CubeModel.CubePosition*)pointer; array[0] = CubeModel.position; } { var array = (vec3*)pointer; for (int i = 0; i < 24; i++) { array[i] = array[i] / 2 * Lengths; } } buffer.UnmapBuffer(); } this.positionBuffer = buffer; } return this.positionBuffer; } else if (bufferName == strColor) { if (this.colorBuffer == null) { //int length = 1; //VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubeColor), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (CubeModel.CubeColor*)pointer; // array[0] = CubeModel.color; // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; // another way to do this: this.colorBuffer = CubeModel.color.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.colorBuffer; } else if (bufferName == strNormal) { if (this.normalBuffer == null) { //int length = 1; //VertexBuffer buffer = VertexBuffer.Create(typeof(CubeModel.CubeNormal), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (CubeModel.CubeNormal*)pointer; // array[0] = CubeModel.normal; // buffer.UnmapBuffer(); //} //this.normalBuffer = buffer; // another way to do this: this.normalBuffer = CubeModel.normal.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.normalBuffer; } else { return null; } }
/// <summary> /// Get picked geometry from a <see cref="PickableNode"/> with <see cref="DrawArraysCmd"/> as index buffer. /// </summary> /// <param name="node"></param> /// <param name="positionBuffer"></param> /// <param name="drawCommand"></param> public DrawArraysPicker(PickableNode node, VertexBuffer positionBuffer, IDrawCommand drawCommand) : base(node, positionBuffer, drawCommand) { }
/// <summary> /// Get picked geometry. /// </summary> /// <param name="node"></param> /// <param name="positionBuffer"></param> /// <param name="drawCommand"></param> public PickerBase(PickableNode node, VertexBuffer positionBuffer, IDrawCommand drawCommand) { this.Node = node; this.PositionBuffer = positionBuffer; this.DrawCommand = drawCommand; }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == position) { if (this.positionBuffer == null) { int length = this.markerCount * 2; VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); var array = (vec3*)pointer; for (int i = 0; i < this.markerCount; i++) { array[i * 2 + 0] = new vec3(-0.5f + (float)i / (float)(this.markerCount - 1), 0.5f, 0); array[i * 2 + 1] = new vec3(-0.5f + (float)i / (float)(this.markerCount - 1), -0.5f, 0); } buffer.UnmapBuffer(); } this.positionBuffer = buffer; } return this.positionBuffer; } else { throw new NotImplementedException(); } }
/// <summary> /// A smallest unit that can render somthing. /// </summary> /// <param name="program"></param> /// <param name="vao"></param> /// <param name="positionBuffer"></param> /// <param name="states"></param> public IPickableRenderMethod(ShaderProgram program, VertexArrayObject vao, VertexBuffer positionBuffer, params GLState[] states) : base(program, vao, states) { this.PositionBuffer = positionBuffer; }
public unsafe VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBuffer != null) { return positionBuffer; } int length = (faceCount * 2 + 2) * (pipeline.Count - 1); VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); var array = (vec3*)pointer.ToPointer(); int index = 0; var max = new vec3(float.MinValue, float.MinValue, float.MinValue); var min = new vec3(float.MaxValue, float.MaxValue, float.MaxValue); for (int i = 1; i < pipeline.Count; i++) { vec3 p1 = pipeline[i - 1]; vec3 p2 = pipeline[i]; vec3 vector = p2 - p1;// 从p1到p2的向量 // 找到互相垂直的三个向量:vector, orthogontalVector1和orthogontalVector2 vec3 orthogontalVector1 = new vec3(vector.y - vector.z, vector.z - vector.x, vector.x - vector.y); vec3 orthogontalVector2 = vector.cross(orthogontalVector1); orthogontalVector1 = orthogontalVector1.normalize() * (float)Math.Sqrt(this.radius); orthogontalVector2 = orthogontalVector2.normalize() * (float)Math.Sqrt(this.radius); for (int faceIndex = 0; faceIndex < faceCount + 1; faceIndex++) { double angle = (Math.PI * 2 * faceIndex) / faceCount; vec3 delta = orthogontalVector1 * (float)Math.Cos(angle) + orthogontalVector2 * (float)Math.Sin(angle); vec3 tmp1 = p1 + delta, tmp2 = p2 + delta; tmp1.UpdateMax(ref max); tmp1.UpdateMin(ref min); tmp2.UpdateMax(ref max); tmp2.UpdateMin(ref min); array[index++] = tmp1; array[index++] = tmp2; } } buffer.UnmapBuffer(); this.ModelSize = max - min; this.positionBuffer = buffer; } return this.positionBuffer; } else if (bufferName == strBrightness) { if (brightnessBuffer != null) { return brightnessBuffer; } int length = (faceCount * 2 + 2) * (pipeline.Count - 1); VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); var array = (vec3*)pointer.ToPointer(); var random = new Random(); for (int i = 0; i < buffer.Length; i++) { var x = (float)(random.NextDouble() * 0.5 + 0.5); array[i] = new vec3(x, x, x); } buffer.UnmapBuffer(); } this.brightnessBuffer = buffer; return this.brightnessBuffer; } else { throw new ArgumentException(); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { float[] positions = model.GetPositions(); //int length = positions.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(float), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (float*)pointer; // for (int i = 0; i < positions.Length; i++) // { // array[i] = positions[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; // another way to do this: this.positionBuffer = positions.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.positionBuffer; } else if (bufferName == strColor) { if (this.colorBuffer == null) { float[] normals = model.GetNormals(); for (int i = 0; i < normals.Length; i++) { if (normals[i] < 0) { normals[i] = -normals[i]; } } //int length = normals.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(float), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (float*)pointer; // for (int i = 0; i < normals.Length; i++) // { // array[i] = normals[i]; // } // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; // another way to do this: this.colorBuffer = normals.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.colorBuffer; } else if (bufferName == strNormal) { if (this.normalBuffer == null) { float[] normals = model.GetNormals(); //int length = normals.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(float), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (float*)pointer; // for (int i = 0; i < normals.Length; i++) // { // array[i] = normals[i]; // } // buffer.UnmapBuffer(); //} //this.normalBuffer = buffer; // another way to do this: this.normalBuffer = normals.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.normalBuffer; } else { return null; } }
/// <summary> /// Get picked geometry from a <see cref="PickableNode"/> with <see cref="DrawArraysCmd"/> as index buffer. /// </summary> /// <param name="node"></param> /// <param name="positionBuffer"></param> /// <param name="drawCommand"></param> public DrawElementsPicker(PickableNode node, VertexBuffer positionBuffer, DrawElementsCmd drawCommand) : base(node, positionBuffer) { this.DrawCommand = drawCommand; }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (this.positionBuffer == null) { var array = new vec3[positions.Length]; for (int i = 0; i < positions.Length; i++) { array[i] = positions[i] / 2 * this.lengths; } VertexBuffer buffer = array.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); this.positionBuffer = buffer; } return this.positionBuffer; } else { throw new ArgumentException(); } }
/// <summary> /// /// </summary> protected override void DoInitialize() { // init shader program. ShaderProgram program = this.renderProgramProvider.GetShaderProgram(); ShaderProgram pickProgram = this.pickProgramProvider.GetShaderProgram(); // init vertex attribute buffer objects. IBufferable model = this.DataSource; VertexBuffer positionBuffer = null; VertexShaderAttribute[] vertexAttributeBuffers; { var list = new List <VertexShaderAttribute>(); foreach (AttributeMap.NamePair item in this.attributeMap) { VertexBuffer buffer = model.GetVertexAttributeBuffer( item.NameInIBufferable, item.VarNameInShader); if (buffer == null) { throw new Exception(string.Format("[{0}] returns null buffer pointer!", model)); } list.Add(new VertexShaderAttribute(buffer, item.VarNameInShader)); if (item.VarNameInShader == this.PositionNameInVertexShader) { positionBuffer = buffer; } } vertexAttributeBuffers = list.ToArray(); } // 由于picking.vert/frag只支持vec3的position buffer,所以有此硬性规定。 if (positionBuffer == null || positionBuffer.Config != VBOConfig.Vec3) { throw new Exception(string.Format("Position buffer must use a type composed of 3 float as PropertyBuffer<T>'s T!")); } // init index buffer. IndexBuffer indexBuffer = model.GetIndexBuffer(); // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care this rule when designing a model's index buffer. var ptr = indexBuffer as OneIndexBuffer; if (ptr != null) { GLState glState = new PrimitiveRestartState(ptr.ElementType); this.stateList.Add(glState); this.picker = new OneIndexPicker(this); } else { this.picker = new ZeroIndexPicker(this); } // init VAO. var vertexArrayObject = new VertexArrayObject(indexBuffer, vertexAttributeBuffers); vertexArrayObject.Initialize(program); var pickingVAO = new VertexArrayObject(indexBuffer, new VertexShaderAttribute(positionBuffer, "in_Position")); pickingVAO.Initialize(pickProgram); // sets fields. this.RenderProgram = program; this.PickProgram = pickProgram; this.vertexShaderAttributes = vertexAttributeBuffers; this.positionBuffer = positionBuffer; this.indexBuffer = indexBuffer; this.vertexArrayObject = vertexArrayObject; this.pickVertexArrayObject = pickingVAO; }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == position) { if (this.positionBuffer == null) { //int length = BigDipperModel.positions.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer.ToPointer(); // for (int i = 0; i < BigDipperModel.positions.Length; i++) // { // array[i] = BigDipperModel.positions[i]; // } // buffer.UnmapBuffer(); //} //this.positionBuffer = buffer; // another way to do this: this.positionBuffer = BigDipperModel.positions.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.positionBuffer; } else if (bufferName == color) { if (this.colorBuffer == null) { //int length = BigDipperModel.colors.Length; //VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); //unsafe //{ // IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); // var array = (vec3*)pointer.ToPointer(); // for (int i = 0; i < BigDipperModel.colors.Length; i++) // { // array[i] = BigDipperModel.colors[i]; // } // buffer.UnmapBuffer(); //} //this.colorBuffer = buffer; // another way to do this: this.colorBuffer = BigDipperModel.colors.GenVertexBuffer(VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); } return this.colorBuffer; } else { throw new NotImplementedException(); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public VertexBuffer GetVertexAttributeBuffer(string bufferName, string varNameInShader) { if (bufferName == position) { if (this.positionBuffer == null) { int length = (this.quadCount + 1) * 2; VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); var array = (vec3*)pointer; for (int i = 0; i < (this.quadCount + 1); i++) { array[i * 2 + 0] = new vec3(-0.5f + (float)i / (float)(this.quadCount), 0.5f, 0); array[i * 2 + 1] = new vec3(-0.5f + (float)i / (float)(this.quadCount), -0.5f, 0); } buffer.UnmapBuffer(); } this.positionBuffer = buffer; } return positionBuffer; } else if (bufferName == color) { if (this.colorBuffer == null) { int length = (this.quadCount + 1) * 2; VertexBuffer buffer = VertexBuffer.Create(typeof(vec3), length, VBOConfig.Vec3, varNameInShader, BufferUsage.StaticDraw); unsafe { IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly); var array = (vec3*)pointer; for (int i = 0; i < (this.quadCount + 1); i++) { int x = this.bitmap.Width * i / this.quadCount; if (x == this.bitmap.Width) { x = this.bitmap.Width - 1; } vec3 value = this.bitmap.GetPixel(x, 0).ToVec3(); array[i * 2 + 0] = value; array[i * 2 + 1] = value; } buffer.UnmapBuffer(); } this.colorBuffer = buffer; } return colorBuffer; } else { throw new NotImplementedException(); } }