Changeset 2894


Ignore:
Timestamp:
Sep 11, 2013, 11:05:11 PM (6 years ago)
Author:
touky
Message:

btphystest : added new camera functions. Added funny camera stuff for cat simulation.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/camera.cpp

    r2816 r2894  
    3232    m_drawgroup = DRAWGROUP_CAMERA;
    3333
     34    ivec2 screen_size = Video::GetSize();
     35    m_fov = 45.f;
     36    m_near = -1000.f;
     37    m_far = 1000.f;
     38    m_screen_size = (float)screen_size.x;
     39    m_screen_ratio = (float)screen_size.x / (float)screen_size.y;
     40    m_is_shifted = false;
     41
    3442    /* Create a default perspective */
    3543    SetProjection(mat4::perspective(45.f, 800.f, 600.f, -1000.f, 1000.f));
     
    4351}
    4452
     53//-----------------------------------------------------------------------------
     54//View functions
     55//--
    4556void Camera::SetView(mat4 const &view)
    4657{
     
    5364    m_view_matrix = mat4::lookat(eye, target, up);
    5465    m_position = eye;
     66    m_target_distance = length(target - m_position);
     67}
     68
     69void Camera::SetView(vec3 pos, vec3 rot)
     70{
     71    SetView(pos, quat::fromeuler_xyz(rot));
    5572}
    5673
     
    6178                                 rot.transform(vec3(0.f, 1.f, 0.f)));
    6279    m_position = pos;
    63 }
    64 
     80    m_target_distance = .0f;
     81}
     82
     83mat4 Camera::GetView()
     84{
     85    return m_view_matrix;
     86}
     87
     88//-----------------------------------------------------------------------------
     89//Projections functions
     90//--
    6591void Camera::SetProjection(mat4 const &proj)
    6692{
     
    6894}
    6995
    70 mat4 Camera::GetView()
    71 {
    72     return m_view_matrix;
     96void Camera::SetProjection(float fov, float near, float far)
     97{
     98    SetProjection(fov, near, far, m_screen_size, m_screen_ratio);
     99}
     100
     101void Camera::SetProjection(float fov, float near, float far, float screen_size, float screen_ratio)
     102{
     103    m_fov = fov;
     104    m_near = near;
     105    m_far = far;
     106    m_screen_size = screen_size;
     107    m_screen_ratio = screen_ratio;
     108
     109    if (m_fov > .0f)
     110    {
     111        if (m_is_shifted)
     112            SetProjection(mat4::shifted_perspective(m_fov, screen_size, screen_ratio, m_far - m_near));
     113        else
     114            SetProjection(mat4::perspective(m_fov, screen_size, screen_size * screen_ratio, m_near, m_far));
     115    }
     116    else
     117        SetProjection(mat4::ortho(screen_size, screen_size * screen_ratio, m_near, m_far));
    73118}
    74119
     
    78123}
    79124
     125//-----------------------------------------------------------------------------
     126//Projections manipulation functions
     127//--
     128void Camera::SetFov(float fov)
     129{
     130    SetProjection(fov, m_near, m_far, m_screen_size, m_screen_ratio);
     131}
     132
     133void Camera::SetScreenInfos(float screen_size)
     134{
     135    SetScreenInfos(screen_size, m_screen_ratio);
     136}
     137
     138void Camera::SetScreenInfos(float screen_size, float screen_ratio)
     139{
     140    SetProjection(m_fov, m_near, m_far, screen_size, screen_ratio);
     141}
     142
     143void Camera::SetDrawInfos(float far)
     144{
     145    SetDrawInfos(m_near, far);
     146}
     147
     148void Camera::SetDrawInfos(float near, float far)
     149{
     150    SetProjection(m_fov, near, far, m_screen_size, m_screen_ratio);
     151}
     152
     153void Camera::UseShift(bool should_shift)
     154{
     155    m_is_shifted = should_shift;
     156    SetProjection(m_fov, m_near, m_far, m_screen_size, m_screen_ratio);
     157}
     158
     159
     160//-----------------------------------------------------------------------------
     161//camera manipulation Functions
     162//--
     163void Camera::SetPosition(vec3 pos)
     164{
     165    if (m_target_distance > .0f)
     166    {
     167        vec4 up = m_view_matrix * vec4(0.f, 1.f, 0.f, 0.f);
     168        vec4 target = m_view_matrix * vec4(0.f, 0.f, -m_target_distance, 0.f);
     169        SetView(m_position, m_position + target.xyz, up.xyz);
     170    }
     171    else
     172        m_view_matrix = m_view_matrix * mat4::translate(pos - m_position);
     173    m_position = pos;
     174}
     175
     176void Camera::SetTarget(vec3 target)
     177{
     178    vec4 up = inverse(m_view_matrix) * vec4(0.f, 1.f, 0.f, 0.f);
     179    SetView(m_position, target, up.xyz);
     180}
     181
     182void Camera::SetUp(vec3 up)
     183{
     184    vec4 target = inverse(m_view_matrix) * vec4(0.f, 0.f, -max(m_target_distance, 1.f), 0.f);
     185    SetView(m_position, m_position + target.xyz, up);
     186}
     187
     188void Camera::SetRotation(vec3 rot)
     189{
     190    SetRotation(quat::fromeuler_xyz(rot));
     191}
     192
     193void Camera::SetRotation(quat rot)
     194{
     195    SetView(m_position, rot);
     196}
     197
     198//--
    80199vec3 Camera::GetPosition()
    81200{
     
    83202}
    84203
     204vec3 Camera::GetTarget()
     205{
     206    return m_position + (inverse(m_view_matrix) * vec4(0.f, 0.f, -max(m_target_distance, 1.f), 0.f)).xyz;
     207}
     208
     209vec3 Camera::GetUp()
     210{
     211    return (inverse(m_view_matrix) * vec4(0.f, 1.f, 0.f, 0.f)).xyz;
     212}
     213
     214vec3 Camera::GetRotationEuler()
     215{
     216    return vec3::toeuler(GetRotation());
     217}
     218
     219quat Camera::GetRotation()
     220{
     221    return quat(inverse(m_view_matrix));
     222}
     223
     224// Calculate the frustum height at a given distance from the camera.
     225float Camera::GetFrustumHeightAtDistance(float distance, float fov)
     226{
     227        return 2.f * distance * lol::tan(fov * .5f * (F_PI / 180.f));
     228}
     229
     230// Calculate the FOV needed to get a given frustum height at a given distance.
     231float Camera::GetFOVForHeightAndDistance(float distance, float height)
     232{
     233        return 2.f * lol::atan(height * .5f / distance) * (180.f / F_PI);
     234}
     235
    85236void Camera::TickGame(float seconds)
    86237{
  • trunk/src/camera.h

    r2816 r2894  
    3030    char const *GetName() { return "<camera>"; }
    3131
     32    //View functions
    3233    void SetView(mat4 const &view);
    3334    void SetView(vec3 eye, vec3 target, vec3 up);
     35    void SetView(vec3 pos, vec3 rot);
    3436    void SetView(vec3 pos, quat rot);
    3537
     38    mat4 GetView();
     39
     40    //Projections functions
     41//private:
    3642    void SetProjection(mat4 const &proj);
     43public:
     44    void SetProjection(float fov, float near, float far);
     45    void SetProjection(float fov, float near, float far, float screen_size, float screen_ratio);
    3746
    38     mat4 GetView();
    3947    mat4 GetProjection();
    4048
     49    //Projections manipulation functions
     50    void SetFov(float fov);
     51    void SetScreenInfos(float screen_size);
     52    void SetScreenInfos(float screen_size, float screen_ratio);
     53    void SetDrawInfos(float far);
     54    void SetDrawInfos(float near, float far);
     55    void UseShift(bool should_shift);
     56
     57    float GetFov()          { return m_fov; }
     58    float GetScreenSize()   { return m_screen_size; }
     59    float GetScreenRatio()  { return m_screen_ratio; }
     60    float GetNear()         { return m_near; }
     61    float GetFar()          { return m_far; }
     62    bool  IsShifted()       { return m_is_shifted; }
     63
     64    //camera manipulation Functions
     65    void SetPosition(vec3 pos);
     66    void SetTarget(vec3 target);
     67    void SetUp(vec3 up);
     68    void SetRotation(vec3 rot);
     69    void SetRotation(quat rot);
     70
    4171    vec3 GetPosition();
     72    vec3 GetTarget();
     73    vec3 GetUp();
     74    vec3 GetRotationEuler();
     75    quat GetRotation();
     76
     77    //Convenience functions
     78    float GetFrustumHeightAtDistance(float distance, float fov);
     79    float GetFOVForHeightAndDistance(float distance, float height);
    4280
    4381protected:
     
    4684
    4785private:
    48     mat4 m_view_matrix, m_proj_matrix;
     86    //Work datas
     87    mat4    m_view_matrix;
     88    mat4    m_proj_matrix;
     89
     90    //Datas used to create above matrix
     91    float   m_target_distance;
     92    float   m_fov;
     93    float   m_screen_size;
     94    float   m_screen_ratio;
     95    float   m_near;
     96    float   m_far;
     97    bool    m_is_shifted;
    4998};
    5099
     
    52101
    53102#endif /* __CAMERA_H__ */
    54 
  • trunk/src/math/vector.cpp

    r2863 r2894  
    769769}
    770770
     771//Returns a standard perspective matrix
    771772template<> mat4 mat4::perspective(float fov_y, float width,
    772773                                  float height, float near, float far)
     
    780781}
    781782
     783//Returns a perspective matrix with the camera location shifted to be on the near plane
    782784template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size,
    783785                                          float screen_ratio_xy, float draw_distance)
     
    788790    float far = near + draw_distance;
    789791
    790     return mat4::perspective(fov_y, screen_size * screen_ratio_xy, screen_size, near - .000001f, far) * mat4::translate(.0f, .0f, -near);
     792    return mat4::perspective(fov_y, screen_size * screen_ratio_xy, screen_size, near - .000001f, far) *
     793           mat4::translate(.0f, .0f, -near);
    791794}
    792795
  • trunk/test/btphystest.cpp

    r2892 r2894  
    3939#define JUMP_HEIGHT         30.f
    4040#define JUMP_STRAFE         .5f
     41#define TARGET_TIMER        10.f + (rand(4.f) - 2.f)
    4142
    4243int gNumObjects = 64;
     
    8485                      vec3(0.f, 0.f, 0.f),
    8586                      vec3(0, 1, 0));
    86     m_camera->SetProjection(mat4::perspective(60.f, (float)Video::GetSize().x, (float)Video::GetSize().y, .1f, 1000.f));
     87    m_camera->SetProjection(60.f, .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
     88    m_target_timer = TARGET_TIMER;
     89    m_cam_target = -1;
    8790#else
    8891    m_camera->SetView(vec3(50.f, 50.f, 0.f),
    8992                      vec3(0.f, 0.f, 0.f),
    9093                      vec3(0, 1, 0));
    91     m_camera->SetProjection(mat4::perspective(45.f, (float)Video::GetSize().x, (float)Video::GetSize().y, .1f, 1000.f));
     94    m_camera->SetProjection(45.f, .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
    9295#endif
    9396    g_scene->PushCamera(m_camera);
     
    305308    float cam_factor = .0f;
    306309    vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
    307     vec3 cam_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
    308310    mat4 world_cam = g_scene->GetCamera()->GetView();
    309311    mat4 cam_screen = g_scene->GetCamera()->GetProjection();
    310312
     313    m_target_timer -= seconds;
     314    if (m_target_timer < .0f)
     315    {
     316        m_target_timer = TARGET_TIMER;
     317        if (m_cam_target == -1)
     318            m_cam_target = rand(m_physobj_list.Count());
     319        else
     320            m_cam_target = -1;
     321    }
     322
    311323    for (int i = 0; i < m_physobj_list.Count(); i++)
    312324    {
     
    316328        vec3 obj_loc = PhysObj->GetPhysic()->GetTransform().v3.xyz;
    317329
    318         cam_center += obj_loc;
    319         cam_factor += 1.f;
    320 
    321         mat4 LocalPos = mat4::translate(obj_loc);
    322         vec3 vpos;
    323 
    324         LocalPos = world_cam * LocalPos;
    325         vpos = LocalPos.v3.xyz;
    326         cam_min_max[0] = min(vpos.xyz, cam_min_max[0]);
    327         cam_min_max[1] = max(vpos.xyz, cam_min_max[1]);
    328 
    329         LocalPos = cam_screen * LocalPos;
    330         vpos = (LocalPos.v3 / LocalPos.v3.w).xyz;
    331         screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
    332         screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
    333 
     330        if (m_cam_target == -1 || m_cam_target == i)
     331        {
     332            cam_center += obj_loc;
     333            cam_factor += 1.f;
     334
     335            mat4 LocalPos = mat4::translate(obj_loc);
     336            vec3 vpos;
     337
     338            LocalPos = world_cam * LocalPos;
     339            mat4 LocalPos0 = LocalPos;
     340
     341            int j = 2;
     342            while (j-- > 0)
     343            {
     344                if (j == 1)
     345                    LocalPos = mat4::translate(vec3(-4.f)) * LocalPos0;
     346                else
     347                    LocalPos = mat4::translate(vec3(4.f)) * LocalPos0;
     348
     349                LocalPos = cam_screen * LocalPos;
     350                vpos = (LocalPos.v3 / LocalPos.v3.w).xyz;
     351                screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
     352                screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
     353            }
     354        }
     355
     356        //Jump handling
    334357        //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
    335358        if (lol::abs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
     
    346369    }
    347370
    348     vec3 min_max_diff = (cam_min_max[1] - cam_min_max[0]);
    349     float screen_size = max(max(lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)),
    350                         max(    lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)));
    351371    float fov_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
    352372                          max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
    353373
    354     //m_camera->SetProjection(mat4::perspective(30.f * fov_ratio * 1.1f, 1280.f, 960.f, .1f, 1000.f));
     374    vec3 new_target = cam_center / cam_factor;
     375    float fov_dp = .0f;
     376    float loc_dp = .0f;
     377
     378    //ideally fov is on the target
     379    if (lol::abs(fov_ratio - 1.f) < .2f)
     380        fov_dp = ((m_cam_target == -1)?(.7f):(.2f));
     381    else
     382        fov_dp = ((m_cam_target == -1)?(1.7f):(.9f));
     383
     384    //ideally loc is on the target
     385    if (length(new_target - m_camera->GetTarget()) < 6.f)
     386        loc_dp = ((m_cam_target == -1)?(.5f):(.03f));
     387    else
     388        loc_dp = ((m_cam_target == -1)?(.9f):(.5f));
     389
     390    m_camera->SetFov(damp(m_camera->GetFov(), m_camera->GetFov() * fov_ratio * 1.1f, fov_dp, seconds));
     391    vec3 tmp = damp(m_camera->GetTarget(), new_target, loc_dp, seconds);
    355392    m_camera->SetView((mat4::rotate(10.f * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
    356                       //vec3(70.f, 30.f, 0.f),
    357                       cam_center / cam_factor, vec3(0, 1, 0));
     393                      tmp, vec3(0, 1, 0));
    358394#endif //USE_BODIES
    359395#endif //CAT_MODE
  • trunk/test/btphystest.h

    r2889 r2894  
    7474
    7575    float                           m_loop_value;
     76    float                           m_target_timer;
     77    int                             m_cam_target;
    7678};
    7779
  • trunk/test/front_camera_sprite.lolfx

    r2892 r2894  
    7878
    7979    //need 130 : ivec2 tex_size = textureSize(in_texture, 0);
    80     int sample_nb = 5;
     80    int sample_nb = 2;
    8181    if (color.a < 0.9)
    8282    {
Note: See TracChangeset for help on using the changeset viewer.