1
0
mirror of synced 2025-01-19 01:14:08 +01:00

Properly batch edit mip maps if multiple textures are selected in the importer

This commit is contained in:
KillzXGaming 2019-08-27 19:03:01 -04:00
parent 4d2d3502eb
commit d426bb490e
9 changed files with 207 additions and 52 deletions

View File

@ -44,6 +44,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Toolbox\Lib\OpenTK.dll</HintPath>
</Reference>
<Reference Include="OpenTK.GLControl, Version=3.0.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Toolbox\Lib\OpenTK.GLControl.dll</HintPath>
</Reference>
<Reference Include="Syroot.BinaryData">
<HintPath>..\Toolbox\Lib\Syroot.BinaryData.dll</HintPath>
<Private>False</Private>
@ -62,6 +66,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />

View File

@ -43,7 +43,7 @@ namespace FirstPlugin
#region Text Converter Interface
public TextFileType TextFileType => TextFileType.Xml;
public bool CanConvertBack => true;
public bool CanConvertBack => false;
public string ConvertToString()
{
@ -82,7 +82,7 @@ namespace FirstPlugin
private Header header;
public void Load(System.IO.Stream stream)
{
CanSave = true;
CanSave = false;
header = new Header();
header.Read(new FileReader(stream), FileName);

View File

@ -28,6 +28,7 @@ namespace FirstPlugin.Forms
public void LoadBflyt(BFLYT.Header header, string fileName)
{
LayoutViewer viewer = new LayoutViewer();
viewer.Dock = DockStyle.Fill;
this.Controls.Add(viewer);
}

View File

@ -28,12 +28,29 @@
/// </summary>
private void InitializeComponent()
{
this.glControl1 = new OpenTK.GLControl();
this.SuspendLayout();
//
// glControl1
//
this.glControl1.BackColor = System.Drawing.Color.Black;
this.glControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.glControl1.Location = new System.Drawing.Point(0, 0);
this.glControl1.Name = "glControl1";
this.glControl1.Size = new System.Drawing.Size(625, 414);
this.glControl1.TabIndex = 0;
this.glControl1.VSync = false;
this.glControl1.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl1_Paint);
this.glControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.glControl1_MouseDown);
this.glControl1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.glControl1_MouseMove);
this.glControl1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.glControl1_MouseUp);
this.glControl1.Resize += new System.EventHandler(this.glControl1_Resize);
//
// LayoutViewer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.glControl1);
this.Name = "LayoutViewer";
this.Size = new System.Drawing.Size(625, 414);
this.ResumeLayout(false);
@ -41,5 +58,7 @@
}
#endregion
private OpenTK.GLControl glControl1;
}
}

View File

@ -7,14 +7,102 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using Toolbox.Library;
using Toolbox.Library.Rendering;
namespace FirstPlugin.Forms
{
public partial class LayoutViewer : UserControl
{
private RenderableTex backgroundTex;
public LayoutViewer()
{
InitializeComponent();
}
private void glControl1_Paint(object sender, PaintEventArgs e)
{
if (!Runtime.OpenTKInitialized)
return;
glControl1.Context.MakeCurrent(glControl1.WindowInfo);
OnRender();
}
private void OnRender()
{
GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, glControl1.Width, glControl1.Height, 0, -1, 1);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.ClearColor(System.Drawing.Color.FromArgb(40, 40, 40));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
DrawBackground();
glControl1.SwapBuffers();
}
private void DrawBackground()
{
if (backgroundTex == null)
backgroundTex = RenderableTex.FromBitmap(Properties.Resources.GridBackground);
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, backgroundTex.TexID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)backgroundTex.TextureWrapR);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)backgroundTex.TextureWrapT);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)backgroundTex.TextureMagFilter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)backgroundTex.TextureMinFilter);
float scale = 4;
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.PushMatrix();
GL.Scale(scale, scale, scale);
GL.Translate(0, 0, 0);
GL.Color4(Color.White);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(-1, -1);
GL.Vertex3(-Width, -Height, 0);
GL.TexCoord2(0, -1);
GL.Vertex3(Width, -Height, 0);
GL.TexCoord2(0, 0);
GL.Vertex3(Width, Height, 0);
GL.TexCoord2(-1, 0);
GL.Vertex3(-Width, Height, 0);
GL.End();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PopMatrix();
}
private void glControl1_MouseDown(object sender, MouseEventArgs e)
{
}
private void glControl1_MouseUp(object sender, MouseEventArgs e)
{
}
private void glControl1_MouseMove(object sender, MouseEventArgs e)
{
}
private void glControl1_Resize(object sender, EventArgs e)
{
glControl1.Invalidate();
}
}
}

View File

