News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Read Files obj

Started by stevmjon, October 04, 2021, 01:52:34 AM

Previous topic - Next topic

stevmjon

howdy

i have just made some code that reads .obj files. this is the first time i have done this, so can someone look at the code demo and tell me if i have gone about this the correct way.
i only wanted some of the code in the file, so focused only on that. also i would put this data into another array for use in game, but didn't do this for this test demo, nor did i use the surface data in the .mtl file for textures (i left it in there anyway).
i just wanted to see the data on screen and see if it was correct.

the commands i used was SplitToArray() that uses a temp array to read the data.
> first searched the file for "v " (v + space) as the first data on the line, then read the text data into float array.
> then searched the file for "f " (f + space) as the fisrt data on the line, then read the text data into string array (step 1/2).
   >then read that string array to split it by the "//" , as there was numbers before and after this, then put data into int array (step 2/2).

you can use notepad / notepad 2.0 to see the data yourself.

  i appreciate any advice, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin



Just putting this link here for reference.   

OBJ FILE FORMAT

stevmjon

thanks for the link kev, that has interesting reference i didn't know about.

the point of posting this thread, was mainly to know if my use of "SplitToArray" was the correct way to read the string data, one line at a time.

> so, reading the vector data "v " was straight forward, i only used "SplitToArray" once per line here. the data was seperated by blank spaces so was simple check.
> then, reading the face data "f " was multiple use of "SplitToArray" per line. first i needed the data between spaces, then needed each seperate data checked again by seperating data by // .

so is this correct, or should i go through the "f " line one character at a time instead, grabbong data this way? i don't know how check one character at a time...

  any advice is welcomed, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#3
 
Quote
 so is this correct, or should i go through the "f " line one character at a time instead, grabbong data this way? i don't know how check one character at a time.

 


 I was writing an example but life got in the way, but prolly both ways.  So for lines that are always known to be simple, like a vertex with 3 values separated by spaces, then split is a nice easy and quick way.  But if the values have some secondary data, then parsing is character by character might indeed be a better way.    You can prolly get away with doing an instring on the face lines for Hash characters and then choosing how to proceed.   I don't really know what formats to expect so I can't really help there.    Might be an idea to have a look at some more object files.  See if any common and easy patterns seem obvious.    



PlayBASIC Code: [Select]
Function LoadOBj(File$)

if Fileexist(file$)=false then exitfunction false

size=filesize(file$)
fh=readnewfile(file$)
if fh
// Load entire file into a string
s$=readchr$(fh,Size)
closefile fh

// Strip line feeds
s$=replace$(s$,chr$(10),"")


// Split to this array
Dim Obj$(10000)
RowCount = splittoarray(s$,chr$(13),Obj$())

// Array use to translate floats into
Dim _FloatArray#(256)


// Scan the file for control codes
For lp =0 to RowCount-1

s$=Obj$(lp)


ThisCHR = mid(s$,1)

// check for comment row, if so, skip
if ThisCHR=asc("#") then continue


Pos = instring(s$," ")
if pos<1 then continue ; Skip with no spaces
Opcode$=left$(s$,pos-1)
Params$ =cutleft$(s$,pos)

// ---------------------------------------------------
Select Opcode$
// ---------------------------------------------------

// --------------------------------------------------
// v = Vertex
// --------------------------------------------------
case "v"
// Vertex data separated by spaces
Count= splittoarray(Params$," ",_FloatArray#())
if Count=3
print _FloatArray#(0)
print _FloatArray#(1)
print _FloatArray#(2)
else
print "Error in vertex ?"
endif

// vn = vertex normal
case "vn"

// vt = vertex face ?
case "vt"


// --------------------------------------------------
// f = Face
// --------------------------------------------------
case "f"


EndSelect

next


endif



EndFunction Status









stevmjon

thanks for the input kev.

thought i would post a screenshot of the file loaded into my game, and displayed as wireframe for now.
the object is the house, and i added a few cubes in the scene as well.

very exciting for me, as this is the first time i have done this. so now i can make 3D objects using 3D software, then load them into the game, yay.

stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.