Changeset 2929


Ignore:
Timestamp:
Sep 15, 2013, 1:42:49 AM (6 years ago)
Author:
touky
Message:

mat4 : removed useless ortho(). Fixed shifted_perspective so it actually works.
numeric : Added SmoothClamp & swap
camera : fixed target&up issues and added screen-scale factor.
touky_demo : correct camera integration & first pass on uro refactor. Also added Makefile to vcproj

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/camera.cpp

    r2914 r2929  
    33//
    44// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
     5//                2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
    56//   This program is free software; you can redistribute it and/or
    67//   modify it under the terms of the Do What The Fuck You Want To
     
    3940    m_screen_ratio = (float)screen_size.x / (float)screen_size.y;
    4041    m_is_shifted = false;
     42    m_screen_scale = vec2(1.f);
     43
     44    m_target_distance = .0f;
    4145
    4246    /* Create a default perspective */
    4347    SetProjection(mat4::perspective(45.f, 800.f, 600.f, -1000.f, 1000.f));
    44     SetView(mat4::lookat(vec3(0.f, 50.f, 50.f),
    45                          vec3(0.f),
    46                          vec3(0.f, 1.f, 0.f)));
     48    SetView(mat4::lookat(vec3(0.f, 50.f, 50.f), vec3(0.f), vec3(0.f, 1.f, 0.f)));
    4749}
    4850
     
    5456//View functions
    5557//--
     58
     59//WARNING : Don't forget the position
    5660void Camera::SetView(mat4 const &view)
    5761{
     
    6973void Camera::SetView(vec3 pos, vec3 rot)
    7074{
    71     SetView(pos, quat::fromeuler_xyz(rot));
     75    SetView(pos, quat::fromeuler_zyx(rot.zyx));
    7276}
    7377
    7478void Camera::SetView(vec3 pos, quat rot)
    7579{
    76     m_view_matrix = mat4::lookat(pos,
    77                                  pos + rot.transform(vec3(0.f, 0.f, -max(m_target_distance, 1.f))),
    78                                  rot.transform(vec3(0.f, 1.f, 0.f)));
     80    m_view_matrix = inverse(mat4(rot)) * mat4::translate(-pos);
    7981    m_position = pos;
    8082}
     
    105107    m_screen_size = screen_size;
    106108    m_screen_ratio = screen_ratio;
    107 
     109    mat4 screen_scale = mat4::scale(vec3(m_screen_scale.xy, 1.f));
    108110    if (m_fov > .0f)
    109111    {
    110112        if (m_is_shifted)
    111             SetProjection(mat4::shifted_perspective(m_fov, screen_size, screen_ratio, m_far - m_near));
     113            SetProjection(screen_scale * mat4::shifted_perspective(m_fov, screen_size, screen_ratio, m_near, m_far));
    112114        else
    113             SetProjection(mat4::perspective(m_fov, screen_size, screen_size * screen_ratio, m_near, m_far));
     115            SetProjection(screen_scale * mat4::perspective(m_fov, screen_size, screen_size * screen_ratio, m_near, m_far));
    114116    }
    115117    else
    116         SetProjection(mat4::ortho(screen_size, screen_size * screen_ratio, m_near, m_far));
     118        SetProjection(screen_scale * mat4::ortho(screen_size, screen_size * screen_ratio, m_near, m_far));
    117119}
    118120
     
    148150{
    149151    SetProjection(m_fov, near, far, m_screen_size, m_screen_ratio);
     152}
     153
     154void Camera::SetScreenScale(vec2 screen_scale)
     155{
     156    m_screen_scale = screen_scale;
     157    SetProjection(m_fov, m_near, m_far, m_screen_size, m_screen_ratio);
    150158}
    151159
     
    164172//camera manipulation Functions
    165173//--
    166 void Camera::SetPosition(vec3 pos)
    167 {
    168     if (m_target_distance > .0f)
    169         SetView(m_position, m_position + GetTarget(), GetUp());
     174void Camera::SetPosition(vec3 pos, bool keep_target)
     175{
     176    if (keep_target)
     177        SetView(pos, GetTarget(), GetUp());
    170178    else
    171179        SetView(GetView() * mat4::translate(pos - m_position));
     
    173181}
    174182
    175 void Camera::SetTarget(vec3 target)
    176 {
    177     SetView(m_position, target, GetUp());
    178 }
    179 
    180 void Camera::SetUp(vec3 up)
    181 {
    182     SetView(m_position, GetTarget(), up);
     183void Camera::SetTarget(vec3 target, vec3 up)
     184{
     185    SetView(m_position, target, up);
    183186}
    184187
    185188void Camera::SetRotation(vec3 rot)
    186189{
    187     SetRotation(quat::fromeuler_xyz(rot));
     190    SetView(m_position, rot);
    188191}
    189192
  • trunk/src/camera.h

    r2899 r2929  
    5353    void SetDrawInfos(float far);
    5454    void SetDrawInfos(float near, float far);
     55    void SetScreenScale(vec2 scale);
    5556    void UseShift(bool should_shift);
    5657    void UseTarget(bool use_target);
     
    6162    float GetNear()         { return m_near; }
    6263    float GetFar()          { return m_far; }
     64    vec2  GetScreenScale()  { return m_screen_scale; }
    6365    bool  IsShifted()       { return m_is_shifted; }
    6466    bool  IsTargeting()     { return (m_target_distance != .0f); }
    6567
    6668    //camera manipulation Functions
    67     void SetPosition(vec3 pos);
    68     void SetTarget(vec3 target);
    69     void SetUp(vec3 up);
     69    void SetPosition(vec3 pos, bool keep_target=false);
     70    void SetTarget(vec3 target, vec3 up);
    7071    void SetRotation(vec3 rot);
    7172    void SetRotation(quat rot);
     
    9798    float   m_near;
    9899    float   m_far;
     100    vec2    m_screen_scale;
    99101    bool    m_is_shifted;
     102    bool    m_fix_up;
    100103};
    101104
  • trunk/src/lol/math/vector.h

    r2914 r2929  
    18051805    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
    18061806    static Mat4<T> ortho(T width, T height, T near, T far);
    1807     static Mat4<T> ortho(T screen_size, T screen_ratio_xy, T draw_distance);
    18081807    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
    18091808    static Mat4<T> perspective(T fov_y, T width, T height, T near, T far);
    1810     static Mat4<T> shifted_perspective(T fov_y, T screen_size, T screen_ratio_xy, T draw_distance);
     1809    static Mat4<T> shifted_perspective(T fov_y, T screen_size, T screen_ratio_yx, T near, T far);
    18111810
    18121811    void printf() const;
  • trunk/src/math/vector.cpp

    r2915 r2929  
    746746}
    747747
    748 template<> mat4 mat4::ortho(float screen_size, float screen_ratio_xy, float draw_distance)
    749 {
    750     return mat4::ortho(screen_size * screen_ratio_xy, screen_size, .00001f, draw_distance);
    751 }
    752 
    753748template<> mat4 mat4::frustum(float left, float right, float bottom,
    754749                              float top, float near, float far)
     
    783778//Returns a perspective matrix with the camera location shifted to be on the near plane
    784779template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size,
    785                                           float screen_ratio_xy, float draw_distance)
     780                                          float screen_ratio_yx, float near, float far)
    786781{
    787782    float new_fov_y = fov_y * (F_PI / 180.0f);
    788 
    789     float near = (screen_size * .5f) / lol::tan(new_fov_y * .5f);
    790     float far = near + draw_distance;
    791 
    792     return mat4::perspective(fov_y, screen_size * screen_ratio_xy, screen_size, near - .000001f, far) *
    793            mat4::translate(.0f, .0f, -near);
     783    float dist_scr = (screen_size * screen_ratio_yx * .5f) / tanf(new_fov_y * .5f);
     784
     785    return mat4::perspective(fov_y, screen_size, screen_size * screen_ratio_yx,
     786                             max(.001f, dist_scr + near),
     787                             max(.001f, dist_scr + far)) *
     788           mat4::translate(.0f, .0f, -dist_scr);
    794789}
    795790
  • trunk/src/numeric.h

    r2816 r2929  
    4444}
    4545
     46template< class T > inline void Swap( T& A, T& B )
     47{
     48        const T Temp = A;
     49        A = B;
     50        B = Temp;
     51}
     52
     53//SmoothClamp clamps x in [a - sd, b + sd] and returns a value in [a, b] that is smoothed at the borders.
     54static inline float SmoothClamp(float &x, float a, float b, float sd)
     55{
     56        if (b < a)
     57                Swap(a, b);
     58
     59    float tx = x;
     60    float ta = a - sd;
     61    float tb = b + sd;
     62        if (sd > 0.f)
     63        {
     64                if ((b - a) < 2.f * sd)
     65                        sd = .5f * (b - a);
     66
     67                if (tx < a + sd && tx > a - sd)
     68                {
     69                        float t = (tx - a) / sd;
     70                        t = 0.25f * (t + 1.0f) * (t + 1.0f);
     71                        tx = a + t * sd;
     72                }
     73                else if (tx < b + sd && tx > b - sd)
     74                {
     75                        float t = -(tx - b) / sd;
     76                        t = 0.25f * (t + 1.0f) * (t + 1.0f);
     77                        tx = b - t * sd;
     78                }
     79        }
     80
     81    x = lol::clamp(x, ta, tb);
     82        return lol::clamp(tx, a, b);
     83}
     84
    4685} /* namespace lol */
    4786
  • trunk/test/meshviewer.cpp

    r2925 r2929  
    296296            cur_cam->SetProjection(mat4::ortho(screen_size * fov_ratio * 1.1f, 1600.f / 600.f, 1000.f));
    297297        else if (fov_ratio > .0f)
    298             cur_cam->SetProjection(mat4::shifted_perspective(m_fov_damp, screen_size * fov_ratio * 1.1f, 1600.f / 600.f, 1000.f));
     298            cur_cam->SetProjection(mat4::shifted_perspective(m_fov_damp, screen_size * fov_ratio * 1.1f, 1600.f / 600.f, .00001f, 1000.f));
    299299
    300300        vec3 cam_center = cam_min_max[0] + min_max_diff * .5f;
  • trunk/test/physics/lolbtphysicsintegration.h

    r2910 r2929  
    2020namespace lol
    2121{
    22     //Override Gamegroups names for Physic-useage
     22    //Override Gamegroups names for Physic-usage
    2323    //"_ENT_" means that this is a group for Entities that use EasyPhysic primitives.
    2424    //"_EZP_" means that this is a group for EasyPhysic primitives.
Note: See TracChangeset for help on using the changeset viewer.