forked from enlightenment/efl
evas: Evas_3D - Revision frustum culling.
Summary: Add API that checks OBB's points of node are inside frustum of camera. Add example with frustum culling. @feature Reviewers: Hermet, cedric, raster CC: cedric Differential Revision: https://phab.enlightenment.org/D942 Signed-off-by: Cedric BAIL <c.bail@partner.samsung.com>
This commit is contained in:
parent
bebcc49bd0
commit
a25aa1aa1a
|
@ -22,4 +22,5 @@
|
|||
/evas_3d_pick
|
||||
/evas_3d_proxy
|
||||
/evas_cxx_rectangle
|
||||
/evas_3d_aabb
|
||||
/evas_3d_aabb
|
||||
/evas_3d_frustum
|
||||
|
|
|
@ -197,6 +197,11 @@ evas_3d_md2_SOURCES = evas-3d-md2.c
|
|||
evas_3d_md2_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
|
||||
evas_3d_md2_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
|
||||
|
||||
EXTRA_PROGRAMS += evas_3d_frustum
|
||||
evas_3d_frustum_SOURCES = evas-3d-frustum.c
|
||||
evas_3d_frustum_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
|
||||
evas_3d_frustum_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
|
||||
|
||||
EXTRA_PROGRAMS += evas_3d_aabb
|
||||
evas_3d_aabb_SOURCES = evas-3d-aabb.c
|
||||
evas_3d_aabb_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
|
||||
|
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 574 KiB |
|
@ -0,0 +1,256 @@
|
|||
/*
|
||||
* This example shows how to work frustum culling.
|
||||
* Use keys Up/Down for moving far plane of frustum.
|
||||
* Use keys Left/Right for changing camera view.
|
||||
* See in terminal output value of z coordinate of far plane of frustum
|
||||
* and check OBB's points inside frustum.
|
||||
* @see evas_3d_node_obb_frustum_check.
|
||||
* Compile with "gcc -o evas-3d-frustum evas-3d-frustum.c `pkg-config --libs --cflags evas ecore ecore-evas eo`"
|
||||
*/
|
||||
|
||||
#define EFL_EO_API_SUPPORT
|
||||
#define EFL_BETA_API_SUPPORT
|
||||
|
||||
#include <Ecore.h>
|
||||
#include <Ecore_Evas.h>
|
||||
#include <Evas.h>
|
||||
#include <Eo.h>
|
||||
|
||||
#define WIDTH 800
|
||||
#define HEIGHT 600
|
||||
|
||||
typedef struct _Scene_Data
|
||||
{
|
||||
Eo *root_node;
|
||||
Eo *camera_node;
|
||||
Eo *light_node;
|
||||
Eo *mesh_node_model;
|
||||
Eo *scene;
|
||||
Eo *camera;
|
||||
Eo *light;
|
||||
Eo *mesh_model;
|
||||
Eo *material_model;
|
||||
Eo *texture_model;
|
||||
} Scene_Data;
|
||||
|
||||
Evas *evas;
|
||||
Evas_Object *background,*image;
|
||||
Evas_Real fleft = -5, fright = 5, fbottom = -5, fup = 5, fnear = 20, ffar = 40;
|
||||
|
||||
static void
|
||||
_on_delete(Ecore_Evas *ee EINA_UNUSED)
|
||||
{
|
||||
ecore_main_loop_quit();
|
||||
}
|
||||
|
||||
static void
|
||||
_on_canvas_resize(Ecore_Evas *ee)
|
||||
{
|
||||
int w, h;
|
||||
|
||||
ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
|
||||
|
||||
evas_object_resize(background, w, h);
|
||||
evas_object_resize(image, w, h);
|
||||
evas_object_move(image, 0, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
|
||||
{
|
||||
|
||||
Scene_Data *scene = (Scene_Data *)data;
|
||||
Evas_Event_Key_Down *ev = event_info;
|
||||
int frustum;
|
||||
|
||||
if (!strcmp("Up", ev->key))
|
||||
{
|
||||
ffar += 5;
|
||||
eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar));
|
||||
eo_do(scene->mesh_node_model,
|
||||
frustum = evas_3d_node_obb_frustum_check(scene->camera_node));
|
||||
fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum);
|
||||
}
|
||||
else if(!strcmp("Down", ev->key))
|
||||
{
|
||||
ffar -= 5;
|
||||
eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar));
|
||||
eo_do(scene->mesh_node_model,
|
||||
frustum = evas_3d_node_obb_frustum_check(scene->camera_node));
|
||||
fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum);
|
||||
}
|
||||
else if(!strcmp("Return", ev->key))
|
||||
{
|
||||
eo_do(scene->mesh_node_model,
|
||||
frustum = evas_3d_node_obb_frustum_check(scene->camera_node));
|
||||
fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum);
|
||||
}
|
||||
else if (!strcmp("Left", ev->key))
|
||||
{
|
||||
eo_do(scene->camera_node, evas_3d_node_position_set(50.0, 0.0, 0.0),
|
||||
evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0));
|
||||
fprintf(stdout, "Changed position and view of camera\n");
|
||||
}
|
||||
else if (!strcmp("Right", ev->key))
|
||||
{
|
||||
eo_do(scene->camera_node, evas_3d_node_position_set(0.0, 0.0, 50.0);
|
||||
evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, -1.0, 0.0));
|
||||
fprintf(stdout, "Changed position and view of camera\n");
|
||||
}
|
||||
else
|
||||
fprintf(stdout, "Press Right/Left/Up/Bottom keys\n");
|
||||
}
|
||||
|
||||
static Eina_Bool
|
||||
_animate_scene_model(void *data)
|
||||
{
|
||||
static int frame = 0;
|
||||
Scene_Data *scene = (Scene_Data *)data;
|
||||
|
||||
eo_do(scene->mesh_node_model,
|
||||
evas_3d_node_mesh_frame_set(scene->mesh_model, frame),
|
||||
evas_3d_node_orientation_angle_axis_set(90, 1.0, 0.0, 0.0));
|
||||
frame += 20;
|
||||
|
||||
if (frame > 256 * 18) frame = 0;
|
||||
|
||||
return ECORE_CALLBACK_RENEW;
|
||||
}
|
||||
|
||||
static void
|
||||
_camera_setup(Scene_Data *data)
|
||||
{
|
||||
data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
|
||||
data->camera_node = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
|
||||
evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
|
||||
eo_do(data->camera_node,
|
||||
evas_3d_node_camera_set(data->camera),
|
||||
evas_3d_node_position_set(0.0, 0.0, 50.0);
|
||||
evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, -1.0, 0.0));
|
||||
|
||||
eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
|
||||
}
|
||||
|
||||
static void
|
||||
_light_setup(Scene_Data *data)
|
||||
{
|
||||
data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
|
||||
eo_do(data->light,
|
||||
evas_3d_light_ambient_set( 0.2, 0.2, 0.2, 1.0);
|
||||
evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0);
|
||||
evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));
|
||||
|
||||
data->light_node = eo_add_custom(EVAS_3D_NODE_CLASS,evas,
|
||||
evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
|
||||
eo_do(data->light_node,
|
||||
evas_3d_node_light_set(data->light),
|
||||
evas_3d_node_position_set(0.0, 0.0, 0.0),
|
||||
evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
|
||||
|
||||
eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
|
||||
}
|
||||
|
||||
static void
|
||||
_mesh_setup_model(Scene_Data *data)
|
||||
{
|
||||
data->mesh_model = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
data->material_model = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
|
||||
data->texture_model = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
|
||||
|
||||
eo_do(data->texture_model,
|
||||
evas_3d_texture_file_set("eagle.png", NULL),
|
||||
evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_NEAREST, EVAS_3D_TEXTURE_FILTER_NEAREST),
|
||||
evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT, EVAS_3D_WRAP_MODE_REPEAT));
|
||||
|
||||
eo_do(data->material_model,
|
||||
evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
|
||||
evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
|
||||
evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
|
||||
evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE);
|
||||
evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
|
||||
evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.01, 0.01, 0.01, 1.0),
|
||||
evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
|
||||
evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
|
||||
evas_3d_material_texture_set( EVAS_3D_MATERIAL_DIFFUSE, data->texture_model),
|
||||
evas_3d_material_shininess_set(100.0));
|
||||
|
||||
eo_do(data->mesh_model,
|
||||
evas_3d_mesh_file_set(EVAS_3D_MESH_FILE_TYPE_MD2, "eagle.md2", NULL),
|
||||
evas_3d_mesh_frame_material_set(0, data->material_model),
|
||||
evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE));
|
||||
}
|
||||
|
||||
static void
|
||||
_scene_setup(Scene_Data *data)
|
||||
{
|
||||
data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
|
||||
|
||||
data->root_node = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
|
||||
evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
|
||||
_camera_setup(data);
|
||||
_light_setup(data);
|
||||
_mesh_setup_model(data);
|
||||
|
||||
data->mesh_node_model = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
|
||||
evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
|
||||
eo_do(data->mesh_node_model,
|
||||
evas_3d_node_position_set(0, 0, 0);
|
||||
evas_3d_node_scale_set(0.3, 0.3, 0.3),
|
||||
evas_3d_node_orientation_angle_axis_set(90, 1.0, 1.0, 0.0));
|
||||
eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node_model));
|
||||
eo_do(data->mesh_node_model, evas_3d_node_mesh_add(data->mesh_model));
|
||||
|
||||
eo_do(data->scene,
|
||||
evas_3d_scene_size_set( WIDTH, HEIGHT),
|
||||
evas_3d_scene_background_color_set(0.5, 0.5, 0.5, 0.0),
|
||||
evas_3d_scene_root_node_set(data->root_node);
|
||||
evas_3d_scene_camera_node_set(data->camera_node));
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
Scene_Data data;
|
||||
|
||||
Ecore_Evas *ecore_evas;
|
||||
|
||||
if (!ecore_evas_init()) return 0;
|
||||
|
||||
setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
|
||||
|
||||
ecore_evas = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
|
||||
|
||||
if (!ecore_evas) return 0;
|
||||
|
||||
ecore_evas_callback_delete_request_set(ecore_evas, _on_delete);
|
||||
ecore_evas_callback_resize_set(ecore_evas, _on_canvas_resize);
|
||||
ecore_evas_show(ecore_evas);
|
||||
|
||||
evas = ecore_evas_get(ecore_evas);
|
||||
|
||||
_scene_setup(&data);
|
||||
|
||||
background = evas_object_rectangle_add(evas);
|
||||
evas_object_color_set(background, 0, 0, 0, 255);
|
||||
evas_object_move(background, 0, 0);
|
||||
evas_object_resize(background, WIDTH, HEIGHT);
|
||||
evas_object_show(background);
|
||||
|
||||
image = evas_object_image_filled_add(evas);
|
||||
evas_object_move(image, 0, 0);
|
||||
evas_object_resize(image, WIDTH, HEIGHT);
|
||||
evas_object_show(image);
|
||||
|
||||
evas_object_focus_set(image, EINA_TRUE);
|
||||
eo_do(image, evas_obj_image_scene_set(data.scene));
|
||||
|
||||
ecore_timer_add(0.1, _animate_scene_model, &data);
|
||||
|
||||
evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, &data);
|
||||
ecore_main_loop_begin();
|
||||
|
||||
ecore_evas_free(ecore_evas);
|
||||
ecore_evas_shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1378,4 +1378,99 @@ _evas_3d_node_bounding_box_get(Eo *obj EINA_UNUSED, Evas_3D_Node_Data *pd, Evas_
|
|||
if (z2) *z2 = pd->aabb.p1.z;
|
||||
}
|
||||
|
||||
EOLIAN static int
|
||||
_evas_3d_node_obb_frustum_check(Eo *obj EINA_UNUSED, Evas_3D_Node_Data *pd, Evas_3D_Node *camera_node)
|
||||
{
|
||||
Evas_Mat4 matrix_eye;
|
||||
Evas_Mat4 matrix_local_to_world;
|
||||
Evas_Mat4 matrix_mv;
|
||||
Evas_Mat4 matrix_mvp;
|
||||
Evas_Vec4 plane_right, plane_left, plane_bottom, plane_top, plane_far, plane_near, tmp;
|
||||
int frustum = 0;
|
||||
Evas_3D_Node_Data *camera_pd = eo_data_scope_get(camera_node, EVAS_3D_CAMERA_CLASS);
|
||||
Evas_3D_Camera_Data *camera = eo_data_scope_get(camera_pd->data.camera.camera, EVAS_3D_CAMERA_CLASS);
|
||||
|
||||
|
||||
if (camera_pd->type != EVAS_3D_NODE_TYPE_CAMERA)
|
||||
{
|
||||
ERR("Nodes type mismatch.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#define CHECK_IN_FRUSTUM_MIN(name) \
|
||||
(((plane_##name.x * pd->obb.p0.x + plane_##name.y * pd->obb.p0.y + plane_##name.z * pd->obb.p0.z + plane_##name.w) >= 0) ? EINA_TRUE : EINA_FALSE)
|
||||
|
||||
#define CHECK_IN_FRUSTUM_MAX(name) \
|
||||
(((plane_##name.x * pd->obb.p1.x + plane_##name.y * pd->obb.p1.y + plane_##name.z * pd->obb.p1.z + plane_##name.w) >= 0) ? EINA_TRUE : EINA_FALSE)
|
||||
|
||||
#define NORMALIZE(name) \
|
||||
evas_vec4_copy(&tmp, &plane_##name); \
|
||||
plane_##name.x = plane_##name.x / sqrtf(evas_vec4_length_square_get(&tmp)); \
|
||||
plane_##name.y = plane_##name.y / sqrtf(evas_vec4_length_square_get(&tmp)); \
|
||||
plane_##name.z = plane_##name.z / sqrtf(evas_vec4_length_square_get(&tmp)); \
|
||||
plane_##name.w = plane_##name.w / sqrtf(evas_vec4_length_square_get(&tmp));
|
||||
|
||||
/*get need matrix like multiply view matrix with projection matrix*/
|
||||
evas_mat4_inverse_build(&matrix_eye, &camera_pd->position_world, &camera_pd->orientation_world, &camera_pd->scale_world);
|
||||
evas_mat4_build(&matrix_local_to_world, &pd->position_world, &pd->orientation_world, &pd->scale_world);
|
||||
evas_mat4_multiply(&matrix_mv, &matrix_eye, &matrix_local_to_world);
|
||||
evas_mat4_multiply(&matrix_mvp, &camera->projection, &matrix_mv);
|
||||
|
||||
/*get planes and normilize results*/
|
||||
evas_vec4_set(&plane_right, matrix_mvp.m[3] - matrix_mvp.m[0],
|
||||
matrix_mvp.m[7] - matrix_mvp.m[4],
|
||||
matrix_mvp.m[11] - matrix_mvp.m[8],
|
||||
matrix_mvp.m[15] - matrix_mvp.m[12]);
|
||||
NORMALIZE(right)
|
||||
|
||||
evas_vec4_set(&plane_left, matrix_mvp.m[3] + matrix_mvp.m[0],
|
||||
matrix_mvp.m[7] + matrix_mvp.m[4],
|
||||
matrix_mvp.m[11] + matrix_mvp.m[8],
|
||||
matrix_mvp.m[15] + matrix_mvp.m[12]);
|
||||
NORMALIZE(left)
|
||||
|
||||
evas_vec4_set(&plane_bottom, matrix_mvp.m[3] + matrix_mvp.m[1],
|
||||
matrix_mvp.m[7] + matrix_mvp.m[5],
|
||||
matrix_mvp.m[11] + matrix_mvp.m[9],
|
||||
matrix_mvp.m[15] + matrix_mvp.m[13]);
|
||||
NORMALIZE(bottom)
|
||||
|
||||
evas_vec4_set(&plane_top, matrix_mvp.m[3] - matrix_mvp.m[1],
|
||||
matrix_mvp.m[7] - matrix_mvp.m[5],
|
||||
matrix_mvp.m[11] - matrix_mvp.m[9],
|
||||
matrix_mvp.m[15] - matrix_mvp.m[13]);
|
||||
NORMALIZE(top)
|
||||
|
||||
evas_vec4_set(&plane_far, matrix_mvp.m[3] - matrix_mvp.m[2],
|
||||
matrix_mvp.m[7] - matrix_mvp.m[6],
|
||||
matrix_mvp.m[11] - matrix_mvp.m[10],
|
||||
matrix_mvp.m[15] - matrix_mvp.m[14]);
|
||||
NORMALIZE(far)
|
||||
|
||||
evas_vec4_set(&plane_near, matrix_mvp.m[3] + matrix_mvp.m[2],
|
||||
matrix_mvp.m[7] + matrix_mvp.m[6],
|
||||
matrix_mvp.m[11] + matrix_mvp.m[10],
|
||||
matrix_mvp.m[15] + matrix_mvp.m[14]);
|
||||
NORMALIZE(near)
|
||||
|
||||
#undef NORMALIZE
|
||||
|
||||
/*check OBB points in frustum (Ax + By + Cz + D >= 0)*/
|
||||
if (CHECK_IN_FRUSTUM_MIN(right) && CHECK_IN_FRUSTUM_MIN(left)
|
||||
&& CHECK_IN_FRUSTUM_MIN(bottom) && CHECK_IN_FRUSTUM_MIN(top)
|
||||
&& CHECK_IN_FRUSTUM_MIN(far) && CHECK_IN_FRUSTUM_MIN(near))
|
||||
frustum |= 1;
|
||||
|
||||
if (CHECK_IN_FRUSTUM_MAX(right) && CHECK_IN_FRUSTUM_MAX(left)
|
||||
&& CHECK_IN_FRUSTUM_MAX(bottom) && CHECK_IN_FRUSTUM_MAX(top)
|
||||
&& CHECK_IN_FRUSTUM_MAX(far) && CHECK_IN_FRUSTUM_MAX(near))
|
||||
frustum |= 2;
|
||||
|
||||
#undef CHECK_IN_FRUSTUM_MIN
|
||||
#undef CHECK_IN_FRUSTUM_MAX
|
||||
|
||||
return frustum;
|
||||
}
|
||||
|
||||
|
||||
#include "canvas/evas_3d_node.eo.c"
|
||||
|
|
|
@ -354,6 +354,26 @@ class Evas_3D_Node (Evas_3D_Object, Evas_Common_Interface)
|
|||
@in Evas_Real *z2;
|
||||
}
|
||||
}
|
||||
|
||||
obb_frustum_check {
|
||||
/*
|
||||
|
||||
* Check is the obb of node in frustum of camera node.
|
||||
*
|
||||
* @param camera_node The given node of camera.
|
||||
* @param node The given node.
|
||||
* @return @c 0 if the obb is not in frustum, @c 1 if only min coordinate of obb is in frustum,
|
||||
* @c 2 if only max coordinate of obb is in frustum, @c 3 if both coordinates of obb is in frustum.
|
||||
|
||||
* If the camera_node is not of type EVAS_3D_NODE_TYPE_CAMERA error wrong type of node will be generated and returned @ -1.
|
||||
|
||||
* @ingroup Evas_3D_Node
|
||||
*/
|
||||
return int;
|
||||
params {
|
||||
@in Evas_3D_Node *camera_node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
properties {
|
||||
|
|
Loading…
Reference in New Issue