X files and color

Blitz3D Forums/Blitz3D Programming/X files and color

Does anyone know why vertex color will not load from an X file in blitz or in gile? (yes the flag is 2). Is it a non supported feature of X files or is it new in for DX9/8

DX7 X-File format does support vertex colours, but the question is does Blitz load them?

Do you have a mesh with vertex colours that you can try and load into Blitz...If not I will have a look for one.

[Edit] I hear that UU lets you paint vertex colours on a mesh, if you can do that and export to .X then you could try loading that mesh.

Ok, this is the last version of my DX exporter I can find and it does handle both textureUV and vertexcolors on the mesh I tried it with...not sure about vertex alpha though.

;savemeshdx9(mymesh,"mymesh.x") 
;(c) JMK 2005

Function savemeshdx9(mesh,file$)

NumberofSurfaces=CountSurfaces(mesh)
If NumberofSurfaces=0 Return

output=WriteFile(file$)
WriteLine output,"xof 0302txt 0064"
WriteLine output,""


;Header 
WriteLine output,"Header {"
WriteLine output,"1;"
WriteLine output,"0;"
WriteLine output,"1;"
WriteLine output,"}"
WriteLine output,""

;TX Matrix
WriteLine output,"Frame Root {"
WriteLine output," FrameTransformMatrix {"
WriteLine output,"  1.000000, 0.000000, 0.000000, 0.000000,"
WriteLine output,"  0.000000, 1.000000, 0.000000, 0.000000,"
WriteLine output,"  0.000000, 0.000000, 1.000000, 0.000000,"
WriteLine output,"  0.000000, 0.000000, 0.000000, 1.000000;;"
WriteLine output,"}"
WriteLine output,""



For s=1 To NumberofSurfaces
Surface=GetSurface(mesh,s)
MeshName$="Mesh Surface_"+s+" {"
WriteLine output,MeshName$
NumberofVertices=CountVertices(Surface)
WriteLine output,NumberofVertices+";"

For v=0 To NumberofVertices-2
WriteLine output,VertexX(Surface,v)+";"+VertexY(Surface,v)+";"+VertexZ(Surface,v)+";,"
Next
WriteLine output,VertexX(Surface,v)+";"+VertexY(Surface,v)+";"+VertexZ(Surface,v)+";;"
;WriteLine output,""

NumberofTriangles=CountTriangles(Surface)
WriteLine output," "+NumberofTriangles+";"

For t=0 To NumberofTriangles-2
index1=TriangleVertex(Surface,t,0)
index2=TriangleVertex(Surface,t,1) 
index3=TriangleVertex(Surface,t,2)
tl$=" 3;"+index1+","+index2+","+index3+";,"
WriteLine output,tl$
Next

index1=TriangleVertex(Surface,t,0)
index2=TriangleVertex(Surface,t,1) 
index3=TriangleVertex(Surface,t,2)
tl$=" 3;"+index1+","+index2+","+index3+";;"
WriteLine output,tl$


;WriteLine output,""


WriteLine output,"MeshMaterialList {"
WriteLine output,"1;"
WriteLine output,"1;"
WriteLine output,"0;;"
WriteLine output,""


WriteLine output,"Material {"
WriteLine output,"1.000000;1.000000;1.000000;1.000000;;"
WriteLine output,"0.000000;"
WriteLine output,"1.000000;1.000000;1.000000;;"
WriteLine output,"0.000000;0.000000;0.000000;;"

WriteLine output,"TextureFilename {"

;does his work?
b=GetSurfaceBrush(Surface)
bt=GetBrushTexture(b)
use_tex$=TextureName$(bt)
FreeTexture bt
FreeBrush b
; so strip the path
For i2= Len(use_tex$) To 1 Step -1
	If Mid$(use_tex$,i2,1)="" Then
	use_tex$=Right$(use_tex$,Len(use_tex$)-i2)
	Exit
