evas: pick example uses sphere primitives.
authorBogdan Devichev <b.devichev@samsung.com>
Wed, 18 Mar 2015 13:47:50 +0000 (14:47 +0100)
committerCedric BAIL <cedric@osg.samsung.com>
Wed, 18 Mar 2015 14:22:21 +0000 (15:22 +0100)
Reviewers: cedric, Hermet, raster

Subscribers: cedric

Differential Revision: https://phab.enlightenment.org/D2169

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/examples/evas/evas-3d-pick.c

index 360e72c..bcd9c84 100644 (file)
@@ -6,7 +6,7 @@
  * and parameters which can be got from this function.
  *
  * @verbatim
- * gcc -o evas-3d-pick evas-3d-pick.c `pkg-config --libs --cflags evas ecore ecore-evas eo` -lm
+ * gcc -o evas-3d-pick evas-3d-pick.c evas-3d-primitives.c `pkg-config --libs --cflags evas ecore ecore-evas eo` -lm
  * @endverbatim
  */
 
 #include <Ecore.h>
 #include <Ecore_Evas.h>
 #include "evas-common.h"
+#include "evas-3d-primitives.h"
 
 #define  WIDTH          400
 #define  HEIGHT         400
 
-static const char *earth_image = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/wood.jpg";
-
-typedef struct _vec4
-{
-    float   x;
-    float   y;
-    float   z;
-    float   w;
-} vec4;
-
-typedef struct _vec3
-{
-    float   x;
-    float   y;
-    float   z;
-} vec3;
-
-typedef struct _vec2
-{
-    float   x;
-    float   y;
-} vec2;
-
-typedef struct _vertex
-{
-    vec3    position;
-    vec3    normal;
-    vec3    tangent;
-    vec4    color;
-    vec3    texcoord;
-} vertex;
+static const char *image_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/wood.jpg";
 
 static Ecore_Evas *ecore_evas = NULL;
 static Evas *evas = NULL;
@@ -72,11 +43,7 @@ static Eo *mesh_node = NULL;
 static Eo *mesh = NULL;
 static Eo *material = NULL;
 static Eo *texture_diffuse = NULL;
-
-static int vertex_count = 0;
-static vertex *vertices = NULL;
-static int index_count = 0;
-static unsigned short *indices = NULL;
+static const vec2 tex_scale = {1, 1};
 
 static Eina_Bool
 _animate_scene(void *data)
@@ -111,150 +78,6 @@ _on_canvas_resize(Ecore_Evas *ee)
    evas_object_move(image, 0, 0);
 }
 
