public void SetTriangles(IMeshImp mr, short[] triangleIndices) { if (triangleIndices == null || triangleIndices.Length == 0) { throw new ArgumentException("triangleIndices must not be null or empty"); } ((MeshImp)mr).NElements = triangleIndices.Length; int vboBytes; int trisBytes = triangleIndices.Length * sizeof(short); if (((MeshImp)mr).ElementBufferObject == 0) { GL.GenBuffers(1, out ((MeshImp)mr).ElementBufferObject); } // Upload the index buffer (elements inside the vertex buffer, not color indices as per the IndexPointer function!) GL.BindBuffer(BufferTarget.ElementArrayBuffer, ((MeshImp)mr).ElementBufferObject); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(trisBytes), triangleIndices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != trisBytes) { throw new ApplicationException(String.Format( "Problem uploading vertex buffer to VBO (offsets). Tried to upload {0} bytes, uploaded {1}.", trisBytes, vboBytes)); } GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void SetUVs(IMeshImp mr, float2[] uvs) { if (uvs == null || uvs.Length == 0) { throw new ArgumentException("UVs must not be null or empty"); } int vboBytes; int uvsBytes = uvs.Length * 2 * sizeof(float); if (((MeshImp)mr).UVBufferObject == 0) { GL.GenBuffers(1, out ((MeshImp)mr).UVBufferObject); } GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).UVBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(uvsBytes), uvs, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != uvsBytes) { throw new ApplicationException(String.Format( "Problem uploading uv buffer to VBO (uvs). Tried to upload {0} bytes, uploaded {1}.", uvsBytes, vboBytes)); } GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void SetColors(IMeshImp mr, uint[] colors) { if (colors == null || colors.Length == 0) { throw new ArgumentException("colors must not be null or empty"); } int vboBytes; int colsBytes = colors.Length * sizeof(uint); if (((MeshImp)mr).ColorBufferObject == 0) { GL.GenBuffers(1, out ((MeshImp)mr).ColorBufferObject); } GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).ColorBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(colsBytes), colors, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != colsBytes) { throw new ApplicationException(String.Format( "Problem uploading color buffer to VBO (colors). Tried to upload {0} bytes, uploaded {1}.", colsBytes, vboBytes)); } GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void SetNormals(IMeshImp mr, float3[] normals) { if (normals == null || normals.Length == 0) { throw new ArgumentException("Normals must not be null or empty"); } int vboBytes; int normsBytes = normals.Length * 3 * sizeof(float); if (((MeshImp)mr).NormalBufferObject == 0) { GL.GenBuffers(1, out ((MeshImp)mr).NormalBufferObject); } GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).NormalBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(normsBytes), normals, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != normsBytes) { throw new ApplicationException(String.Format( "Problem uploading normal buffer to VBO (normals). Tried to upload {0} bytes, uploaded {1}.", normsBytes, vboBytes)); } GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void Render(IMeshImp mr) { if (((MeshImp)mr).VertexBufferObject != 0) { GL.EnableVertexAttribArray(Helper.VertexAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).VertexBufferObject); GL.VertexAttribPointer(Helper.VertexAttribLocation, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp)mr).ColorBufferObject != 0) { GL.EnableVertexAttribArray(Helper.ColorAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).ColorBufferObject); GL.VertexAttribPointer(Helper.ColorAttribLocation, 4, VertexAttribPointerType.UnsignedByte, true, 0, IntPtr.Zero); } if (((MeshImp)mr).UVBufferObject != 0) { GL.EnableVertexAttribArray(Helper.UvAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).UVBufferObject); GL.VertexAttribPointer(Helper.UvAttribLocation, 2, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp)mr).NormalBufferObject != 0) { GL.EnableVertexAttribArray(Helper.NormalAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).NormalBufferObject); GL.VertexAttribPointer(Helper.NormalAttribLocation, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp)mr).ElementBufferObject != 0) { GL.BindBuffer(BufferTarget.ElementArrayBuffer, ((MeshImp)mr).ElementBufferObject); GL.DrawElements(BeginMode.Triangles, ((MeshImp)mr).NElements, DrawElementsType.UnsignedShort, IntPtr.Zero); //GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length); } if (((MeshImp)mr).VertexBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.VertexAttribLocation); } if (((MeshImp)mr).ColorBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.ColorAttribLocation); } if (((MeshImp)mr).NormalBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.NormalAttribLocation); } if (((MeshImp)mr).UVBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.UvAttribLocation); } }
/// <summary> /// Call this method on the mainthread after RenderContext.Render in order to cleanup all not used Buffers from GPU memory. /// </summary> public void Cleanup() { if (_toBeDeletedMeshImps == null || _toBeDeletedMeshImps.Count == 0) { return; } while (_toBeDeletedMeshImps.Count > 0) { IMeshImp tobeDeletedMeshImp = _toBeDeletedMeshImps.Pop(); Remove(tobeDeletedMeshImp); } }
private void Remove(IMeshImp meshImp) { if (meshImp.VerticesSet) { _renderContextImp.RemoveVertices(meshImp); } if (meshImp.NormalsSet) { _renderContextImp.RemoveNormals(meshImp); } if (meshImp.ColorsSet) { _renderContextImp.RemoveColors(meshImp); } if (meshImp.UVsSet) { _renderContextImp.RemoveUVs(meshImp); } if (meshImp.TrianglesSet) { _renderContextImp.RemoveTriangles(meshImp); } if (meshImp.BoneWeightsSet) { _renderContextImp.RemoveBoneWeights(meshImp); } if (meshImp.BoneIndicesSet) { _renderContextImp.RemoveBoneIndices(meshImp); } if (meshImp.TangentsSet) { _renderContextImp.RemoveTangents(meshImp); } if (meshImp.BiTangentsSet) { _renderContextImp.RemoveBiTangents(meshImp); } // Force collection GC.Collect(); }
public void SetTangents(IMeshImp mr, float4[] tangents) { throw new NotImplementedException(); }
public void SetBiTangents(IMeshImp mr, float3[] bitangents) { throw new NotImplementedException(); }
/* Not using tangent space normal maps at the moment public void SetVertexData(IMeshImp mr, float3[] vertices, float2[] uvs, float3[] normals) { if (vertices == null || vertices.Length == 0) { throw new ArgumentException("Vertices must not be null or empty"); } int vboBytes; int vertsBytes = vertices.Length * 3 * sizeof(float); if (((MeshImp)mr).VertexBufferObject == 0) GL.GenBuffers(1, out ((MeshImp)mr).VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).VertexBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertsBytes), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != vertsBytes) throw new ApplicationException(String.Format( "Problem uploading vertex buffer to VBO (vertices). Tried to upload {0} bytes, uploaded {1}.", vertsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // normals if (normals == null || normals.Length == 0) { throw new ArgumentException("Normals must not be null or empty"); } int normsBytes = normals.Length * 3 * sizeof(float); if (((MeshImp)mr).NormalBufferObject == 0) GL.GenBuffers(1, out ((MeshImp)mr).NormalBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).NormalBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(normsBytes), normals, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != normsBytes) throw new ApplicationException(String.Format( "Problem uploading normal buffer to VBO (normals). Tried to upload {0} bytes, uploaded {1}.", normsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // UVs if (uvs == null || uvs.Length == 0) { throw new ArgumentException("UVs must not be null or empty"); } int uvsBytes = uvs.Length * 2 * sizeof(float); if (((MeshImp)mr).UVBufferObject == 0) GL.GenBuffers(1, out ((MeshImp)mr).UVBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp)mr).UVBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(uvsBytes), uvs, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != uvsBytes) throw new ApplicationException(String.Format( "Problem uploading uv buffer to VBO (uvs). Tried to upload {0} bytes, uploaded {1}.", uvsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // Generate Tangents and Bitangents } */ /// <summary> /// Binds the colors onto the GL Rendercontext and assigns an ColorBuffer index to the passed <see cref="IMeshImp" /> instance. /// </summary> /// <param name="mr">The <see cref="IMeshImp" /> instance.</param> /// <param name="colors">The colors.</param> /// <exception cref="System.ArgumentException">colors must not be null or empty</exception> /// <exception cref="System.ApplicationException"></exception> public void SetColors(IMeshImp mr, uint[] colors) { if (colors == null || colors.Length == 0) { throw new ArgumentException("colors must not be null or empty"); } int vboBytes; int colsBytes = colors.Length*sizeof (uint); if (((MeshImp) mr).ColorBufferObject == 0) GL.GenBuffers(1, out ((MeshImp) mr).ColorBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).ColorBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (colsBytes), colors, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != colsBytes) throw new ApplicationException(String.Format( "Problem uploading color buffer to VBO (colors). Tried to upload {0} bytes, uploaded {1}.", colsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
/// <summary> /// Binds the UV coordinates onto the GL Rendercontext and assigns an UVBuffer index to the passed <see cref="IMeshImp" /> instance. /// </summary> /// <param name="mr">The <see cref="IMeshImp" /> instance.</param> /// <param name="uvs">The UV's.</param> /// <exception cref="System.ArgumentException">UVs must not be null or empty</exception> /// <exception cref="System.ApplicationException"></exception> public void SetUVs(IMeshImp mr, float2[] uvs) { if (uvs == null || uvs.Length == 0) { throw new ArgumentException("UVs must not be null or empty"); } int vboBytes; int uvsBytes = uvs.Length*2*sizeof (float); if (((MeshImp) mr).UVBufferObject == 0) GL.GenBuffers(1, out ((MeshImp) mr).UVBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).UVBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (uvsBytes), uvs, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != uvsBytes) throw new ApplicationException(String.Format( "Problem uploading uv buffer to VBO (uvs). Tried to upload {0} bytes, uploaded {1}.", uvsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
/// <summary> /// Removes the tangents. /// </summary> /// <param name="mesh">The mesh.</param> public void RemoveTangents(IMeshImp mesh) { throw new NotImplementedException(); }
/// <summary> /// Removes the normals. /// </summary> /// <param name="mesh">The mesh.</param> public void RemoveNormals(IMeshImp mesh) { throw new NotImplementedException(); }
private IMeshImp RegisterNewMesh(Mesh mesh) { // Configure newly created MeshImp to reflect Mesh's properties on GPU (allocate buffers) IMeshImp meshImp = _renderContextImp.CreateMeshImp(); // Begin Setup GPU Buffers / allocate GPU memory if (mesh.VerticesSet) { _renderContextImp.SetVertices(meshImp, mesh.Vertices); } if (mesh.UVsSet) { _renderContextImp.SetUVs(meshImp, mesh.UVs); } if (mesh.NormalsSet) { _renderContextImp.SetNormals(meshImp, mesh.Normals); } if (mesh.ColorsSet) { _renderContextImp.SetColors(meshImp, mesh.Colors); } if (mesh.BoneIndicesSet) { _renderContextImp.SetBoneIndices(meshImp, mesh.BoneIndices); } if (mesh.BoneWeightsSet) { _renderContextImp.SetBoneWeights(meshImp, mesh.BoneWeights); } if (mesh.TrianglesSet) { _renderContextImp.SetTriangles(meshImp, mesh.Triangles); } if (mesh.TangentsSet) { _renderContextImp.SetTangents(meshImp, mesh.Tangents); } if (mesh.BiTangentsSet) { _renderContextImp.SetBiTangents(meshImp, mesh.BiTangents); } // End Setup GPU Buffers // Setup handler to observe changes of the mesh data and dispose event (deallocation) mesh.MeshChanged += MeshChanged; _identifierToMeshImpDictionary.Add(mesh.SessionUniqueIdentifier, meshImp); return(meshImp); }
public void SetNormals(IMeshImp mr, float3[] normals) { throw new System.NotImplementedException(); }
public void SetVertices(IMeshImp mesh, float3[] vertices) { throw new System.NotImplementedException(); }
public void SetColors(IMeshImp mr, uint[] colors) { throw new System.NotImplementedException(); }
/// <summary> /// Removes the bone indices. /// </summary> /// <param name="mesh">The mesh.</param> public void RemoveBoneIndices(IMeshImp mesh) { throw new NotImplementedException(); }
public void SetTriangles(IMeshImp mr, ushort[] triangleIndices) { throw new System.NotImplementedException(); }
/// <summary> /// Binds the triangles onto the GL Rendercontext and assigns an ElementBuffer index to the passed <see cref="IMeshImp" /> instance. /// </summary> /// <param name="mr">The <see cref="IMeshImp" /> instance.</param> /// <param name="triangleIndices">The triangle indices.</param> /// <exception cref="System.ArgumentException">triangleIndices must not be null or empty</exception> /// <exception cref="System.ApplicationException"></exception> public void SetTriangles(IMeshImp mr, ushort[] triangleIndices) { if (triangleIndices == null || triangleIndices.Length == 0) { throw new ArgumentException("triangleIndices must not be null or empty"); } ((MeshImp) mr).NElements = triangleIndices.Length; int vboBytes; int trisBytes = triangleIndices.Length*sizeof (short); if (((MeshImp) mr).ElementBufferObject == 0) GL.GenBuffers(1, out ((MeshImp) mr).ElementBufferObject); // Upload the index buffer (elements inside the vertex buffer, not color indices as per the IndexPointer function!) GL.BindBuffer(BufferTarget.ElementArrayBuffer, ((MeshImp) mr).ElementBufferObject); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr) (trisBytes), triangleIndices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != trisBytes) throw new ApplicationException(String.Format( "Problem uploading vertex buffer to VBO (offsets). Tried to upload {0} bytes, uploaded {1}.", trisBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
// This is not yet possible. // FBO in WebGL is in beta stadium - late 2016 public void RenderDeferred(IMeshImp mr) { throw new System.NotImplementedException(); }
/// <summary> /// Renders the specified <see cref="IMeshImp" />. /// </summary> /// <param name="mr">The <see cref="IMeshImp" /> instance.</param> public void Render(IMeshImp mr) { if (((MeshImp) mr).VertexBufferObject != 0) { GL.EnableVertexAttribArray(Helper.VertexAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).VertexBufferObject); GL.VertexAttribPointer(Helper.VertexAttribLocation, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp) mr).ColorBufferObject != 0) { GL.EnableVertexAttribArray(Helper.ColorAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).ColorBufferObject); GL.VertexAttribPointer(Helper.ColorAttribLocation, 4, VertexAttribPointerType.UnsignedByte, true, 0, IntPtr.Zero); } if (((MeshImp) mr).UVBufferObject != 0) { GL.EnableVertexAttribArray(Helper.UvAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).UVBufferObject); GL.VertexAttribPointer(Helper.UvAttribLocation, 2, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp) mr).NormalBufferObject != 0) { GL.EnableVertexAttribArray(Helper.NormalAttribLocation); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).NormalBufferObject); GL.VertexAttribPointer(Helper.NormalAttribLocation, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); } if (((MeshImp) mr).ElementBufferObject != 0) { GL.BindBuffer(BufferTarget.ElementArrayBuffer, ((MeshImp) mr).ElementBufferObject); GL.DrawElements(BeginMode.Triangles, ((MeshImp) mr).NElements, DrawElementsType.UnsignedShort, IntPtr.Zero); //GL.DrawArrays(GL.Enums.BeginMode.POINTS, 0, shape.Vertices.Length); } if (((MeshImp) mr).VertexBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.VertexAttribLocation); } if (((MeshImp) mr).ColorBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.ColorAttribLocation); } if (((MeshImp) mr).NormalBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.NormalAttribLocation); } if (((MeshImp) mr).UVBufferObject != 0) { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableVertexAttribArray(Helper.UvAttribLocation); } }
public void SetBoneIndices(IMeshImp mr, float4[] boneIndices) { throw new System.NotImplementedException(); }
/// <summary> /// Binds the normals onto the GL Rendercontext and assigns an NormalBuffer index to the passed <see cref="IMeshImp" /> instance. /// </summary> /// <param name="mr">The <see cref="IMeshImp" /> instance.</param> /// <param name="normals">The normals.</param> /// <exception cref="System.ArgumentException">Normals must not be null or empty</exception> /// <exception cref="System.ApplicationException"></exception> public void SetNormals(IMeshImp mr, float3[] normals) { if (normals == null || normals.Length == 0) { throw new ArgumentException("Normals must not be null or empty"); } int vboBytes; int normsBytes = normals.Length*3*sizeof (float); if (((MeshImp) mr).NormalBufferObject == 0) GL.GenBuffers(1, out ((MeshImp) mr).NormalBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, ((MeshImp) mr).NormalBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (normsBytes), normals, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out vboBytes); if (vboBytes != normsBytes) throw new ApplicationException(String.Format( "Problem uploading normal buffer to VBO (normals). Tried to upload {0} bytes, uploaded {1}.", normsBytes, vboBytes)); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void SetUVs(IMeshImp mr, float2[] uvs) { throw new System.NotImplementedException(); }