EndIf
Next

If use_tex$="" Then use_tex$="xxx.jpg"

WriteLine output,Chr$(34)+use_tex$+Chr$(34)+";" 

WriteLine output,"}"
WriteLine output,"}"
WriteLine output,"}"



WriteLine output,"MeshNormals {"
WriteLine output,NumberofVertices+";"

For v=0 To NumberofVertices-1
vnx#=VertexNX(Surface,v)
vny#=VertexNY(Surface,v)
vnz#=VertexNZ(Surface,v)
l$=vnx#+";"+vny#+";"+vnz#+";"
If v=NumberofVertices-1 Then l$=l$+";" Else l$=l$+","
WriteLine output,l$ 
Next


WriteLine output," "+NumberofTriangles+";" 
For t=0 To NumberofTriangles-1
in1=TriangleVertex(Surface,t,0)
in2=TriangleVertex(Surface,t,1) 
in3=TriangleVertex(Surface,t,2)
l$=" 3;"+in1+","+in2+","+in3+";"
If t=NumberofTriangles-1 Then l$=l$+";" Else l$=l$+","
WriteLine output,l$ 
Next
WriteLine output,"}"

WriteLine output,"MeshTextureCoords {"
WriteLine output,NumberofVertices+";"
For v=0 To NumberofVertices-1
tc$=VertexU(Surface,v)+";"+VertexV(Surface,v)+";"
If v=NumberofVertices-1 Then tc$=tc$+";" Else tc$=tc$+","
WriteLine output,tc$
Next
WriteLine output,"}"

WriteLine output,"}"

