Since I struggled with this, I figured I'd just release it for anyone to use for whatever
It's code to parse VVD files and get stuff such as a table of vertexes and header info.
Feel free to use it for whatever.
/*
Vehicle Decal System
JakeFromStateCS
vcals/lib/models/cl_vvd.lua
Handles parsing of VVD files
Thanks to "raspbian got DABBED on#0572" in the Gmod Dev Discord for helping me figure this shit out
*/
--Pass our global table to this file
VCals = VCals or {};
--Pass our global config table to this file
VCals.Config = VCals.Config or {};
--Create a global decal table
VCals.Models = VCals.Models or {};
--This will hold all the functions for parsing VVDs
VCals.Models.VVD = {};
VCals.Models.Cache = VCals.Models.Cache or {};
function VCals.Models.VVD:ParseHeader( openFile )
--Stores the header data to be returned after we read it
local headerData = {
id = openFile:ReadLong(),
version = openFile:ReadLong(),
checksum = openFile:ReadLong(),
numLODs = openFile:ReadLong(),
numLODVertexes = openFile:ReadLong()
};
--Read 7 more longs for 8 levels of detail ( LODs )
for i = 2, 8 do
openFile:ReadLong();
end;
headerData.numFixups = openFile:ReadLong();
headerData.fixupTableStart = openFile:ReadLong();
headerData.vertexDataStart = openFile:ReadLong();
headerData.tangentDataStart = openFile:ReadLong();
return headerData;
end;
function VCals.Models.VVD:ParseFixupTable( openFile )
local fixupTable = {
lod = openFile:ReadLong(),
sourceVertexID = openFile:ReadLong(),
numVertexes = openFile:ReadLong()
};
return fixupTable;
end;
function VCals.Models.VVD:ParseVertex( openFile )
--Skip the first 16 bytes since it's bone weight and we don't care about that
openFile:Read( 16 );
local vertex = {
position = Vector( openFile:ReadFloat(), openFile:ReadFloat(), openFile:ReadFloat() ),
normal = Vector( openFile:ReadFloat(), openFile:ReadFloat(), openFile:ReadFloat() ),
texCoord = { u = openFile:ReadFloat(), v = openFile:ReadFloat() }
};
return vertex;
end;
--[[
VCals.Models.VVD:Parse( String/fileName ):
Parses the .mdl file for the visual mesh
]]--
function VCals.Models.VVD:Parse( fileName )
--Open the file in binary mode
local openFile = file.Open( fileName, "rb", "GAME" );
if( !openFile ) then
VCals.Debug:Print( Color( 255, 255, 0 ), "Unable to read file: " .. fileName );
return;
end;
local modelData = {
headers = self:ParseHeader( openFile )
};
modelData.fixupTables = {};
PrintTable( modelData.headers );
for curLOD = 1, modelData.headers.numLODs do
local fixupTable = self:ParseFixupTable( openFile );
if( fixupTable ) then
modelData.fixupTables[fixupTable.lod] = fixupTable;
end;
end;
modelData.vertexes = {};
--Read the vertexes
openFile:Seek( modelData.headers.vertexDataStart );
for vertexID = 1, modelData.headers.numLODVertexes do
table.insert( modelData.vertexes, self:ParseVertex( openFile ) );
end;
return modelData;
end;
Sorry, you need to Log In to post a reply to this thread.