Simulating CameraProject() in BlitzPlus possible?

BlitzPlus Forums/BlitzPlus Beginners Area/Simulating CameraProject() in BlitzPlus possible?

videz(Posted 2015) [#1]
I know this is a bit of a stretch to ask but might as well try and put this out in the open and confirm if this is possible.

How do you simulate this function in BlitzPlus through computation given the camera position and Entity position in Perspective mode projection?


Matty(Posted 2015) [#2]
With a bit of mathematics?

You would need to transform a set of 3d coordinates relative to another position (the camera) and then map them onto a 2d plane....there is plenty of information on how to do this online.....wikipedia is a helpful starting point.


videz(Posted 2015) [#3]
Ok, so I found this Freebasic code somewhere and its close..

' http://www.dreamincode.net/forums/topic/239174-3d-perspective-projection/
 ' translated to FreeBasic

 '' ----------------------------------------   
 '' Formula to solve Sx  
 '' ----------------------------------------  
 '' Ez = distance from eye to the center of the screen 
 '' Ex = X coordinate of the eye  
 '' Px = X coordinate of the 3D point  
 '' Pz = Z coordinate of the 3D point  
 ''  
 ''              Ez*(Px-Ex)  
 '' Sx  = -----------------------  + Ex     
 ''                Ez+Pz 
 '' ----------------------------------------   
 '' Formula to solve Sy  
 '' ----------------------------------------  
 '' Ez = distance from eye to the center of the screen 
 '' Ey = Y coordinate of the eye   
 '' Py = Y coordinate of the 3D point  
 '' Pz = Z coordinate of the 3D point      
 ''  
 ''            Ez*(Py-Ey)  
 '' Sy  = -------------------  + Ey        
 ''              Ez+Pz 

type Vector2i  
     as integer x, y 
end type 

    
type Vector3i 
    as integer x, y, z 
end type

sub main() 
     '' lets assume 640x480 res.  
     '' Our "eye" is where we are viewing from, which 
     '' is about 800 pixels towards me and in the center of  
     '' the screen.  

     dim as Vector3i eye = Type(320, 240, 800)  
        
     '' This is the point that we're projecting onto  
     '' our 2D plane.  

     dim as Vector3i P = Type(600, 200, 1000) 

     '' This will be the 2D coords of our perspective projection.  

     dim as Vector2i S  

     '' ----------------------------------------   
     '' Formula to solve Sx  
     '' ----------------------------------------  
     '' Ez = distance from eye to the center of the screen 
     '' Ex = X coordinate of the eye  
     '' Px = X coordinate of the 3D point  
     '' Pz = Z coordinate of the 3D point  
     ''  
     ''              Ez*(Px-Ex)  
     '' Sx  = -----------------------  + Ex     
     ''                Ez+Pz   

     S.x = (eye.z * (P.x-eye.x)) / (eye.z + P.z) + eye.x 

     '' ----------------------------------------   
     '' Formula to solve Sy  
     '' ----------------------------------------  
     '' Ez = distance from eye to the center of the screen 
     '' Ey = Y coordinate of the eye   
     '' Py = Y coordinate of the 3D point  
     '' Pz = Z coordinate of the 3D point      
     ''  
     ''            Ez*(Py-Ey)  
     '' Sy  = -------------------  + Ey        
     ''              Ez+Pz  

     S.y = (eye.z * (P.y-eye.y)) / (eye.z + P.z) + eye.y  

     print "Result of projection."
     print "x: "; S.x
     print "y: "; S.y
     
end sub



What I'm not sure of is the defined Z distance of the eye (cam) = 800 and the object's distance = 1000.. Is this relative to world center 0,0,0??


Matty(Posted 2015) [#4]
I would imagine....the author of that code is basically saying that the eye position is roughly 800 pixels away from the screen.

Ie.... the screen of 640x480 is a plane which is sitting 800 pixels in front of your eyes.

He could have picked another value....basically it's like setting the field of view.

Try looking up some info on optics focal lengths etc.....


_PJ_(Posted 2015) [#5]
Yep, Matty has the right of it.

Basically you only have 2 dimensions and are wanting to simulate this as a viewport onto 3D space - therefore you have a completely arbitrary choice on the focal length and the field of view - There's no "right answer" here, it's largely dependant on current resolution aspect ratio (widescreen formats typically imply a wider field-of-view angle) and what you determine to give the best results in terms of the simulated depth.


videz(Posted 2015) [#6]
Hi PJ,

Can you provide some examples with adding FOV and Aspect Ratio to complete this computation? I assume Aspect ratio is the screen width and height so this is already given. and the FOV is just standard normal perspective view like in B3D default

Thanks.