aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/extantz/camera.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-05-10 05:46:30 +1000
committerDavid Walter Seikel2014-05-10 05:46:30 +1000
commit3e1744fb8d8f904b573486ecaf3c547338cfbe54 (patch)
tree810d526b702daf6704b6652ac2c71c1f6eb0bb5f /src/extantz/camera.c
parentCopied evas_3d_utils.h coz they made it private, dammit. (diff)
downloadSledjHamr-3e1744fb8d8f904b573486ecaf3c547338cfbe54.zip
SledjHamr-3e1744fb8d8f904b573486ecaf3c547338cfbe54.tar.gz
SledjHamr-3e1744fb8d8f904b573486ecaf3c547338cfbe54.tar.bz2
SledjHamr-3e1744fb8d8f904b573486ecaf3c547338cfbe54.tar.xz
Fixed up evas_3D camera. Should adjust the Irrlicht one to suit.
Diffstat (limited to 'src/extantz/camera.c')
-rw-r--r--src/extantz/camera.c126
1 files changed, 83 insertions, 43 deletions
diff --git a/src/extantz/camera.c b/src/extantz/camera.c
index b956a6e..7bc83d2 100644
--- a/src/extantz/camera.c
+++ b/src/extantz/camera.c
@@ -1,32 +1,71 @@
1#include "extantz.h" 1#include "extantz.h"
2 2
3#include "evas_3d_utils.h"
4
5static inline void evas_euler_to_quaternion(Evas_Vec4 *out, Evas_Vec3 *in)
6{
7 // Assuming the angles are in radians.
8 Evas_Real c1 = cos(in->y / 2.0); // heading (yaw/spin) .. y
9 Evas_Real s1 = sin(in->y / 2.0);
10 Evas_Real c2 = cos(in->z / 2.0); // attitude (pitch) .. x
11 Evas_Real s2 = sin(in->z / 2.0);
12 Evas_Real c3 = cos(in->x / 2.0); // bank (roll) .. z
13 Evas_Real s3 = sin(in->x / 2.0);
14 Evas_Real c1c2 = c1 * c2;
15 Evas_Real s1s2 = s1 * s2;
16
17 out->w =c1c2 * c3 - s1s2 * s3;
18 out->x =c1c2 * s3 + s1s2 * c3;
19 out->y =s1 * c2 * c3 + c1 * s2 * s3;
20 out->z =c1 * s2 * c3 - s1 * c2 * s3;
21}
22
23static inline void evas_quaternion_normalise(Evas_Vec4 *in)
24{
25 Evas_Real n = sqrt(in->x * in->x + in->y * in->y + in->z * in->z + in->w * in->w);
26 in->x /= n;
27 in->y /= n;
28 in->z /= n;
29 in->w /= n;
30}
31
32static inline void evas_quaternion_multiply(Evas_Vec4 *out, const Evas_Vec4 *q1, const Evas_Vec4 *q2)
33{
34 out->x = q1->x * q2->w + q1->y * q2->z - q1->z * q2->y + q1->w * q2->x;
35 out->y = -q1->x * q2->z + q1->y * q2->w + q1->z * q2->x + q1->w * q2->y;
36 out->z = q1->x * q2->y - q1->y * q2->x + q1->z * q2->w + q1->w * q2->z;
37 out->w = -q1->x * q2->x - q1->y * q2->y - q1->z * q2->z + q1->w * q2->w;
38}
39
40static inline void evas_quaternion_conjugate(Evas_Vec4 *in)
41{
42 in->x = -in->x;
43 in->y = -in->y;
44 in->z = -in->z;
45 in->w = in->w;
46}
3 47
4// TODO - Rotate the direction vector by the rotation to convert from input to camera coords.
5Eina_Bool animateCamera(Scene_Data *scene) 48Eina_Bool animateCamera(Scene_Data *scene)
6{ 49{
7 Evas_Real x, y, z, w; 50 Evas_Vec3 pos, rotate;
8 EPhysics_Quaternion *quat = ephysics_quaternion_new(); 51 Evas_Vec4 orient, rotateQ, result, move;
9 EPhysics_Quaternion *quat1 = ephysics_quaternion_new(); 52
10 EPhysics_Quaternion *result = ephysics_quaternion_new(); 53 evas_vec3_set(&rotate, DEGREE_TO_RADIAN(scene->move->r), DEGREE_TO_RADIAN(scene->move->s), DEGREE_TO_RADIAN(scene->move->p));
11 54 eo_do(scene->camera_node, evas_3d_node_orientation_get(EVAS_3D_SPACE_PARENT, &orient.x, &orient.y, &orient.z, &orient.w));
12 // Camera movement. 55 evas_euler_to_quaternion(&rotateQ, &rotate);
13 ephysics_quaternion_euler_set(quat1, scene->move->r, scene->move->s, scene->move->t); 56 evas_quaternion_multiply(&result, &orient, &rotateQ);
14 eo_do(scene->camera_node, evas_3d_node_orientation_get(EVAS_3D_SPACE_PARENT, &x, &y, &z, &w)); 57 evas_quaternion_normalise(&result);
15 ephysics_quaternion_set(quat, x, y, z, w); 58 eo_do(scene->camera_node, evas_3d_node_orientation_set(result.x, result.y, result.z, result.w));
16 ephysics_quaternion_multiply(quat, quat1, result); 59
17 ephysics_quaternion_normalize(result); 60 evas_vec4_set(&move, scene->move->x, scene->move->y, scene->move->z, 0);
18 ephysics_quaternion_get(result, &x, &y, &z, &w); 61 eo_do(scene->camera_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &pos.x, &pos.y, &pos.z));
19 eo_do(scene->camera_node, evas_3d_node_orientation_set(x, y, z, w)); 62 evas_quaternion_multiply(&rotateQ, &result, &move);
20 63 evas_quaternion_conjugate(&result);
21 eo_do(scene->camera_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z)); 64 evas_quaternion_multiply(&move, &rotateQ, &result);
22 x -= scene->move->x; 65 pos.x += move.x;
23 y -= scene->move->y; 66 pos.y += move.y;
24 z -= scene->move->z; 67 pos.z += move.z;
25 eo_do(scene->camera_node, evas_3d_node_position_set(x, y, z)); 68 eo_do(scene->camera_node, evas_3d_node_position_set(pos.x, pos.y, pos.z));
26
27 free(result);
28 free(quat1);
29 free(quat);
30 69
31 return EINA_TRUE; 70 return EINA_TRUE;
32} 71}
@@ -45,14 +84,14 @@ static void _on_camera_input_down(void *data, Evas *evas, Evas_Object *obj, void
45 if (0 == strcmp(ev->key, "Escape")) 84 if (0 == strcmp(ev->key, "Escape"))
46 { 85 {
47 } 86 }
48 else if (0 == strcmp(ev->key, "Left")) move->r = 2.0; 87 else if (0 == strcmp(ev->key, "Left")) move->s = 2.0;
49 else if (0 == strcmp(ev->key, "Right")) move->r = -2.0; 88 else if (0 == strcmp(ev->key, "Right")) move->s = -2.0;
50 else if (0 == strcmp(ev->key, "Up")) move->x = 2.0; 89 else if (0 == strcmp(ev->key, "Up")) move->z = -2.0;
51 else if (0 == strcmp(ev->key, "Down")) move->x = -2.0; 90 else if (0 == strcmp(ev->key, "Down")) move->z = 2.0;
52 else if (0 == strcmp(ev->key, "Prior")) move->z = -2.0; 91 else if (0 == strcmp(ev->key, "Prior")) move->y = 2.0; // Pg Up for humans.
53 else if (0 == strcmp(ev->key, "Next")) move->z = 2.0; 92 else if (0 == strcmp(ev->key, "Next")) move->y = -2.0; // Pg Dn for humans.
54 else if (0 == strcmp(ev->key, "Home")) move->y = 2.0; 93 else if (0 == strcmp(ev->key, "Home")) move->x = -2.0;
55 else if (0 == strcmp(ev->key, "End")) move->y = -2.0; 94 else if (0 == strcmp(ev->key, "End")) move->x = 2.0;
56 else if (0 == strcmp(ev->key, "space")) move->jump = 1.0; 95 else if (0 == strcmp(ev->key, "space")) move->jump = 1.0;
57 else printf("Unexpected down keystroke - %s\n", ev->key); 96 else printf("Unexpected down keystroke - %s\n", ev->key);
58 } 97 }
@@ -137,14 +176,14 @@ static void _on_camera_input_up(void *data, Evas *evas, Evas_Object *obj, void *
137 if (0 == strcmp(ev->key, "Escape")) 176 if (0 == strcmp(ev->key, "Escape"))
138 { 177 {
139 } 178 }
140 else if (0 == strcmp(ev->key, "Left")) move->r = 0.0; 179 else if (0 == strcmp(ev->key, "Left")) move->s = 0.0;
141 else if (0 == strcmp(ev->key, "Right")) move->r = 0.0; 180 else if (0 == strcmp(ev->key, "Right")) move->s = 0.0;
142 else if (0 == strcmp(ev->key, "Up")) move->x = 0.0; 181 else if (0 == strcmp(ev->key, "Up")) move->z = 0.0;
143 else if (0 == strcmp(ev->key, "Down")) move->x = 0.0; 182 else if (0 == strcmp(ev->key, "Down")) move->z = 0.0;
144 else if (0 == strcmp(ev->key, "Prior")) move->z = 0.0; 183 else if (0 == strcmp(ev->key, "Prior")) move->y = 0.0;
145 else if (0 == strcmp(ev->key, "Next")) move->z = 0.0; 184 else if (0 == strcmp(ev->key, "Next")) move->y = 0.0;
146 else if (0 == strcmp(ev->key, "Home")) move->y = 0.0; 185 else if (0 == strcmp(ev->key, "Home")) move->x = 0.0;
147 else if (0 == strcmp(ev->key, "End")) move->y = 0.0; 186 else if (0 == strcmp(ev->key, "End")) move->x = 0.0;
148 else if (0 == strcmp(ev->key, "space")) move->jump = 0.0; 187 else if (0 == strcmp(ev->key, "space")) move->jump = 0.0;
149 else printf("Unexpected up keystroke - %s\n", ev->key); 188 else printf("Unexpected up keystroke - %s\n", ev->key);
150 } 189 }
@@ -187,13 +226,14 @@ Evas_3D_Node *cameraAdd(Evas *evas, Scene_Data *scene, Evas_Object *image)
187 Evas_3D_Camera *camera; 226 Evas_3D_Camera *camera;
188 227
189 camera = eo_add(EVAS_3D_CAMERA_CLASS, evas, 228 camera = eo_add(EVAS_3D_CAMERA_CLASS, evas,
190 evas_3d_camera_projection_perspective_set(60.0, 1.0, 1.0, 500.0) 229 evas_3d_camera_projection_perspective_set(90.0, 1.0, 1.0, 1024.0)
191 ); 230 );
192 231
193 result = eo_add_custom(EVAS_3D_NODE_CLASS, evas, evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA), 232 result = eo_add_custom(EVAS_3D_NODE_CLASS, evas, evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA),
194 evas_3d_node_camera_set(camera), 233 evas_3d_node_camera_set(camera),
195 evas_3d_node_position_set(50.0, 0.0, 20.0), 234 evas_3d_node_position_set(0.0, 20.0, 30.0),
196 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 20.0, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0) 235// evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 20.0, EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0)
236 evas_3d_node_orientation_set(0.0, 0.0, 0.0, 1.0)
197 ); 237 );
198 238
199 eo_do(scene->root_node, evas_3d_node_member_add(result)); 239 eo_do(scene->root_node, evas_3d_node_member_add(result));