public void CreateBoneList(STGenericObject ob, FMDL mdl) { string[] nodeArrStrings = new string[mdl.Skeleton.Node_Array.Length]; int CurNode = 0; foreach (int thing in mdl.Skeleton.Node_Array) { nodeArrStrings[CurNode++] = mdl.Skeleton.bones[thing].Text; } foreach (Vertex v in ob.vertices) { foreach (string bn in v.boneNames) { foreach (var defBn in nodeArrStrings.Select((Value, Index) => new { Value, Index })) { if (bn == defBn.Value) { v.boneIds.Add(defBn.Index); } } } } }
public void CreateIndexList(STGenericObject ob, FMDL mdl = null) { BoneIndices = new List <ushort>(); List <string> boneNames = new List <string>(); foreach (Vertex v in ob.vertices) { foreach (string bn in v.boneNames) { if (!boneNames.Contains(bn)) { boneNames.Add(bn); } } } foreach (STBone bone in mdl.Skeleton.bones) { foreach (string bnam in boneNames) { if (bone.Text == bnam) { int index = boneNames.IndexOf(bone.Text); STConsole.WriteLine($"Adding bone to shape index list! {bone.Text} {index}"); BoneIndices.Add((ushort)index); } } } }
public GFMDLStructs.BoundingBox GenerateBoundingBox(STGenericObject mesh) { float minX = float.MaxValue; float minY = float.MaxValue; float minZ = float.MaxValue; float maxX = float.MinValue; float maxY = float.MinValue; float maxZ = float.MinValue; for (int v = 0; v < mesh.vertices.Count; v++) { minX = Math.Min(minX, mesh.vertices[v].pos.X); minY = Math.Min(minY, mesh.vertices[v].pos.Y); minZ = Math.Min(minZ, mesh.vertices[v].pos.Z); maxX = Math.Max(maxX, mesh.vertices[v].pos.X); maxY = Math.Max(maxY, mesh.vertices[v].pos.Y); maxZ = Math.Max(maxZ, mesh.vertices[v].pos.Z); } var bounding = new GFMDLStructs.BoundingBox(); bounding.MinX = minX; bounding.MinY = minY; bounding.MinZ = minZ; bounding.MaxX = maxX; bounding.MaxY = maxY; bounding.MaxZ = maxZ; return(bounding); }
public void CreateIndexList(STGenericObject ob, FMDL mdl = null) { BoneIndices = new List <ushort>(); List <string> boneNames = new List <string>(); foreach (Vertex v in ob.vertices) { foreach (string bn in v.boneNames) { if (!boneNames.Contains(bn)) { boneNames.Add(bn); } } } int index = 0; foreach (STBone bone in mdl.Skeleton.bones) { foreach (string bnam in boneNames) { if (bone.Text == bnam) { BoneIndices.Add((ushort)index); } } index++; } }
public void CreateBoneList(STGenericObject ob, FMDL mdl) { if (mdl.Skeleton.Node_Array == null) { mdl.Skeleton.Node_Array = new int[0]; } string[] nodeArrStrings = new string[mdl.Skeleton.Node_Array.Length]; short[] nodeRigidIndex = new short[mdl.Skeleton.Node_Array.Length]; int CurNode = 0; foreach (int thing in mdl.Skeleton.Node_Array) { nodeArrStrings[CurNode] = mdl.Skeleton.bones[thing].Text; nodeRigidIndex[CurNode] = mdl.Skeleton.bones[thing].RigidMatrixIndex; CurNode++; } foreach (Vertex v in ob.vertices) { List <int> RigidIds = new List <int>(); foreach (string bn in v.boneNames) { int i = 0; foreach (var defBn in nodeArrStrings.Select((Value, Index) => new { Value, Index })) { if (bn == defBn.Value) { //Add these after smooth matrices if (nodeRigidIndex[i] != -1) { RigidIds.Add(defBn.Index); } else { if (v.boneIds.Count < 4) { v.boneIds.Add(defBn.Index); } } } i++; } } if (RigidIds.Count > 0) { foreach (int id in RigidIds) { if (v.boneIds.Count < 4) { v.boneIds.Add(id); } } } } }
private static void SetUniforms(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { //UV Scale shader.SetFloat("ColorUVScaleU", 1); shader.SetFloat("ColorUVScaleV", 1); //UV Translate shader.SetFloat("ColorUVTranslateU", 0); shader.SetFloat("ColorUVTranslateV", 0); }
private static void DrawModelWireframe(STGenericObject p, ShaderProgram shader) { // use vertex color for wireframe color shader.SetInt("colorOverride", 1); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); GL.Enable(EnableCap.LineSmooth); GL.LineWidth(1.5f); GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); shader.SetInt("colorOverride", 0); }
private static void DrawModelSelection(STGenericObject p, ShaderProgram shader) { GL.Uniform1(shader["colorOverride"], 1); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); GL.Enable(EnableCap.LineSmooth); GL.LineWidth(1.3f); GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Uniform1(shader["colorOverride"], 0); GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); }
private void SetVertexAttributes(STGenericObject m, ShaderProgram shader) { GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position); GL.VertexAttribPointer(shader.GetAttribute("vPosition"), 3, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 0); //+12 GL.VertexAttribPointer(shader.GetAttribute("vNormal"), 3, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 12); //+12 GL.VertexAttribPointer(shader.GetAttribute("vTangent"), 3, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 24); //+12 GL.VertexAttribPointer(shader.GetAttribute("vUV0"), 2, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 36); //+8 GL.VertexAttribPointer(shader.GetAttribute("vColor"), 4, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 44); //+16 GL.VertexAttribIPointer(shader.GetAttribute("vBone"), 4, VertexAttribIntegerType.Int, GenericRenderedObject.DisplayVertex.Size, new IntPtr(60)); //+16 GL.VertexAttribPointer(shader.GetAttribute("vWeight"), 4, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 76); //+16 GL.VertexAttribPointer(shader.GetAttribute("vUV1"), 2, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 92); //+8 GL.VertexAttribPointer(shader.GetAttribute("vUV2"), 2, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 100); //+8 GL.VertexAttribPointer(shader.GetAttribute("vBinormal"), 3, VertexAttribPointerType.Float, false, GenericRenderedObject.DisplayVertex.Size, 108); //+12 GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements); }
private static void DrawMdoelHoverSelection(STGenericObject p, ShaderProgram shader, bool IsSelected, bool IsHovered) { if (IsHovered && IsSelected) { GL.Uniform4(shader["pickingColor"], hoverColor); } else if (IsHovered || IsSelected) { GL.Uniform4(shader["pickingColor"], selectColor); } else { GL.Uniform4(shader["pickingColor"], new Vector4(1)); } }
private static void DrawMdoelHoverSelection(STGenericObject p, SF.Shader shader, bool IsSelected, bool IsHovered) { if (IsHovered && IsSelected) { shader.SetVector4("pickingColor", hoverColor); } else if (IsHovered || IsSelected) { shader.SetVector4("pickingColor", selectColor); } else { shader.SetVector4("pickingColor", new Vector4(1)); } }
public void SetModelAttributes(STGenericObject obj) { chkBoxEnablePositions.Enabled = true; chkBoxEnablePositions.Checked = obj.HasPos; chkBoxEnableNormals.Checked = obj.HasNrm; chkBoxEnableUVs.Checked = obj.HasUv0; chkBoxEnableTans.Checked = obj.HasUv0; chkBoxEnableBitans.Checked = obj.HasUv0; chkBoxEnableWeightIndices.Checked = obj.HasWeights; chkBoxEnableVertColors.Checked = obj.HasVertColors; chkResetUVParams.Checked = true; chkBoxTransformMatrix.Checked = true; if (!obj.HasPos) { DisableAttribute(chkBoxEnablePositions, comboBoxFormatPositions); } if (!obj.HasNrm) { DisableAttribute(chkBoxEnableNormals, comboBoxFormatPositions); } if (!obj.HasUv0) { DisableAttribute(chkBoxEnableUVs, comboBoxFormatUvs); } //Note. Bitans/tans uses uvs to generate if (!obj.HasUv0) { DisableAttribute(chkBoxEnableTans, comboBoxFormatTangents); } if (!obj.HasUv0) { DisableAttribute(chkBoxEnableBitans, comboBoxFormatBitans); } if (!obj.HasWeights && !obj.HasIndices) { DisableAttribute(chkBoxEnableWeightIndices, comboBoxFormatWeights); DisableAttribute(chkBoxEnableWeightIndices, comboBoxFormatIndices); } if (!obj.HasVertColors) { DisableAttribute(chkBoxEnableVertColors, comboBoxFormatVertexColors); } EnableUV1 = obj.HasUv1; EnableUV2 = obj.HasUv2; }
private static void DrawModelSelection(STGenericObject p, SF.Shader shader) { //This part needs to be reworked for proper outline. Currently would make model disappear /* GL.Enable(EnableCap.DepthTest); * GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace); * * GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit); * * GL.StencilMask(0x00); * * GL.StencilFunc(StencilFunction.Always, 1, 0xFF); // all fragments should update the stencil buffer * GL.StencilMask(0xFF); // enable writing to the stencil buffer * GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); * * GL.StencilFunc(StencilFunction.Notequal, 1, 0xFF); * GL.StencilMask(0x00); // enable writing to the stencil buffer * GL.Disable(EnableCap.DepthTest); * * shader.SetInt("colorOverride", 1); * * GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); * GL.LineWidth(2.0f); * GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); * GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); * * shader.SetInt("colorOverride", 0); * * GL.StencilMask(0xFF); * GL.Enable(EnableCap.DepthTest);*/ // Override the model color with white in the shader. shader.SetInt("colorOverride", 1); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); GL.Enable(EnableCap.LineSmooth); GL.LineWidth(1.3f); GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); shader.SetInt("colorOverride", 0); GL.DrawElements(PrimitiveType.Triangles, p.lodMeshes[p.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, p.Offset); }
private void SetTextureUniforms(STGenericMaterial mat, STGenericObject m, ShaderProgram shader) { SetDefaultTextureAttributes(mat, shader); LoadDebugTextureMaps(shader); shader.SetInt("RedChannel", 0); shader.SetInt("GreenChannel", 1); shader.SetInt("BlueChannel", 2); shader.SetInt("AlphaChannel", 3); LoadPBRMaps(shader); foreach (STGenericMatTexture matex in mat.TextureMaps) { if (matex.Type == STGenericMatTexture.TextureType.Diffuse) { shader.SetBoolToInt("HasDiffuse", true); TextureUniform(shader, mat, true, "DiffuseMap", matex); } } }
public void Export(object sender, EventArgs args) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Supported Formats|*.bfmdl;*.fbx;*.dae; *.obj;*.csv;|" + "Bfres Model|*.bfmdl|" + "FBX |*.fbx|" + "DAE |*.dae|" + "OBJ |*.obj|" + "CSV |*.csv|" + "All files(*.*)|*.*"; sfd.DefaultExt = ".bfobj"; sfd.FileName = Text; if (sfd.ShowDialog() == DialogResult.OK) { string ext = System.IO.Path.GetExtension(sfd.FileName); ext = ext.ToLower(); switch (ext) { case ".bfmdl": Model.Export(sfd.FileName, GetResFile()); break; case ".csv": CsvModel csv = new CsvModel(); foreach (FSHP shape in shapes) { STGenericObject obj = new STGenericObject(); obj.ObjectName = shape.Text; obj.vertices = shape.vertices; obj.faces = shape.lodMeshes[shape.DisplayLODIndex].faces; csv.objects.Add(obj); int CurVtx = 0; foreach (Vertex v in shape.vertices) { if (v.boneIds[0] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[0])); } if (v.boneIds[1] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[1])); } if (v.boneIds[2] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[2])); } if (v.boneIds[3] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[3])); } CurVtx++; } } System.IO.File.WriteAllBytes(sfd.FileName, csv.Save()); break; default: AssimpData assimp = new AssimpData(); assimp.SaveFromModel(this, sfd.FileName); break; } } }
public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { var cmbMaterial = ((CMB.CMBMaterialWrapper)mat).CMBMaterial; var cmbMesh = ((CMB.CmbMeshWrapper)m); bool HasNoNormals = cmbMesh.Mesh.HasNormal == false; shader.SetBoolToInt("HasNoNormals", HasNoNormals); shader.SetBoolToInt("isTransparent", cmbMaterial.BlendEnabled); SetGLCullMode(cmbMaterial.CullMode); if (cmbMaterial.BlendEnabled) { GL.Enable(EnableCap.Blend); GL.BlendColor(cmbMaterial.BlendColor.R / 255, cmbMaterial.BlendColor.G / 255, cmbMaterial.BlendColor.B / 255, cmbMaterial.BlendColor.A / 255); GL.BlendFunc(ConvertBlendFunc(cmbMaterial.BlendFunction.AlphaSrcFunc), ConvertBlendFunc(cmbMaterial.BlendFunction.AlphaDstFunc)); } else { GL.Disable(EnableCap.Blend); } if (cmbMaterial.AlphaTest.Enabled) { GL.Enable(EnableCap.AlphaTest); GL.AlphaFunc(ConvertTestFunction(cmbMaterial.AlphaTest.Function), cmbMaterial.AlphaTest.Reference / 255f); } else { GL.Disable(EnableCap.AlphaTest); } }
public void DrawModel(GLControl control, STSkeleton Skeleton, STGenericMaterial Material, STGenericObject m, ShaderProgram shader) { if (m.PolygonGroups.Count > 0) { foreach (var group in m.PolygonGroups) { if (group.faces.Count <= 3) { return; } SetRenderData(Material, shader, m); SetUniforms(Material, shader, m); SetUniformBlocks(Material, shader, m); SetBoneUniforms(control, shader, Skeleton, m); SetVertexAttributes(m, shader); SetTextureUniforms(Material, m, shader); if (m.IsSelected || m.GetMaterial().IsSelected) { DrawModelSelection(group, shader); } else if (Runtime.RenderModelWireframe) { DrawModelWireframe(group, shader); } else { if (Runtime.RenderModels) { GL.DrawElements(PrimitiveType.Triangles, group.displayFaceSize, DrawElementsType.UnsignedInt, group.Offset); } } } } else { if (m.lodMeshes.Count <= 0 || m.lodMeshes[m.DisplayLODIndex].faces.Count <= 3) { return; } SetUniforms(Material, shader, m); SetUniformBlocks(Material, shader, m); SetBoneUniforms(control, shader, Skeleton, m); SetVertexAttributes(m, shader); SetTextureUniforms(Material, m, shader); if (m.IsSelected) { DrawModelSelection(m, shader); } else if (Runtime.RenderModelWireframe) { DrawModelWireframe(m, shader); } else { if (Runtime.RenderModels) { GL.DrawElements(PrimitiveType.Triangles, m.lodMeshes[m.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, m.Offset); } } } }
public void LoadGenericMesh(STGenericObject obj, Action updateVertices) { objects.Add(obj); UpdateVertices = updateVertices; }
public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { var h3dMaterialWrapper = (H3DMaterialWrapper)mat; var h3dMaterial = h3dMaterialWrapper.Material; if (h3dMaterial.MaterialParams.AlphaTest.Enabled) { GL.Enable(EnableCap.AlphaTest); } else { GL.Disable(EnableCap.AlphaTest); } float alphaRef = h3dMaterial.MaterialParams.AlphaTest.Reference / 255f; GL.AlphaFunc(ConvertAlphaFunction(h3dMaterial.MaterialParams.AlphaTest.Function), alphaRef); }
private void SetUniformBlocks(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { }
public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { var bmdMaterial = (BMDMaterialWrapper)mat; //This is only for translucency //Todo figure out how bmd determines transparent materials shader.SetBoolToInt("isTransparent", bmdMaterial.isTransparent); GXToOpenGL.SetBlendState(bmdMaterial.Material.BMode); GXToOpenGL.SetCullState(bmdMaterial.Material.CullMode); // GXToOpenGL.SetDepthState(bmdMaterial.Material.ZMode, false); // GXToOpenGL.SetDitherEnabled(bmdMaterial.Material.Dither); }
public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { shader.SetBoolToInt("NoSkinning", Skeleton.bones.Count == 0); if (mat.Text == "driver_cloth") { GL.Disable(EnableCap.CullFace); } else { GL.Enable(EnableCap.CullFace); } }
private static void SetBoneUniforms(GLControl control, ShaderProgram shader, STSkeleton Skeleton, STGenericObject mesh) { int i = 0; foreach (var bone in Skeleton.bones) { Matrix4 transform = bone.invert * bone.Transform; GL.UniformMatrix4(GL.GetUniformLocation(shader.programs[control], String.Format("bones[{0}]", i++)), false, ref transform); } /* foreach (var FaceGroup in fshp.Shape.FaceGroups) * { * if (FaceGroup.BoneIndexList == null) * continue; * * for (int i = 0; i < FaceGroup.BoneIndexList.Length; i++) * { * GL.Uniform1(GL.GetUniformLocation(shader.programs[control], String.Format("boneIds[{0}]", i)), FaceGroup.BoneIndexList[i]); * * Matrix4 transform = fmdl.Skeleton.Renderable.bones[(int)FaceGroup.BoneIndexList[i]].invert * fmdl.Skeleton.Renderable.bones[(int)FaceGroup.BoneIndexList[i]].Transform; * GL.UniformMatrix4(GL.GetUniformLocation(shader.programs[control], String.Format("bones[{0}]", i)), false, ref transform); * } * }*/ }
public void Export(object sender, EventArgs args) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Supported Formats|*.bfmdl;*.fbx;*.dae; *.obj;*.csv;|" + "Bfres Model|*.bfmdl|" + "FBX |*.fbx|" + "DAE |*.dae|" + "OBJ |*.obj|" + "CSV |*.csv|" + "All files(*.*)|*.*"; sfd.DefaultExt = ".bfobj"; sfd.FileName = Text; if (sfd.ShowDialog() == DialogResult.OK) { string ext = System.IO.Path.GetExtension(sfd.FileName); ext = ext.ToLower(); switch (ext) { case ".bfmdl": if (GetResFileU() != null) { } else { Model.Export(sfd.FileName, GetResFile()); } break; case ".csv": CsvModel csv = new CsvModel(); foreach (FSHP shape in shapes) { STGenericObject obj = new STGenericObject(); obj.ObjectName = shape.Text; obj.vertices = shape.vertices; obj.faces = shape.lodMeshes[shape.DisplayLODIndex].faces; csv.objects.Add(obj); int CurVtx = 0; foreach (Vertex v in shape.vertices) { if (v.boneIds[0] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[0])); } if (v.boneIds[1] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[1])); } if (v.boneIds[2] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[2])); } if (v.boneIds[3] != 0) { obj.vertices[CurVtx].boneNames.Add(shape.GetBoneNameFromIndex(this, v.boneIds[3])); } CurVtx++; } } System.IO.File.WriteAllBytes(sfd.FileName, csv.Save()); break; default: List <STGenericTexture> Surfaces = new List <STGenericTexture>(); foreach (FSHP fshp in shapes) { foreach (var bntx in PluginRuntime.bntxContainers) { foreach (var tex in fshp.GetMaterial().TextureMaps) { if (bntx.Textures.ContainsKey(tex.Name)) { Surfaces.Add(bntx.Textures[tex.Name]); } } } foreach (var ftex in PluginRuntime.ftexContainers) { foreach (var tex in fshp.GetMaterial().TextureMaps) { if (ftex.Textures.ContainsKey(tex.Name)) { Surfaces.Add(ftex.Textures[tex.Name]); } } } } Console.WriteLine("tex count " + Surfaces.Count); AssimpData assimp = new AssimpData(); assimp.SaveFromModel(this, sfd.FileName, Surfaces); break; } } }
//Render data to display by per material and per mesh public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { }
public void MontoscriptToOBJ(object sender, EventArgs args) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "All files(*.*)|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { KclMonoscript monscript = new KclMonoscript(); monscript.ReadKCL(ofd.FileName); SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog() == DialogResult.OK) { var model = new STGenericModel(); var mesh = new STGenericObject(); mesh.faces = new List <int>(); var meshes = new List <STGenericObject>(); model.Objects = meshes; List <uint> Attributes = new List <uint>(); int ft = 0; foreach (var prisim in monscript.Prisims) { if (!Attributes.Contains(prisim.CollisionType)) { Attributes.Add(prisim.CollisionType); mesh = new STGenericObject(); mesh.Text = prisim.CollisionType.ToString("X"); meshes.Add(mesh); } var triangle = monscript.GetTriangle(prisim); var normal = triangle.Normal; var pointA = triangle.PointA; var pointB = triangle.PointB; var pointC = triangle.PointC; Vertex vtx = new Vertex(); Vertex vtx2 = new Vertex(); Vertex vtx3 = new Vertex(); vtx.pos = pointA; vtx2.pos = pointB; vtx3.pos = pointC; vtx.nrm = normal; vtx2.nrm = normal; vtx3.nrm = normal; mesh.faces.Add(ft); mesh.faces.Add(ft + 1); mesh.faces.Add(ft + 2); mesh.vertices.Add(vtx); mesh.vertices.Add(vtx2); mesh.vertices.Add(vtx3); ft += 3; } OBJ.ExportModel(sfd.FileName, model, new List <STGenericTexture>()); } } }
public static byte[] CreateVertexDataBuffer(STGenericObject mesh, List <int> SkinningIndices, IList <MeshAttribute> attributes) { var stride = GetTotalBufferStride(attributes); var mem = new System.IO.MemoryStream(); using (var writer = new FileWriter(mem)) { //Generate a buffer based on the attributes used for (int v = 0; v < mesh.vertices.Count; v++) { writer.SeekBegin(v * stride); for (int a = 0; a < attributes.Count; a++) { uint numAttributes = attributes[a].ElementCount; List <float> values = new List <float>(); switch ((VertexType)attributes[a].VertexType) { case VertexType.Position: values.Add(mesh.vertices[v].pos.X); values.Add(mesh.vertices[v].pos.Y); values.Add(mesh.vertices[v].pos.Z); break; case VertexType.Normal: OpenTK.Vector4 normal = new OpenTK.Vector4(mesh.vertices[v].nrm.X, mesh.vertices[v].nrm.Y, mesh.vertices[v].nrm.Z, mesh.vertices[v].normalW); values.Add(normal.X); values.Add(normal.Y); values.Add(normal.Z); values.Add(normal.W); break; case VertexType.Color1: values.Add(mesh.vertices[v].col.X * 255); values.Add(mesh.vertices[v].col.Y * 255); values.Add(mesh.vertices[v].col.Z * 255); values.Add(mesh.vertices[v].col.W * 255); break; case VertexType.Color2: values.Add(mesh.vertices[v].col2.X * 255); values.Add(mesh.vertices[v].col2.Y * 255); values.Add(mesh.vertices[v].col2.Z * 255); values.Add(mesh.vertices[v].col2.W * 255); break; case VertexType.UV1: values.Add(mesh.vertices[v].uv0.X); values.Add(1 - mesh.vertices[v].uv0.Y); break; case VertexType.UV2: values.Add(mesh.vertices[v].uv1.X); values.Add(1 - mesh.vertices[v].uv1.Y); break; case VertexType.UV3: values.Add(mesh.vertices[v].uv2.X); values.Add(1 - mesh.vertices[v].uv2.Y); break; case VertexType.UV4: values.Add(mesh.vertices[v].uv3.X); values.Add(1 - mesh.vertices[v].uv3.Y); break; case VertexType.BoneWeight: var weights = mesh.vertices[v].boneWeights; for (int b = 0; b < numAttributes; b++) { if (weights.Count > b) { values.Add(weights[b]); } else { values.Add(0); } } break; case VertexType.BoneID: var boneIds = mesh.vertices[v].boneIds; for (int b = 0; b < numAttributes; b++) { if (boneIds.Count > b) { values.Add(SkinningIndices.IndexOf(boneIds[b])); } else { values.Add(0); } } break; case VertexType.Bitangent: values.Add(mesh.vertices[v].bitan.X); values.Add(mesh.vertices[v].bitan.Y); values.Add(mesh.vertices[v].bitan.Z); values.Add(mesh.vertices[v].bitan.W); break; case VertexType.Tangents: values.Add(mesh.vertices[v].tan.X); values.Add(mesh.vertices[v].tan.Y); values.Add(mesh.vertices[v].tan.Z); values.Add(mesh.vertices[v].tan.W); break; default: throw new Exception("unsupported format!"); } WriteBuffer(writer, numAttributes, values.ToArray(), (BufferFormat)attributes[a].BufferFormat, (VertexType)attributes[a].VertexType); } } } return(mem.ToArray()); }
public virtual void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { }
public void LoadFile(string FileName, bool IsModel = false) { if (!IsModel) { MessageBox.Show("Not valid model csv"); return; } string line = null; List <Model> models = new List <Model>(); TextReader csv = new StreamReader(FileName); Model model = new Model(); STGenericObject STobj = new STGenericObject(); Vertex vtx = new Vertex(); STGenericObject.LOD_Mesh lod = new STGenericObject.LOD_Mesh(); int Index = 0; int ww = 0; while (true) { line = csv.ReadLine(); if (line != null) { if (line.StartsWith("Obj Name")) { model = new Model(); STobj = new STGenericObject(); model.Name = line.Split(':')[1].Replace("\n", ""); model.subType = DataSubType.position; models.Add(model); STobj.ObjectName = model.Name; lod = new STGenericObject.LOD_Mesh(); lod.IndexFormat = STIndexFormat.UInt16; lod.PrimitiveType = STPolygonType.Triangle; STobj.lodMeshes.Add(lod); STobj.VertexBufferIndex = Index; objects.Add(STobj); Index++; } else if (line.StartsWith("tex_Array:")) { } else if (line.StartsWith("Bone_Suport")) { } else if (line.StartsWith("Color_Suport")) { } else if (line.StartsWith("UV_Num:")) { int uvCount; int.TryParse(line.Split(':')[1].Replace("\n", ""), out uvCount); model.UVChannelCount = uvCount; } else if (line.StartsWith("vert_Array")) { model.type = DataType.vertex; } else if (line.StartsWith("face_Array")) { model.type = DataType.faces; } else if (line.StartsWith("bone_Array")) { model.type = DataType.bones; } else { string[] values = line.Replace("\n", "").Replace("\r", "").Split(','); if (model.type == DataType.vertex) { switch (model.subType) { case DataSubType.position: vtx = new Vertex(); STobj.vertices.Add(vtx); STobj.HasPos = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); vtx.pos = new Vector3(X, Y, Z); model.subType = DataSubType.normals; break; case DataSubType.normals: STobj.HasNrm = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); vtx.nrm = new Vector3(X, Y, Z); model.subType = DataSubType.colors; break; case DataSubType.colors: STobj.HasVertColors = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); float.TryParse(values[2], out Z); float.TryParse(values[3], out W); vtx.col = new Vector4(X / 255, Y / 255, Z / 255, W / 255); model.subType = DataSubType.uv0; break; case DataSubType.uv0: STobj.HasUv0 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv0 = new Vector2(X, Y); if (model.UVChannelCount == 1) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv1; } break; case DataSubType.uv1: STobj.HasUv1 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv1 = new Vector2(X, Y); if (model.UVChannelCount == 2) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv2; } break; case DataSubType.uv2: STobj.HasUv2 = true; float.TryParse(values[0], out X); float.TryParse(values[1], out Y); vtx.uv2 = new Vector2(X, Y); if (model.UVChannelCount == 3) { model.subType = DataSubType.position; } else { model.subType = DataSubType.uv3; } break; case DataSubType.uv3: float.TryParse(values[0], out X); float.TryParse(values[1], out Y); model.subType = DataSubType.position; break; } } if (model.type == DataType.faces) { int face; foreach (string v in values) { var cleaned = v.Replace(".0", string.Empty); int.TryParse(cleaned, out face); lod.faces.Add(face - 1); } } if (model.type == DataType.bones) { STobj.HasWeights = true; STobj.HasIndices = true; Array.Resize(ref values, values.Length - 1); List <string> bones = new List <string>(); List <float> weights = new List <float>(); int bbs = 0; foreach (string obj in values) { if (bbs == 0) { bones.Add(obj); bbs += 1; } else { float.TryParse(obj, out X); weights.Add(X); bbs = 0; } } STobj.bones.Add(bones.ToArray()); STobj.weightsT.Add(weights.ToArray()); } } } else { break; } } if (objects[0].weightsT.Count != objects[0].vertices.Count) { throw new Exception("Incorrect vertex amount"); } foreach (STGenericObject obj in objects) { obj.lodMeshes[0].GenerateSubMesh(); for (int v = 0; v < obj.vertices.Count; v++) { foreach (string bn in obj.bones[v]) { obj.vertices[v].boneNames.Add(bn); } foreach (float f in obj.weightsT[v]) { obj.vertices[v].boneWeights.Add(f); } } foreach (Vertex v in obj.vertices) { if (v.boneNames.Count == 1) { Console.WriteLine($"{v.boneNames[0]} {v.boneWeights[0]}"); } if (v.boneNames.Count == 2) { Console.WriteLine($"{v.boneNames[0]} {v.boneWeights[0]} {v.boneNames[1]} {v.boneWeights[1]}"); } if (v.boneNames.Count == 3) { Console.WriteLine($"{v.boneNames[0]} {v.boneWeights[0]} {v.boneNames[1]} {v.boneWeights[1]} {v.boneNames[2]} {v.boneWeights[2]}"); } if (v.boneNames.Count == 4) { Console.WriteLine($"{v.boneNames[0]} {v.boneWeights[0]} {v.boneNames[1]} {v.boneWeights[1]} {v.boneNames[2]} {v.boneWeights[2]} {v.boneNames[3]} {v.boneWeights[3]}"); } } } csv.Close(); csv = null; }
public virtual void SetBoneUniforms(GLControl control, ShaderProgram shader, STSkeleton Skeleton, STGenericObject mesh) { int i = 0; foreach (var bone in Skeleton.bones) { Matrix4 transform = bone.invert * bone.Transform; GL.UniformMatrix4(GL.GetUniformLocation(shader.programs[control], String.Format("bones[{0}]", i++)), false, ref transform); } }