-static inline vec3
-_normalize(const vec3 *v)
-{
-    double  l = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
-    vec3    vec;
-
-    vec.x = v->x / l;
-    vec.y = v->y / l;
-    vec.z = v->z / l;
-
-    return vec;
-}
-
-static void
-_sphere_fini()
-{
-   free(vertices);
-   free(indices);
-}
-
-static void
-_sphere_init(int precision)
-{
-   int i, j;
-   unsigned short *index;
-
-   vertex_count = (precision + 1) * (precision + 1);
-   index_count = precision * precision * 6;
-
-   /* Allocate buffer. */
-   vertices = malloc(sizeof(vertex) * vertex_count);
-   indices = malloc(sizeof(unsigned short) * index_count);
-
-   for (i = 0; i <= precision; i++)
-     {
-        double lati = (M_PI * (double)i) / (double)precision;
-        double y = cos(lati);
-        double r = fabs(sin(lati));
-
-        for (j = 0; j <= precision; j++)
-          {
-             double longi = (M_PI * 2.0 * j) / precision;
-             vertex *v = &vertices[i * (precision  + 1) + j];
-
-             if (j == 0 || j == precision) v->position.x = 0.0;
-             else v->position.x = r * sin(longi);
-
-             v->position.y = y;
-
-             if (j == 0 || j == precision) v->position.z = r;
-             else v->position.z = r * cos(longi);
-
-             v->normal = v->position;
-
-             if (v->position.x > 0.0)
-               {
-                  v->tangent.x = -v->normal.y;
-                  v->tangent.y =  v->normal.x;
-                  v->tangent.z =  v->normal.z;
-               }
-             else
-               {
-                  v->tangent.x =  v->normal.y;
-                  v->tangent.y = -v->normal.x;
-                  v->tangent.z =  v->normal.z;
-               }
-
-             v->color.x = v->position.x;
-             v->color.y = v->position.y;
-             v->color.z = v->position.z;
-             v->color.w = 1.0;
-
-             if (j == precision) v->texcoord.x = 1.0;
-             else if (j == 0) v->texcoord.x = 0.0;
-             else v->texcoord.x = (double)j / (double)precision;
-
-             if (i == precision) v->texcoord.y = 1.0;
-             else if (i == 0) v->texcoord.y = 0.0;
-             else v->texcoord.y = 1.0 - (double)i / (double)precision;
-          }
-     }
-
-   index = &indices[0];
-
-   for (i = 0; i < precision; i++)
-     {
-        for (j = 0; j < precision; j++)
-          {
-             *index++ = i * (precision + 1) + j;
-             *index++ = i * (precision + 1) + j + 1;
-             *index++ = (i + 1) * (precision + 1) + j;
-
-             *index++ = (i + 1) * (precision + 1) + j;
-             *index++ = i * (precision + 1) + j + 1;
-             *index++ = (i + 1) * (precision + 1) + j + 1;
-          }
-     }
-
-   for (i = 0; i < index_count; i += 3)
-     {
-        vertex *v0 = &vertices[indices[i + 0]];
-        vertex *v1 = &vertices[indices[i + 1]];
-        vertex *v2 = &vertices[indices[i + 2]];
-
-        vec3 e1, e2;
-        float du1, du2, dv1, dv2, f;
-        vec3 tangent;
-
-        e1.x = v1->position.x - v0->position.x;
-        e1.y = v1->position.y - v0->position.y;
-        e1.z = v1->position.z - v0->position.z;
-
-        e2.x = v2->position.x - v0->position.x;
-        e2.y = v2->position.y - v0->position.y;
-        e2.z = v2->position.z - v0->position.z;
-
-        du1 = v1->texcoord.x - v0->texcoord.x;
-        dv1 = v1->texcoord.y - v0->texcoord.y;
-
-        du2 = v2->texcoord.x - v0->texcoord.x;
-        dv2 = v2->texcoord.y - v0->texcoord.y;
-
-        f = 1.0 / (du1 * dv2 - du2 * dv1);
-
-        tangent.x = f * (dv2 * e1.x - dv1 * e2.x);
-        tangent.y = f * (dv2 * e1.y - dv1 * e2.y);
-        tangent.z = f * (dv2 * e1.z - dv1 * e2.z);
-
-        v0->tangent = tangent;
-     }
-
-   for (i = 0; i <= precision; i++)
-     {
-        for (j = 0; j <= precision; j++)
-          {
-             if (j == precision)
-               {
-                  vertex *v = &vertices[i * (precision  + 1) + j];
-                  v->tangent = vertices[i * (precision + 1)].tangent;
-               }
-          }
-     }
-}
-
 static void
 _on_mouse_down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj,
                void *event_info)
@@ -327,43 +150,21 @@ main(void)
                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
    eo_do(camera_node,
          evas_3d_node_camera_set(camera),
-         evas_3d_node_position_set(0.0, 0.0, 5.0),
+         evas_3d_node_position_set(0.0, 0.0, 2.5),
          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(root_node, evas_3d_node_member_add(camera_node));
 
    /* Add the cube mesh. */
-   _sphere_init(100);
 
    mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
-   eo_do(mesh,
-         evas_3d_mesh_vertex_count_set(vertex_count),
-         evas_3d_mesh_frame_add(0),
-         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
-                                            sizeof(vertex),
-                                            &vertices[0].position),
-         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
-                                            sizeof(vertex),
-                                            &vertices[0].normal),
-         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TANGENT,
-                                            sizeof(vertex),
-                                            &vertices[0].tangent),
-         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
-                                            sizeof(vertex), &vertices[0].color),
-         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
-                                            sizeof(vertex),
-                                            &vertices[0].texcoord),
-
-         evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
-                                     index_count, &indices[0]),
-         evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
+   evas_3d_add_sphere_frame(mesh, 0, 100, tex_scale);
 
    material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
-   eo_do(mesh, evas_3d_mesh_frame_material_set(0, material));
 
    texture_diffuse = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
    eo_do(texture_diffuse,
-         evas_3d_texture_file_set(earth_image, NULL),
+         evas_3d_texture_file_set(image_path, NULL),
          evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_LINEAR,
                                     EVAS_3D_TEXTURE_FILTER_LINEAR));
    eo_do(material,
@@ -386,7 +187,8 @@ main(void)
 
    eo_do(root_node, evas_3d_node_member_add(mesh_node));
    eo_do(mesh_node, evas_3d_node_mesh_add(mesh));
-   eo_do(mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE));
+   eo_do(mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
+               evas_3d_mesh_frame_material_set(0, material));
 
    /* Set up scene. */
    eo_do(scene,
@@ -415,7 +217,6 @@ main(void)
 
    ecore_evas_free(ecore_evas);
    ecore_evas_shutdown();
-   _sphere_fini();
 
    return 0;
 }