BackBufferToTex texture reversed?

BlitzMax Forums/MiniB3D Module/BackBufferToTex texture reversed?

JetFireDX(Posted 2008) [#1]
I have been trying to port a chunk of code for realtime shadows from the Code archives for B3D to Minib3D.

The problem is I am having problems on my Mac using BackBufferToTex. It works but is yielding an image that is flipped vertically.

See here...

The ship is a model I am working on. The shadowy looking version of it is taken orthogonally from a second camera placed infront of the ship. The other plane has a simple texture for orientation. You can see in the image how the scene is appearing wrong. None of the objects or camera has any rotations applied to it.

Here's the code.

Import sidesign.minib3d
Include "primatives.bmx" 'Contains my CreatePlane()
Include "../Toolbox.bmx" 'the TMouse class is here for input.

mouse:TMouse = New TMouse

Global width=1280,height=720,depth=32,mode=0,rate=30
Graphics3D width,height,depth,mode,rate
'ClearTextureFilters()

Global mainCamera = CreateCamera()
CameraZoom(mainCamera,1.75)
CameraClsColor(mainCamera,64,64,64)

Global shadowTex = CreateTexture(256,256,1+16+32+256)

Global texCamera = CreateCamera()
CameraProjMode(texCamera,1) 'Make texCamera Orthogonal
CameraZoom(texCamera,1.75) 'Set zoom factor for texCamera
CameraClsColor(texCamera,255,255,255)
CameraViewport(texCamera,0,0,TextureWidth(shadowTex),TextureHeight(shadowTex))
HideEntity(texCamera) 'doShadowTex will show and hide this camera again.

Global texPlane = CreatePlane()
ScaleEntity(texPlane,5,5,5)
EntityTexture(texPlane,shadowTex)
PositionEntity(texPlane,10,0,30)

'For testing
Global plane = CreatePlane()
Global planeTex = LoadTexture("up.png")
ScaleEntity(plane,5,5,5)
EntityTexture(plane,planeTex)
PositionEntity(plane,10,10,30)

Global obj=LoadMesh("med2/Space Fighter.b3d")
ScaleMesh obj,0.25,0.25,0.25
EntityShininess(obj,0.35)
PositionEntity (obj,0,0,30)

Global light=CreateLight()
PositionEntity(light,10,10,0)

Global zoom:Float = 1.75
Global spd:Float = 1.0
Repeat
	
	Cls

	MoveEntity mainCamera,(KeyDown(KEY_D)-KeyDown(KEY_A))*spd,0,(KeyDown(KEY_W)-KeyDown(KEY_S))*spd
	
	mxs#=mxs#-(mouse.MouseXSpeed()/7.0)
	mys#=mys#+(mouse.MouseYSpeed()/7.0)
	
	RotateEntity mainCamera,mys#,mxs#,0

	If KeyDown(KEY_UP) Or KeyDown(KEY_DOWN)
		zoom = zoom + (KeyDown(KEY_UP)-KeyDown(KEY_DOWN))*0.01
		CameraZoom mainCamera,zoom
	End If

	mouse.update()
	mouse.flush()
	
	RotateEntity(obj,0,EntityYaw(obj)+1,0)

	doShadowTex(light,texCamera,shadowTex,obj)

	RenderWorld

	Flip

Until KeyHit(KEY_ESCAPE)

End

Function doShadowTex(lite:TEntity,cam:TCamera,inTex:TTexture,target:TEntity)

	PointEntity(lite,target)
	PositionEntity(cam,0,0,5)
	
	'For now we will waste time and memory by copying the mesh and doing our bidding with it.
	Local mesh = CopyEntity(target)
	Local brush = CreateBrush()
	BrushColor(brush,0,0,0)
	BrushShininess(brush,0.0)
	BrushBlend brush,2
	BrushFX brush,16
	HideEntity(target) 
	HideEntity(texPlane)
	
	PositionEntity(mesh,EntityX(target),EntityY(target),EntityZ(target))
	PaintEntity(mesh,brush)

	ShowEntity(cam)
	
	RenderWorld()
	
	BackBufferToTex(inTex)
	
	HideEntity(cam)

	ShowEntity(target)
	ShowEntity(texPlane)
	FreeEntity(mesh)

EndFunction


Comment out the mouse type and replace the CreatePlane with CreateCube or something and it should be the same. This is driving me crazy that the cube mapping demos work fine but this is wrong.


JetFireDX(Posted 2008) [#2]
Just checked it is the same with a cube, so I am pretty sure my UV coords in my CreatePlane() are good.


Sledge(Posted 2008) [#3]
This sounds familiar -- I used BackBufferToTex() for reflections a while back so the flipped image was quite handy. The quick fix is to alter the appropriate UV's I suppose.


JetFireDX(Posted 2008) [#4]
I ended up doing that in the code I was working on, but for something else I have going it would be nice to be able to just have it on the screen correctly.

No big deal. The ported realtime shadows are almost done. I am currently tweaking it to kill a couple bugs and "interesting" behavior in certain circumstances. Also it will be a lot nicer to handle as a blitzmax type instead of a couple functions floating around.