Camera as mirror?

Blitz3D Forums/Blitz3D Programming/Camera as mirror?

How do I create a rear-view mirror using camera 2? I think that the camera view should be flipped vertically.

camera01=CreateCamera()
camera02=CreateCamera()
CameraViewport camera01,0,0,1024,768
CameraViewport camera02,384,0,256,128
CameraClsColor camera01,128,191,255
CameraClsColor camera02,128,191,255


camera01=CreateCamera()
camera02=CreateCamera(camera02)
TurnEntity camera02,0,180,0
HideEntity camera02

in the main loop :
; disable parent, to hide camera01 without the 02
entityparent camera02,0
hideentity camera01
ShowEntity camera02
renderworld
=> Copyrect from the camera02 to a texturebuffer, and apply the texture to a mesh that have UV coord form 1 to 0 instead of 0 to 1 .
entityparent camera02,camera01
Hideentity camera02

then make your renderworld:flip etc...

Notice your rear view will be view with a quad that you'll parent to the camera01

Something like this :

;============== TEMPLATE ===================
;	<! CONST ===================================
	Const C_GW#			=	1024
	Const C_GH#			=	768
	Const C_GD%			=	0
	Const C_GM%			=	2
	Const C_Key_ESC%	=	1
	Const C_Key_WIR%	=	17
	Const C_Key_LF%		=	203
	Const C_Key_RT%		=	205
	Const C_Key_UP%		=	200
	Const C_Key_DN%		=	208
; 	================================= /! CONST >



	Graphics3D C_GW,C_GH,C_GD,C_GM
	SetBuffer BackBuffer()


; <! const user =================================
	Const C_PivH#	=	1.8 ; metres
;	===================================/! const >


	world	=	CreatePivot	()
	Piv		=	CreatePivot	(world)
	cam		=	CreateCamera(Piv)
				CameraClsColor cam,180,220,255
				CameraRange cam,.1,100

; <! Rear View ===================================
	RearViewSize	=	256
	RearQuad=	CreateMesh		(cam)
		s	=	CreateSurface	(RearQuad)
		v0	=	AddVertex		(s, -1.0,.75,1.01,+1,+0)
				AddVertex		(s, -.25,.75,1.01,+0,+0)
				AddVertex		(s, -.25,.00,1.01,+0,+1)
				AddVertex		(s, -1.0,.00,1.01,+1,+1)
				AddTriangle 	s,v0+0,v0+1,v0+2
				AddTriangle 	s,v0+0,v0+2,v0+3

	RearTex	=	CreateTexture(RearViewSize,RearViewSize,1)
	RearBuffer=	TextureBuffer(RearTex)

				EntityTexture	RearQuad,RearTex
				EntityFX RearQuad,1+8

	RearCam			=	CreateCamera(cam)
						TurnEntity	RearCam,0,180,0
						HideEntity	RearCam
	CameraViewport		rearCam,0,0,RearViewSize,RearViewSize
; ================================= /! Rear View >


; <! Insert Scene ================================

	Plan	=	CreatePlane(1,world)
				EntityColor plan,30,80,20
	For i = 1 To 10
		c	=	CreateCube(world)
				MoveEntity c,Rand(-50,50),1,Rand(-50,50)
	Next
	redCube=CreateCube()
			PositionEntity	redCube,-10,1,-10
			EntityColor		redCube,255,0,0

	blueCube=CreateCube()
			PositionEntity	blueCube,+10,1,+10
			EntityColor		blueCube,0,0,255

;	===================================/! Insert >

