Here's the code I used:
` Analyzes an InstaTree mesh file to find which vertices correspond to the trunk (material 0)
` and which the leaves (material 1). The program assumes that the first n vertices have material 0
` and the remaining m have material 1 where n+m = total number of vertices.
` It also assumes that the poly data occur before the material list data.
` Functionally the same as original but uses Matrix1 memory banks instead.
` Green Gandalf 21 December 2013, edited 23 December 2013.
sync
set dir "objects"
make bank from file 1, "tree_4.x"
` find start of vertex data - poly data then follows
` assumes indicated by following line in the X file (without the `)
` Mesh staticMesh {
bankSize = get bank size(1)
print " bank size = ", bankSize
endOfLine$ = chr$(13)+chr$(10) ` just a convenient way of representing the end of line pair of bytes
offset0 = 0
repeat
` find next end of line
offset1 = search bank(1, endOfLine$, offset0)
lineLength = offset1 - offset0
temp$ = bank string$(1, offset0, lineLength)
offset0 = offset1 + 2 ` skip end of line pair
` check for unexpected end of file
if offset0 >= bankSize then print "Oops! Near end of file at offset0 = ", offset0, "!" : wait key : end
until trim$(temp$) = "Mesh staticMesh {"
` find next end of line and read the vertex count
offset1 = search bank(1, endOfLine$, offset0)
lineLength = offset1 - offset0
temp$ = bank string$(1, offset0, lineLength)
offset0 = offset1 + 2 ` skip end of line pair
vMax = val(left$(temp$, len(temp$)-1)) ` remove the semicolon at the end of the string first
` now skip past the vertex data - the pair ;; marks the end of the data list
offset1 = search bank(1, ";;", offset0)
offset0 = offset1 + 4 ` skip end of line pair as usual as well
` find next end of line and read the poly count
offset1 = search bank(1, endOfLine$, offset0)
lineLength = offset1 - offset0
temp$ = bank string$(1, offset0, lineLength)
offset0 = offset1 + 2 ` skip end of line pair
pMax = val(left$(temp$, len(temp$)-1)) - 1 ` remove the semicolon at the end of the string first
dim polys(pMax, 2)
for p = 0 to pMax
` find the three vertex numbers for each poly
` find next end of line, read the vertex index numbers and store for use later
offset1 = search bank(1, endOfLine$, offset0)
lineLength = offset1 - offset0
temp$ = bank string$(1, offset0, lineLength)
offset0 = offset1 + 2 ` skip end of line pair
` next four lines are same as before
temp = len(first token$(temp$, ";")) : temp$ = ","+right$(temp$, len(temp$) - temp)
polys(p, 0) = val(next token$(","))
polys(p, 1) = val(next token$(","))
polys(p, 2) = val(next token$(","))
next p
` now find the material list header
offset1 = search bank(1, "MeshMaterialList {", offset0)
offset0 = search bank(1, endOfLine$, offset1) + 2
` skip the number of materials - we know there are 2 don't we?
offset0 = search bank(1, endOfLine$, offset0) + 2
` and the number of polys - we know there are 1+pMax of those :)
offset0 = search bank(1, endOfLine$, offset0) + 2
polyID = -1
` now scan the material list to find the first occurrence of material 1
for p = 0 to pMax
offset1 = search bank(1, endOfLine$, offset0)
temp$ = bank string$(1, offset0, offset1 - offset0)
offset0 = offset1 + 2 ` skip end of line pair
material = val(first token$(temp$, ",")) ` we don't need to worry about the final ";" because we end before then
if material = 1 then last = p-1 : exit ` found the first occurrence of material 1
next p
` now find the last vertex used for material 0
lastVertex = polys(0, 0) ` the last vertex can be initialised to this one
for p = 0 to last
lastVertex = max(lastVertex, max(polys(p, 0), max(polys(p, 1), polys(p, 2))))
next p
` finally find the two texture filenames
` find trunk texture first
offset0 = search bank(1, chr$(34), offset0) ` found opening "
inc offset0
offset1 = search bank(1, chr$(34), offset0) ` found closing "
barkTextureName$ = bank string$(1, offset0, offset1 - offset0)
offset0 = offset1 + 2 ` skip end of line pair
` now find the leaf texture in the same way
offset0 = search bank(1, chr$(34), offset0) ` found opening "
inc offset0
offset1 = search bank(1, chr$(34), offset0) ` found closing "
leafTextureName$ = bank string$(1, offset0, offset1 - offset0)
` print results
print " last vertex found = ", lastVertex, ", total vertices = ", vMax
print " bark texture file = ", barkTextureName$
print " leaf texture file = ", leafTextureName$
wait key
end
I attach a sample X file used for testing.
All I need to do is to add this code to my other code which changes the X file data and texture.
I'm sure my code could be optimised and improved - but it works nicely for the objects in question.
Aside Odd. As I was typing this the following message popped up on my screen:
Quote: "The MySQL server had problems trying to fulfill that last query, it might be under heavy load right now or it might have been a linking/query error. Please try again in 10 minutes. If it happens again drop a note to paul@thegamecreators.com trying to give as much detail as possible"