From 403e7d0eb4378c6c21012774ae5b41f22f265b6f Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 25 Apr 2021 19:45:53 +0200 Subject: [PATCH] Added struct definitions and marshaling code to obtain texture numbers for brush model surfaces --- Assets/Scripts/Data/QModel.cs | 16 ++++++++++++++++ Assets/Scripts/Data/QTexture.cs | 23 +++++++++++++++++++++++ Assets/Scripts/Modules/BrushModel.cs | 2 ++ 3 files changed, 41 insertions(+) diff --git a/Assets/Scripts/Data/QModel.cs b/Assets/Scripts/Data/QModel.cs index 37ec567..dd02aea 100644 --- a/Assets/Scripts/Data/QModel.cs +++ b/Assets/Scripts/Data/QModel.cs @@ -258,6 +258,8 @@ public struct QSurface [MarshalAs(UnmanagedType.ByValArray, SizeConst = QConstants.MaxLightmaps)] public int[] cachedLight; public bool cachedDLight; public IntPtr samples; // Pointer to byte + + public QTexInfo TextureInfo => Marshal.PtrToStructure(texInfo); public IEnumerable GetPolygons() { @@ -297,3 +299,17 @@ public struct QGLPolyVert public QVec2 textureUV; public QVec2 lightmapUV; } + +/// +/// Managed equivalent of mtexinfo_t +/// +[StructLayout(LayoutKind.Sequential, Pack = 0)] +public struct QTexInfo +{ + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2 * 4)] public float[] vecs; + public float mipAdjust; + public IntPtr texture; // Pointer to texture_t + public int flags; + + public QTexture Texture => Marshal.PtrToStructure(texture); +} diff --git a/Assets/Scripts/Data/QTexture.cs b/Assets/Scripts/Data/QTexture.cs index 9e85928..104eca0 100644 --- a/Assets/Scripts/Data/QTexture.cs +++ b/Assets/Scripts/Data/QTexture.cs @@ -1,6 +1,25 @@ using System; using System.Runtime.InteropServices; +/// +/// Managed equivalent of texture_t +/// +[StructLayout(LayoutKind.Sequential, Pack = 0, CharSet = CharSet.Ansi)] +public struct QTexture +{ + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string name; + public uint width, height; + public IntPtr glTexture; // Pointer to gltexture_t + public IntPtr fullBright; // Pointer to gltexture_t + public IntPtr warpImage; // Pointer to gltexture_t + + public uint TextureNum => QGLTexture.TexNumFromPtr(glTexture); + public uint FullBrightNum => QGLTexture.TexNumFromPtr(fullBright); + public uint WarpImageNum => QGLTexture.TexNumFromPtr(warpImage); + + // Rest of the fields are left out, so we don't unnecessarily marshal any unused data +} + /// /// Managed equivalent of gltexture_t /// @@ -23,6 +42,10 @@ public class QGLTexture public char shirt; public char pants; public int visFrame; + + // Conveniently make use of the fact that texNum is the first field in QGLTexture, + // so we don't have to marshal the entire struct to get the texture number out of it. + public static uint TexNumFromPtr(IntPtr ptr) => ptr == IntPtr.Zero ? 0 : unchecked((uint)Marshal.ReadInt32(ptr)); } /// diff --git a/Assets/Scripts/Modules/BrushModel.cs b/Assets/Scripts/Modules/BrushModel.cs index 8f5dda7..f3f8940 100644 --- a/Assets/Scripts/Modules/BrushModel.cs +++ b/Assets/Scripts/Modules/BrushModel.cs @@ -52,6 +52,8 @@ public class BrushModel mesh.UploadMeshData(true); meshes.Add(mesh); } + + var texNum = surfaces[surfIdx].TextureInfo.Texture.TextureNum; } // DEBUG