3dphotoshoot

changeset 24:2712c5da2e00

getting sensor input (hack)
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 16 Jun 2015 06:17:59 +0300
parents a94af102872f
children ac80210d5fbe
files android/Makefile src/android/amain.c src/android/sensordef.c src/android/sensordef.h src/game.cc src/game.h src/sdr.c
diffstat 7 files changed, 230 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/android/Makefile	Sat Jun 13 05:35:43 2015 +0300
     1.2 +++ b/android/Makefile	Tue Jun 16 06:17:59 2015 +0300
     1.3 @@ -66,7 +66,7 @@
     1.4  	rm -f $(obj) $(lib) $(apk-release) $(apk-debug) $(ant_files)
     1.5  	rm -rf $(ant_dirs)
     1.6  	rm -f assets/data assets/sdr
     1.7 -	rmdir assets
     1.8 +	[ -d assets ] && rmdir assets || true
     1.9  
    1.10  .PHONY: install
    1.11  install: install-debug
     2.1 --- a/src/android/amain.c	Sat Jun 13 05:35:43 2015 +0300
     2.2 +++ b/src/android/amain.c	Tue Jun 16 06:17:59 2015 +0300
     2.3 @@ -5,11 +5,15 @@
     2.4  #include <jni.h>
     2.5  #include "amain.h"
     2.6  #include "native_glue.h"
     2.7 +#include <android/sensor.h>
     2.8 +#include "sensordef.h"
     2.9  #include "logger.h"
    2.10  #include "game.h"
    2.11  #include "camera.h"
    2.12  #include "timer.h"
    2.13  
    2.14 +#define EVID_SENS_ACCEL		(LOOPER_ID_USER + 0)
    2.15 +#define EVID_SENS_ROT		(LOOPER_ID_USER + 1)
    2.16  
    2.17  static void handle_command(struct android_app *app, int32_t cmd);
    2.18  static int handle_input(struct android_app *app, AInputEvent *ev);
    2.19 @@ -21,6 +25,8 @@
    2.20  static EGLSurface surf;
    2.21  static EGLContext ctx;
    2.22  
    2.23 +static int redisp_pending = 1;	/* TODO stop busy-looping */
    2.24 +
    2.25  static int width, height;
    2.26  static int init_done;
    2.27  
    2.28 @@ -28,7 +34,11 @@
    2.29  static JNIEnv *jni;
    2.30  static jclass activity_class;
    2.31  
    2.32 -
    2.33 +static ASensorManager *sensorman;
    2.34 +static ASensor const *sens_accel;
    2.35 +static ASensor const *sens_rot;
    2.36 +static ASensorEventQueue *sens_evq_accel;
    2.37 +static ASensorEventQueue *sens_evq_rot;
    2.38  
    2.39  void android_main(struct android_app *app_ptr)
    2.40  {
    2.41 @@ -47,13 +57,96 @@
    2.42  	}
    2.43  	activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
    2.44  
    2.45 +	{
    2.46 +		int i, num_sensors;
    2.47 +		ASensorList sensors;
    2.48 +
    2.49 +		sensorman = ASensorManager_getInstance();
    2.50 +		num_sensors = ASensorManager_getSensorList(sensorman, &sensors);
    2.51 +		printf("found %d sensors:\n", num_sensors);
    2.52 +
    2.53 +		for(i=0; i<num_sensors; i++) {
    2.54 +			ASensor const *sens = sensors[i];
    2.55 +			int type = ASensor_getType(sens);
    2.56 +			float res = ASensor_getResolution(sens);
    2.57 +			int mindel = ASensor_getMinDelay(sens);
    2.58 +
    2.59 +			printf(" %d) [%d:%s] %s - %s (res: %g, min-delay: %d)\n", i,
    2.60 +					type, sensor_typestr(type), ASensor_getName(sens),
    2.61 +					ASensor_getVendor(sens), res, mindel);
    2.62 +		}
    2.63 +
    2.64 +		// try to get a linear acceleration sensor
    2.65 +		printf("Requesting \"linear acceleration\" sensor ...\n");
    2.66 +		if(!(sens_accel = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_LINEAR_ACCELERATION))) {
    2.67 +			fprintf(stderr, "No linear acceleration sensor found, aborting!\n");
    2.68 +			return;
    2.69 +		}
    2.70 +
    2.71 +		// try to get a game rotation vector sensor
    2.72 +		printf("Requesting \"game rotation vector\" sensor ...\n");
    2.73 +		if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_GAME_ROTATION_VECTOR))) {
    2.74 +			printf("Requesting \"rotation vector\" sensor ...\n");
    2.75 +			if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_ROTATION_VECTOR))) {
    2.76 +				fprintf(stderr, "No rotation sensor found, aborting!\n");
    2.77 +				return;
    2.78 +			}
    2.79 +		}
    2.80 +
    2.81 +		if(!(sens_evq_accel = ASensorManager_createEventQueue(sensorman, app->looper,
    2.82 +						EVID_SENS_ACCEL, 0, 0))) {
    2.83 +			fprintf(stderr, "failed to create acceleration sensor event queue\n");
    2.84 +			return;
    2.85 +		}
    2.86 +
    2.87 +		if(!(sens_evq_rot = ASensorManager_createEventQueue(sensorman, app->looper,
    2.88 +						EVID_SENS_ROT, 0, 0))) {
    2.89 +			fprintf(stderr, "failed to create rotation sensor event queue\n");
    2.90 +			return;
    2.91 +		}
    2.92 +	}
    2.93 +
    2.94 +
    2.95  	for(;;) {
    2.96 -		int num_events;
    2.97 +		int num_events, evid;
    2.98  		struct android_poll_source *pollsrc;
    2.99 +		ASensorEvent sens_ev;
   2.100  
   2.101 -		while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
   2.102 -			if(pollsrc) {
   2.103 -				pollsrc->process(app, pollsrc);
   2.104 +		while((evid = ALooper_pollAll(redisp_pending ? 0 : -1, 0, &num_events, (void**)&pollsrc)) >= 0) {
   2.105 +
   2.106 +			switch(evid) {
   2.107 +			case EVID_SENS_ACCEL:
   2.108 +				if(sens_accel) {
   2.109 +					float accel[3];
   2.110 +					while(ASensorEventQueue_getEvents(sens_evq_accel, &sens_ev, 1) > 0) {
   2.111 +						accel[0] = sens_ev.acceleration.x;
   2.112 +						accel[1] = sens_ev.acceleration.y;
   2.113 +						accel[2] = sens_ev.acceleration.z;
   2.114 +					}
   2.115 +
   2.116 +					// TODO: integrate over time
   2.117 +					game_6dof_translation(accel[0], accel[1], accel[2]);
   2.118 +				}
   2.119 +				break;
   2.120 +
   2.121 +			case EVID_SENS_ROT:
   2.122 +				if(sens_rot) {
   2.123 +					float qrot[4];
   2.124 +					while(ASensorEventQueue_getEvents(sens_evq_rot, &sens_ev, 1) > 0) {
   2.125 +						int i;
   2.126 +						for(i=0; i<4; i++) {
   2.127 +							qrot[i] = sens_ev.data[i];
   2.128 +						}
   2.129 +					}
   2.130 +
   2.131 +					game_6dof_rotation(qrot[0], qrot[1], qrot[2], qrot[3]);
   2.132 +				}
   2.133 +				break;
   2.134 +
   2.135 +			default:
   2.136 +				if(pollsrc) {
   2.137 +					pollsrc->process(app, pollsrc);
   2.138 +				}
   2.139  			}
   2.140  		}
   2.141  
   2.142 @@ -62,7 +155,7 @@
   2.143  			return;
   2.144  		}
   2.145  
   2.146 -		if(init_done) {
   2.147 +		if(init_done && redisp_pending) {
   2.148  			game_display(get_time_msec());
   2.149  			eglSwapBuffers(dpy, surf);
   2.150  		}
   2.151 @@ -115,11 +208,27 @@
   2.152  
   2.153  	case APP_CMD_GAINED_FOCUS:
   2.154  		/* app focused */
   2.155 +		printf("Command: APP_CMD_GAINED_FOCUS\n");
   2.156 +		if(sens_accel) {
   2.157 +			ASensorEventQueue_enableSensor(sens_evq_accel, sens_accel);
   2.158 +			ASensorEventQueue_setEventRate(sens_evq_accel, sens_accel, 1000000 / 60);
   2.159 +		}
   2.160 +		if(sens_rot) {
   2.161 +			ASensorEventQueue_enableSensor(sens_evq_rot, sens_rot);
   2.162 +			ASensorEventQueue_setEventRate(sens_evq_rot, sens_rot, 1000000 / 60);
   2.163 +		}
   2.164  		//cam_start_video();
   2.165  		break;
   2.166  
   2.167  	case APP_CMD_LOST_FOCUS:
   2.168  		/* app lost focus */
   2.169 +		printf("Command: APP_CMD_LOST_FOCUS\n");
   2.170 +		if(sens_accel) {
   2.171 +			ASensorEventQueue_disableSensor(sens_evq_accel, sens_accel);
   2.172 +		}
   2.173 +		if(sens_rot) {
   2.174 +			ASensorEventQueue_disableSensor(sens_evq_rot, sens_rot);
   2.175 +		}
   2.176  		//cam_stop_video();
   2.177  		break;
   2.178  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/android/sensordef.c	Tue Jun 16 06:17:59 2015 +0300
     3.3 @@ -0,0 +1,41 @@
     3.4 +#include "sensordef.h"
     3.5 +
     3.6 +static const char *names[] = {
     3.7 +	"unknown",
     3.8 +	"accelerometer",
     3.9 +	"magnetic field",
    3.10 +	"orientation",
    3.11 +	"gyroscope",
    3.12 +	"light",
    3.13 +	"pressure",
    3.14 +	"temperature",
    3.15 +	"proximity",
    3.16 +	"gravity",
    3.17 +	"linear acceleration",
    3.18 +	"rotation vector",
    3.19 +	"relative humidity",
    3.20 +	"ambient temperature",
    3.21 +	"magnetic field (uncalibrated)",
    3.22 +	"game rotation vector",
    3.23 +	"gyroscope (uncalibrated)",
    3.24 +	"significant motion",
    3.25 +	"step detector",
    3.26 +	"step counter",
    3.27 +	"geomagnetic rotation",
    3.28 +	"heart rate",
    3.29 +	"tilt detector",
    3.30 +	"wake gesture",
    3.31 +	"glance gesture",
    3.32 +	"pick up gesture",
    3.33 +	"wrist tilt gesture"
    3.34 +};
    3.35 +
    3.36 +#define NUM_SENSOR_TYPES	(sizeof names / sizeof *names)
    3.37 +
    3.38 +const char *sensor_typestr(int type)
    3.39 +{
    3.40 +	if(type < 0 || type >= NUM_SENSOR_TYPES) {
    3.41 +		type = 0;
    3.42 +	}
    3.43 +	return names[type];
    3.44 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/android/sensordef.h	Tue Jun 16 06:17:59 2015 +0300
     4.3 @@ -0,0 +1,47 @@
     4.4 +#ifndef SENSORDEF_H_
     4.5 +#define SENSORDEF_H_
     4.6 +
     4.7 +#include <android/sensor.h>
     4.8 +
     4.9 +/* the sensor.h header file in NDK 10d is missing a lot of sensor types,
    4.10 + * here is the full list.
    4.11 + */
    4.12 +#define SENSOR_TYPE_ACCELEROMETER			1
    4.13 +#define SENSOR_TYPE_MAGNETIC_FIELD			2
    4.14 +#define SENSOR_TYPE_ORIENTATION				3
    4.15 +#define SENSOR_TYPE_GYROSCOPE				4
    4.16 +#define SENSOR_TYPE_LIGHT					5
    4.17 +#define SENSOR_TYPE_PRESSURE				6
    4.18 +#define SENSOR_TYPE_TEMPERATURE				7
    4.19 +#define SENSOR_TYPE_PROXIMITY				8
    4.20 +#define SENSOR_TYPE_GRAVITY					9
    4.21 +#define SENSOR_TYPE_LINEAR_ACCELERATION		10
    4.22 +#define SENSOR_TYPE_ROTATION_VECTOR			11
    4.23 +#define SENSOR_TYPE_RELATIVE_HUMIDITY		12
    4.24 +#define SENSOR_TYPE_AMBIENT_TEMPERATURE		13
    4.25 +#define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED	14
    4.26 +#define SENSOR_TYPE_GAME_ROTATION_VECTOR	15
    4.27 +#define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED	16
    4.28 +#define SENSOR_TYPE_SIGNIFICANT_MOTION		17
    4.29 +#define SENSOR_TYPE_STEP_DETECTOR			18
    4.30 +#define SENSOR_TYPE_STEP_COUNTER			19
    4.31 +#define SENSOR_TYPE_GEOMAGNETIC_ROTATION	20
    4.32 +#define SENSOR_TYPE_HEART_RATE				21
    4.33 +#define SENSOR_TYPE_TILT_DETECTOR			22
    4.34 +#define SENSOR_TYPE_WAKE_GESTURE			23
    4.35 +#define SENSOR_TYPE_GLANCE_GESTURE			24
    4.36 +#define SENSOR_TYPE_PICK_UP_GESTURE			25
    4.37 +#define SENSOR_TYPE_WRIST_TILT_GESTURE		26
    4.38 +
    4.39 +#ifdef __cplusplus
    4.40 +extern "C" {
    4.41 +#endif
    4.42 +
    4.43 +const char *sensor_typestr(int type);
    4.44 +
    4.45 +#ifdef __cplusplus
    4.46 +}
    4.47 +#endif
    4.48 +
    4.49 +
    4.50 +#endif	/* SENSORDEF_H_ */
     5.1 --- a/src/game.cc	Sat Jun 13 05:35:43 2015 +0300
     5.2 +++ b/src/game.cc	Tue Jun 16 06:17:59 2015 +0300
     5.3 @@ -9,6 +9,7 @@
     5.4  #include "texture.h"
     5.5  #include "shader.h"
     5.6  #include "text.h"
     5.7 +#include "vmath/vmath.h"
     5.8  
     5.9  static void draw_quad(float hsz, float vsz);
    5.10  
    5.11 @@ -21,6 +22,9 @@
    5.12  
    5.13  static SdrProg *sdr_cam, *sdr_tex;
    5.14  
    5.15 +static Quaternion qrot;
    5.16 +static Vector3 trans;
    5.17 +
    5.18  extern "C" int game_init(void)
    5.19  {
    5.20  	//glEnable(GL_DEPTH_TEST);
    5.21 @@ -109,12 +113,12 @@
    5.22  	gl_load_identity();
    5.23  
    5.24  
    5.25 -	// draw some text
    5.26 +	// print the rotation quaternion
    5.27  	text_color(1, 1, 1, 1);
    5.28 -	for(int i=0; i<25; i++) {
    5.29 -		text_position(0, i);
    5.30 -		text_printf("%d%s line of text", i, i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th")));
    5.31 -	}
    5.32 +	text_position(0, 0);
    5.33 +	text_printf("translation (% .3f, % .3f, % .3f)", trans.x, trans.y, trans.z);
    5.34 +	text_position(0, 1);
    5.35 +	text_printf("Rotation quat ([% 1.3f, % 1.3f, % 1.3f], % 1.3f)", qrot.v.x, qrot.v.y, qrot.v.z, qrot.s);
    5.36  }
    5.37  
    5.38  static void draw_quad(float hsz, float vsz)
    5.39 @@ -217,3 +221,17 @@
    5.40  	*/
    5.41  }
    5.42  
    5.43 +void game_6dof_translation(float dx, float dy, float dz)
    5.44 +{
    5.45 +	trans.x = dx;
    5.46 +	trans.y = dy;
    5.47 +	trans.z = dz;
    5.48 +}
    5.49 +
    5.50 +void game_6dof_rotation(float qx, float qy, float qz, float qw)
    5.51 +{
    5.52 +	qrot.v.x = qx;
    5.53 +	qrot.v.y = qy;
    5.54 +	qrot.v.z = qz;
    5.55 +	qrot.s = qw;
    5.56 +}
     6.1 --- a/src/game.h	Sat Jun 13 05:35:43 2015 +0300
     6.2 +++ b/src/game.h	Tue Jun 16 06:17:59 2015 +0300
     6.3 @@ -17,6 +17,9 @@
     6.4  void game_mouse_button(int id, int bn, int press, int x, int y);
     6.5  void game_mouse_motion(int id, int x, int y);
     6.6  
     6.7 +void game_6dof_translation(float dx, float dy, float dz);
     6.8 +void game_6dof_rotation(float qx, float qy, float qz, float qw);
     6.9 +
    6.10  /* provided by the system frontend */
    6.11  void set_mouse_pos(int x, int y);
    6.12  void set_mouse_cursor(int enable);
     7.1 --- a/src/sdr.c	Sat Jun 13 05:35:43 2015 +0300
     7.2 +++ b/src/sdr.c	Tue Jun 16 06:17:59 2015 +0300
     7.3 @@ -149,7 +149,6 @@
     7.4  
     7.5  	filesize = ass_fseek(fp, 0, SEEK_END);
     7.6  	/*filesize = ass_ftell(fp);*/
     7.7 -	printf("  filesize: %ld\n", filesize);
     7.8  	ass_fseek(fp, 0, SEEK_SET);
     7.9  
    7.10  	if(!(src = malloc(filesize + 1))) {