1
0
mirror of synced 2024-11-30 18:24:39 +01:00

Add bmd drawing

This commit is contained in:
KillzXGaming 2019-07-11 19:38:29 -04:00
parent 7fddf179f6
commit 7f679b9fa6
14 changed files with 192 additions and 8 deletions

Binary file not shown.

View File

@ -8,12 +8,16 @@ using System.Windows.Forms;
using Switch_Toolbox.Library; using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO; using Switch_Toolbox.Library.IO;
using Switch_Toolbox.Library.Forms; using Switch_Toolbox.Library.Forms;
using Switch_Toolbox.Library.Rendering;
using SuperBMDLib; using SuperBMDLib;
using System.Drawing; using System.Drawing;
using SuperBMDLib.Rigging;
using SuperBMDLib.Geometry.Enums;
using SuperBMDLib.Util;
namespace FirstPlugin namespace FirstPlugin
{ {
public class BMD : TreeNodeFile, IFileFormat, IContextMenuNode public class BMD : TreeNodeFile, IFileFormat, IContextMenuNode, ITextureContainer
{ {
public FileType FileType { get; set; } = FileType.Layout; public FileType FileType { get; set; } = FileType.Layout;
@ -44,6 +48,51 @@ namespace FirstPlugin
return types.ToArray(); return types.ToArray();
} }
} }
Viewport viewport
{
get
{
var editor = LibraryGUI.GetObjectEditor();
return editor.GetViewport();
}
set
{
var editor = LibraryGUI.GetObjectEditor();
editor.LoadViewport(value);
}
}
public Dictionary<string, STGenericTexture> Textures { get; set; }
bool DrawablesLoaded = false;
public override void OnClick(TreeView treeView)
{
if (Runtime.UseOpenGL && !Runtime.UseLegacyGL)
{
if (viewport == null)
{
viewport = new Viewport(ObjectEditor.GetDrawableContainers());
viewport.Dock = DockStyle.Fill;
}
if (!DrawablesLoaded)
{
ObjectEditor.AddContainer(DrawableContainer);
DrawablesLoaded = true;
}
viewport.ReloadDrawables(DrawableContainer);
LibraryGUI.LoadEditor(viewport);
viewport.Text = Text;
}
}
public BMD_Renderer Renderer;
public DrawableContainer DrawableContainer = new DrawableContainer();
public Model BMDFile; public Model BMDFile;
private TreeNode TextureFolder; private TreeNode TextureFolder;
private TreeNode ShapeFolder; private TreeNode ShapeFolder;
@ -53,6 +102,14 @@ namespace FirstPlugin
Text = FileName; Text = FileName;
CanSave = true; CanSave = true;
//Set renderer
Renderer = new BMD_Renderer();
DrawableContainer.Name = FileName;
DrawableContainer.Drawables.Add(Renderer);
Textures = new Dictionary<string, STGenericTexture>();
BMD_Renderer.TextureContainers.Add(this);
ShapeFolder = new TreeNode("Shapes"); ShapeFolder = new TreeNode("Shapes");
TextureFolder = new TreeNode("Textures"); TextureFolder = new TreeNode("Textures");
Nodes.Add(ShapeFolder); Nodes.Add(ShapeFolder);
@ -62,15 +119,75 @@ namespace FirstPlugin
for (int i = 0; i < BMDFile.Shapes.Shapes.Count; i++) for (int i = 0; i < BMDFile.Shapes.Shapes.Count; i++)
{ {
var shpWrapper = new BMDShapeWrapper(BMDFile.Shapes.Shapes[i]); var shpWrapper = new BMDShapeWrapper(BMDFile.Shapes.Shapes[i], BMDFile, i);
shpWrapper.Text = $"Shape {i}"; shpWrapper.Text = $"Shape {i}";
ShapeFolder.Nodes.Add(shpWrapper); ShapeFolder.Nodes.Add(shpWrapper);
Renderer.Meshes.Add(shpWrapper);
var polyGroup = new STGenericPolygonGroup();
shpWrapper.PolygonGroups.Add(polyGroup);
var curShape = BMDFile.Shapes.Shapes[i];
var VertexAttributes = BMDFile.VertexData.Attributes;
int vertexID = 0;
foreach (SuperBMDLib.Geometry.Packet pack in curShape.Packets)
{
foreach (SuperBMDLib.Geometry.Primitive prim in pack.Primitives)
{
List<SuperBMDLib.Geometry.Vertex> triVertices = J3DUtility.PrimitiveToTriangles(prim);
for (int triIndex = 0; triIndex < triVertices.Count; triIndex += 3)
{
polyGroup.faces.AddRange(new int[] { vertexID + 2, vertexID + 1, vertexID });
for (int triVertIndex = 0; triVertIndex < 3; triVertIndex++)
{
SuperBMDLib.Geometry.Vertex vert = triVertices[triIndex + triVertIndex];
Vertex vertex = new Vertex();
vertex.pos = VertexAttributes.Positions[(int)vert.GetAttributeIndex(GXVertexAttribute.Position)];
shpWrapper.vertices.Add(vertex);
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Normal))
vertex.nrm = VertexAttributes.Normals[(int)vert.NormalIndex];
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color0))
{
var color0 = VertexAttributes.Color_0[(int)vert.Color0Index];
vertex.col = new OpenTK.Vector4(color0.R, color0.G, color0.B, color0.A);
}
for (int texCoordNum = 0; texCoordNum < 8; texCoordNum++)
{
if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Tex0 + texCoordNum))
{
switch (texCoordNum)
{
case 0:
vertex.uv0 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
break;
case 1:
vertex.uv1 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
break;
case 2:
vertex.uv2 = VertexAttributes.TexCoord_0[(int)vert.TexCoord0Index];
break;
}
}
}
vertexID++;
}
}
}
}
} }
for (int i = 0; i < BMDFile.Textures.Textures.Count; i++) for (int i = 0; i < BMDFile.Textures.Textures.Count; i++)
{ {
var texWrapper = new BMDTextureWrapper(BMDFile.Textures.Textures[i]); var texWrapper = new BMDTextureWrapper(BMDFile.Textures.Textures[i]);
TextureFolder.Nodes.Add(texWrapper); TextureFolder.Nodes.Add(texWrapper);
Textures.Add(texWrapper.Text, texWrapper);
} }
} }

