Rear-view mirror - camera or mirror?

Blitz3D Forums/Blitz3D Programming/Rear-view mirror - camera or mirror?

puki(Posted 2005) [#1]
Hi.

When creating a rear-view mirror for a car driving game, like Driver/GTA3, you can simply create a viewport (the rear-view mirror) to display the view of a camera facing backwards. This works, however, the image in the viewport is not mirrored.

Is it possible to simply flip the viewport's, or even the rear-view camera's, display to show a true mirror image or do I have to manually create a mirror and do it that way (which may or may not be the intial answer)?

I was kind of hoping there would be a simple solution.


GfK(Posted 2005) [#2]
Use CopyRect to copy each vertical line in the 'mirror' image and flip it horizontally.

I might have some code somewhere...

[edit] Found it. Its a bit untidy, and not commented as it was never written for anybody else to use, but its not difficult to understand.

XX, YY, W and H are the X/Y coords, width and height of the rear view mirror image (which is drawn directly onto the screen with RenderWorld).

The rear view mirror image is then grabbed to an imagebuffer (tmp), flipped horizontally in the For/Next loop into imagebuffer tmp2, and then pasted back to its original position (XX,YY). Finally, the two temporary Imagebuffers are freed.

Function RearViewMirror()
	If CameraMode = 1 Or CameraMode = 3
		XX = (GraphicsWidth() / 2) - ((GraphicsWidth()/4)/2)
		YY = GraphicsHeight() *.05
		W = GraphicsWidth()/4
		H = GraphicsHeight()/12		
		tmp = CreateImage(W,H)
		GrabImage tmp,XX,YY
		tmp2 = CreateImage(W,H)
		SetBuffer ImageBuffer(tmp2)
		For X = 0 To W-1
			CopyRect X,0,1,H-1,W-X,0,ImageBuffer(tmp)
		Next
		Color 0,0,0
		Rect 0,0,ImageWidth(tmp2),ImageHeight(tmp2),False
		SetBuffer BackBuffer()
		DrawBlock tmp2,XX,YY
		FreeImage tmp2
		FreeImage tmp
	EndIf
End Function



puki(Posted 2005) [#3]
Oooh, thanks "GfK" - I'll take a snifter at that.

Interesting solution - I wouldn't have used CopyRect as I don't play around much with the 2D commands.

EDIT:
Hmmm - didn't work - sniff.


KuRiX(Posted 2005) [#4]
Perhaps you could render to an image buffer, then mirror the image (it is easy) and show the image...


puki(Posted 2005) [#5]
That's what "GfK" is doing - I've never used ImageBuffer before, so I'll have to play about with it.


big10p(Posted 2005) [#6]
I think you can horizontally/vertically flip a camera render by scaling it with negative values. Scale the camera, that is, e.g. ScaleEntity mirror_cam,-1,1,1 will mirror it, I think.

[edit] Tom, I think you're right. Oh well. :/


Tom(Posted 2005) [#7]
How about, use a sprite/quad for the mirror, flipping the U texture coords, then just copyrect from rear view cam to texture.

[Edit] big10P, I think negative scaling might cause a flipmesh type problem with all geometry.

voila!
Tom


puki(Posted 2005) [#8]
Mmm, "Scouse's" idea is interesting too.

I'll have to work out how to use the CopyRect with the camera though.


GfK(Posted 2005) [#9]
How about, use a sprite/quad for the mirror, flipping the U texture coords, then just copyrect from rear view cam to texture.
Reason I didn't do it that way, was because I still needed to render the camera, then copy it to a texture. The texture had to be base 2, but the dimensions of the rear view mirror were 4:1 or something.

So instead of farting about with scaling textures and messing with UVs, I went for the CopyRect method, which was plenty fast enough anyway.

I think you can horizontally/vertically flip a camera render by scaling it with negative values.
This works in a fashion, but it also flips the normals of any object the camera sees.

Mmm, "Scouse's" idea is interesting too.

I'll have to work out how to use the CopyRect with the camera though.
Assuming you can get the image onto your texture at the right size/scale, then the best way would be to use SpriteViewMode 2, and disable backface culling.


Damien Sturdy(Posted 2005) [#10]
Ive got some pretty interesting water going on using multi pass rendering, copyrect and sin/cos commands. This is how i did my mirror too BTW :)


_PJ_(Posted 2005) [#11]
Wow this stuff wont slow it down too much???


[Edit] big10P, I think negative scaling might cause a flipmesh type problem with all geometry.



I hope this isnt the case. I think it's probably the most efficient solution here.


Damien Sturdy(Posted 2005) [#12]
Course it will ;) mind you, ive done it before so i would know :P

Copyrect is the nicest way to do it :)