Function to load DDS in "already made" models

Blitz3D Forums/Blitz3D Programming/Function to load DDS in "already made" models

I haven't tested this so much but until now
the function is working good,

1- First, create a new folder to save all
your .dds textures.
2- Convert any texture to dds and save it
into the folder USING THE SAME TEXTURE NAME but
with .dds extension of course.
3- Paste my code anywhere but be sure it is
scanned before your main loop so that
the Global variable gets the correct value.
Add the correct value to the Global, this is the
path to your dds folder (for example ".\DDS\")

4-Now, use the LOAD_DDS(mesh) for each
mesh you want to replace the textures, the function only works
for single meshes, not for childs, if you have an animated mesh
you will have to use the function with each child.

Basically the function search for a texture with the same name
into the dds's folder, and if found, the texture's brush is changed so
that it uses the dds texture.

;_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \/
Global dds_folder_path$=".\DDS" ;-> for example: ".\dds"
Function LOAD_DDS(mesh,flags_for_dds=1)
	Local c%=0
	Local s%=0
	Local br%=0
	Local tex%=0
	Local texname$=""

	Local ddspath$=""
	Local ddstex%=0

	Local ss$=""
	Local count%=0

	;check if DDS folder path is ok:
	dds_folder_path=Replace(dds_folder_path,"/","")
	If Not Right(dds_folder_path,1)="" Then dds_folder_path=dds_folder_path+""
	;--------------

	For c=1 To CountSurfaces(mesh)
		s=GetSurface(mesh,c)

		br=0 : tex=0 : ddstex=0
		
		br=GetSurfaceBrush(s)
		tex=GetBrushTexture(br,0) ; -> texture index =0
		texname=TextureName(tex)
		
		;extract only texture_name from path:
		For count=1 To Len(texname)
			ss=Right(texname,count)
			If Left(ss,1)="" Or Left(ss,1)="/"
			texname=Mid(ss,2)
			Exit
			EndIf
		Next
		;-------------
		
		texname=Left(texname,Len(texname)-3)+"dds"
		ddspath=dds_folder_path+texname

		ddstex=LoadTexture(ddspath,flags_for_dds)
		If ddstex>0
		BrushTexture br,ddstex,0,0 ; -> texture index =0
		PaintSurface s,br
		EndIf


		If br>0 Then FreeBrush br
		If tex>0 Then FreeTexture tex
		If ddstex>0 Then FreeTexture ddstex

	Next
End Function
;_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /\


Paolo.

can you not just open an "already made" file, find the texture filename strings and changed the extension to dds, or better yet, just use a program eg. ultimateunwrap3d?

wouldn't using this method also mean you have to load the old texture as well as the dds?

Or simply convert the mesh file :)

This will convert any mesh file and save a copy as "filename_dds.extension" with all texture file references replaced by .dds reference.
SwitchToDDS("RomanBath.b3d")
Print "Finito!"
WaitKey()
End

Function SwitchToDDS(file$)

	Local extmatch$[3]
	
	extmatch[0] = ".png"
	extmatch[1] = ".jpg"
	extmatch[2] = ".bmp"
	extmatch[3] = ".tga"

	dds = Asc(".") + (Asc("d") Shl 8) + (Asc("d") Shl 16) + (Asc("s") Shl 24)
	dot = Asc(".")

	count = 0
	
	f = ReadFile(file)
	If f
		
		ms = MilliSecs()
				
		bank = CreateBank(FileSize(file))
		ReadBytes(bank,f,0,BankSize(bank))
		CloseFile f

		For i = 0 To BankSize(bank)-5
			If PeekByte(bank,i) = dot
				a$ = Lower(Chr(PeekByte(bank,i)) + Chr(PeekByte(bank,i+1)) + Chr(PeekByte(bank,i+2)) + Chr(PeekByte(bank,i+3)))
				For j = 0 To 3
					If a = extmatch[j]
						PokeInt bank,i,dds
						count = count + 1
					EndIf
				Next
			EndIf
		Next

		DebugLog "SwitchToDDS("+Chr(34)+file+Chr(34)+")"
		If count = 0
			DebugLog "  No matches found"
		Else
			ext$ = Right(file,Len(file)-Instr(file,"."))
		
			f = WriteFile( Left(file$,Instr(file,".")-1)+"_dds."+ext )
			If f
				WriteBytes(bank,f,0,BankSize(bank))
				CloseFile f
			EndIf
		
			DebugLog "  Replaced "+count+" texture extensions"
		EndIf

		ms = MilliSecs() - ms
		DebugLog "  Finished in "+ms+"ms" 

	EndIf

End Function


I don't understand how I didn't see this

DDS textures will load when used by meshes that are loaded using Blitz3D's various load mesh commands.



... I simply thought only the LoadTexture() was working with dds ... :)

but anyway ... Fredborg !!! that function is great, and beyond
my understanding with all those "asc" "asc" and more "asc" :)

Paolo.

must have been the excitement of finally having dds support.
i'm excited about it too, although i'm not sure why as i don't have a project that would need them. :(

dds = Asc(".") + (Asc("d") Shl 8) + (Asc("d") Shl 16) + (Asc("s") Shl 24)
this is a macro to write a fourcc (four-character code), which is neater and simpler than writing each byte separately, instead you write it as an integer/dword and bit shift the ascii value by the appropriate amount to get it's integer equivalent.


... I simply thought only the LoadTexture() was working with dds ... :)



Yes, but when you have old models you need to change the structure so that they automatically load .dds's (if you have converted the old textures first of course). "New" LoadTexture() is good too but doesn't help with old models that have embedded non-dds texture references.

@Mustang: Go get a cup of coffee and scroll up a bit :)

Fredborg..is this upgrade means that Giles will be updated by DDS support?? I would like to buy it if it is case..

also, I can confirm the ALT+TAB bug,
minimizing the window while running in full screen make the
dds textures buggy ...


GF2-mx200 winxp DX9.0c

is this the case for all cards?


Paolo.

Ask Nvidia why they implemented a buggy/incorrect algorithm previous to GeForce4 (especially on your Geforce1 GPU which is what GF2 mx is) ...

btw: Alt - Tabbing always is critical with Blitz3D and fullscreen ... Not only with DDS.