View File

@ -6,18 +6,47 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Switch_Toolbox.Library; using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO; using Switch_Toolbox.Library.IO;
using Switch_Toolbox.Library.Rendering;
using Switch_Toolbox.Library.Forms; using Switch_Toolbox.Library.Forms;
using SuperBMDLib.Geometry; using SuperBMDLib.Geometry;
namespace FirstPlugin namespace FirstPlugin
{ {
public class BMDShapeWrapper : STGenericObject public class BMDShapeWrapper : GenericRenderedObject
{ {
SuperBMDLib.Model ParentModel;
Shape BMDShape; Shape BMDShape;
public BMDShapeWrapper(Shape shape) private STGenericMaterial material;
public BMDShapeWrapper(Shape shape, SuperBMDLib.Model model, int Index)
{ {
BMDShape = shape; BMDShape = shape;
ParentModel = model;
material = new STGenericMaterial();
// material.Text = $"Material {Index}";
var mat = model.Materials.m_Materials[Index];
int textureUnit = 1;
if (mat.TextureIndices[0] != -1)
{
int texIndex = mat.TextureIndices[0];
STGenericMatTexture matTexture = new STGenericMatTexture();
matTexture.Name = ParentModel.Textures[texIndex].Name;
matTexture.Type = STGenericMatTexture.TextureType.Diffuse;
matTexture.textureUnit = textureUnit++;
matTexture.wrapModeS = 0;
matTexture.wrapModeT = 0;
material.TextureMaps.Add(matTexture);
}
}
public override STGenericMaterial GetMaterial()
{
return material;
} }
public override void OnClick(TreeView treeView) public override void OnClick(TreeView treeView)

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox.Library.Rendering;
using GL_EditorFramework.GL_Core;
using GL_EditorFramework.Interfaces;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace FirstPlugin
{
public class BMD_Renderer : GenericModelRenderer
{
public override void OnRender(GLControl control)
{
}
}
}

View File

@ -253,6 +253,7 @@
<Compile Include="FileFormats\Audio\Archives\BFGRP.cs" /> <Compile Include="FileFormats\Audio\Archives\BFGRP.cs" />
<Compile Include="FileFormats\Texture\TPL.cs" /> <Compile Include="FileFormats\Texture\TPL.cs" />
<Compile Include="FileFormats\Rom\GCDisk.cs" /> <Compile Include="FileFormats\Rom\GCDisk.cs" />
<Compile Include="GL\BMD_Renderer.cs" />
<Compile Include="libWiiSharp\TPL.cs" /> <Compile Include="libWiiSharp\TPL.cs" />
<Compile Include="FileFormats\GFBMDL\GFBMDL.cs" /> <Compile Include="FileFormats\GFBMDL\GFBMDL.cs" />
<Compile Include="FileFormats\GFBMDL\GFBMDL_Wrappers.cs" /> <Compile Include="FileFormats\GFBMDL\GFBMDL_Wrappers.cs" />

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Switch_Toolbox.Library
{
public interface ITextureContainer
{
Dictionary<string, STGenericTexture> Textures { get; set; }
}
}

View File

@ -14,6 +14,8 @@ namespace Switch_Toolbox.Library.Rendering
{ {
public class GenericModelRenderer : AbstractGlDrawable public class GenericModelRenderer : AbstractGlDrawable
{ {
public static List<ITextureContainer> TextureContainers = new List<ITextureContainer>();
public List<GenericRenderedObject> Meshes = new List<GenericRenderedObject>(); public List<GenericRenderedObject> Meshes = new List<GenericRenderedObject>();
public STSkeleton Skeleton = new STSkeleton(); public STSkeleton Skeleton = new STSkeleton();
@ -309,14 +311,14 @@ namespace Switch_Toolbox.Library.Rendering
string activeTex = tex.Name; string activeTex = tex.Name;
/* foreach (var bntx in TextureContainers) foreach (var container in TextureContainers)
{ {
if (TextureContainers.ContainsKey(activeTex)) if (container.Textures.ContainsKey(activeTex))
{ {
BindGLTexture(bntx, tex, shader, activeTex); BindGLTexture(tex, shader, container.Textures[activeTex]);
return tex.textureUnit + 1; return tex.textureUnit + 1;
} }
}*/ }
return tex.textureUnit + 1; return tex.textureUnit + 1;
} }

View File

@ -215,6 +215,7 @@
<Compile Include="Compression\YAZ0.cs" /> <Compile Include="Compression\YAZ0.cs" />
<Compile Include="Compression\ZCMP.cs" /> <Compile Include="Compression\ZCMP.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Interfaces\ITextureContainer.cs" />
<Compile Include="Rendering\GenericModelRenderer\GenericModelRenderer.cs" /> <Compile Include="Rendering\GenericModelRenderer\GenericModelRenderer.cs" />
<Compile Include="Rendering\GenericModelRenderer\GenericRenderedObject.cs" /> <Compile Include="Rendering\GenericModelRenderer\GenericRenderedObject.cs" />
<Compile Include="Security\Cryptography\crc32.cs" /> <Compile Include="Security\Cryptography\crc32.cs" />

Binary file not shown.

Binary file not shown.