;meshvertexcolors
WriteLine output,"MeshVertexColors {"
WriteLine output,NumberofVertices+";"
For v=0 To NumberofVertices-1
tc$=v+";"
tc$=tc$+Int(VertexRed#(Surface,v))+";"
tc$=tc$+Int(VertexGreen#(Surface,v))+";"
tc$=tc$+Int(VertexBlue#(Surface,v))+";"
tc$=tc$+VertexAlpha#(Surface,v)+";"
If v=NumberofVertices-1 Then tc$=tc$+";" Else tc$=tc$+","
WriteLine output,tc$
Next
WriteLine output,"}"
Next 


WriteLine output,"}"

WriteLine output,"}"

CloseFile output

End Function
 


Thanks for that,
Here is my mesh export i wrote. Ill compare the two later and see if ive missed anything out.

Global XE_XF,XE_MAXtextures

Type XE_texdata
	Field idx,h,fn$
End Type

;X Exporter:
Function writeX_as_children(filenameout$,mesh,meshname$)


XE_XF=WriteFile (filenameout);

;File version 
WriteLine XE_XF,"xof 0302txt 0064"
WriteLine XE_XF,""
;Header 
WriteLine XE_XF,"Header {"
WriteLine XE_XF,"1;"
WriteLine XE_XF,"0;"
WriteLine XE_XF,"1;"
WriteLine XE_XF,"}"
WriteLine XE_XF,""

WriteLine XE_XF,"//Exported By T.Ed Registered to "+user_name$


;Write all materials in mesh
XE_MAXtextures=0
RecursiveAddMaterial(mesh)


;Start of frame root
WriteLine XE_XF,"Frame "+meshname$+" {"
WriteLine XE_XF,"   FrameTransformMatrix {"
WriteLine XE_XF,"      1.000000,0.000000,0.000000,0.000000,"
WriteLine XE_XF,"      0.000000,1.000000,0.000000,0.000000,"
WriteLine XE_XF,"      0.000000,0.000000,1.000000,0.000000,"
WriteLine XE_XF,"      0.000000,0.000000,0.000000,1.000000;;"
WriteLine XE_XF,"   }"

RecursiveAddMesh(mesh)

WriteLine XE_XF,"} //End of root"
CloseFile XE_XF
End Function


Function RecursiveAddMaterial(h)
DebugLog CountChildren(h)
For cc =1 To CountChildren(h)
	chi=GetChild (h,cc)

	If EntityClass$(chi)="Mesh"
		;get neccacaries
		surf=GetSurface(chi,1)
		brush=GetSurfaceBrush(surf)
		tex=GetBrushTexture (brush)
		
		;get rid of tex path
		XE_XFilen$=strip_path(TextureName(tex))

		;mark possible 'new' texture index
		Tindex=XE_MAXtextures

		;see id texture is alreadt in database, if so make tindex match the existing one
		For txs.XE_texdata = Each XE_texdata
			If txs\fn=XE_XFilen$ Then noadd=1:Tindex=txs\idx
		Next

		;Save texture data in index with surface handle for an index key
		txs.XE_texdata = New XE_texdata
		txs\h = surf
		txs\idx = Tindex
		txs\fn = XE_XFilen$
		
		;only add texture if a new one is present
		If Not noadd
			WriteLine XE_XF,"Material dx_brush"+Str(Tindex)+" {"
			WriteLine XE_XF,"   1.000000;1.000000;1.000000;1.000000;;"
			WriteLine XE_XF,"   0.000000;"
			WriteLine XE_XF,"   1.000000;1.000000;1.000000;;"
			WriteLine XE_XF,"   0.000000;0.000000;0.000000;;"
			WriteLine XE_XF,"   TextureFilename {"
			WriteLine XE_XF,"      "+Chr(34)+XE_XFilen$+Chr(34)+";"
			WriteLine XE_XF,"   }"
			WriteLine XE_XF,"}"
			XE_MAXtextures=XE_MAXtextures+1
		EndIf 

		;do the cleaning
		FreeTexture Tex
		FreeBrush brush 
	EndIf 

	RecursiveAddMaterial(chi)
Next 

End Function



Function RecursiveAddMesh(h)

DebugLog CountChildren(h)

	For cc=1 To CountChildren(h)
		chi=GetChild (h,cc)
		chiname$=Replace$(EntityName(chi)," ","_") 
		;open branch frame
		WriteLine XE_XF,"   Frame "+ChiName$+" {"
		WriteLine XE_XF,"      FrameTransformMatrix {"
		WriteLine XE_XF,"         1.000000,0.000000,0.000000,0.000000,"
		WriteLine XE_XF,"         0.000000,1.000000,0.000000,0.000000,"
		WriteLine XE_XF,"         0.000000,0.000000,1.000000,0.000000,"
		WriteLine XE_XF,"         0.000000,0.000000,0.000000,1.000000;;"
		WriteLine XE_XF,"      }"
		
		If EntityClass$(chi)="Mesh"
			surf=GetSurface(chi,1)
			verts=CountVertices(surf)
			tris=CountTriangles(surf)
			;meshinfo
			WriteLine XE_XF,"      Mesh "+ChiName$+" {"


			;Number of verts;
			WriteLine XE_XF,"         "+verts+";"


			;X;Y;Z; of verts;
			For v=0 To verts-1
				If v=verts-1 Then term$=";;" Else term$=";,"
				WriteLine XE_XF,"         "+VertexX (surf,v)+";"+VertexY (surf,v)+";"+VertexZ (surf,v)+term$
			Next

			
			;No of tris;
			WriteLine XE_XF,"         "+tris+";"


			;Tri ordering 3;t0,t1,t2;,
			For t=0 To Tris-1
				If t=tris-1 Then term$=";;" Else term$=";,"
				WriteLine XE_XF,"         3;"+TriangleVertex(Surf,t,0)+","+TriangleVertex(Surf,t,1)+","+TriangleVertex(Surf,t,2)+term$
			Next 

			;Material List
			WriteLine XE_XF,"         MeshMaterialList {"
			WriteLine XE_XF,"            1;";num of materials for mesh
			WriteLine XE_XF,"            "+tris+";";number of faces/tris
			For t=0 To tris-1
				If t=tris-1 Then term$=";;" Else term$=","
				WriteLine XE_XF,"            0"+term$ ; face indexes?
			Next 
			WriteLine XE_XF,"            {dx_brush"+LookUpTindex(surf)+"}"
			WriteLine XE_XF,"         } // end of material list"

			;Normals List
			WriteLine XE_XF,"         MeshNormals {"
			WriteLine XE_XF,"            "+verts+";"
			For v=0 To verts-1
				If v=verts-1 Then term$=";;" Else term$=";,"
				WriteLine XE_XF,"            "+VertexNX (surf,v)+";"+VertexNY (surf,v)+";"+VertexNZ (surf,v)+term$
			Next
			WriteLine XE_XF,"            "+tris+";"
			For t=0 To Tris-1
				If t=tris-1 Then term$=";;" Else term$=";,"
				WriteLine XE_XF,"            3;"+TriangleVertex(Surf,t,0)+","+TriangleVertex(Surf,t,1)+","+TriangleVertex(Surf,t,2)+term$
			Next 
			WriteLine XE_XF,"         } // end of normal list"

			;Texturecoords List
			WriteLine XE_XF,"         MeshTextureCoords {"
			WriteLine XE_XF,"            "+verts+";"
			For v=0 To verts-1
				If v=verts-1 Then term$=";;" Else term$=";,"
				WriteLine XE_XF,"            "+VertexU (surf,v)+";"+VertexV (surf,v)+term$
			Next
			WriteLine XE_XF,"         } // end of Texturecoord list"

			;Vertexcolor List
			WriteLine XE_XF,"         MeshVertexColors {"
			WriteLine XE_XF,"            "+verts+";"
			For v=0 To verts-1
				If v=verts-1 Then term$=";;" Else term$=";,"
				vred#=VertexRed (surf,v)/256
				vgreen#=VertexGreen (surf,v)/256
				vblue#=VertexBlue (surf,v)/256
				WriteLine XE_XF,"            "+v+";"+vred+";"+vgreen+";"+vblue+";"+VertexAlpha (surf,v)+term$
			Next
			WriteLine XE_XF,"         } // end of Vertexcolor list"

			;End of mesh
			WriteLine XE_XF,"      } // end of mesh block for "+ChiName$



			 
		EndIf
		
		RecursiveAddMesh(chi)

		;close branch frame
		WriteLine XE_XF,"   } // End of frame"+ChiName$
	Next 

End Function




I can now get the vertex colors to work in blitz if i flag 2 ok, but isnt there a way to set flags in x(like you can in b3d), to tell blitz how to treat the mesh without having to resort to lots of recursive entityfx setting?

One thing that confuses me is writing of the mesh vertexcolors, you are doing them as actual RGBA values. I am using 0-1 range for colors ie rval#=red/255. Which seems to work ok in blitz, but which is correct?, i checked giles x xport and it does this color division as well.

The other thing im having problems with is texture scale, anyone know how can i introduce a scaled texture into the file.


I can now get the vertex colors to work in blitz if i flag 2 ok, but isnt there a way to set flags in x(like you can in b3d), to tell blitz how to treat the mesh without having to resort to lots of recursive entityfx setting?



EntityFX has no meaning in X-File format and I can't see a way of coding it into the file so that Blitz automatically enables vertex colour.


One thing that confuses me is writing of the mesh vertexcolors, you are doing them as actual RGBA values. I am using 0-1 range for colors ie rval#=red/255. Which seems to work ok in blitz, but which is correct?, i checked giles x xport and it does this color division as well.



Having checked the doc's it does look like 0.0f-1.0f is the correct range to use.


The other thing im having problems with is texture scale, anyone know how can i introduce a scaled texture into the file.



I think you would have to use the texture scale to modify the texture coordinates when you save them.