Help with projecting verts
Blitz3D Forums/Blitz3D Programming/Help with projecting verts
| ||
Hi, In my quest for shadows maps I still need a way to project vertices into camera space without values being clipped when the point is behind the near camera plane. This code copies Blitzs CameraProject(), but I'm 'hoping' there's a way to extend it to provide good values when Z < 0, if anyone feels upto trying, give this code a try: Graphics3D 800,600,32,2 Global ScreenX# Global ScreenY# Global grWidth = GraphicsWidth() Global grHeight = GraphicsHeight() Global grHalfWidth = grWidth * .5 Global grHalfHeight = grHeight * .5 cam=CreateCamera() CameraRange cam,.001,10 PositionEntity cam,0,0,0 cZoom# = 1.0 sphere=CreateSphere() ScaleEntity sphere,.1,.1,.1 x# = 1 y# = 0 z# = 2 While Not KeyHit(1) If KeyDown(2) cZoom=cZoom - .02 If KeyDown(3) cZoom=cZoom + .02 If KeyDown(203) x=x-.1 If KeyDown(205) x=x+.1 If KeyDown(200) y=y+.1 If KeyDown(208) y=y-.1 If KeyDown(30) z=z+.05 If KeyDown(44) z=z-.05 CameraZoom cam,cZoom PositionEntity sphere,x,y,z RenderWorld Text 10,0,"X: "+x Text 10,14,"Y: "+y Text 10,28,"Z: "+z Text 10,44,"Zoom: "+cZoom CameraProject cam,x,y,z Text 10,70,"BB Projected: "+ProjectedX()+","+ ProjectedY() WorldToScreen(cZoom,x,y,z) Text 10,84,"Func Projected: "+ScreenX+","+ScreenY Text 10,560,"cursors to adjust X,Y, 'a' & 'z' to adjust Z, '1' & '2' to adjust Zoom" Color 255,0,0 Oval ScreenX-2,ScreenY-2,4,4,1 Color 255,255,255 Flip Wend WaitKey End ; x,y,z must be in camera space! Function WorldToScreen(Zoom#,x#,y#,z#) If z>=0 ; in front of camera f# = Zoom * grHalfWidth ScreenX = (x*f)/z + grHalfWidth ScreenY = grHeight - ((y*f)/z + grHalfHeight) Else ; behind camera, not working! f# = Zoom * grHalfWidth ScreenX = (x*f)/z + grHalfWidth ScreenY = grHeight - ((y*f)/z + grHalfHeight) End If End Function Function FovToZoom#(FOV#) Return 1.0 / Tan(FOV#/2.0) End Function Function ZoomToFov#(Zoom#) Return 2*ATan(1.0/Zoom) End Function Cheers Tom |
| ||
Else ; behind camera f# = Zoom * grHalfWidth ScreenX = (-x*f)/z + grHalfWidth ScreenY = grHeight - ((-y*f)/z + grHalfHeight) End If or, equally... Else ; behind camera f# = -Zoom * grHalfWidth ScreenX = (x*f)/z + grHalfWidth ScreenY = grHeight - ((y*f)/z + grHalfHeight) End If Perhaps? Is this what you want? |