Assigning textures to a cube ?

Blitz3D Forums/Blitz3D Beginners Area/Assigning textures to a cube ?

jwe(Posted 2005) [#1]
I want to create a cube (with CreateCube()) and assign each side of it with a different texture.
I could not find an example for this.
Thanks for any help


BlackJumper(Posted 2005) [#2]
I tackled this using some code by Birdie that makes a segmented cube rather than straightforward CreateCube().

Graphics3D 800, 600

Global burn = LoadAnimTexture("burnhole.jpg", 2, 128, 128, 0, 8)
Global raze = LoadAnimTexture("raze.jpg",2, 128, 128, 0, 8)
Global burnbrush = CreateBrush()
Global razebrush = CreateBrush()
BrushTexture burnbrush, burn, 0
BrushTexture razebrush, raze, 0

Global cam = CreateCamera()
PositionEntity cam, 0,8,-15
Global light = CreateLight()
Global cube = create_scube();CreateCube()

PositionEntity cube, 0, -2, 10
PositionEntity light, 10, 40, -20

Repeat
SeedRnd(MilliSecs())
For ii = 1 To 20
	CreateBurningCube()
Next
	

While Not KeyHit(1)
	Delay 100
	UpdateBurningCube()
	RenderWorld
	Flip
Wend 

WaitKey
For ditch.BurningCube = Each BurningCube
	Delete ditch
Next
Until MouseHit(1)

End
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Type BurningCube
	 Field itsmesh
	 Field x
	 Field y
	 Field z
	 Field lifetime
End Type

;==================================================================================
Function CreateBurningCube()
	newcube.BurningCube = New BurningCube
	newcube\x = Rnd(20)-10
	newcube\y = Rnd(20)-10
	newcube\z = Rnd(20)-10
	newcube\itsmesh = create_scube()
	PositionEntity newcube\itsmesh, newcube\x, newcube\y, newcube\z
	newcube\lifetime = 24 + Rnd(500)
End Function
;==================================================================================
Function UpdateBurningCube()
	For acube.BurningCube = Each BurningCube
		Select True
			   Case acube\lifetime > 15   ; burn the top
					If acube\lifetime < 25 Then
						burningbrush = CreateBrush()
						BrushTexture burningbrush, burn, 23 - acube\lifetime
						surf = GetSurface(acube\itsmesh, 5)
						PaintSurface surf, burningbrush
						FreeBrush burningbrush
					EndIf
			   Case acube\lifetime = 15
					black = CreateBrush (0,0,0)
					;BrushBlend black, 1
					BrushAlpha black, 0
					surf = GetSurface(acube\itsmesh, 5)
					PaintSurface surf, black
					FreeBrush black
			
			   Case acube\lifetime = 8
					black = CreateBrush (0,0,0)
					BrushAlpha black, 0
					For s = 1 To 4
						surf = GetSurface(acube\itsmesh, s)
						PaintSurface surf, black
					Next
					FreeBrush black
			   Case acube\lifetime < 8    ; burn the bottom
			
			   Case acube\lifetime = 0
				FreeEntity acube\itsmesh
			   Default
					razingbrush = CreateBrush()
					BrushTexture razingbrush, raze, 15 - acube\lifetime
					For s = 1 To 4
						surf = GetSurface(acube\itsmesh, s)
						PaintSurface surf, razingbrush
					Next
		End Select
		
	If acube\lifetime > 0 Then
		acube\lifetime = acube\lifetime - 1
	EndIf
	Next
End Function
;==================================================================================
Function create_scube()             ; original code by Birdie
	Local cmesh,surf,v0,v1,v2,v3
	cmesh=CreateMesh()
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,-1,1,-1,0,0)
	v1=AddVertex(surf,1,1,-1,1,0)
	v2=AddVertex(surf,-1,-1,-1,0,1)
	v3=AddVertex(surf,1,-1,-1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,-1,1,1,0,0)
	v1=AddVertex(surf,-1,1,-1,1,0)
	v2=AddVertex(surf,-1,-1,1,0,1)
	v3=AddVertex(surf,-1,-1,-1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,1,1,1,0,0)
	v1=AddVertex(surf,-1,1,1,1,0)
	v2=AddVertex(surf,1,-1,1,0,1)
	v3=AddVertex(surf,-1,-1,1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,1,1,-1,0,0)
	v1=AddVertex(surf,1,1,1,1,0)
	v2=AddVertex(surf,1,-1,-1,0,1)
	v3=AddVertex(surf,1,-1,1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,-1,1,1,0,0)
	v1=AddVertex(surf,1,1,1,1,0)
	v2=AddVertex(surf,-1,1,-1,0,1)
	v3=AddVertex(surf,1,1,-1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	surf=CreateSurface(cmesh)
	v0=AddVertex(surf,-1,-1,-1,0,0)
	v1=AddVertex(surf,1,-1,-1,1,0)
	v2=AddVertex(surf,-1,-1,1,0,1)
	v3=AddVertex(surf,1,-1,1,1,1)
	AddTriangle surf,v0,v1,v2
	AddTriangle surf,v1,v3,v2
	UpdateNormals cmesh
	Return cmesh
End Function


You can use these animstrips that are referenced in the code...
[img http:\\www.blackjumper.com\selling\burnhole.jpg]

[img http:\\www.blackjumper.com\selling\raze.jpg]


jwe(Posted 2005) [#3]
Isnt there a easier way ?
If it isnt, its better designing and skinning a cube in milkshape.


BlackD(Posted 2005) [#4]
Yes, you would be. :) Alternatively you could create a single texture with 6 textures in it, then use the VertexCoords commands to assign a different part of the image to each set of triangles and .. No wait - even that you couldn't do as each corner is comprised of one vertex, not three.

So yeah - impossible to do with CreateCube() >_<


big10p(Posted 2005) [#5]
even that you couldn't do as each corner is comprised of one vertex, not three.
Not true. CreateCube() creates a cube made up of six independant (unwelded) quads. This is needed to get the lighting right. If the corners were all sharing a single vertex, the cube lighting would be smoothed and look wrong. You can see the difference by using UpdateNormals - which smoothes the normals - on a cube.


BlackJumper(Posted 2005) [#6]
OK, well maybe this will help...

; Paints a CreateCube() with different colours
; coded by BlackJumper

Graphics3D 640,480 
SetBuffer BackBuffer() 

Global camera=CreateCamera() 
light=CreateLight() 
RotateEntity light,90,0,0 

PositionEntity camera, 0, 0, -10

thecube = MakeCube()

While Not KeyDown( 1 ) 
	TurnEntity thecube, 1,1,1
RenderWorld 


Flip 

Wend 

End 


;====================================================================================================
Function MakeCube()
				shape = CreateCube()
				EntityFX shape, 2

			;	DebugLog "childern of cube = " + CountChildren(shape)
			;	For kids = 1 To CountChildren(shape)
			;	DebugLog "childern of cube = " + CountChildren(shape)
			;	kidshape = GetChild(shape, kids)
			;	For n = 1 To CountSurfaces(kids)
			;	DebugLog n
			;		surf = GetSurface(kidshape, n)
			;		DebugLog "triangles in prism = " + CountTriangles(surf)
			;		VertexColor surf, n, 255,0,0
			;	Next
			;	Next
			
			;	DebugLog "surfaces of cube = " + CountSurfaces(shape)

				For n = 1 To CountSurfaces(shape)
					surf = GetSurface(shape, n)
			;	DebugLog n + "    number vertices = " + CountVertices(surf)
				verts = CountVertices(surf)

				For v = 1 To CountVertices(surf)
					VertexColor surf, v, 255-(255.0/verts)*v,Abs((255.0/verts)*v-128),(255.0/verts)*v
				Next
				Next
				
				UpdateNormals shape
	
	Return shape
	
End Function
;====================================================================================================


.... shows how to access individual faces (sortof)


Ross C(Posted 2005) [#7]
Or, just create your own cube :o) Look for skybox code in the archives. The function creates a cube, with 6 surfaces.


puki(Posted 2005) [#8]
This is simply what "jwe" wants.

Just replace the texture names with the ones you want to use

; Skybox example
; for the Skybox with Terragen tutorial
; 27th of Feb 2002
;
; Tracer
;
; modified by "puki" - Oct 2005

Graphics3D 640,480

cam = CreateCamera()
PositionEntity cam,0,0,-5

light = CreateLight()

cube = create_cubebox()
While Not KeyDown(1)
	TurnEntity cube,1,1,1	
	RenderWorld
	Flip
Wend


Function create_cubebox()
	mesh = CreateMesh()

	;front face

	brush = LoadBrush("front.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,-1,+1,-1,0,0
	AddVertex surface,+1,+1,-1,1,0
	AddVertex surface,+1,-1,-1,1,1
	AddVertex surface,-1,-1,-1,0,1
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	;right face
	brush = LoadBrush("right.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,+1,+1,-1,0,0
	AddVertex surface,+1,+1,+1,1,0
	AddVertex surface,+1,-1,+1,1,1
	AddVertex surface,+1,-1,-1,0,1
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	;back face
	brush = LoadBrush("back.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,+1,+1,+1,0,0
	AddVertex surface,-1,+1,+1,1,0
	AddVertex surface,-1,-1,+1,1,1
	AddVertex surface,+1,-1,+1,0,1
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	;left face
	brush = LoadBrush("left.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,-1,+1,+1,0,0
	AddVertex surface,-1,+1,-1,1,0
	AddVertex surface,-1,-1,-1,1,1
	AddVertex surface,-1,-1,+1,0,1
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	;top face
	brush = LoadBrush("top.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,-1,+1,+1,0,1
	AddVertex surface,+1,+1,+1,0,0
	AddVertex surface,+1,+1,-1,1,0
	AddVertex surface,-1,+1,-1,1,1
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	;bottom face	
	brush = LoadBrush("bottom.bmp",49)
	surface = CreateSurface(mesh,brush)
	AddVertex surface,-1,-1,-1,1,0
	AddVertex surface,+1,-1,-1,1,1
	AddVertex surface,+1,-1,+1,0,1
	AddVertex surface,-1,-1,+1,0,0
	AddTriangle surface,0,1,2
	AddTriangle surface,0,2,3
	FreeBrush brush

	EntityFX mesh,1 ; make fullbright
	Return mesh

End Function



jwe(Posted 2005) [#9]
Thanks a lot, it was more complicated than i thought.