Generating Freeword3D Road mesh from ASCII data?
Blitz3D Forums/Blitz3D Programming/Generating Freeword3D Road mesh from ASCII data?
| ||
Hi, i just bought Freeworld3D mainly for its road generation but it does not export the mesh as an object. Instead, it generates an ascii file and the author provides a formula for importing into other 3D engines. Can anyone help me get this into B3D so that i can export it as a mesh? http://www.freeworld3d.org/tutorials/roadsystem.html |
| ||
The format is very easy. Try to do it yourself. you need to create a mesh using Createmesh, then CreateSurface(mesh), then AddVertex the number of Vertices (x,y,z,u,v and normals every line) as defined in the header, then use Addtriangle to use the previously created Vertices by Index to create a number of Triangles. That's all you may also want to PaintMesh the mesh with the LoadBrush'ed texture file as defined in the file header, as well as EntityAlpha for the Opaqueness value. Probably there can be multiple meshes in one file (see first parameter "Road Count"), not sure of that. To read the file you may use re=OpenFile("mesh.asc") num_roads=readline(re) road_name$=readline(re) road_texfile$=readline(re) road_opaque#=readline(re) road_verts=readline(re) for i=0 to road_verts-1 lin$=readline$(re) ; parse line for x,y,z,u and v and use AddVertex with it ; Also parse the normals (nx,ny,nz) and set them for this vertex next road_tris=readline(re) While not EOF(re) lin$=readline$(re) print lin$ ; dump to screen for debugging ; here you may parse lin$ for indexes, they come in groups of 3, each group makes a triangle, using Addtriangle with it. wend closefile re |
| ||
LOL, i just about got that far but im lacking in the string handling departement.. i think im getting there :) How am i going to separate each number from each line? This is the bit im having trouble with.. sounds like a trivial task i know but i cant get my head round it at the moment. Your code does help a lot though, thanks :) |
| ||
Actually, give me some time, this is easier than i think. |
| ||
seperating numbers is more or less fiddling work, using Mid$, You may move everything to an array, eg: ; read the x y z u v nx ny nz block dim n#(road_verts*8) b$="" count=0 ;... for j=0 to road_verts-1 lin$=readline(re) for i=1 to len(lin$) mi$=(lin$,i,1) b$=b$+mi$ if (mi$=" ") or i=len(lin$) n(count)=b$ b$="" count=count+1 endif next next Now you should have the data in the array n(), groups of 8 values. Of course you could do it somehow else, surely better. That's why I said fiddling. |
| ||
I was just going to post begging for some more help, you read my mind :) |
| ||
Btw, I have edited it slightly, so you see how to parse every line of the vertex info block. The Index Block may be parsed about the same way. Yeah, I'm a medium. Hey - stop thinking THAT! |
| ||
Jfk, im still having probs getting the mesh onscreen. This is what i have at the moment. Can you see where i am going wrong? I have noticed that the last set of numbers are in just one long line in the text file. I have also found that the decimal places once converted from string are rounded off a few digits?? Graphics3D 800,600,32,2 WireFrame True re=OpenFile("d:\roads.txt") cam=CreateCamera() num_roads=ReadLine(re) road_name$=ReadLine(re) road_texfile$=ReadLine(re) road_opaque#=ReadLine(re) road_verts=ReadLine(re) mesh=CreateMesh() surface=CreateSurface(mesh) count=0 b$="" For k=1 To road_verts txt$=ReadLine$(re) For i=1 To Len(txt$) mi$=Mid$(txt$,i,1) b$=b$+mi$ If mi$=" " Or i=Len(txt$) count=count+1 Select count Case 1 x#=b Case 2 y#=b Case 3 z#=b Case 4 u#=b Case 5 v#=b Case 6 nx#=b Case 7 ny#=b Case 8 nz#=b End Select b$="" If i=Len(txt$) count=0 AddVertex(surface,x#,y#,z#,u#,v#) VertexNormal surface,0,nx#,ny#,nz# Print x+" "+y+" "+z+" "+u+" "+v+" "+nx+" "+ny+" "+nz EndIf EndIf Next Next indice_count=ReadLine$(re) txt$=ReadLine$(re) For i=1 To Len(txt$) mi$=Mid$(txt$,i,1) b$=b$+mi$ If mi$=" " Or i=Len(txt$) count=count+1 tcount=tcount+1 Select tcount Case 1 v0=b Case 2 v1=b Case 3 v2=b End Select If tcount=3 tcount=0 AddTriangle(surface,v0,v1,v2) EndIf b$="" EndIf Next Print i Print count PositionEntity mesh,0,0,0 RenderWorld Flip WaitKey |
| ||
Right now I don't see where the problem is. With this fiddling stuff always make sure to write a lot of debuging and control info to the screen to make sure the data is read in correctly. Try this: cam=CreateCamera() translateentity cam,0,2,-30 ; <<<<<<<< add this and maybe you need to clear b$ before you start parsing the last line (indicies) BTW yes Blitz may round some floating point numbers to the single precision Blitz is using. Shouldn't be a problem. |
| ||
Cool, ill give it a go |
| ||
I done it! The only problem is, the road does not sit perfectly on the matching heightmap in 3dws.. i think its to do with the numbers being rounded off in the Blitz conversion. Is there any way to preserve thoes numbers? |
| ||
Maybe you need to weld the vertices. I think there's an example in the archives. |
| ||
Wow, this Freeworld3D 2.0 looks cool It is cheap too. The ' Road System' is a feature that I have longed for in a terrain creator. The ' Vegetation System' seems interesting too. Anything that requires me doing less work is worth buying. |
| ||
Andy, is it a depth buffer fighting problem?, does it flicker at all? You could try adding a small value to the height (Y value) of the vertices maybe? |