forked from enlightenment/efl
evas: Evas_3D - add common code for primitive shape.
Reviewers: Hermet, cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2012 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
This commit is contained in:
parent
789633b321
commit
f377770cc1
|
@ -248,7 +248,7 @@ evas_3d_cube_rotate_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
|
|||
evas_3d_cube_rotate_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
|
||||
|
||||
EXTRA_PROGRAMS += evas_3d_shadows
|
||||
evas_3d_shadows_SOURCES = evas-3d-shadows.c
|
||||
evas_3d_shadows_SOURCES = evas-3d-shadows.c evas-3d-primitives.c evas-3d-primitives.h
|
||||
evas_3d_shadows_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
|
||||
evas_3d_shadows_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
|
||||
|
||||
|
|
|
@ -1,131 +1,192 @@
|
|||
#include <Eo.h>
|
||||
#include <Evas.h>
|
||||
#include <Ecore.h>
|
||||
#include <Ecore_Evas.h>
|
||||
#include <math.h>
|
||||
#define EFL_EO_API_SUPPORT
|
||||
#define EFL_BETA_API_SUPPORT
|
||||
|
||||
#define SET_VERTEX_DATA eo_do(mesh, evas_3d_mesh_vertex_count_set(vcount),\
|
||||
evas_3d_mesh_frame_add(0);\
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,\
|
||||
sizeof(vec3), &vertices[0]);\
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,\
|
||||
sizeof(vec3), &normals[0]));\
|
||||
free(vertices);\
|
||||
free(normals);
|
||||
#include "evas-3d-primitives.h"
|
||||
|
||||
typedef struct _vec3
|
||||
// TODO Use an external library of linear algebra.
|
||||
inline void
|
||||
_vec3_subtract(vec3 *out, const vec3 *a, const vec3 *b)
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} vec3;
|
||||
out->x = a->x - b->x;
|
||||
out->y = a->y - b->y;
|
||||
out->z = a->z - b->z;
|
||||
}
|
||||
|
||||
static const float cube_vertices[] =
|
||||
inline void
|
||||
_vec3_copy(vec3 *dst, const vec3 *src)
|
||||
{
|
||||
dst->x = src->x;
|
||||
dst->y = src->y;
|
||||
dst->z = src->z;
|
||||
}
|
||||
|
||||
inline void
|
||||
_vec3_cross_product(vec3 *out, const vec3 *a, const vec3 *b)
|
||||
{
|
||||
vec3 tmp;
|
||||
|
||||
tmp.x = a->y * b->z - a->z * b->y;
|
||||
tmp.y = a->z * b->x - a->x * b->z;
|
||||
tmp.z = a->x * b->y - a->y * b->x;
|
||||
|
||||
_vec3_copy(out, &tmp);
|
||||
}
|
||||
|
||||
inline void
|
||||
_vec3_normalize(vec3 *out)
|
||||
{
|
||||
float size = out->x * out->x + out->y *out->y + out->z * out->z;
|
||||
size = sqrt(size);
|
||||
out->x /= size;
|
||||
out->y /= size;
|
||||
out->z /= size;
|
||||
}
|
||||
|
||||
const float cube_vertices[] =
|
||||
{
|
||||
/* Front */
|
||||
-1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0,
|
||||
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
|
||||
-1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
|
||||
1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0,
|
||||
-1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
|
||||
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
|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.0, 0.0,
|
||||
-1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, -1.0, 0.0, 0.0,
|
||||
};
|
||||
|
||||
static const unsigned short cube_indices[] =
|
||||
const unsigned short cube_indices[] =
|
||||
{
|
||||
/* Front */
|
||||
0, 1, 2, 2, 1, 3,
|
||||
|
||||
/* Back */
|
||||
4, 5, 6, 6, 5, 7,
|
||||
|
||||
/* Left */
|
||||
8, 9, 10, 10, 9, 11,
|
||||
|
||||
/* Right */
|
||||
12, 13, 14, 14, 13, 15,
|
||||
|
||||
/* Top */
|
||||
16, 17, 18, 18, 17, 19,
|
||||
|
||||
/* Bottom */
|
||||
20, 21, 22, 22, 21, 23
|
||||
};
|
||||
|
||||
static const float square_vertices[] =
|
||||
const float square_vertices[] =
|
||||
{
|
||||
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
|
||||
-1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
|
||||
1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
|
||||
-1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
|
||||
-1.0, 1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
|
||||
1.0, 1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
|
||||
-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
|
||||
1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0,
|
||||
};
|
||||
|
||||
static const unsigned short square_indices[] = {0, 1, 2, 2, 1, 3};
|
||||
const unsigned short square_indices[] = {0, 1, 2, 2, 1, 3};
|
||||
|
||||
static void
|
||||
_set_grid_indices(Eo *mesh, int count)
|
||||
#define ALLOCATE_VERTEX_DATA \
|
||||
vec3 *vertices = malloc(sizeof(vec3) * vcount); \
|
||||
vec3 *normals = malloc(sizeof(vec3) * vcount); \
|
||||
vec2 *tex_coord = malloc(sizeof(vec2) * vcount); \
|
||||
vec3 *tangents = malloc(sizeof(vec3) * vcount); \
|
||||
unsigned short *indices = malloc(sizeof(short) * icount);
|
||||
|
||||
#define SET_VERTEX_DATA(frame) \
|
||||
eo_do(mesh, evas_3d_mesh_vertex_count_set(vcount), \
|
||||
evas_3d_mesh_frame_add(frame), \
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_POSITION, \
|
||||
sizeof(vec3), &vertices[0]), \
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_NORMAL, \
|
||||
sizeof(vec3), &normals[0]), \
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_TEXCOORD, \
|
||||
sizeof(vec2), &tex_coord[0]), \
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_TANGENT, \
|
||||
sizeof(vec3), &tangents[0]), \
|
||||
evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, \
|
||||
icount , &indices[0])); \
|
||||
free(vertices); \
|
||||
free(normals); \
|
||||
free(tangents); \
|
||||
free(tex_coord); \
|
||||
free(indices);
|
||||
|
||||
#define SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, v_array, v_count, i_array, i_count) \
|
||||
eo_do(mesh, \
|
||||
evas_3d_mesh_vertex_count_set(v_count), \
|
||||
evas_3d_mesh_frame_add(frame), \
|
||||
evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_POSITION, \
|
||||
15 * sizeof(float), &v_array[ 0]), \
|
||||
evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_NORMAL, \
|
||||
15 * sizeof(float), &v_array[ 3]), \
|
||||
evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_COLOR, \
|
||||
15 * sizeof(float), &v_array[ 6]), \
|
||||
evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_TEXCOORD, \
|
||||
15 * sizeof(float), &v_array[10]), \
|
||||
evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_TANGENT, \
|
||||
15 * sizeof(float), &v_array[12]), \
|
||||
evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, \
|
||||
i_count, &i_array[0]), \
|
||||
evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
|
||||
|
||||
vec3 _get_func_normal(Surface *func, float x, float y)
|
||||
{
|
||||
vec3 v00, v01, v10, d1, d2, normal;
|
||||
|
||||
v00 = func(x, y);
|
||||
v01 = func(x, y + 0.01);
|
||||
v10 = func(x + 0.01, y);
|
||||
_vec3_subtract(&d1, &v00, &v01);
|
||||
_vec3_subtract(&d2, &v01, &v10);
|
||||
|
||||
_vec3_cross_product(&normal, &d1, &d2);
|
||||
|
||||
_vec3_normalize(&normal);
|
||||
|
||||
return normal;
|
||||
}
|
||||
|
||||
void
|
||||
_generate_grid_indices(unsigned short *indices, int count)
|
||||
{
|
||||
int i, j;
|
||||
unsigned short *index, *indices;
|
||||
unsigned short *index = &indices[0];
|
||||
int vccount = count + 1;
|
||||
int icount = count * count * 6;
|
||||
|
||||
indices = malloc(sizeof(short) * icount);
|
||||
index = indices;
|
||||
|
||||
for(j = 0; j < count; j++)
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
*index++ = i + vccount * j;
|
||||
*index++ = (unsigned short)(i + vccount * j);
|
||||
*index++ = i + vccount * (j + 1);
|
||||
*index++ = i + 1 + vccount * (j + 1);
|
||||
|
||||
*index++ = i + vccount * j;
|
||||
*index++ = i + 1 + vccount * j;
|
||||
*index++ = i + vccount * (j + 1) + 1;
|
||||
*index++ = i + vccount * j;
|
||||
*index++ = i + 1 + vccount * j;
|
||||
*index++ = i + vccount * (j + 1) + 1;
|
||||
}
|
||||
eo_do(mesh,
|
||||
evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
|
||||
icount , &indices[0]));
|
||||
free(indices);
|
||||
}
|
||||
|
||||
static void
|
||||
_set_tape_indices(Eo *mesh, int count)
|
||||
void
|
||||
_generate_tape_indices(unsigned short *indices, int count)
|
||||
{
|
||||
unsigned short *index, *indices;
|
||||
int i, vccount = count + 1;
|
||||
int icount = count * 6;
|
||||
indices = malloc(sizeof(short) * icount);
|
||||
index = indices;
|
||||
unsigned short *index = &indices[0];
|
||||
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
|
@ -133,31 +194,26 @@ _set_tape_indices(Eo *mesh, int count)
|
|||
*index++ = i + 1;
|
||||
*index++ = i + 1 + vccount;
|
||||
|
||||
*index++ = i;
|
||||
*index++ = i + vccount;
|
||||
*index++ = i + vccount + 1;
|
||||
*index++ = i;
|
||||
*index++ = i + vccount;
|
||||
*index++ = i + vccount + 1;
|
||||
}
|
||||
eo_do(mesh,
|
||||
evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
|
||||
icount , &indices[0]));
|
||||
free(indices);
|
||||
}
|
||||
|
||||
static void
|
||||
_set_ball(Eo *mesh, int p)
|
||||
void
|
||||
evas_3d_add_sphere_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
|
||||
{
|
||||
int vcount, vccount, i, j;
|
||||
double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi;
|
||||
|
||||
int vcount, icount, vccount, i, j;
|
||||
icount = p * p * 6;
|
||||
vccount = p + 1;
|
||||
vcount = vccount * vccount;
|
||||
|
||||
ALLOCATE_VERTEX_DATA
|
||||
|
||||
double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi;
|
||||
dtheta = M_PI / p;
|
||||
dfi = 2 * M_PI / p;
|
||||
|
||||
vec3 *vertices = malloc(sizeof(vec3) * vcount);
|
||||
vec3 *normals = malloc(sizeof(vec3) * vcount);
|
||||
|
||||
for (j = 0; j < vccount; j++)
|
||||
{
|
||||
theta = j * dtheta;
|
||||
|
@ -168,125 +224,199 @@ _set_ball(Eo *mesh, int p)
|
|||
fi = i * dfi;
|
||||
sinfi = sin(fi);
|
||||
cosfi = cos(fi);
|
||||
vertices[i + j * vccount].x = sinth * cosfi;
|
||||
vertices[i + j * vccount].y = sinth * sinfi;
|
||||
vertices[i + j * vccount].z = costh;
|
||||
vertices[i + j * vccount].x = sinth * sinfi;
|
||||
vertices[i + j * vccount].y = costh;
|
||||
vertices[i + j * vccount].z = sinth * cosfi;
|
||||
|
||||
normals[i + j * vccount].x = vertices[i + j * vccount].x;
|
||||
normals[i + j * vccount].y = vertices[i + j * vccount].y;
|
||||
normals[i + j * vccount].z = vertices[i + j * vccount].z;
|
||||
normals[i + j * vccount] = vertices[i + j * vccount];
|
||||
|
||||
tangents[i + j * vccount].x = vertices[i + j * vccount].z;
|
||||
tangents[i + j * vccount].y = vertices[i + j * vccount].y;
|
||||
tangents[i + j * vccount].z = -vertices[i + j * vccount].x;
|
||||
|
||||
tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
|
||||
}
|
||||
}
|
||||
|
||||
_set_grid_indices(mesh, p);
|
||||
_generate_grid_indices(indices, p);
|
||||
|
||||
SET_VERTEX_DATA
|
||||
SET_VERTEX_DATA(frame)
|
||||
}
|
||||
|
||||
static void
|
||||
_set_cylinder(Eo *mesh, int p)
|
||||
void
|
||||
evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 tex_scale)
|
||||
{
|
||||
int vcount, vccount, i;
|
||||
double dfi, fi, sinfi, cosfi;
|
||||
|
||||
int vcount, icount, vccount, i, j;
|
||||
icount = p * p * 6;
|
||||
vccount = p + 1;
|
||||
vcount = 2 * vccount;
|
||||
vcount = vccount * vccount;
|
||||
|
||||
dfi = 2 * M_PI / p;
|
||||
ALLOCATE_VERTEX_DATA
|
||||
|
||||
vec3 *vertices = malloc(sizeof(vec3) * vcount);
|
||||
vec3 *normals = malloc(sizeof(vec3) * vcount);
|
||||
double v, u, d = 1.0 / p;
|
||||
|
||||
for (i = 0; i < vccount; i++)
|
||||
for (j = 0; j < vccount; j++)
|
||||
{
|
||||
fi = i * dfi;
|
||||
sinfi = sin(fi);
|
||||
cosfi = cos(fi);
|
||||
vertices[i + vccount].x = vertices[i].x = cosfi;
|
||||
vertices[i].y = -0.5;
|
||||
vertices[i + vccount].z = vertices[i].z = sinfi;
|
||||
vertices[i + vccount].y = 0.5;
|
||||
u = j * d - 0.5;
|
||||
for (i = 0; i < vccount; i++)
|
||||
{
|
||||
v = i * d - 0.5;
|
||||
vertices[i + j * vccount] = func(v, u);
|
||||
normals[i + j * vccount] = _get_func_normal(func, v, u);
|
||||
|
||||
normals[i + vccount].x = normals[i].x = cosfi;
|
||||
normals[i + vccount].y = normals[i].y = 0;
|
||||
normals[i + vccount].z = normals[i].z = sinfi;
|
||||
tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
|
||||
}
|
||||
}
|
||||
|
||||
_set_tape_indices(mesh, p);
|
||||
|
||||
SET_VERTEX_DATA
|
||||
_generate_grid_indices(indices, p);
|
||||
SET_VERTEX_DATA(frame)
|
||||
}
|
||||
|
||||
static void
|
||||
_set_cone(Eo *mesh, int p)
|
||||
void
|
||||
evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int p, vec2 tex_scale)
|
||||
{
|
||||
int vcount, vccount, i;
|
||||
double dfi, fi, sinfi, cosfi;
|
||||
int vcount, icount, vccount, i, j;
|
||||
icount = p * p * 6;
|
||||
vccount = p + 1;
|
||||
vcount = vccount * vccount;
|
||||
|
||||
ALLOCATE_VERTEX_DATA
|
||||
|
||||
double d, sinth, costh, fi, theta, sinfi, cosfi;
|
||||
|
||||
d = 2 * M_PI / p;
|
||||
|
||||
for (j = 0; j < vccount; j++)
|
||||
{
|
||||
theta = j * d;
|
||||
sinth = sin(theta);
|
||||
costh = cos(theta);
|
||||
for (i = 0; i < vccount; i++)
|
||||
{
|
||||
fi = i * d;
|
||||
sinfi = sin(fi);
|
||||
cosfi = cos(fi);
|
||||
vertices[i + j * vccount].x = (1 + rratio * cosfi) * costh;
|
||||
vertices[i + j * vccount].y = (1 + rratio * cosfi) * sinth;
|
||||
vertices[i + j * vccount].z = rratio * sinfi;
|
||||
|
||||
normals[i + j * vccount].x = cosfi * costh;
|
||||
normals[i + j * vccount].y = cosfi * sinth;
|
||||
normals[i + j * vccount].z = sinfi;
|
||||
|
||||
tangents[i + j * vccount].x = - sinfi * costh;
|
||||
tangents[i + j * vccount].y = - sinfi * sinth;
|
||||
tangents[i + j * vccount].z = cosfi;
|
||||
|
||||
_vec3_normalize(&normals[i + j * vccount]);
|
||||
|
||||
tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
|
||||
}
|
||||
}
|
||||
|
||||
_generate_grid_indices(indices, p);
|
||||
|
||||
SET_VERTEX_DATA(frame)
|
||||
}
|
||||
|
||||
void
|
||||
evas_3d_add_cylinder_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
|
||||
{
|
||||
int vcount, icount, vccount, i;
|
||||
icount = p * 6;
|
||||
vccount = p + 1;
|
||||
vcount = 2 * vccount;
|
||||
|
||||
dfi = 2 * M_PI / p;
|
||||
ALLOCATE_VERTEX_DATA
|
||||
|
||||
vec3 *vertices = malloc(sizeof(vec3) * vcount);
|
||||
vec3 *normals = malloc(sizeof(vec3) * vcount);
|
||||
double dfi, fi, sinfi, cosfi;
|
||||
dfi = 2 * M_PI / p;
|
||||
|
||||
for (i = 0; i < vccount; i++)
|
||||
{
|
||||
fi = i * dfi;
|
||||
sinfi = sin(fi);
|
||||
cosfi = cos(fi);
|
||||
vertices[i].x = cosfi;
|
||||
vertices[i + vccount].x = vertices[i].x = sinfi ;
|
||||
vertices[i].y = -0.5;
|
||||
vertices[i + vccount].z = vertices[i].z = cosfi;
|
||||
vertices[i + vccount].y = 0.5;
|
||||
|
||||
normals[i + vccount].x = normals[i].x = sinfi;
|
||||
normals[i + vccount].y = normals[i].y = 0;
|
||||
normals[i + vccount].z = normals[i].z = cosfi;
|
||||
|
||||
tangents[i + vccount].x = tangents[i].x = cosfi;
|
||||
tangents[i + vccount].y = tangents[i].y = 0;
|
||||
tangents[i + vccount].z = tangents[i].z = -sinfi;
|
||||
|
||||
tex_coord[i].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i].y = 0;
|
||||
tex_coord[i+ vccount].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i+ vccount].y = tex_scale.y;
|
||||
}
|
||||
|
||||
_generate_tape_indices(indices, p);
|
||||
|
||||
SET_VERTEX_DATA(frame)
|
||||
}
|
||||
|
||||
void
|
||||
evas_3d_add_cone_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
|
||||
{
|
||||
int vcount, icount, vccount, i;
|
||||
double dfi, fi, sinfi, cosfi;
|
||||
|
||||
icount = p * 6;
|
||||
vccount = p + 1;
|
||||
vcount = 2 * vccount;
|
||||
|
||||
dfi = 2.0 * M_PI / p;
|
||||
|
||||
ALLOCATE_VERTEX_DATA
|
||||
|
||||
for (i = 0; i < vccount; i++)
|
||||
{
|
||||
fi = i * dfi;
|
||||
sinfi = sin(fi);
|
||||
cosfi = cos(fi);
|
||||
vertices[i].x = sinfi;
|
||||
vertices[i].y = 0;
|
||||
vertices[i].z = sinfi;
|
||||
vertices[i].z = cosfi;
|
||||
vertices[i + vccount].x = 0;
|
||||
vertices[i + vccount].y = 1;
|
||||
vertices[i + vccount].z = 0;
|
||||
|
||||
normals[i + vccount].x = normals[i].x = cosfi * 0.71;
|
||||
normals[i + vccount].x = normals[i].x = sinfi * 0.71;
|
||||
normals[i + vccount].y = normals[i].y = 0.71;
|
||||
normals[i + vccount].z = normals[i].z = sinfi * 0.71;
|
||||
normals[i + vccount].z = normals[i].z = cosfi * 0.71;
|
||||
|
||||
tangents[i + vccount].x = tangents[i].x = cosfi;
|
||||
tangents[i + vccount].y = tangents[i].y = 0;
|
||||
tangents[i + vccount].z = tangents[i].z = -sinfi;
|
||||
|
||||
tex_coord[i].x = i / (float)(vccount-1) * tex_scale.x;
|
||||
tex_coord[i].y = 0;
|
||||
tex_coord[i+ vccount].x = tex_coord[i].x;
|
||||
tex_coord[i+ vccount].y = tex_scale.y;
|
||||
}
|
||||
|
||||
_set_tape_indices(mesh, p);
|
||||
_generate_tape_indices(indices, p);
|
||||
|
||||
SET_VERTEX_DATA
|
||||
SET_VERTEX_DATA(frame)
|
||||
}
|
||||
|
||||
static void
|
||||
_set_square(Eo *mesh)
|
||||
void
|
||||
evas_3d_add_square_frame(Eo *mesh, int frame)
|
||||
{
|
||||
eo_do(mesh, evas_3d_mesh_vertex_count_set(4),
|
||||
evas_3d_mesh_frame_add(0);
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
|
||||
8 * sizeof(float), &square_vertices[0]);
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,
|
||||
8 * sizeof(float), &square_vertices[3]),
|
||||
evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_TEXCOORD,
|
||||
8 * sizeof(float), &square_vertices[6]);
|
||||
evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
|
||||
6 , &square_indices[0]);
|
||||
evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES);
|
||||
evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
|
||||
SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, square_vertices, 4, square_indices, 6)
|
||||
}
|
||||
|
||||
static void
|
||||
_set_cube(Eo *mesh)
|
||||
void
|
||||
evas_3d_add_cube_frame(Eo *mesh, int frame)
|
||||
{
|
||||
eo_do(mesh,
|
||||
evas_3d_mesh_vertex_count_set(24),
|
||||
evas_3d_mesh_frame_add(0),
|
||||
|
||||
evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
|
||||
12 * sizeof(float), &cube_vertices[0]),
|
||||
evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
|
||||
12 * sizeof(float), &cube_vertices[3]),
|
||||
evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
|
||||
12 * sizeof(float), &cube_vertices[6]),
|
||||
evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
|
||||
12 * sizeof(float), &cube_vertices[10]),
|
||||
|
||||
evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
|
||||
36, &cube_indices[0]),
|
||||
evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
|
||||
SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, cube_vertices, 24, cube_indices, 36)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
#ifndef EVAS_3D_PRIMITIVES_H
|
||||
#define EVAS_3D_PRIMITIVES_H
|
||||
|
||||
#include <Eo.h>
|
||||
#include <Evas.h>
|
||||
#include <math.h>
|
||||
|
||||
// TODO Use an external library of linear algebra.
|
||||
typedef struct _vec3
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} vec3;
|
||||
|
||||
typedef struct _vec2
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
} vec2;
|
||||
|
||||
/* The type of user-defined parametric surface function.*/
|
||||
typedef vec3 (Surface)(float x, float y);
|
||||
|
||||
/* Set frame as sphere. */
|
||||
void
|
||||
evas_3d_add_sphere_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
|
||||
|
||||
/* Set frame as user defined parametric surface.A parametric surface is a
|
||||
* surface in the Euclidean space R3 which is defined by a parametric equation
|
||||
* with two parameters. */
|
||||
void
|
||||
evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int precision, vec2 tex_scale);
|
||||
|
||||
/* Set frame as sphere as torus */
|
||||
void
|
||||
evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int precision, vec2 tex_scale);
|
||||
|
||||
/* Set frame as cylinder. */
|
||||
void
|
||||
evas_3d_add_cylinder_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
|
||||
|
||||
/* Set frame as cone. */
|
||||
void
|
||||
evas_3d_add_cone_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
|
||||
|
||||
/* Set frame as square. */
|
||||
void
|
||||
evas_3d_add_square_frame(Eo *mesh, int frame);
|
||||
|
||||
/* Set frame as cube. */
|
||||
void
|
||||
evas_3d_add_cube_frame(Eo *mesh, int frame);
|
||||
|
||||
#endif // EVAS_3D_PRIMITIVES_H
|
|
@ -5,7 +5,7 @@
|
|||
* @see evas_3d_scene_shadows_enable_set(Eina_Bool _shadows_enabled)
|
||||
* @see evas_3d_object_callback_register
|
||||
*
|
||||
* Compile with gcc -o evas-3d-shadows evas-3d-shadows.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo eina` -lm
|
||||
* Compile with gcc -o gcc -o evas-3d-shadows evas-3d-shadows.c evas-3d-primitives.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo eina` -lm
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
|
@ -23,8 +23,8 @@
|
|||
#include <Ecore_Evas.h>
|
||||
#include <Eina.h>
|
||||
#include <math.h>
|
||||
#include "evas-3d-primitives.c"
|
||||
#include "evas-3d-common.h"
|
||||
#include "evas-3d-primitives.h"
|
||||
#include "evas-common.h"
|
||||
|
||||
#define WIDTH 1024
|
||||
#define HEIGHT 1024
|
||||
|
@ -37,6 +37,7 @@
|
|||
|
||||
static const char *model_path = PACKAGE_EXAMPLES_DIR EVAS_3D_MODEL_FOLDER "/sonic.md2";
|
||||
static const char *image_path = PACKAGE_EXAMPLES_DIR EVAS_3D_IMAGE_FOLDER "/sonic.png";
|
||||
static const vec2 tex_scale = {1, 1};
|
||||
|
||||
Ecore_Evas *ecore_evas = NULL;
|
||||
Evas *evas = NULL;
|
||||
|
@ -171,7 +172,7 @@ static void
|
|||
_sphere_setup(Body_3D *sphere)
|
||||
{
|
||||
sphere->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
_set_ball(sphere->mesh, 50);
|
||||
evas_3d_add_sphere_frame(sphere->mesh, 0, 50, tex_scale);
|
||||
_body_material_set(sphere, 1, 0.0, 0.0);
|
||||
|
||||
sphere->node =
|
||||
|
@ -185,7 +186,7 @@ static void
|
|||
_cone_setup(Body_3D *cone)
|
||||
{
|
||||
cone->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
_set_cone(cone->mesh, 100);
|
||||
evas_3d_add_cone_frame(cone->mesh, 0, 100, tex_scale);
|
||||
_body_material_set(cone, 0.8, 0.5, 0.5);
|
||||
|
||||
cone->node =
|
||||
|
@ -200,7 +201,7 @@ static void
|
|||
_cylinder_setup(Body_3D *cylinder)
|
||||
{
|
||||
cylinder->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
_set_cylinder(cylinder->mesh, 50);
|
||||
evas_3d_add_cylinder_frame(cylinder->mesh, 0, 50, tex_scale);
|
||||
_body_material_set(cylinder, 0.0, 0.0, 1.0);
|
||||
|
||||
cylinder->node =
|
||||
|
@ -214,9 +215,9 @@ static void
|
|||
_square_setup(Body_3D *square)
|
||||
{
|
||||
square->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
_set_square(square->mesh);
|
||||
evas_3d_add_square_frame(square->mesh, 0);
|
||||
|
||||
_body_material_set(square, 0.9, 1, 1);
|
||||
_body_material_set(square, 0.4, 0.4, 0.4);
|
||||
|
||||
square->node =
|
||||
eo_add(EVAS_3D_NODE_CLASS, evas,
|
||||
|
@ -231,7 +232,7 @@ static void
|
|||
_box_setup(Body_3D *box)
|
||||
{
|
||||
box->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
|
||||
_set_cube(box->mesh);
|
||||
evas_3d_add_cube_frame(box->mesh, 0);
|
||||
|
||||
_body_material_set(box, 0, 1, 0);
|
||||
|
||||
|
|
Loading…
Reference in New Issue