@ -127,31 +127,31 @@ namespace FirstPlugin
}
private Thread Thread;
public void SetupSettings()
public void SetupSettings(TextureImporterSettings setting)
{
if (SelectedTexSettings.Format == SurfaceFormat.Invalid || SelectedIndex == -1)
if (setting.Format == SurfaceFormat.Invalid || SelectedIndex == -1)
return;
WidthLabel.Text = $"Width: {SelectedTexSettings.TexWidth}";
HeightLabel.Text = $"Height: {SelectedTexSettings.TexHeight}";
WidthLabel.Text = $"Width: {setting.TexWidth}";
HeightLabel.Text = $"Height: {setting.TexHeight}";
if (Thread != null && Thread.IsAlive)
Thread.Abort();
if (formatComboBox.SelectedItem is SurfaceDim)
SelectedTexSettings.SurfaceDim = (SurfaceDim)formatComboBox.SelectedItem;
setting.SurfaceDim = (SurfaceDim)formatComboBox.SelectedItem;
if (formatComboBox.SelectedItem is SurfaceFormat)
{
SelectedTexSettings.Format = (SurfaceFormat)formatComboBox.SelectedItem;
setting.Format = (SurfaceFormat)formatComboBox.SelectedItem;
listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString();
listViewCustom1.Items[SelectedIndex].SubItems[1].Text = setting.Format.ToString();
}
if (SelectedTexSettings.Format == SurfaceFormat.BC7_UNORM ||
SelectedTexSettings.Format == SurfaceFormat.BC7_SRGB)
if (setting.Format == SurfaceFormat.BC7_UNORM ||
setting.Format == SurfaceFormat.BC7_SRGB)
{
compressionModeCB.Visible = true;
compModeLbl.Visible = true;
@ -171,28 +171,28 @@ namespace FirstPlugin
Thread = new Thread((ThreadStart)(() =>
{
SelectedTexSettings.IsFinishedCompressing = false;
setting.IsFinishedCompressing = false;
ToggleOkButton(false);
pictureBox1.Image = bitmap;
var mips = SelectedTexSettings.GenerateMipList(CompressionMode);
SelectedTexSettings.DataBlockOutput.Clear();
SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));
var mips = setting.GenerateMipList(CompressionMode);
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));
ToggleOkButton(true);
SelectedTexSettings.IsFinishedCompressing = true;
setting.IsFinishedCompressing = true;
if (SelectedTexSettings.DataBlockOutput.Count > 0) {
if (SelectedTexSettings.Format == SurfaceFormat.BC5_SNORM)
if (setting.DataBlockOutput.Count > 0) {
if (setting.Format == SurfaceFormat.BC5_SNORM)
{
bitmap = DDSCompressor.DecompressBC5(mips[0],
(int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true);
(int)setting.TexWidth, (int)setting.TexHeight, true);
}
else
{
bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0],
SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, TextureData.ConvertFormat(SelectedTexSettings.Format), new byte[0]);
setting.TexWidth, setting.TexHeight, TextureData.ConvertFormat(setting.Format), new byte[0]);
}
}
@ -243,11 +243,11 @@ namespace FirstPlugin
}
}
SetupSettings();
SetupSettings(SelectedTexSettings);
}
else if (formatComboBox.SelectedIndex > -1 && SelectedTexSettings != null)
{
SetupSettings();
SetupSettings(SelectedTexSettings);
}
}
@ -263,7 +263,7 @@ namespace FirstPlugin
ImgDimComb.SelectedItem = SelectedTexSettings.SurfaceDim;
SetupSettings();
SetupSettings(SelectedTexSettings);
if (ForceMipCount)
MipmapNum.Maximum = SelectedTexSettings.MipCount;
@ -286,12 +286,15 @@ namespace FirstPlugin
if (!IsLoaded)
return;
if (MipmapNum.Value > 0)
SelectedTexSettings.MipCount = (uint)MipmapNum.Value;
else
SelectedTexSettings.MipCount = 1;
foreach (int index in listViewCustom1.SelectedIndices)
{
if (MipmapNum.Value > 0)
settings[index].MipCount = (uint)MipmapNum.Value;
else
settings[index].MipCount = 1;
SetupSettings();
SetupSettings(settings[index]);
}
}
private void BinaryTextureImporterList_KeyDown(object sender, KeyEventArgs e)
@ -309,7 +312,7 @@ namespace FirstPlugin
{
if (formatComboBox.SelectedIndex > -1 && SelectedTexSettings != null)
{
SetupSettings();
SetupSettings(SelectedTexSettings);
}
}
@ -317,7 +320,7 @@ namespace FirstPlugin
{
if (ImgDimComb.SelectedIndex > -1 && SelectedTexSettings != null)
{
SetupSettings();
SetupSettings(SelectedTexSettings);
}
}
}

View File

