3d resizing aspect ratio

Everything related to 3D programming
Ziltch
User
User
Posts: 61
Joined: Sun Aug 03, 2003 12:05 am
Location: Australia

3d resizing aspect ratio

Post by Ziltch »

We can now resize 3d windows, which is great!
I was wondering if the aspect ratio can be controlled without letterboxing the camera?

Code show the aspect ratio squishing the viewport when the window is resized.

Code: Select all

Procedure SizeWindow()  
  RenderWorld()
  FlipBuffers()
EndProcedure

 
If InitEngine3D()=0  Or  InitSprite() = 0
  MessageRequester("Error", "Error with InitEngine3D!", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "Aspect Ratio Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  OpenWindowedScreen(WindowID(0), 0, 0, 800,600,1, 0, 0, 0)
EndIf

CreateCamera(0,0,0,100,100)
MoveCamera(0,0,150 ,-15, #PB_Absolute)
CameraLookAt(0,0,0,0)

BindEvent(#PB_Event_SizeWindow, @SizeWindow()) 

CreateIcoSphere(0,25,3)
CreateEntity(0,MeshID(0),0,50,0,0)

CreateCube(1,25)
CreateEntity(1,MeshID(1),0,-50,0,0)

CameraBackColor(0 , RGB(120,20,20))
Light1=CreateLight(#PB_Any, RGB(25, 25, 180), -500, 200, 199, #PB_Light_Point)
Light2=CreateLight(#PB_Any, RGB(180, 25, 25), 500, 200, 199, #PB_Light_Point)
Light3=CreateLight(#PB_Any, RGB(25, 180, 25), 0, -500, 0, #PB_Light_Point)

Repeat
  Repeat
    Event = WindowEvent()
   
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  RenderWorld()
  
  FlipBuffers()
  Delay(1)
 
ForEver
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: 3d resizing aspect ratio

Post by miso »

There is no camera aspect ratio implemented in Pb, but you can maintain it by clipping the viewport with the resizecamera command.

Edit:
I'm always prototyping when I answer questions, so my codes here are never clean or errorproof. I'm thinking something like this:
Edit2: I don't think it behaves the same in both directions. If someone finds better solution, would not mind a better example.

Code: Select all

Global.i camerawidth,cameraheight,targetwindowwidth,targetwindowheight,cameraoffsetx,cameraoffsety,targetaspect.f,aspect.f
#MAINWINDOW = 0
#MAINCAMERA = 0

ExamineDesktops()
maxwidth.i=DesktopWidth(0)
maxheight.i=DesktopHeight(0)
targetwindowWidth=8
targetwindowHeight=6
targetaspect=targetwindowwidth/targetwindowheight
windowWidth=800
windowHeight=600

Procedure.f lerp(a.f,b.f,t.f)
  ProcedureReturn(((1.0-t.f)*a) + (b*t))
EndProcedure

Procedure.f invlerp(a.f,b.f,v.f)
  Define errorlimit.f=0.0001
  If a = b : ProcedureReturn(1) : EndIf
  ProcedureReturn (v-a)/(b-a)
EndProcedure

Procedure.f remap(iMin.f,iMax.f,oMin.f,oMax.f,v.f)
  Define t.f
  t.f=invlerp(iMin,iMax,v)
  ProcedureReturn(lerp(oMin,oMax,t))
EndProcedure


Procedure checkwindowsizeforcamera()
  
aspect=WindowWidth(#mainwindow)/WindowHeight(#MAINWINDOW)
If WindowWidth(#MAINWINDOW)>WindowHeight(#MAINWINDOW)
  camerawidth = remap(0,targetaspect*targetwindowwidth,0,100,aspect*WindowWidth(#MAINWINDOW))
  cameraheight = 100
Else
  camerawidth = 100
  cameraheight = remap(0,targetaspect*targetwindowheight,0,100,aspect*WindowHeight(#MAINWINDOW))
EndIf
  ResizeCamera(#MAINCAMERA,(100-camerawidth)/2,(100-cameraheight)/2,camerawidth,cameraheight)
EndProcedure




Procedure SizeWindow()  
  checkwindowsizeforcamera()
  RenderWorld()
  FlipBuffers()
EndProcedure

If InitEngine3D()=0  Or  InitSprite() = 0
  MessageRequester("Error", "Error with InitEngine3D!", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "Aspect Ratio Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  OpenWindowedScreen(WindowID(0), 0, 0, 800,600,1, 0, 0, 0)
EndIf

CreateCamera(#MAINCAMERA,0,0,100,100)
MoveCamera(#MAINCAMERA,0,150 ,-15, #PB_Absolute)
CameraLookAt(#MAINCAMERA,0,0,0)

BindEvent(#PB_Event_SizeWindow, @SizeWindow()) 

CreateIcoSphere(0,25,3)
CreateEntity(0,MeshID(0),0,50,0,0)

CreateCube(1,25)
CreateEntity(1,MeshID(1),0,-50,0,0)

CameraBackColor(0 , RGB(120,20,20))
Light1=CreateLight(#PB_Any, RGB(25, 25, 180), -500, 200, 199, #PB_Light_Point)
Light2=CreateLight(#PB_Any, RGB(180, 25, 25), 500, 200, 199, #PB_Light_Point)
Light3=CreateLight(#PB_Any, RGB(25, 180, 25), 0, -500, 0, #PB_Light_Point)

Repeat
  Repeat
    Event = WindowEvent()
   
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  RenderWorld()
  
  FlipBuffers()
  Delay(1)
 
ForEver
Ziltch
User
User
Posts: 61
Joined: Sun Aug 03, 2003 12:05 am
Location: Australia

Re: 3d resizing aspect ratio

Post by Ziltch »

Thank you !!!!!!!!!!!!
That is just what I needed.

I totally misunderstood the ResizeCamera command.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: 3d resizing aspect ratio

Post by miso »

I'm not sure, it was me who did misunderstanding. Looks like calling the resizecamera keeps the aspectratio without any trick.

Code: Select all

#MAINWINDOW = 0
#MAINCAMERA = 0

Procedure checkwindowsizeforcamera()
  ClearScreen(0)
  ResizeCamera(#MAINCAMERA,0,0,100,100)
EndProcedure

Procedure SizeWindow()  
  checkwindowsizeforcamera()
  RenderWorld()
  FlipBuffers()
EndProcedure

If InitEngine3D()=0  Or  InitSprite() = 0
  MessageRequester("Error", "Error with InitEngine3D!", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "Aspect Ratio Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  OpenWindowedScreen(WindowID(0), 0, 0, 800,600,1, 0, 0, 0)
EndIf

CreateCamera(#MAINCAMERA,0,0,100,100)
MoveCamera(#MAINCAMERA,0,150 ,-15, #PB_Absolute)
CameraLookAt(#MAINCAMERA,0,0,0)

BindEvent(#PB_Event_SizeWindow, @SizeWindow()) 

CreateIcoSphere(0,25,3)
CreateEntity(0,MeshID(0),0,50,0,0)

CreateEntity(2,MeshID(0),0,50,150,50)
ScaleEntity(2,3,3,3)
CreateCube(1,25)
CreateEntity(1,MeshID(1),0,-50,0,0)

CameraBackColor(0 , RGB(120,20,20))
Light1=CreateLight(#PB_Any, RGB(25, 25, 180), -500, 200, 199, #PB_Light_Point)
Light2=CreateLight(#PB_Any, RGB(180, 25, 25), 500, 200, 199, #PB_Light_Point)
Light3=CreateLight(#PB_Any, RGB(25, 180, 25), 0, -500, 0, #PB_Light_Point)

Repeat
  Repeat
    Event = WindowEvent()
   
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  RenderWorld()
  
  FlipBuffers()
  Delay(1)
 
ForEver
Ziltch
User
User
Posts: 61
Joined: Sun Aug 03, 2003 12:05 am
Location: Australia

Re: 3d resizing aspect ratio

Post by Ziltch »

That is easier! It makes sense that the camera needs to also resize with a window resize.
Thanks again.
Post Reply