summaryrefslogtreecommitdiff
path: root/src/lib/evas/include/evas_3d_utils.h
diff options
context:
space:
mode:
authorOleksandr Shcherbina <o.shcherbina@samsung.com>2014-12-01 06:26:28 +0100
committerCedric BAIL <cedric@osg.samsung.com>2014-12-01 06:26:32 +0100
commit54c1667d4e4dfc160fa8a7ce6118d6032645314a (patch)
treec5c1162727d02b2b50b5a83f19cf25b7043a6343 /src/lib/evas/include/evas_3d_utils.h
parent4366057dc640283d450701b828e48ff83e19f612 (diff)
evas: Evas_3D - add bounding sphere, revision frustum culling
Summary: Move check visibility of node from evas_3d_node to evas_3d_camera Move functionality (normalize, check distance, calculate frustum) in evas_3d_utils.h (we are planing use evas_is_sphere_in_frustum in evas_gl_3d.c - don't render mesh if it non visible) Add possibility check frustum by box, aabb, central point Refactor example frustum culling @feature Reviewers: Hermet, raster, cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D1420 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/lib/evas/include/evas_3d_utils.h')
-rw-r--r--src/lib/evas/include/evas_3d_utils.h264
1 files changed, 264 insertions, 0 deletions
diff --git a/src/lib/evas/include/evas_3d_utils.h b/src/lib/evas/include/evas_3d_utils.h
index 8e02a07223..139351d203 100644
--- a/src/lib/evas/include/evas_3d_utils.h
+++ b/src/lib/evas/include/evas_3d_utils.h
@@ -17,8 +17,10 @@ typedef struct _Evas_Mat3 Evas_Mat3;
17typedef struct _Evas_Mat4 Evas_Mat4; 17typedef struct _Evas_Mat4 Evas_Mat4;
18typedef struct _Evas_Box2 Evas_Box2; 18typedef struct _Evas_Box2 Evas_Box2;
19typedef struct _Evas_Box3 Evas_Box3; 19typedef struct _Evas_Box3 Evas_Box3;
20typedef struct _Evas_Line3 Evas_Line3;
20typedef struct _Evas_Triangle3 Evas_Triangle3; 21typedef struct _Evas_Triangle3 Evas_Triangle3;
21typedef struct _Evas_Ray3 Evas_Ray3; 22typedef struct _Evas_Ray3 Evas_Ray3;
23typedef struct _Evas_Sphere Evas_Sphere;
22 24
23struct _Evas_Color 25struct _Evas_Color
24{ 26{
@@ -79,6 +81,12 @@ struct _Evas_Box3
79 Evas_Vec3 p1; 81 Evas_Vec3 p1;
80}; 82};
81 83
84struct _Evas_Line3
85{
86 Evas_Vec3 point;
87 Evas_Vec3 direction;
88};
89
82struct _Evas_Triangle3 90struct _Evas_Triangle3
83{ 91{
84 Evas_Vec3 p0; 92 Evas_Vec3 p0;
@@ -92,6 +100,12 @@ struct _Evas_Ray3
92 Evas_Vec3 dir; 100 Evas_Vec3 dir;
93}; 101};
94 102
103struct _Evas_Sphere
104{
105 Evas_Vec3 center;
106 Evas_Real radius;
107};
108
95/* 2D vector */ 109/* 2D vector */
96static inline void 110static inline void
97evas_vec2_set(Evas_Vec2 *dst, Evas_Real x, Evas_Real y) 111evas_vec2_set(Evas_Vec2 *dst, Evas_Real x, Evas_Real y)
@@ -1594,6 +1608,17 @@ evas_box2_intersect_2d(const Evas_Box2 *box, const Evas_Vec2 *org, const Evas_Ve
1594 return EINA_TRUE; 1608 return EINA_TRUE;
1595} 1609}
1596 1610
1611static inline Evas_Real
1612evas_determinant_3D(Evas_Real matrix[3][3])
1613{
1614 return (matrix[0][0] * matrix[1][1] * matrix[2][2]) +
1615 (matrix[0][1] * matrix[1][2] * matrix[2][0]) +
1616 (matrix[0][2] * matrix[1][0] * matrix[2][1]) -
1617 (matrix[0][2] * matrix[1][1] * matrix[2][0]) -
1618 (matrix[0][1] * matrix[1][0] * matrix[2][2]) -
1619 (matrix[0][0] * matrix[1][2] * matrix[2][1]);
1620}
1621
1597static inline Eina_Bool 1622static inline Eina_Bool
1598evas_box3_ray3_intersect(const Evas_Box3 *box, const Evas_Ray3 *ray) 1623evas_box3_ray3_intersect(const Evas_Box3 *box, const Evas_Ray3 *ray)
1599{ 1624{
@@ -1722,3 +1747,242 @@ evas_reciprocal_sqrt(Evas_Real x)
1722 u.i = 0x5f3759df - (u.i >> 1); 1747 u.i = 0x5f3759df - (u.i >> 1);
1723 return u.f * (1.5f - u.f * u.f * x * 0.5f); 1748 return u.f * (1.5f - u.f * u.f * x * 0.5f);
1724} 1749}
1750
1751static inline void
1752evas_build_sphere(const Evas_Box3 *box, Evas_Sphere *sphere)
1753{
1754 Evas_Vec3 tmp;
1755
1756 evas_vec3_set(&sphere->center, (0.5 * (box->p0.x + box->p1.x)), (0.5 * (box->p0.y + box->p1.y)), (0.5 * (box->p0.z + box->p1.z)));
1757 evas_vec3_set(&tmp, sphere->center.x - box->p0.x, sphere->center.y - box->p0.y, sphere->center.z - box->p0.z);
1758
1759 sphere->radius = sqrtf(evas_vec3_dot_product(&tmp, &tmp));
1760}
1761
1762static inline void
1763evas_plane_normalize(Evas_Vec4 *plane)
1764{
1765 Evas_Vec3 tmp;
1766 Evas_Real length;
1767 evas_vec3_set(&tmp, plane->x, plane->y, plane->z);
1768 length = evas_vec3_length_get(&tmp);
1769 plane->x = plane->x / length;
1770 plane->y = plane->y / length;
1771 plane->z = plane->z / length;
1772 plane->w = plane->w / length;
1773}
1774
1775static inline Eina_Bool
1776evas_intersection_line_of_two_planes(Evas_Line3 *line, Evas_Vec4 *plane1, Evas_Vec4 *plane2)
1777{
1778 //TODO:parallel case
1779 Evas_Vec3 planes3D[2];
1780
1781 evas_vec3_set(&planes3D[0], plane1->x, plane1->y, plane1->z);
1782 evas_vec3_set(&planes3D[1], plane2->x, plane2->y, plane2->z);
1783
1784 evas_vec3_cross_product(&line->direction, &planes3D[0], &planes3D[1]);
1785
1786#define SOLVE_EQUATION(x, y, z) \
1787 line->point.x = 0; \
1788 line->point.y = (plane2->w * plane1->z - plane1->w * plane2->z) / line->direction.x; \
1789 line->point.z = (plane2->y * plane1->w - plane1->y * plane2->w) / line->direction.x;
1790
1791 if (line->direction.x && plane1->z)
1792 {
1793 SOLVE_EQUATION(x, y, z)
1794 }
1795 else if (line->direction.y && plane1->x)
1796 {
1797 SOLVE_EQUATION(y, z, x)
1798 }
1799 else
1800 {
1801 SOLVE_EQUATION(z, x, y)
1802 }
1803#undef SOLVE_EQUATION
1804
1805 return EINA_TRUE;
1806}
1807
1808static inline Eina_Bool
1809evas_intersection_point_of_three_planes(Evas_Vec3 *point, Evas_Vec4 *plane1, Evas_Vec4 *plane2, Evas_Vec4 *plane3)
1810{
1811 //TODO:parallel case
1812 int i;
1813 Evas_Real delta, deltax, deltay, deltaz;
1814 Evas_Real matrix_to_det[3][3];
1815 Evas_Vec4 planes[3];
1816
1817 planes[0] = *plane1;
1818 planes[1] = *plane2;
1819 planes[2] = *plane3;
1820
1821 for (i = 0; i < 3; i++)
1822 {
1823 matrix_to_det[0][i] = planes[i].x;
1824 matrix_to_det[1][i] = planes[i].y;
1825 matrix_to_det[2][i] = planes[i].z;
1826 }
1827 delta = evas_determinant_3D(matrix_to_det);
1828
1829 for (i = 0; i < 3; i++)
1830 matrix_to_det[0][i] = planes[i].w;
1831 deltax = evas_determinant_3D(matrix_to_det);
1832
1833 for (i = 0; i < 3; i++)
1834 {
1835 matrix_to_det[0][i] = planes[i].x;
1836 matrix_to_det[1][i] = planes[i].w;
1837 }
1838 deltay = evas_determinant_3D(matrix_to_det);
1839
1840 for (i = 0; i < 3; i++)
1841 {
1842 matrix_to_det[1][i] = planes[i].y;
1843 matrix_to_det[2][i] = planes[i].w;
1844 }
1845 deltaz = evas_determinant_3D(matrix_to_det);
1846
1847 evas_vec3_set(point, -deltax/delta, -deltay/delta, -deltaz/delta);
1848
1849 return EINA_TRUE;
1850}
1851
1852static inline Evas_Real
1853evas_point_plane_distance(Evas_Vec3 *point, Evas_Vec4 *plane)
1854{
1855 return plane->x * point->x + plane->y * point->y + plane->z * point->z + plane->w;
1856}
1857
1858static inline Evas_Real
1859evas_point_line_distance(Evas_Vec3 *point, Evas_Line3 *line)
1860{
1861 Evas_Vec3 temp, sub;
1862
1863 evas_vec3_subtract(&sub, point, &line->point);
1864 evas_vec3_cross_product(&temp, &sub, &line->direction);
1865
1866 return evas_vec3_length_get(&temp) / evas_vec3_length_get(&line->direction);
1867}
1868
1869static inline Eina_Bool
1870evas_is_sphere_in_frustum(Evas_Sphere *bsphere, Evas_Vec4 *planes)
1871{
1872 int i;
1873 Evas_Line3 line;
1874 Evas_Vec3 point, sub;
1875 Evas_Real distances[6] = {0};
1876 int intersected_planes[3];
1877 int intersected_planes_count = 0;
1878
1879 for (i = 0; i < 6; i++)
1880 {
1881 distances[i] = evas_point_plane_distance(&bsphere->center, &planes[i]);
1882 }
1883
1884 for (i = 0; i < 6; i++)
1885 {
1886 if (distances[i] <= -bsphere->radius)
1887 {
1888 return EINA_FALSE;
1889 }
1890 else if (distances[i] <= 0)
1891 {
1892 intersected_planes[intersected_planes_count] = i;
1893 intersected_planes_count++;
1894 }
1895 }
1896
1897 if ((intersected_planes_count == 0) || (intersected_planes_count == 1))
1898 return EINA_TRUE;
1899 else if (intersected_planes_count == 2)
1900 {
1901 evas_intersection_line_of_two_planes(&line, &planes[intersected_planes[0]], &planes[intersected_planes[1]]);
1902 return (evas_point_line_distance(&bsphere->center, &line) < bsphere->radius) ? EINA_TRUE : EINA_FALSE;
1903 }
1904 else if (intersected_planes_count == 3)
1905 {
1906 evas_intersection_point_of_three_planes(&point, &planes[intersected_planes[0]], &planes[intersected_planes[1]], &planes[intersected_planes[2]]);
1907 evas_vec3_subtract(&sub, &point, &bsphere->center);
1908 return (evas_vec3_length_get(&sub) < bsphere->radius) ? EINA_TRUE : EINA_FALSE;
1909 }
1910
1911 return EINA_FALSE;
1912}
1913
1914static inline Eina_Bool
1915evas_is_point_in_frustum(Evas_Vec3 *point, Evas_Vec4 *planes)
1916{
1917 int i;
1918 for (i = 0; i < 6; i++)
1919 if (evas_point_plane_distance(point, &planes[i]) <= 0) return EINA_FALSE;
1920 return EINA_TRUE;
1921}
1922
1923static inline Eina_Bool
1924evas_is_box_in_frustum(Evas_Box3 *box, Evas_Vec4 *planes)
1925{
1926 int i;
1927 for (i = 0; i < 6; i++)
1928 {
1929 if (planes[i].x * box->p0.x + planes[i].y * box->p0.y + planes[i].z * box->p0.z + planes[i].w > 0)
1930 continue;
1931 if (planes[i].x * box->p1.x + planes[i].y * box->p0.y + planes[i].z * box->p0.z + planes[i].w > 0)
1932 continue;
1933 if (planes[i].x * box->p1.x + planes[i].y * box->p1.y + planes[i].z * box->p0.z + planes[i].w > 0)
1934 continue;
1935 if (planes[i].x * box->p0.x + planes[i].y * box->p1.y + planes[i].z * box->p0.z + planes[i].w > 0)
1936 continue;
1937 if (planes[i].x * box->p0.x + planes[i].y * box->p0.y + planes[i].z * box->p1.z + planes[i].w > 0)
1938 continue;
1939 if (planes[i].x * box->p1.x + planes[i].y * box->p0.y + planes[i].z * box->p1.z + planes[i].w > 0)
1940 continue;
1941 if (planes[i].x * box->p1.x + planes[i].y * box->p1.y + planes[i].z * box->p1.z + planes[i].w > 0)
1942 continue;
1943 if (planes[i].x * box->p0.x + planes[i].y * box->p1.y + planes[i].z * box->p1.z + planes[i].w > 0)
1944 continue;
1945 return EINA_FALSE;
1946 }
1947
1948 return EINA_TRUE;
1949}
1950
1951static inline void
1952evas_frustum_calculate(Evas_Vec4 *planes, Evas_Mat4 *matrix_vp)
1953{
1954 int i;
1955 evas_vec4_set(&planes[0], matrix_vp->m[3] - matrix_vp->m[0],
1956 matrix_vp->m[7] - matrix_vp->m[4],
1957 matrix_vp->m[11] - matrix_vp->m[8],
1958 matrix_vp->m[15] - matrix_vp->m[12]);
1959
1960 evas_vec4_set(&planes[1], matrix_vp->m[3] + matrix_vp->m[0],
1961 matrix_vp->m[7] + matrix_vp->m[4],
1962 matrix_vp->m[11] + matrix_vp->m[8],
1963 matrix_vp->m[15] + matrix_vp->m[12]);
1964
1965 evas_vec4_set(&planes[2], matrix_vp->m[3] + matrix_vp->m[1],
1966 matrix_vp->m[7] + matrix_vp->m[5],
1967 matrix_vp->m[11] + matrix_vp->m[9],
1968 matrix_vp->m[15] + matrix_vp->m[13]);
1969
1970 evas_vec4_set(&planes[3], matrix_vp->m[3] - matrix_vp->m[1],
1971 matrix_vp->m[7] - matrix_vp->m[5],
1972 matrix_vp->m[11] - matrix_vp->m[9],
1973 matrix_vp->m[15] - matrix_vp->m[13]);
1974
1975 evas_vec4_set(&planes[4], matrix_vp->m[3] - matrix_vp->m[2],
1976 matrix_vp->m[7] - matrix_vp->m[6],
1977 matrix_vp->m[11] - matrix_vp->m[10],
1978 matrix_vp->m[15] - matrix_vp->m[14]);
1979
1980 evas_vec4_set(&planes[5], matrix_vp->m[3] + matrix_vp->m[2],
1981 matrix_vp->m[7] + matrix_vp->m[6],
1982 matrix_vp->m[11] + matrix_vp->m[10],
1983 matrix_vp->m[15] + matrix_vp->m[14]);
1984 for (i = 0; i < 6; i++)
1985 {
1986 evas_plane_normalize(&planes[i]);
1987 }
1988}