@ -147,9 +147,9 @@ namespace FirstPlugin
private Thread Thread;
public void SetupSettings()
public void SetupSettings(GTXImporterSettings setting)
{
if (SelectedTexSettings.Format == GX2.GX2SurfaceFormat.INVALID || SelectedIndex == -1)
if (setting.Format == GX2.GX2SurfaceFormat.INVALID || SelectedIndex == -1)
return;
if (Thread != null && Thread.IsAlive)
@ -157,12 +157,12 @@ namespace FirstPlugin
if (formatComboBox.SelectedItem is GX2.GX2SurfaceFormat)
{
SelectedTexSettings.Format = (GX2.GX2SurfaceFormat)formatComboBox.SelectedItem;
setting.Format = (GX2.GX2SurfaceFormat)formatComboBox.SelectedItem;
listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString();
listViewCustom1.Items[SelectedIndex].SubItems[1].Text = setting.Format.ToString();
}
HeightLabel.Text = $"Height: {SelectedTexSettings.TexHeight}";
WidthLabel.Text = $"Width: {SelectedTexSettings.TexWidth}";
HeightLabel.Text = $"Height: {setting.TexHeight}";
WidthLabel.Text = $"Width: {setting.TexWidth}";
Bitmap bitmap = Toolbox.Library.Imaging.GetLoadingImage();
@ -170,20 +170,20 @@ namespace FirstPlugin
Thread = new Thread((ThreadStart)(() =>
{
SelectedTexSettings.IsFinishedCompressing = false;
setting.IsFinishedCompressing = false;
ToggleOkButton(false);
var mips = SelectedTexSettings.GenerateMipList();
SelectedTexSettings.DataBlockOutput.Clear();
SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));
var mips = setting.GenerateMipList();
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));
ToggleOkButton(true);
SelectedTexSettings.Compress();
setting.Compress();
bitmap = FTEX.DecodeBlockGetBitmap(mips[0], SelectedTexSettings.
TexWidth, SelectedTexSettings.TexHeight, FTEX.ConvertFromGx2Format(
(Syroot.NintenTools.Bfres.GX2.GX2SurfaceFormat)SelectedTexSettings.Format), new byte[0]);
bitmap = FTEX.DecodeBlockGetBitmap(mips[0], setting.
TexWidth, setting.TexHeight, FTEX.ConvertFromGx2Format(
(Syroot.NintenTools.Bfres.GX2.GX2SurfaceFormat)setting.Format), new byte[0]);
if (pictureBox1.InvokeRequired)
{
@ -243,11 +243,11 @@ namespace FirstPlugin
}
}
SetupSettings();
SetupSettings(SelectedTexSettings);
}
else if (formatComboBox.SelectedIndex > -1 && SelectedTexSettings != null)
{
SetupSettings();
SetupSettings(SelectedTexSettings);
}
}
@ -260,7 +260,7 @@ namespace FirstPlugin
SelectedTexSettings = settings[listViewCustom1.SelectedIndices[0]];
formatComboBox.SelectedItem = SelectedTexSettings.Format;
SetupSettings();
SetupSettings(SelectedTexSettings);
MipmapNum.Maximum = STGenericTexture.GenerateTotalMipCount(
SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight) + 1;
@ -605,14 +605,14 @@ namespace FirstPlugin
if (!IsLoaded)
return;
if (SelectedTexSettings != null)
foreach (int index in listViewCustom1.SelectedIndices)
{
if (MipmapNum.Value > 0)
SelectedTexSettings.MipCount = (uint)MipmapNum.Value;
settings[index].MipCount = (uint)MipmapNum.Value;
else
SelectedTexSettings.MipCount = 1;
settings[index].MipCount = 1;
SetupSettings();
SetupSettings(settings[index]);
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 465 KiB

After

Width:  |  Height:  |  Size: 465 KiB

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using System.Runtime.InteropServices;
@ -14,7 +15,7 @@ namespace Toolbox.Library.Rendering
public int width, height;
public int TexID;
public PixelInternalFormat pixelInternalFormat;
public PixelFormat pixelFormat;
public OpenTK.Graphics.OpenGL.PixelFormat pixelFormat;
public PixelType pixelType = PixelType.UnsignedByte;
public TextureTarget TextureTarget = TextureTarget.Texture2D;
public bool GLInitialized = false;
@ -88,6 +89,23 @@ namespace Toolbox.Library.Rendering
GL.TexParameter(TextureTarget, param, value);
}
public static RenderableTex FromBitmap(Bitmap bitmap)
{
RenderableTex tex = new RenderableTex();
tex.TextureTarget = TextureTarget.Texture2D;
tex.TextureWrapS = TextureWrapMode.Repeat;
tex.TextureWrapT = TextureWrapMode.Repeat;
tex.TextureMinFilter = TextureMinFilter.Linear;
tex.TextureMagFilter = TextureMagFilter.Linear;
tex.width = bitmap.Width;
tex.height = bitmap.Height;
tex.pixelInternalFormat = PixelInternalFormat.Rgb8;
tex.pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
tex.GLInitialized = true;
tex.TexID = GenerateOpenGLTexture(tex, bitmap);
return tex;
}
private bool UseMipmaps = false;
public void LoadOpenGLTexture(STGenericTexture GenericTexture, int ArrayStartIndex = 0, bool LoadArrayLevels = false)
{
@ -268,6 +286,27 @@ namespace Toolbox.Library.Rendering
}
}
public static int GenerateOpenGLTexture(RenderableTex t, Bitmap bitmap, bool generateMips = false)
{
if (!t.GLInitialized)
return -1;
int texID = GL.GenTexture();
GL.BindTexture(t.TextureTarget, texID);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
if (generateMips)
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
return texID;
}
public static int GenerateOpenGLTexture(RenderableTex t, List<STGenericTexture.Surface> ImageData)
{
if (!t.GLInitialized)