;	<! LOOP ===================================
	st%		=	MilliSecs()
	lt%		=	0
	Repeat
		; <! Time based Movement ===========
			mt%		=	MilliSecs()
			rdt%	=	mt-(lt+st)
			dt#		=	Float(rdt)
			If rdt<2	rdt=2
			If rdt>60	rdt=60
			lt		=	lt+dt
		; ==========/! Time based Movement >


			CAM_Maj_Cam	(Piv,cam,dt,0)

			; <! Update Rear View ============
				EntityParent	RearCam,0
				HideEntity		cam
				ShowEntity		RearCam
				RenderWorld
				CopyRect		0,0,RearViewSize,RearViewSize,0,0,BackBuffer(),RearBuffer
				EntityParent	RearCam,Cam
				HideEntity		RearCam
				ShowEntity		Cam
			; ============/! Update Rear View >

		UpdateWorld (dt#/30)
		RenderWorld
		; <! Insert Post render =============
			Text 10,10,"fps :"+FPS_GetFps(lt)
		; =============/!Insert Post render >
		; Touche sortie + touche "fil de fer"
			If KeyHit(C_Key_ESC) Exit
			If KeyHit(C_Key_WIR) wire=1-wire:WireFrame wire
		Flip
	Forever
;	===================================/! LOOP >
FreeEntity world
ClearWorld 1,1,1
EndGraphics()
End



Function CAM_Maj_Cam(Pivot%,Camera%,dt#,Methode%=0,Ground$="",Gravity#=0,Entity%=0)
	If camera<>0 And pivot<>0
		Select methode
			Case 0 ; fps like
				mx#		=	C_GW/2-MouseX()
				my#		=	C_GH/2-MouseY()
				MoveMouse	C_GW/2,C_GH/2
				Camrx#	=	EntityPitch (Camera,1)-my#/4
				Camry#	=	EntityYaw	(Pivot,1)+mx#/4
				camdx#	=	(KeyDown(C_Key_RT)-KeyDown(C_Key_LF))*DT#/50
				camdz#	=	(KeyDown(C_Key_UP)-KeyDown(C_Key_DN))*DT#/50
			Case 1 ; mode libre
				camdz#	=	(KeyDown(C_Key_UP)-KeyDown(C_Key_DN))*DT#/50
		End Select

		RotateEntity	Pivot,0,Camry#,0
		RotateEntity	camera,Camrx#,0,0
		MoveEntity		Pivot,camdx,0,camdz

		Select Upper(Ground)
			Case ""
				PositionEntity	Pivot,EntityX(Pivot,1),C_PivH,EntityZ(Pivot,1)
			Case "TERRAIN"
				Terr_y#	=	TerrainY(entity,EntityX(Pivot,1),0,EntityZ(Pivot,1))
				PositionEntity	camera,EntityX(camera,1),Terr_y+C_PivH,EntityZ(Pivot,1)
			Case "PICK"
				LinePick	(EntityX(Pivot,1),EntityY(Pivot,1),EntityZ(Pivot,1),0,-2*C_PivH,0)
				If PickedEntity()<>0
					PositionEntity	Pivot,EntityX(Pivot,1),PickedY()+C_PivH,EntityZ(Pivot,1)
				Else
					If Gravity>0
						MoveEntity Pivot,0,-Gravity*dt#/60,0
					EndIf
				EndIf
		End Select
	EndIf
End Function





Type Fps_Counter
 Field FpsT%
 Field FpsCur%
 Field FpsRate%
End Type

Function FPS_GetFps(Time%)
 fps.FPS_Counter=Last FPS_Counter
 If fps=Null fps.FPS_Counter=New FPS_Counter
 If time>fps\FpsT
  fps\FpsT=Time+1000
  fps\FpsRate=fps\FpsCur
  fps\FpsCur=0
 Else
  fps\FpsCur=fps\FpsCur+1
 EndIf
 Return fps\FpsRate
End Function



instead of using show and hide for the cameras use CameraProjMode

Oh i forgot it, you're right !

How do I create a mirror that's 256 units by 128 units centered on the top edge of the screen? How do I remove the code that allows me to move around with the mouse?

use cameraviewport(camera,startingx,startingy,endingx,endingy)

then, as long as your camera is pointed right it, and your using the right projmode it should work fine.

How do I remove the code that allows me to move around with the mouse?

mmm...maybe you should learn a little more Blitz3D before purchasing other exemple. You seem to ask something, that you don't understand mains functions .

=> however, comment the line "CAM_Maj_Cam (Piv,cam,dt,0)"

Here's the code so far:

Global speed#=0
Global steer#=0
Global gear%=0
Global stick%=0
Global gearstring$="Neutral"
Graphics3D 1024,768,32,1
SetBuffer BackBuffer()
plane=CreatePlane()
EntityColor plane,63,63,63
tex1=LoadTexture("buildingwall03.tga")
tex2=LoadTexture("concrete2.tga")
building=CreateCube()
ScaleMesh building,25,25,25
EntityTexture building,tex1
PositionMesh building,0,25,0
HideEntity building
sidewalk=CreateCube()
ScaleMesh sidewalk,35,1,35
EntityTexture sidewalk,tex2
PositionMesh sidewalk,0,1,0
HideEntity sidewalk
For x = 0 To 74
 For y = 0 To 74
  cube01=CopyEntity(building)
  cube02=CopyEntity(sidewalk)
  PositionEntity cube01,y * 100,1,x * 100
  PositionEntity cube02,y * 100,-1,x * 100
 Next
Next
world=CreatePivot()
pivot=CreatePivot(world)
camera01=CreateCamera(pivot)
CameraClsColor camera01,128,191,255
CameraViewport camera01,0,0,1024,768
MoveEntity camera01,50,5,0
RearQuad=CreateMesh(camera01)
s=CreateSurface(RearQuad)
v0=AddVertex(s,-0.25,1.00,1.01,+1,+0)
v1=AddVertex(s,0.25,1.00,1.01,+0,+0)
v2=AddVertex(s,0.25,0.50,1.01,+0,+1)
v3=AddVertex(s,-0.25,0.50,1.01,+1,+1)
AddTriangle	s,v0,v1,v2
AddTriangle	s,v0,v2,v3
RearTex=CreateTexture(256,128,1)
RearBuffer=TextureBuffer(RearTex)
EntityTexture RearQuad,RearTex
EntityFX RearQuad,1+8
camera02=CreateCamera(camera01)
CameraClsColor camera02,128,191,255
TurnEntity camera02,0,180,0
CameraProjMode camera02,0
CameraViewport camera02,0,0,256,128
light=CreateLight() 
RotateEntity light,0,90,0
gear=0
speed=0
If JoyType(0) Then stick=0
If JoyType(1) Then stick=1
If JoyType(2) Then stick=2
If JoyType(3) Then stick=3
While Not KeyDown(1)
 If JoyHit(13,stick) Then gear=gear-1:If gear<-1 Then gear=-1
 If JoyHit(14,stick) Then gear=gear+1:If gear>1 Then gear=1
 If gear=-1 Then gearstring="Reverse"
 If gear=0 Then gearstring="Neutral"
 If gear=1 Then gearstring="Drive"
 If JoyY(stick) < -0.05
  If gear=1
   speed=speed+0.005
   If speed>2 Then speed=2
   MoveEntity camera01,0,0,speed
  End If
  If gear=-1
   speed=speed-0.005
   If speed<-0.5 Then speed=-0.5
   MoveEntity camera01,0,0,speed
  EndIf
 End If
 If JoyY(stick) > 0.05
  speed=speed-0.01
  If speed<0 Then speed=0
  speed=speed*0.99
  MoveEntity camera01,0,0,speed
 End If
 If JoyY(stick) > -0.05 And JoyY(stick) < 0.05
  speed=speed*0.99
  MoveEntity camera01,0,0,speed
 End If	
 If JoyX(stick) < -0.05 And speed>0
  steer=-JoyX(stick)
  If steer>1 Then steer=1
  TurnEntity camera01,0,steer,0
 End If
 If JoyX(stick) > 0.05 And speed>0
  steer=-JoyX(stick)
  If steer<-1 Then steer=-1
  TurnEntity camera01,0,steer,0
 End If
 If JoyX(stick) < -0.05 And speed<0
  steer=JoyX(stick)
  If steer<-1 Then steer=-1
  TurnEntity camera01,0,steer,0
 End If
 If JoyX(stick) > 0.05 And speed<0
  steer=JoyX(stick)
  If steer>1 Then steer=1
  TurnEntity camera01,0,steer,0
 End If
 If JoyX(stick) > -0.05 And JoyX(stick) < 0.05 Then steer=0
 EntityParent camera02,0
 CameraProjMode	camera01,0
 CameraProjMode	camera02,1
 RenderWorld
 CopyRect 0,0,256,128,0,0,BackBuffer(),RearBuffer
 EntityParent camera02,Camera01
 CameraProjMode	camera02,0
 CameraProjMode	Camera01,1
 UpdateWorld
 RenderWorld 
 Text 0,0,"Car speed:"+speed*100
 Text 0,15,"Gear:"+gearstring
 Flip 
Wend 
End


Two problems: The mirror camera is tilted to the ground and the car stops suddenly in reverse.