/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <GlyphPosition>(varNameInShader, 2, OpenGL.GL_FLOAT, BufferUsage.DynamicDraw)) { buffer.Create(maxCharCount); positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strUV) { if (uvBufferPtr == null) { using (var buffer = new PropertyBuffer <GlyphTexCoord>(varNameInShader, 2, OpenGL.GL_FLOAT, BufferUsage.DynamicDraw)) { buffer.Create(maxCharCount); uvBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(uvBufferPtr); } else { throw new ArgumentException(); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(positions.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < positions.Length; i++) { array[i] = positions[i] / 2 * this.lengths; } } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else { throw new ArgumentException(); } }
protected override void DoInitialize() { // init shader program this.Program = PickingShaderHelper.GetPickingShaderProgram(); // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.NameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } propertyBufferPtrs[index++] = bufferPtr; } this.propertyBufferPtrs = propertyBufferPtrs; this.indexBufferPtr = this.bufferable.GetIndex(); foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.NameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } if (item.NameInIBufferable == positionNameInIBufferable) { this.positionBufferPtr = new PropertyBufferPtr( "in_Position",// in_Postion same with in the PickingShader.vert shader bufferPtr.BufferId, bufferPtr.DataSize, bufferPtr.DataType, bufferPtr.Length, bufferPtr.ByteLength); break; } } // 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 = this.indexBufferPtr as OneIndexBufferPtr; if (ptr != null) { GLSwitch glSwitch = new PrimitiveRestartSwitch(ptr); this.switchList.Add(glSwitch); } // 由于picking.vert/frag只支持vec3的position buffer,所以有此硬性规定。 if (this.positionBufferPtr.DataSize != 3 || this.positionBufferPtr.DataType != OpenGL.GL_FLOAT) { throw new Exception(string.Format("Position buffer must use a type composed of 3 float as PropertyBuffer<T>'s T!")); } }
protected override VertexBufferPtr Upload2GPU() { uint[] buffers = new uint[1]; GL.GetDelegateFor <GL.glGenBuffers>()(1, buffers); GL.GetDelegateFor <GL.glBindBuffer>()(GL.GL_ARRAY_BUFFER, buffers[0]); GL.GetDelegateFor <GL.glBufferData>()(GL.GL_ARRAY_BUFFER, this.ByteLength, this.Header, (uint)this.Usage); PropertyBufferPtr bufferPtr = new PropertyBufferPtr( this.VarNameInVertexShader, buffers[0], this.DataSize, this.DataType, this.Length, this.ByteLength); return(bufferPtr); }
protected override BufferPtr Upload2GPU() { uint[] buffers = new uint[1]; glGenBuffers(1, buffers); glBindBuffer(OpenGL.GL_ARRAY_BUFFER, buffers[0]); glBufferData(OpenGL.GL_ARRAY_BUFFER, this.ByteLength, this.Header, (uint)this.Usage); glBindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); PropertyBufferPtr bufferPtr = new PropertyBufferPtr( this.VarNameInVertexShader, buffers[0], this.DataSize, this.DataType, this.Length, this.ByteLength); return(bufferPtr); }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>( varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(this.model.positions.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < this.model.positions.Length; i++) { array[i] = this.model.positions[i]; } } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strColor) { if (colorBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>( varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(this.model.colors.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < this.model.colors.Length; i++) { array[i] = this.model.colors[i]; } } colorBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(colorBufferPtr); } else { throw new NotImplementedException(); } }
protected override void DoInitialize() { // init shader program this.shaderProgram = PickingShaderHelper.GetPickingShaderProgram(); // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.nameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } propertyBufferPtrs[index++] = bufferPtr; } this.propertyBufferPtrs = propertyBufferPtrs; this.indexBufferPtr = this.bufferable.GetIndex(); foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.nameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } if (item.nameInIBufferable == positionNameInIBufferable) { this.positionBufferPtr = new PropertyBufferPtr( "in_Position",// in_Postion same with in the PickingShader.vert shader bufferPtr.BufferId, bufferPtr.DataSize, bufferPtr.DataType, bufferPtr.Length, bufferPtr.ByteLength); break; } } // 由于picking.vert/frag只支持vec3的position buffer,所以有此硬性规定。 if (this.positionBufferPtr.DataSize != 3 || this.positionBufferPtr.DataType != OpenGL.GL_FLOAT) { throw new Exception(string.Format("Position buffer must use a type composed of 3 float as PropertyBuffer<T>'s T!")); } }
public uint GetVertexCount() { PropertyBufferPtr positionBufferPtr = this.positionBufferPtr; if (positionBufferPtr == null) { return(0); } int byteLength = positionBufferPtr.ByteLength; int vertexLength = positionBufferPtr.DataSize * positionBufferPtr.DataTypeByteLength; uint vertexCount = (uint)(byteLength / vertexLength); return(vertexCount); }
protected override void DoInitialize() { // init shader program ShaderProgram program = new ShaderProgram(); var shaders = (from item in shaderCode select item.CreateShader()).ToArray(); program.Create(shaders); this.shaderProgram = program; foreach (var item in shaders) { item.Delete(); } // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.nameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } propertyBufferPtrs[index++] = bufferPtr; if (item.nameInIBufferable == positionNameInIBufferable) { this.positionBufferPtr = new PropertyBufferPtr( "in_Position",// in_Postion same with in the PickingShader.vert shader bufferPtr.BufferID, bufferPtr.DataSize, bufferPtr.DataType, bufferPtr.Length, bufferPtr.ByteLength); } } this.propertyBufferPtrs = propertyBufferPtrs; // init index buffer object's renderer this.indexBufferPtr = this.bufferable.GetIndex(); this.bufferable = null; this.shaderCode = null; this.propertyNameMap = null; InitializeElementCount(); }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(model.positions.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < model.positions.Length; i++) { array[i] = model.positions[i]; } } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strTexCoord) { if (uvBufferPtr == null) { using (var buffer = new PropertyBuffer <vec2>(varNameInShader, 2, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(model.texCoords.Length); unsafe { var array = (vec2 *)buffer.Header.ToPointer(); for (int i = 0; i < model.texCoords.Length; i++) { array[i] = model.texCoords[i]; } } uvBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(uvBufferPtr); } else { return(null); } }
/// <summary> /// /// </summary> protected override void DoInitialize() { base.DoInitialize(); foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.NameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } if (item.NameInIBufferable == positionNameInIBufferable) { this.positionBufferPtr = new PropertyBufferPtr( "in_Position",// in_Postion same with in the PickingShader.vert shader bufferPtr.BufferId, bufferPtr.DataSize, bufferPtr.DataType, bufferPtr.Length, bufferPtr.ByteLength); break; } } // init index buffer { //IndexBufferPtr indexBufferPtr = this.bufferable.GetIndex(); using (var buffer = new OneIndexBuffer <uint>( this.indexBufferPtr.Mode, BufferUsage.DynamicDraw)) { buffer.Create(this.positionBufferPtr.ByteLength / (this.positionBufferPtr.DataSize * this.positionBufferPtr.DataTypeByteLength)); this.indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr; } } { var oneIndexBufferPtr = this.indexBufferPtr as OneIndexBufferPtr; this.maxElementCount = oneIndexBufferPtr.ElementCount; oneIndexBufferPtr.ElementCount = 0;// 高亮0个图元 } //this.bufferable = null; //this.shaderCodes = null; //this.propertyNameMap = null; }
protected override void DoInitialize() { // init shader program ShaderProgram program = PickingShaderHelper.GetPickingShaderProgram(); var shaders = (from item in shaderCodes select item.CreateShader()).ToArray(); program.Create(shaders); this.shaderProgram = program; foreach (var item in shaders) { item.Delete(); } // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { if (index <= 0) { if (item.nameInIBufferable == this.positionNameInIBufferable) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.nameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } propertyBufferPtrs[index++] = bufferPtr; this.positionBufferPtr = bufferPtr; } } else { if (item.nameInIBufferable == this.positionNameInIBufferable) { throw new Exception("More than 1 property named as position buffer~"); } } } this.propertyBufferPtrs = propertyBufferPtrs; this.indexBufferPtr = this.bufferable.GetIndex(); }
/// <summary> /// /// </summary> protected override void DoInitialize() { // init shader program var program = new ShaderProgram(); var shaders = (from item in shaderCodes select item.CreateShader()).ToArray(); program.Create(shaders); this.Program = program; foreach (var item in shaders) { item.Delete(); } // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.NameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(string.Format("[{0}] returns null buffer pointer!", this.bufferable)); } propertyBufferPtrs[index++] = bufferPtr; } this.propertyBufferPtrs = propertyBufferPtrs; this.indexBufferPtr = this.bufferable.GetIndex(); // 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 = this.indexBufferPtr as OneIndexBufferPtr; if (ptr != null) { GLSwitch glSwitch = new PrimitiveRestartSwitch(ptr); this.switchList.Add(glSwitch); } }
protected override void DoInitialize() { // init shader program ShaderProgram program = new ShaderProgram(); var shaders = (from item in shaderCodes select item.CreateShader()).ToArray(); program.Create(shaders); this.shaderProgram = program; foreach (var item in shaders) { item.Delete(); } // init property buffer objects var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { PropertyBufferPtr bufferPtr = this.bufferable.GetProperty( item.nameInIBufferable, item.VarNameInShader); if (bufferPtr == null) { throw new Exception(); } propertyBufferPtrs[index++] = bufferPtr; } this.propertyBufferPtrs = propertyBufferPtrs; this.indexBufferPtr = this.bufferable.GetIndex(); var ptr = this.indexBufferPtr as OneIndexBufferPtr; if (ptr != null) { var glSwitch = new PrimitiveRestartSwitch(ptr); this.switchList.Add(glSwitch); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <CubeModel.CubePosition>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(1); unsafe { var positionArray = (CubeModel.CubePosition *)buffer.Header.ToPointer(); positionArray[0] = CubeModel.position; } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strColor) { if (colorBufferPtr == null) { using (var buffer = new PropertyBuffer <CubeModel.CubeColor>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(1); unsafe { var colorArray = (CubeModel.CubeColor *)buffer.Header.ToPointer(); colorArray[0] = CubeModel.color; } colorBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(colorBufferPtr); } else if (bufferName == strNormal) { if (normalBufferPtr == null) { using (var buffer = new PropertyBuffer <CubeModel.CubeNormal>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(1); unsafe { var normalArray = (CubeModel.CubeNormal *)buffer.Header.ToPointer(); normalArray[0] = CubeModel.normal; } normalBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(normalBufferPtr); } else { return(null); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <float>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { float[] positions = model.GetPositions(); buffer.Create(positions.Length); unsafe { var array = (float *)buffer.Header.ToPointer(); for (int i = 0; i < positions.Length; i++) { array[i] = positions[i]; } } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strColor) { if (colorBufferPtr == null) { using (var buffer = new PropertyBuffer <float>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { float[] normals = model.GetNormals(); buffer.Create(normals.Length); unsafe { var array = (float *)buffer.Header.ToPointer(); for (int i = 0; i < normals.Length; i++) { array[i] = normals[i]; } } colorBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(colorBufferPtr); } else if (bufferName == strNormal) { if (normalBufferPtr == null) { using (var buffer = new PropertyBuffer <float>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { float[] normals = model.GetNormals(); buffer.Create(normals.Length); unsafe { var array = (float *)buffer.Header.ToPointer(); for (int i = 0; i < normals.Length; i++) { array[i] = normals[i]; } } normalBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(normalBufferPtr); } else { return(null); } }
/// <summary> /// /// </summary> /// <param name="bufferName"></param> /// <param name="varNameInShader"></param> /// <returns></returns> public PropertyBufferPtr GetProperty(string bufferName, string varNameInShader) { if (bufferName == strPosition) { if (positionBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(TetrahedronModel.position.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < TetrahedronModel.position.Length; i++) { array[i] = TetrahedronModel.position[i]; } } positionBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(positionBufferPtr); } else if (bufferName == strColor) { if (colorBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(TetrahedronModel.color.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < TetrahedronModel.color.Length; i++) { array[i] = TetrahedronModel.color[i]; } } colorBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(colorBufferPtr); } else if (bufferName == strNormal) { if (normalBufferPtr == null) { using (var buffer = new PropertyBuffer <vec3>(varNameInShader, 3, OpenGL.GL_FLOAT, BufferUsage.StaticDraw)) { buffer.Create(TetrahedronModel.normal.Length); unsafe { var array = (vec3 *)buffer.Header.ToPointer(); for (int i = 0; i < TetrahedronModel.normal.Length; i++) { array[i] = TetrahedronModel.normal[i]; } } normalBufferPtr = buffer.GetBufferPtr() as PropertyBufferPtr; } } return(normalBufferPtr); } else { return(null); } }