aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/evas/common3d/primitives/primitive_common.c
blob: 882e95216e8418bccb65fc156f5e21ccbdd50349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "primitive_common.h"

void _set_vertex_data_from_array(Evas_Canvas3D_Mesh *mesh,
                                 int frame,
                                 const float *data,
                                 Evas_Canvas3D_Vertex_Attrib attr,
                                 int start,
                                 int attr_count,
                                 int line,
                                 int vcount)
{
   float *address, *out;
   int stride, i, j;
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_copy_set(frame, attr, 0, NULL),
         address = (float *)evas_canvas3d_mesh_frame_vertex_data_map(frame, attr),
         stride = evas_canvas3d_mesh_frame_vertex_stride_get(frame, attr));
   if (stride == 0) stride = sizeof(float) * attr_count;
   for (i = 0; i < vcount; i++)
     {
        out = (float *)((char *)address + stride * i);
        for (j = 0; j < attr_count; j++)
           out[j] = data[start + (line * i) + j];
     }
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_unmap(frame, attr));
}

void
_set_vec3_vertex_data(Evas_Canvas3D_Mesh *mesh,
                      int frame,
                      int vcount,
                      Evas_Vec3 *data,
                      Evas_Canvas3D_Vertex_Attrib attr)
{
   float *address, *out;
   int stride, i;
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_copy_set(frame, attr, 0, NULL),
         address = (float *)evas_canvas3d_mesh_frame_vertex_data_map(frame, attr),
         stride = evas_canvas3d_mesh_frame_vertex_stride_get(frame, attr));
   if (stride == 0) stride = sizeof(float) * 3;
   for (i = 0; i < vcount; i++)
     {
        out = (float *)((char *)address + stride * i);
        out[0] = data[i].x;
        out[1] = data[i].y;
        out[2] = data[i].z;
     }
   free(data);
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_unmap(frame, attr));
}

void
_set_vec2_vertex_data(Evas_Canvas3D_Mesh *mesh,
                      int frame,
                      int vcount,
                      Evas_Vec2 *data,
                      Evas_Canvas3D_Vertex_Attrib attr)
{
   float *address, *out;
   int stride, i;
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_copy_set(frame, attr, 0, NULL),
         address = (float *)evas_canvas3d_mesh_frame_vertex_data_map(frame, attr),
         stride = evas_canvas3d_mesh_frame_vertex_stride_get(frame, attr));
   if (stride == 0) stride = sizeof(float) * 2;
   for (i = 0; i < vcount; i++)
     {
        out = (float *)((char *)address + stride * i);
        out[0] = data[i].x;
        out[1] = data[i].y;
     }
   free(data);
   eo_do(mesh,
         evas_canvas3d_mesh_frame_vertex_data_unmap(frame, attr));
}

void
_generate_indices(unsigned short *indices, int a, int b)
{
   int i, j, a1 = a + 1;
   unsigned short *index = &indices[0];

   for (i = 0; i < b; i++)
     {
        for (j = 0; j < a; j++)
          {
             *index++ = j + a1 * i;
             *index++ = j + a1 * (i + 1);
             *index++ = j + 1 + a1 * (i + 1);

             *index++ = j + a1 * i;
             *index++ = j + 1 +  a1 * i;
             *index++ = j + a1 * (i + 1) + 1;
          }
     }
}

void
_primitives_vec3_copy(Evas_Vec3 *dst, const Evas_Vec3 *src)
{
   dst->x = src->x;
   dst->y = src->y;
   dst->z = src->z;
}

void
_primitives_vec3_subtract(Evas_Vec3 *out, const Evas_Vec3 *a, const Evas_Vec3 *b)
{
   out->x = a->x - b->x;
   out->y = a->y - b->y;
   out->z = a->z - b->z;
}

void
_primitives_vec3_cross_product(Evas_Vec3 *out, const Evas_Vec3 *a, const Evas_Vec3 *b)
{
   Evas_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;

   _primitives_vec3_copy(out, &tmp);
}

void
_primitives_vec3_normalize(Evas_Vec3 *out)
{
   Evas_Real 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;
}

void
evas_common_set_model_from_primitive(Evas_Canvas3D_Mesh *model,
                                     int frame,
                                     Evas_Canvas3D_Primitive_Data *primitive)
{
   Evas_Real ratio = primitive->ratio;
   int precision = primitive->precision;
   Evas_Canvas3D_Surface_Func *surface = primitive->surface;
   Evas_Vec2 tex_scale = primitive->tex_scale;
   Evas_Canvas3D_Primitive_Mode mode = primitive->mode;

   switch (primitive->form)
     {
      case EVAS_CANVAS3D_MESH_PRIMITIVE_NONE:
      case EVAS_CANVAS3D_MESH_PRIMITIVE_COUNT:
        {
           ERR("Primitive with this type cannot be set to mesh.");
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_SQUARE:
        {
           evas_model_set_from_square_primitive(model, frame);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_CUBE:
        {
           evas_model_set_from_cube_primitive(model, frame);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_CYLINDER:
        {
           evas_model_set_from_cylinder_primitive(model, frame, mode,
                                                  precision, tex_scale);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_CONE:
        {
           evas_model_set_from_cone_primitive(model, frame, mode,
                                              precision, tex_scale);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_SPHERE:
        {
           evas_model_set_from_sphere_primitive(model, frame, mode,
                                                precision, tex_scale);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_TORUS:
        {
           evas_model_set_from_torus_primitive(model, frame,
                                               ratio, precision, tex_scale);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_SURFACE:
        {
           evas_model_set_from_surface_primitive(model, frame,
                                                 surface, precision, tex_scale);
           break;
        }
      case EVAS_CANVAS3D_MESH_PRIMITIVE_TERRAIN:
        {
           evas_model_set_from_terrain_primitive(model, frame,
                                                 precision, tex_scale);
           break;
        }
      default:
        {
           ERR("Unknown type of primitive");
        }
     }
}