UV help

Blitz3D Forums/Blitz3D Programming/UV help

Spike314(Posted 2012) [#1]
Hi all
I'm in the middle of creating my own level creator but I've hit a bit of a problem I can't seem to solve and hoping someone can help.

basic my editor is made of single meshes connected together using AddMesh but to optimize my my level i use Fitmesh and a copy of the original Mesh to create low polygon version but i can't seem to figure out the maths for UV to stay the same size as the Added meshes. and there is the problem.

here is the example.
Global FPS_Oldtime, FPS_Newtime, FPS_Ticks
Global FPS_Current,FPS_Final
Global FPS_SampleRate   = 5  ;Take a sample every N ticks
Global FPS_Samples      = 10 ;Samples to average (res of the average)
Global FPS_BufferIndex  = 1
Global FPS_Font
Dim FPS_Buffer(10)

Graphics3D 800,600,32,2

cam=CreateCamera()

l=CreateLight()

Type Test2
	Field Count,M,Mode
End Type

Type test
	Field M
	Field ID
	Field X,Y,Z,Yaw
End Type

Tex = LoadBrush("Textures\Default.png") 
Global M1=CreateMyCube()
EntityFX M1,1
HideEntity M1

For A=1 To 15
T.Test = New Test
			T\M = CopyMesh(M1)
			T\ID = 1
			T\X =  A*2
			T\Z =0
			T\Yaw = -90
			TransformEntity T\M,T\X,0,T\Z,T\Yaw 
		EntityPickMode T\M,2
Next

For A=1 To 15
T.Test = New Test
			T\M = CopyMesh(M1)
			T\ID = 1
			T\X =  0
			T\Z = A*2
			T\Yaw =180
			TransformEntity T\M,T\X,0,T\Z,T\Yaw 
		EntityPickMode T\M,2
Next

M2=CreateMesh()
EntityAlpha M2,0.5

Repeat

;SPACE TO OPTIMIZE
If KeyHit(57) OptmizeSelected(M2,Ent):FreeEntity M2:M2=0

If KeyHit(17) wire = 1 - wire :WireFrame wire

CameraPick Cam,MouseX(),MouseY()

;ADDMESH 
If MouseHit(3)
	If PickedEntity()<>0
		Ent = PickedEntity()
		If M2 = 0 Then M2=CreateMesh():EntityAlpha M2,0.5			
		AddMesh(PickedEntity(),M2)
		HideEntity PickedEntity()
	EndIf
EndIf

;PAINT SURFACE
If MouseHit(1)
	If PickedEntity()
		PaintSurface PickedSurface(),Tex
	EndIf
EndIf

If KeyDown(200) MoveEntity Cam,0,0,1
If KeyDown(208) MoveEntity Cam,0,0,-1
If KeyDown(205) MoveEntity Cam,1,0,0
If KeyDown(203) MoveEntity Cam,-1,0,0

If MouseDown(2)
	TurnEntity Cam,MouseYSpeed()/4,-MouseXSpeed()/4,0
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
EndIf


	UpdateWorld	
	RenderWorld
	Get_FPS()
	Flip

Until KeyHit(1)

Function TransformEntity(Mesh,X#,Y#,Z#,Yaw#)
	ThisPivot = CreatePivot()
	PositionEntity ThisPivot, X#, Y#, Z#, True
	RotateEntity ThisPivot, 0, yaw#, 0, True

	For S=1 To CountSurfaces(Mesh)
		su=GetSurface(mesh,S)
		For v% = 0 To CountVertices(su)-1
				TFormPoint VertexX#(su, v), VertexY#(su, v), VertexZ#(su, v), ThisPivot, 0
				Vx# = TFormedX#()
				Vy# = TFormedY#()
				Vz# = TFormedZ#()

				TFormNormal VertexNX#(su, v), VertexNY#(su, v), VertexNZ#(su, v), ThisPivot, 0
				Vnx# = TFormedX#()
				Vny# = TFormedY#()
				Vnz# = TFormedZ#()

				VertexCoords(Su,v,Vx#, Vy#, Vz#)
				VertexNormal(Su,v,Vnx#, Vny#, Vnz#)
		Next
	Next
	UpdateNormals(Mesh)
	FreeEntity ThisPivot
End Function

Function OptmizeSelected(Mesh,Source)
For T.Test = Each Test
	If Source = T\M
			M=CopyMesh(M1)				
			TransformEntity M,T\X,T\Y,T\Z,T\Yaw
	EndIf
Next
	

DebugLog "RightMost:"+RightMost(Mesh)
DebugLog "LeftMost:"+LeftMost(Mesh)
DebugLog "TopMost:"+TopMost(Mesh)
DebugLog "BottomMost:"+BottomMost(Mesh)
DebugLog "FrontMost:"+FrontMost(Mesh)
DebugLog "BackMost:"+BackMost(Mesh)

FitMesh M,LeftMost(Mesh),BottomMost(Mesh),BackMost(Mesh),RightMost(Mesh)-LeftMost(Mesh),TopMost(Mesh)-BottomMost(Mesh),FrontMost(Mesh)-BackMost(Mesh)

;///PROBLEM IS HERE
	For S=1 To CountSurfaces(M)
		Su=GetSurface(M,S)
		For V=0 To CountVertices(Su)-1
		TFormPoint VertexX(su,v),VertexY(su,v),VertexZ(su,v),Mesh,0
		
		VertexTexCoords su, v,TFormedX(),TFormedZ()
		Next
	Next

T.Test = New Test
	T\M = M
	T\ID = ID
	UpdateNormals M
	EntityPickMode M,2
End Function

Function LeftMost#(mesh)
    Local LeftX#= 6556856 ,RightX#=-6556856 
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexX(su,v)<LeftX# Then LeftX#=VertexX(su,v)
            If VertexX(su,v)>RightX# Then RightX#=VertexX(su,v)
        Next
    Next
    Return LeftX#
End Function
Function RightMost#(mesh)
    Local LeftX# = 6556856   ,RightX# =-6556856       
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexX(su,v)<LeftX Then LeftX=VertexX(su,v)
            If VertexX(su,v)>RightX Then RightX=VertexX(su,v)
        Next
    Next
    Return RightX#
End Function
Function TopMost#(mesh)
    Local BottomY#= 6556856,TopY#=-6556856    
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexY(su,v)<BottomY Then BottomY=VertexY(su,v)
            If VertexY(su,v)>TopY Then TopY=VertexY(su,v)
        Next
    Next
    Return TopY#
End Function
Function BottomMost#(mesh)
    Local BottomY#= 6556856,TopY#=-6556856 
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexY(su,v)<BottomY Then BottomY=VertexY(su,v)
            If VertexY(su,v)>TopY Then TopY=VertexY(su,v)
        Next
    Next
    Return BottomY#
End Function
Function FrontMost#(mesh)
    Local BackZ#= 6556856,FrontZ#=-6556856 
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexZ(su,v)<BackZ Then BackZ=VertexZ(su,v)
            If VertexZ(su,v)>FrontZ Then FrontZ=VertexZ(su,v)
        Next
    Next
    Return FrontZ#
End Function
Function BackMost#(mesh)
    Local BackZ#= 6556856,FrontZ#=-6556856 
    For s = 1 To CountSurfaces(mesh)
        su = GetSurface(mesh,s)
        For v = 0  To CountVertices(su)-1
            If VertexZ(su,v)<BackZ Then BackZ=VertexZ(su,v)
            If VertexZ(su,v)>FrontZ Then FrontZ=VertexZ(su,v)
        Next
    Next
    Return BackZ#
End Function

Function Get_FPS(PosX#=10,PosY#=2)
         If FPS_Font = 0 Then
              FPS_Font=LoadFont("New Times Roman",14)
         EndIf
         FPS_Newtime = MilliSecs()
         FPS_Ticks = FPS_Ticks + 1
         If FPS_Ticks > FPS_SampleRate Then
            FPS_Current = FPS_Newtime - FPS_Oldtime
            If FPS_Current = 0 Then FPS_Current = 1000 Else FPS_Current = 1000/FPS_Current
            FPS_Buffer(FPS_BufferIndex) = FPS_Current
            FPS_BufferIndex = FPS_BufferIndex + 1
            If FPS_BufferIndex > FPS_Samples Then
                 For FPS_Count = 1 To FPS_Samples
                     FPS_Master = FPS_Master + FPS_Buffer(FPS_Count)                      
                 Next
                 FPS_Final = FPS_Master / FPS_Samples
                 FPS_BufferIndex = 1
            EndIf
            FPS_Ticks = 0
         EndIf
         FPS_Oldtime = MilliSecs()
         SetFont(FPS_Font)
         Text(PosX#,PosY#,"FPS:"+FPS_Final+" Tris:"+TrisRendered())
         ;Return FPS_Final
End Function

Function ResizeMesh(mesh,width#,height#,depth#)
   
   ScaleMesh mesh,1/MeshWidth#(mesh)*width#,1/MeshHeight#(mesh)*height#,(1/MeshDepth#(mesh))*depth#

End Function

Function CreateMyCube()
		m=CreateMesh()
	;front face
	b=CreateBrush(0,0,255)
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=CreateBrush(255,0,0)
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=CreateBrush(0,255,255)
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=CreateBrush(255,0,255)
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=CreateBrush(255,255,0)
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;bottom face	
	b=CreateBrush(255,255,255)
	s=CreateSurface( m,b )
	AddVertex s,-1,-1,-1,1,0:AddVertex s,+1,-1,-1,1,1
	AddVertex s,+1,-1,+1,0,1:AddVertex s,-1,-1,+1,0,0
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	
	Return m
End Function


Last edited 2012


Stevie G(Posted 2012) [#2]
You already set the uv within the createmycube function so the uv's will be retained when you addmesh. They do not need to be transformed and recalculated, just leave them as they are.


Spike314(Posted 2012) [#3]
but when i use fitmesh it stretches the UV and that is what I'm trying to prevent. any help on this would be much appreciated.

Last edited 2012