Texture problem with AddMesh...

Blitz3D Forums/Blitz3D Programming/Texture problem with AddMesh...

JA2(Posted 2006) [#1]
Hi, I'm trying to weld a group of objects together. I'm using AddMesh and then trying to texture the new model. The texture appears the same as if I were using seperate models. Is there any way to weld more than 1 models vertices together with other models and be able to texture them as a single model?

This is what I've got so far,

Graphics3D 640,480,16,2
SetBuffer BackBuffer()

;-------;
; Setup ;
;-------;

Camera	= CreateCamera()
Light	= CreateLight()

MoveEntity Camera,0,0,-10
TurnEntity Light,45,45,0

AmbientLight 100,100,100

;---------;
; Texture ;
;---------;

Texture = CreateTexture (32,32)

Color 0,0,150
Rect 0,0,32,32,True
Color 0,0,250
Rect 0,0,32,32,False

CopyRect 0,0,32,32,0,0,FrontBuffer(),TextureBuffer(Texture)

RotateTexture Texture,45

;-------;
; Scene ;
;-------;

Scene = CreateMesh()

CubeA = CreateCube()
CubeB = CreateCube()
CubeC = CreateCube()
CubeD = CreateCube()
CubeE = CreateCube()

PositionMesh CubeA,0,2,0
PositionMesh CubeB,0,-2,0
PositionMesh CubeC,-2,0,0
PositionMesh CubeD,2,0,0
PositionMesh CubeE,0,0,0

AddMesh CubeA,Scene
AddMesh CubeB,Scene
AddMesh CubeC,Scene
AddMesh CubeD,Scene
AddMesh CubeE,Scene

FreeEntity CubeA
FreeEntity CubeB
FreeEntity CubeC
FreeEntity CubeD
FreeEntity CubeE

EntityTexture Scene,Texture

;------;
; Loop ;
;------;

While Not KeyDown (1)

If MouseDown (1) Then WireFrame True Else WireFrame False

TurnEntity Scene,0.1,0.2,0.3

UpdateWorld
RenderWorld
					
Flip
Wend
End



b32(Posted 2006) [#2]
Well, the surfaces are merged into one surface. I think what you want it changing the texture coordinates. You can do that with VertexTexCoords. In the archives, there is a code for box wrapping. It can help as an example maybe:
http://www.blitzbasic.com/codearcs/codearcs.php?code=277


JA2(Posted 2006) [#3]
I'm getting strange results from using the box wrapping. The middle cube gets textured ok but the ones on the outside get kinda skewed.

Do the vertices of a model actually get welded together using the addmesh() command? If not, then how can I weld the boxes together? I see the occasional gap between the edges.


b32(Posted 2006) [#4]
No, they are not welded, but I'm sure there is a welding routine in the archives. For a cube-based shape, you need to generate texture coordinates for each side.
Say, for all sides that point to the front, you can ignore the Z-coordinate and base the UV coordinates on the XY coordinates. To make them fit the mesh, divide the XY coordinates with MeshWidth(). For the back sides goes the same, only the X coordinate should be reversed (-X).
For the left/right side, use Z and X, and for the upper/lower side, use X and Z.
For distinguising the different sides from each other, you could use the normals. For all front sides, the normal is either (0, 0, -1) or (0, 0, 1). You'd have to try that. Because the UpdateNormals() command in Blitz generates vertex normals rather than face normals, you could better try the MeshNormals routine here: http://www.blitzbasic.com/Community/posts.php?topic=64797
(It is a routine by Stevie, in his 2nd post in that topic.
) To use it, you need the Unweld function too (it is on the same page), however I think cubes, created with CreateCube() are unwelded by default.


Stevie G(Posted 2006) [#5]
Try this, using a MESHcull function I wrote. Notice in my version the trisrendered is only 52 as opposed to 60 normally.

Should be easy enough to understand but let me know if not.

Graphics3D 640,480,16,2
SetBuffer BackBuffer()

Const C_Front = 1
Const C_Rear = 4
Const C_Left = 8
Const C_Right = 2
Const C_Top = 16
Const C_Bottom = 32

;-------;
; Setup ;
;-------;

Camera	= CreateCamera()
Light	= CreateLight()

MoveEntity Camera,0,0,-10
TurnEntity Light,45,45,0

AmbientLight 100,100,100

;---------;
; Texture ;
;---------;

Texture = CreateTexture (32,32)

Color 0,0,150
Rect 0,0,32,32,True
Color 0,0,250
Rect 0,0,32,32,False

CopyRect 0,0,32,32,0,0,FrontBuffer(),TextureBuffer(Texture)

RotateTexture Texture,45

;-------;
; Scene ;
;-------;

Scene = CreateMesh()

;Version 1
;CubeA = CreateCube()
;CubeB = CreateCube()
;CubeC = CreateCube()
;CubeD = CreateCube()
;CubeE = CreateCube()

;Version 2
CubeA = MESHcull( CreateCube(), C_Bottom )
CubeB = MESHcull( CreateCube(), C_Top )
CubeC = MESHcull( CreateCube(), C_Right )
CubeD = MESHcull( CreateCube(), C_Left )
CubeE = CreateCube()

PositionMesh CubeA,0,2,0
PositionMesh CubeB,0,-2,0
PositionMesh CubeC,-2,0,0
PositionMesh CubeD,2,0,0
PositionMesh CubeE,0,0,0

AddMesh CubeA,Scene
AddMesh CubeB,Scene
AddMesh CubeC,Scene
AddMesh CubeD,Scene
AddMesh CubeE,Scene

FreeEntity CubeA
FreeEntity CubeB
FreeEntity CubeC
FreeEntity CubeD
FreeEntity CubeE

EntityTexture Scene,Texture

;------;
; Loop ;
;------;

While Not KeyDown (1)

If MouseDown (1) Then WireFrame True Else WireFrame False

TurnEntity Scene,0.1,0.2,0.3

UpdateWorld
RenderWorld

Text 0,0,TrisRendered()
					
Flip
Wend
End






Function MESHcull( Mesh , Flags = 0 )

	If Flags = 0 Return Mesh

	s = GetSurface( Mesh , 1 )
	Copy = CreateMesh()
	Cs = CreateSurface( Copy )
		
	For t = 0 To CountTriangles( s )-1
		;calculate triangle normal
		v0 = TriangleVertex( s, t, 0 )
		v1 = TriangleVertex( s, t, 1 )
		v2 = TriangleVertex( s, t, 2 )
		ax# = VertexX( s, v1 ) - VertexX( s, v0 )
		ay# = VertexY( s, v1 ) - VertexY( s, v0 )	
		az# = VertexZ( s, v1 ) - VertexZ( s, v0 )	
		bx# = VertexX( s, v2 ) - VertexX( s, v1 )
		by# = VertexY( s, v2 ) - VertexY( s, v1 )	
		bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )	
		Nx# = ( ay * bz ) - ( az * by )
		Ny# = ( az * bx ) - ( ax * bz )
		Nz# = ( ax * by ) - ( ay * bx )
		Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz )
		Nx = Nx / Ns
		Ny = Ny / Ns
		Nz = Nz / Ns
				
		OK = True
		For l = 0 To 5
			Flag = 2^l
			If ( Flags And Flag) = Flag
				;determine plane normal
				Select Flag
					Case C_Front	px# = 0 : py# = 0 : pz# = 1	
					Case C_Rear		px# = 0 : py# = 0 : pz# = -1	
					Case C_Left		px# = -1 : py# = 0 : pz# = 0	
					Case C_Right	px# = 1 : py# = 0 : pz# = 0	
					Case C_Top		px# = 0 : py# = 1 : pz# = 0	
					Case C_Bottom	px# = 0 : py# = -1 : pz# = 0	
				End Select
				If Nx = Px And Ny = Py And Nz = Pz OK = False
			EndIf
		Next
								
		If OK
			nv0 = AddVertex( Cs, VertexX( s , v0 ) , VertexY( s, v0 ) , VertexZ( s, v0 ) , VertexU( s, v0 ), VertexV( s, v0 ) )
			nv1 = AddVertex( Cs, VertexX( s , v1 ) , VertexY( s, v1 ) , VertexZ( s, v1 ) , VertexU( s, v1 ), VertexV( s, v1 ))
			nv2 = AddVertex( Cs, VertexX( s , v2 ) , VertexY( s, v2 ) , VertexZ( s, v2 ) , VertexU( s, v2 ), VertexV( s, v2 ))
			VertexNormal Cs , nv0 , Nx, Ny, Nz
			VertexNormal Cs , nv1 , Nx, Ny, Nz
			VertexNormal Cs , nv2 , Nx, Ny, Nz
			AddTriangle( Cs , nv0, nv1, nv2 )
		EndIf
		
	Next
		
	FreeEntity mesh
	Return copy
		
End Function		


Stevie


JA2(Posted 2006) [#6]
Thanks very much for the help :o)

I'm still having problems making the texture tile properly over the new mesh. I'll play around with it today and see if I can get it to work.


Stevie G(Posted 2006) [#7]
JA2,

It's your texture creation thats duff. Is this what you want?

;---------;
; Texture ;
;---------;

Texture = CreateTexture (32,32)
SetBuffer TextureBuffer( Texture )
	Color 0,0,150
	Rect 0,0,32,32,1
	Color 0,0,250
	Line 0,0,31,31
	Line 31,0,0,31
SetBuffer BackBuffer()



JA2(Posted 2006) [#8]
lol, yeah I noticed that too earlier. Silly me for rotating the texture :o)

Thanks for all the help.