Results 1 to 5 of 5

Thread: tbgl_EntityCreateCamera- Question

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170

    tbgl_EntityCreateCamera- Question

    How can I get the coordinates where the %ENT_CAMERA-Entity currently is looking at?

    (TBGL_Demo06_01_MovingIn3D_UsingEntitySystemBasic.tBasic from sampleScripts\TBGL\Basic-folder)
    and btw. what is "dim Sheeps as Long" for? Some internal switch?


    The intention is, to move some mesh or tirangle-list through space and make the camera look at it.
    But camera should be moveable in all directions around the mesh, always looking at it
    so maybe normal controls (left/right/up/down/pgUp/pgDn) refer to the meshes movement, camera will always follow, so makes the same moves

    but if I press any Shift-Key just the camera should
    %VL_Left - rotate left around point of view (my mesh)
    %VK_Up - get closer
    %VK_Down- zoom out
    %VK_PgUp - Camera shall move upwards but still look down to the mesh

    so I need to know: How can I get or even set the look-at-position for the camera-entity

    and another question: how can I make my Triangle-Lists triangles, which I setup with

    TBGL_NewList %ENT_POINTER
    TBGL_BeginPoly %GL_TRIANGLES
    TBGL_Color 0, 127, 0
    TBGL_Vertex -1 , 0 , 0
    TBGL_Normal
    ...etc.

    to be rendered just from one side visible?
    Last edited by ReneMiner; 28-10-2012 at 16:44.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Rene,

    #1
    The sheeps shouldn't be there :P I sometimes use this variable to count frames of animation, and once the count is elapsed, I do something and reset it again. (for example when updating window title with framerate)

    #2
    The camera position is set using TBGL_EntitySetPos, as with any other entity. To make camera look at some place, you need to call TBGL_EntitySetTarget or TBGL_EntitySetTargetPos.
    In case the target is moving, you might want to call it each frame.

    For example, to make camera look at box, you just need to do this:
    TBGL_EntitySetTarget(%sScene, %eCamera, %eBox)
    
    If you want the camera to be placed at fixed offset from the object, you can "stick" the camera to object using parent-child relationship.
    ' -- Stick camera to the box      
    TBGL_EntitySetParent   ( %sScene, %eCamera, %eBox)
    ' -- Now these are relative to the box
    TBGL_EntitySetPos      ( %sScene, %eCamera, 0, 2, 4 )
    TBGL_EntitySetTargetPos( %sScene, %eCamera, 0, 0, 0 )
    
    And finally, if you want something like chase camera in racing games, you keep the objects separate, and dynamically position the camera:
    ' -- Make camera look at box   
    distance = TBGL_EntityGetDistance(%sScene, %eCamera, %eBox)
    TBGL_EntitySetTarget(%sScene, %eCamera, %eBox)
    
    ' -- If we are far enought...
    If distance > 3 Then         
      ' -- We push the camera towards the box                 
      TBGL_EntityPush(%sScene, %eCamera, 0, 0, (distance-3) / frameRate)
      
      ' -- Testing if camera didn't fall to low
      TBGL_EntityGetPos(%sScene, %eCamera, x, y, z)        
      If y < 2 Then ' -- Camera too low
        TBGL_EntityMove(%sScene, %eCamera, 0, 2-y, 0)
      End If
    End If
    
    #3
    To make triangles visible from just one side, you need to enable culling. It is not present in TBGL by default, but you can mix TBGL with native OpenGL.
    So, first add this on top of your script:
    #include "thinbasic_gl.inc"
    
    Then in your code:
    glEnable(%GL_CULL_FACE)
    glCullFace(%GL_BACK) ' -- Or GL_FRONT, depends what you want to achieve
    
    ' -- Define your triangles here
    
    glDisable(%GL_CULL_FACE)
    

    Petr
    Attached Files Attached Files
    Last edited by Petr Schreiber; 28-10-2012 at 20:46.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    OK, I'll just show you, what I'm up to. It's supposed to become a basic 3d-Mesh-Editor
    I know how to dimension all that stuff and creating, drawing but I got problems with them movement-controls, they drive me crazy
    the only that work is pgUp & pgDn
    Also do I experience the light just flickers for a short second.

    EDIT: -done-

    EDIT2: Culling does not seem to work - or did I put it the wrong place?
    Last edited by ReneMiner; 28-10-2012 at 21:00.
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Rene,

    I need to go offline to prepare to work for this week, but this example should get you started.

    The main idea is to have two pivots in the origin of coordinates - one for left/right rotations, another for up/down rotations.

    %eCameraPivotLR is the base
    %eCameraPivotUD is child of %eCameraPivotLR
    %eVirtualCameraBody is child of %eCameraPivotUD
    and finally, %eCamera is child of %eVirtualCameraBody

    Here is sample code:
    set %visualizeCameraMovement = true to see how it works
    set %visualizeCameraMovement = false to see the camera in action
    ' -- Camera on orbit
    
    %visualizeCameraMovement = TRUE ' -- Set to false to see through eyes of camera
    
    '---Load needed modules
    Uses "TBGL"
    
    Begin Const  
      ' -- Scenes
      %sScene = 1
      
      ' -- Entities
      %eCameraPivotLR = 1
      %eCameraPivotUD
    
      %eCamera   
      %eLight    
      %eGridList 
      %eBox    
      %eVirtualCameraBody    
      
    End Const
      
    Function TBMain()
      Dim hWnd As Dword
      
      '---Creates OpenGL window and returns handle
      hWnd = TBGL_CreateWindowEx("Camera for editor, use arrows and pageup/pagedown - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED )
      '---Shows TBGL window
      TBGL_ShowWindow
    
      ' -- First we will create world as place to contain entities
      tbgl_SceneCreate(%sScene) 
      ' -- Camera pivot
      TBGL_EntityCreatePivot( %sScene, %eCameraPivotLR)
      TBGL_EntityCreatePivot( %sScene, %eCameraPivotUD, %eCameraPivotLR)
      
      ' -- Camera
      TBGL_EntityCreateCamera( %sScene, %eCamera) ' -- Camera becomes parent of pivot
      If %visualizeCameraMovement Then
        ' -- We set our eyes somewhere to look better on subject
        TBGL_EntitySetPos      ( %sScene, %eCamera, 0, 10,10 )
      Else  
        ' -- We shift camera slightly to not appear inside virtual camera
        TBGL_EntitySetPos      ( %sScene, %eCamera, 0, 0, -0.55 )
      End If      
    
      ' -- Light  
      TBGL_EntityCreateLight(%sScene, %eLight)
      TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
      TBGL_EntitySetColor(%sScene, %eLight, 255, 255, 255)
      
      ' -- Grid
      TBGL_EntityCreateDLSlot( %sScene, %eGridList, 0, CreateGridAsDList() )
      
      ' -- Box
      TBGL_EntityCreateBox( %sScene, %eBox, 0, 1, 1, 1 )
      TBGL_EntityCreateBox( %sScene, %eVirtualCameraBody, %eCameraPivotUD, 0.1, 0.3, 1 )
      TBGL_EntitySetColor( %sScene, %eVirtualCameraBody, 255, 128, 0 )
      TBGL_EntitySetPos(%sScene, %eVirtualCameraBody, 0, 0, 5)
      
      If %visualizeCameraMovement = FALSE Then
        ' -- Stick camera to virtual camera body
        TBGL_EntitySetParent(%sScene, %eCamera, %eVirtualCameraBody)
      End If  
    
      dim FrameRate as double
      
      '---Resets key status before checking
      TBGL_ResetKeyState()
    
      '---Main script loop
      WHILE TBGL_IsWindow(hWnd)
      
        '---Script will run on different PCs so we must assure
        '---constant speed of movement by scaling movements relative to frame rate
        FrameRate = TBGL_getFrameRate
        
        '---Prepares clear frame
        TBGL_ClearFrame         
          
          ' -- Make camera look at box
          TBGL_EntitySetTarget(%sScene, %eCamera, %eBox)
    
          tbgl_SceneRender(%sScene)
    
        TBGL_DrawFrame                    ' Swaps the buffers - displays rendered image
    
        If TBGL_GetWindowKeyState( hWnd, %VK_UP)     Then TBGL_EntityPush(%sScene, %eVirtualCameraBody, 0, 0, -5/FrameRate) ' -- Move camera closer
        If TBGL_GetWindowKeyState( hWnd, %VK_DOWN)   Then TBGL_EntityPush(%sScene, %eVirtualCameraBody, 0, 0,  5/FrameRate)
    
        If TBGL_GetWindowKeyState( hWnd, %VK_LEFT)   Then TBGL_EntityTurn(%sScene, %eCameraPivotLR, 0, -90/FrameRate, 0)    ' -- Rotate Left-Right pivot
        If TBGL_GetWindowKeyState( hWnd, %VK_RIGHT)  Then TBGL_EntityTurn(%sScene, %eCameraPivotLR, 0,  90/FrameRate, 0)    
        
        If TBGL_GetWindowKeyState( hWnd, %VK_PGUP)   Then TBGL_EntityTurn(%sScene, %eCameraPivotUD, -90/FrameRate, 0, 0)    ' -- Rotate Up-Down pivot
        If TBGL_GetWindowKeyState( hWnd, %VK_PGDN)   Then TBGL_EntityTurn(%sScene, %eCameraPivotUD,  90/FrameRate, 0, 0)
                                                                                
        if TBGL_GetwindowKeyState( hWnd, %VK_ESCAPE) then EXIT WHILE
    
      WEND
    
      '---Closes OpenGL window
      TBGL_DestroyWindow
    End Function
    
    Function CreateGridAsDList() As Long  ' Returns to which display list we save
      local i, j as long
      
      TBGL_NewList 1
        TBGL_UseLighting FALSE
          '---Let's build a grid
          TBGL_BeginPoly %GL_LINES          ' Starts polygon definition based on 2 vertex lines
            TBGL_Color 0,255,0              ' Defines color
      
            For i = -10 To 10
              For j = -10 To 10
                TBGL_Vertex -10, 0,   j     ' Adds vertex
                TBGL_Vertex  10, 0,   j     ' Adds vertex
      
                TBGL_Vertex  i,  0, -10     ' Adds vertex
                TBGL_Vertex  i,  0,  10     ' Adds vertex
      
              Next
            Next
      
          TBGL_EndPoly                 
          TBGL_UseLighting TRUE
      tbgl_EndList 
      
      function = 1 
    End Function
    
    I hope it helps! Must run now, bye bye!


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    I'm still sitting here and cranking my brain at 3:00 in the morning, I'm on edge of a major life crisis and almost about to throw something into that monitor in front of me.

    Edit at 5:00
    finally I got it. with just one pivot, no virtualCameraBody and no radians but a good old projection-table in degrees
    Last edited by ReneMiner; 29-10-2012 at 06:00.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Question of the day ;)
    By Petr Schreiber in forum thinBasic General
    Replies: 8
    Last Post: 23-08-2010, 19:58
  2. C to TB question
    By Michael Clease in forum Other languages
    Replies: 2
    Last Post: 03-06-2010, 12:11
  3. gdi question
    By Lionheart008 in forum UI (User Interface)
    Replies: 6
    Last Post: 07-12-2009, 19:31
  4. UDT question
    By sandyrepope in forum thinBasic General
    Replies: 3
    Last Post: 18-02-2008, 22:33
  5. m15 question
    By kryton9 in forum M15 file format
    Replies: 4
    Last Post: 20-06-2007, 20:18

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •