public GPUMeshBuffers GetVertexIndexBuffers(VBIB vbib) { if (gpuBuffers.TryGetValue(vbib, out var gpuVbib)) { return(gpuVbib); } else { var newGpuVbib = new GPUMeshBuffers(vbib); gpuBuffers.Add(vbib, newGpuVbib); return(newGpuVbib); } }
private DrawCall CreateDrawCall(IKeyValueCollection objectDrawCall, VBIB vbib, GPUMeshBuffers gpuMeshBuffers, IDictionary <string, bool> shaderArguments, RenderMaterial material) { var drawCall = new DrawCall(); switch (objectDrawCall.GetProperty <string>("m_nPrimitiveType")) { case "RENDER_PRIM_TRIANGLES": drawCall.PrimitiveType = PrimitiveType.Triangles; break; default: throw new Exception("Unknown PrimitiveType in drawCall! (" + objectDrawCall.GetProperty <string>("m_nPrimitiveType") + ")"); } drawCall.Material = material; // Add shader parameters from material to the shader parameters from the draw call var combinedShaderParameters = shaderArguments .Concat(material.Material.GetShaderArguments()) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); // Load shader drawCall.Shader = guiContext.ShaderLoader.LoadShader(drawCall.Material.Material.ShaderName, combinedShaderParameters); //Bind and validate shader GL.UseProgram(drawCall.Shader.Program); var indexBufferObject = objectDrawCall.GetSubCollection("m_indexBuffer"); var indexBuffer = default(DrawBuffer); indexBuffer.Id = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_hBuffer")); indexBuffer.Offset = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_nBindOffsetBytes")); drawCall.IndexBuffer = indexBuffer; var indexElementSize = vbib.IndexBuffers[(int)drawCall.IndexBuffer.Id].Size; //drawCall.BaseVertex = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nBaseVertex")); //drawCall.VertexCount = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nVertexCount")); drawCall.StartIndex = Convert.ToUInt32(objectDrawCall.GetProperty <object>("m_nStartIndex")) * indexElementSize; drawCall.IndexCount = Convert.ToInt32(objectDrawCall.GetProperty <object>("m_nIndexCount")); if (objectDrawCall.ContainsKey("m_vTintColor")) { var tintColor = objectDrawCall.GetSubCollection("m_vTintColor").ToVector3(); drawCall.TintColor = new OpenTK.Vector3(tintColor.X, tintColor.Y, tintColor.Z); } if (!drawCall.Material.Textures.ContainsKey("g_tTintMask")) { drawCall.Material.Textures.Add("g_tTintMask", MaterialLoader.CreateSolidTexture(1f, 1f, 1f)); } if (!drawCall.Material.Textures.ContainsKey("g_tNormal")) { drawCall.Material.Textures.Add("g_tNormal", MaterialLoader.CreateSolidTexture(0.5f, 1f, 0.5f)); } if (indexElementSize == 2) { //shopkeeper_vr drawCall.IndexType = DrawElementsType.UnsignedShort; } else if (indexElementSize == 4) { //glados drawCall.IndexType = DrawElementsType.UnsignedInt; } else { throw new Exception("Unsupported index type"); } var m_vertexBuffers = objectDrawCall.GetSubCollection("m_vertexBuffers"); var m_vertexBuffer = m_vertexBuffers.GetSubCollection("0"); // TODO: Not just 0 var vertexBuffer = default(DrawBuffer); vertexBuffer.Id = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_hBuffer")); vertexBuffer.Offset = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_nBindOffsetBytes")); drawCall.VertexBuffer = vertexBuffer; drawCall.VertexArrayObject = guiContext.MeshBufferCache.GetVertexArrayObject( vbib, drawCall.Shader, drawCall.VertexBuffer.Id, drawCall.IndexBuffer.Id); return(drawCall); }