camera functions

Just starting out? Need help? Post your questions and find answers here.
worluk
New User
New User
Posts: 3
Joined: Sat Sep 06, 2003 7:25 pm

camera functions

Post by worluk »

hello folks and well met! i came across purebasic (and this forum) a week ago, and have decided to see what i could do with the demo version of it. so far i'm very impressed with pb, as well as the helpfulness of it's community. it appears the 3d capabilities of pb are growing at the same time that my interest in developing 3d applications & games has peeked, so timing is good all around.

i've written several small proof-of-concept routines thus far to test certain areas of functionality, as well as develope my abilities in these areas, with this language. with each item i compile i see more & more potential. for example, i wanted to learn more about and play with the camera related funtions available, so, i wrote a very simple unoptimized 3d terrain viewer from source i could find and what i could contribute of my own. the functions i am playing with are:

-CameraLocate(0, x, y, z)
-CameraLookAt(0, x, y, z)
-RotateCamera(0, x, y, z)
-MoveCamera(0, x, y, z)
-CameraFOV(0, fov_value)
-CameraRenderMode(0, #crm_constant)

however, i have run into some problems. copy this source to your examples\sources\ folder and run it. there are keyboard commands written to the screen, but basically these keys allow you to alter certain camera function parameters:

*NOTE - this source AS IS assumes you can open a 1280x1024 32bit full screen.

P,L - alters CameraFOV
F9,F10,F11 - alters CameraRenderMode
1,2,3,4,5,6 - alters CameraLocate
Q,W,E,R,T,Y - alters CameraLookAt
arrow up - turbo

try these commands as well:
1+Q, 2+W, 3+E, 4+R, 5+T, 6+Y

also, when you press any of the keys above you can press the UP_ARROW key to apply fast movement.

pressing these next keyboard keys causes a strange studder to occur due to me possibly incorrectly altering the values in the RotateCamera & MoveCamera functions. this behavior is the reason for this post. i am hoping that someone in the community can shed some light on why my example studders like this.

A,S,D,F,G,H - alters RotateCamera
Z,X,C,V,B,N - alters MoveCamera

please keep in mind this was written in haste and is not optimized, it is provided for learning and debugging purposes, and i openly share it with the community.

Code: Select all

;  worluk 9/1/12003
;  3d terrain viewer / playing w/ camera related function capabilities
;
; possible issues:  
;  - using CameraFOV() polys, when zooming in, become no more detailed as you get closer to the terrain.  
;    for an exact example of what i mean modify the z parameter of the CameraLocate function using the 5 and 6 keys
;
;  - issues with RotateCamera(#,x,y,z) - i get strange studdering when i attempt to alter the x,y,or,z parameters
;
;  - issues with MoveCamera(#,x,y,z) - same issue as above.  i get a strange studdering when i attempt to alter the x,y,or,z parameters
;



x1.l = 2000                ; used to alter CameraLocate(,#,x,y,z)
y1.l = 1860
z1.l = 4370

x2.l = 1000               ; used to alter CameraLookAt(,#,x,y,z)
y2.l = -20
z2.l = 375

x3.l = 0              ; used to alter RotateCamera(,#,x,y,z)
y3.l = 0
z3.l = 0

x4.l = 0             ; used to alter MoveCamera(,#,x,y,z)
y4.l = 0
z4.l = 0

fov = 45                  ; used to alter CameraFOV(#,fov)

crm.l = #PB_Camera_Wireframe      ; camera render mode
crm_constant_text$ = ""

turbo.l = 100                  ; this is fast scroll 


If InitEngine3D() And InitSprite() And InitKeyboard() And InitMouse() And OpenScreen(1280, 1024, 32, "game")

  CreateMaterial(0, LoadTexture(0, "data\terrain_texture.jpg"))    
  CreateTerrain("data\terrain.png", MaterialID(0), 8, 1.6, 8, 5)     
    
  CreateCamera(0, 0, 0, 100, 100)    
  CameraRenderMode(0, #PB_Camera_Wireframe)   
  

  Repeat

    ClearScreen(0,0,0)

    ExamineKeyboard()
    
    Gosub process_keyboard

    CameraLocate(0, x1, y1, z1)       ; alter paraameters for these function calls
    CameraLookAt(0, x2, y2, z2)
    RotateCamera(0, x3, y3, z3)
    MoveCamera(0, x4, y4, z4)
    CameraFOV(0, fov) 

    CameraRenderMode(0, crm)  ; #PB_Camera_Plot / #PB_Camera_Wireframe / #PB_Camera_Textured

    RenderWorld()
 
    Gosub draw_stats
    
    FlipBuffers()

  Until KeyboardPushed(#PB_Key_Escape)

  FreeCamera(0)

EndIf
 
End




process_keyboard:

; handle what key(s) are currently pressed

    If KeyboardPushed(#PB_Key_UP)
      turbo=100
    Else
      turbo=0
    EndIf

;CameraLocate(0, x1, y1, z1)
    If KeyboardPushed(#PB_Key_1): x1 = x1-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_2): x1 = x1+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_3): y1 = y1-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_4): y1 = y1+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_5): z1 = z1-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_6): z1 = z1+10 + turbo:  EndIf

;CameraLookAt(0, x2, y2, z2)
    If KeyboardPushed(#PB_Key_Q): x2 = x2-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_W): x2 = x2+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_E): y2 = y2-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_R): y2 = y2+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_T): z2 = z2-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_Y): z2 = z2+10 + turbo:  EndIf

;RotateCamera(0, x3, y3, z3)
    If KeyboardPushed(#PB_Key_A): x3 = x3-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_S): x3 = x3+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_D): y3 = y3-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_F): y3 = y3+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_G): z3 = z3-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_H): z3 = z3+10 + turbo:  EndIf

;MoveCamera(0, x4, y4, z4)
    If KeyboardPushed(#PB_Key_Z): x4 = x4-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_X): x4 = x4+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_C): y4 = y4-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_V): y4 = y4+10 + turbo:  EndIf
    If KeyboardPushed(#PB_Key_B): z4 = z4-10 - turbo:  EndIf
    If KeyboardPushed(#PB_Key_N): z4 = z4+10 + turbo:  EndIf

;CameraFOV(0, fov) 
    If KeyboardPushed(#PB_Key_L): fov = fov - 1: EndIf
    If KeyboardPushed(#PB_Key_P): fov = fov + 1: EndIf

;CameraRenderMode(0, crm)
    If KeyboardPushed(#PB_Key_F9):  crm=#PB_Camera_Plot:      EndIf
    If KeyboardPushed(#PB_Key_F10): crm=#PB_Camera_Wireframe: EndIf
    If KeyboardPushed(#PB_Key_F11): crm=#PB_Camera_Textured:  EndIf

Return






draw_stats:

; write some text to the screen - variables / instructions
 
  LoadFont(1, "Courier New", 9)
 
  StartDrawing(ScreenOutput()) 

    DrawingMode(1) 
    DrawingFont(FontID())
    FrontColor(0,200,0) 

    Locate(0, 1000) 
    DrawText("playing with x,y,z ... esc to exit") 

    Locate(6,15) 
    DrawText("CameraLocate(0, " + Str(x1) + ", " + Str(y1) + ", " + Str(z1)+")   ")
    Locate(250,0) 
    DrawText("x(+,-)=(1,2)")
    Locate(250,15) 
    DrawText("y(+,-)=(3,4)")
    Locate(250,30) 
    DrawText("z(+,-)=(5,6)")
  
    Locate(6,75) 
    DrawText("CameraLookAt(0, " + Str(x2) + ", " + Str(y2) + ", " + Str(z2)+")")
    Locate(250,60) 
    DrawText("x(+,-)=(Q,W)")
    Locate(250,75) 
    DrawText("y(+,-)=(E,R)")
    Locate(250,90) 
    DrawText("z(+,-)=(T,Y)")

    Locate(6,135) 
    DrawText("RotateCamera(0, " + Str(x3) + ", " + Str(y3) + ", " + Str(z3)+")")
    Locate(250,120) 
    DrawText("x(+,-)=(A,S)")
    Locate(250,135) 
    DrawText("y(+,-)=(D,F)")
    Locate(250,150) 
    DrawText("z(+,-)=(G,H)")

    Locate(6,195) 
    DrawText("MoveCamera(0,   " + Str(x4) + ", " + Str(y4) + ", " + Str(z4)+")")
    Locate(250,180) 
    DrawText("x(+,-)=(Z,X)")
    Locate(250,195) 
    DrawText("y(+,-)=(C,V)")
    Locate(250,210) 
    DrawText("z(+,-)=(B,N)")

    Locate(6,255) 
    DrawText("CameraFOV(0, " + Str(fov) + ")")

    Locate(250,255) 
    DrawText("fov(+,-)=(P,L)")
  
    Locate(900, 0)  
    DrawText("frames per second: " + StrF(Engine3DFrameRate(0),1))
    Locate(900, 15) 
    DrawText("# triangles: " + Str(CountRenderedTriangles()))

    Select crm
      Case #PB_Camera_Plot:      crm_constant_text$ = "plot"
      Case #PB_Camera_Wireframe: crm_constant_text$ = "wireframs"
      Case #PB_Camera_Textured:  crm_constant_text$ = "textured"
    EndSelect

    Locate(900, 30)  
    DrawText("camera render mode (F9/F10/F11): " + crm_constant_text$)
  
  StopDrawing() 

Return
worluk
New User
New User
Posts: 3
Joined: Sat Sep 06, 2003 7:25 pm

Post by worluk »

pls disregard this post - i figured it out.
Post Reply