MiniB3D Thread 2

BlitzMax Modules Forums/MiniB3D/MiniB3D Thread 2

Hmm. Little sprite issue (might be just because I haven't used Blitz3D in forever, so I forget things). Copy and paste the following code [*disclaimer* the terrain code is not mine; this just shows the issue]:

Strict

Import "../MiniB3D.bmx"

Local width=800,height=600,depth=32,mode=0
Local move =2.0

Graphics3D width,height,depth,mode

Local  cam:TEntity = CreateCamera()
PositionEntity cam,0,0,-10

Local spr:TSprite = LoadSprite("button_3.png",,cam)
PositionEntity spr,0,0,5

Local light:TEntity = CreateLight(1)

Local terrain:TEntity = LoadTerrain("terrtest.png")
Local tex:TTexture =  LoadTexture("grass.png")

PositionEntity terrain,0,-100,0
ScaleEntity terrain,4,50,4

EntityTexture terrain,tex

Local cx#=0
Local cy#=0
Local cz#=0

Local pitch#=0
Local yaw#=0
Local roll#=0

' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	' control camera

	If KeyDown(KEY_UP) Then cz#=cz#+move
	If KeyDown(KEY_LEFT) Then cx#=cx#-move
	If KeyDown(KEY_RIGHT) Then cx#=cx#+move
	If KeyDown(KEY_DOWN) Then cz#=cz#-move
	
	If KeyDown(KEY_W) Then pitch#=pitch#-1.0
	If KeyDown(KEY_A) Then yaw#=yaw#+1.0
	If KeyDown(KEY_S) Then pitch#=pitch#+1.0
	If KeyDown(KEY_D) Then yaw#=yaw#-1.0

	MoveEntity cam,cx#*0.5,cy#*0.5,cz#*0.5
	RotateEntity cam,pitch#,yaw#,roll#
	
	cx#=0
	cy#=0
	cz#=0

	cx#=0
	cy#=0
	cz#=0

	RenderWorld
	renders=renders+1

	' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
	
	DebugText 0,0,"FPS: "+String(fps)

	Flip


Wend
End

Function LoadTerrain:TMesh(url:Object,parent:TEntity=Null)

	Local pix:TPixmap

	If TPixmap(url)
		pix = TPixmap(url)
	Else
		pix = LoadPixmap(url)
		If pix = Null Return Null
	EndIf

	Local mesh:TMesh = CreateMesh(parent)
	Local surf:TSurface = CreateSurface(mesh,Null)

	Local uscale:Float = 1.0/pix.width
	Local vscale:Float = 1.0/pix.height

	For Local x:Int = 0 Until pix.width
		For Local y:Int = 0 Until pix.height
			Local argb:Int = pix.ReadPixel(x,y)
			Local height:Float = (((argb Shl 16) & $FF) + ((argb Shl 8) & $FF) + (argb & $FF))/(3.0*255.0)

			Local v:Int = AddVertex(surf,x,height,y,x*uscale,y*vscale)
			VertexNormal surf,v,0,0,0
		Next
	Next

	For Local x:Int = 0 Until pix.width-1
		For Local y:Int = 0 Until pix.height-1
			Local v0:Int = x*pix.height + y
			Local v1:Int = v0 + 1
			Local v2:Int = v1 + pix.height
			Local v3:Int = v0 + pix.height
			
			Local t0:Int = AddTriangle(surf,v0,v1,v2)
			Local t1:Int = AddTriangle(surf,v0,v2,v3)
			
			'
			' Simple normal fix
			Local nx0:Float = surf.TriangleNX(t0)
			Local ny0:Float = surf.TriangleNY(t0)
			Local nz0:Float = surf.TriangleNZ(t0)
			Local nx1:Float = surf.TriangleNX(t1)
			Local ny1:Float = surf.TriangleNY(t1)
			Local nz1:Float = surf.TriangleNZ(t1)
						
			VertexNormal surf,v0,surf.VertexNX(v0)+nx0+nx1,surf.VertexNY(v0)+ny0+ny1,surf.VertexNZ(v0)+nz0+nz1
			VertexNormal surf,v1,surf.VertexNX(v1)+nx0    ,surf.VertexNY(v1)+ny0    ,surf.VertexNZ(v1)+nz0
			VertexNormal surf,v2,surf.VertexNX(v2)+nx0+nx1,surf.VertexNY(v2)+ny0+ny1,surf.VertexNZ(v2)+nz0+nz1
			VertexNormal surf,v3,surf.VertexNX(v3)+nx1    ,surf.VertexNY(v3)+ny1    ,surf.VertexNZ(v3)+nz1
		Next
	Next

	'
	' Normalize vertex normals
	For Local v:Int = 0 Until CountVertices(surf)
		Local nx:Float = surf.VertexNX(v)
		Local ny:Float = surf.VertexNY(v)
		Local nz:Float = surf.VertexNZ(v)
		Local d:Float = 1.0 / Sqr(nx*nx + ny*ny + nz*nz)
		VertexNormal surf,v,nx*d,ny*d,nz*d
	Next

	Return mesh
End Function



You can get the 3 image files you need below (right-click/save image as):









OK. Now use the W key to pitch 360 degrees. For half of the trip, the parented sprite faces the camera. For the other half, it faces away (or is inverted). Is this a bug in MiniB3D (shouldn't a parented sprite always face its parent.... or the camera at least?), or is it something I'm missing?