Переглянути джерело

added rotation correction

Fredrik Hultin 10 роки тому
батько
коміт
5cc57781b5
3 змінених файлів з 43 додано та 0 видалено
  1. 6 0
      examples/opengl/main.c
  2. 34 0
      src/openhmd.c
  3. 3 0
      src/openhmdi.h

+ 6 - 0
examples/opengl/main.c

@@ -133,6 +133,12 @@ int main(int argc, char** argv)
 				case SDLK_F1:
 					SDL_WM_ToggleFullScreen(gl.screen);
 					break;
+				case SDLK_F2:
+					{
+						float zero[] = {0, 0, 0, 1};
+						ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
+					}
+					break;
 				default:
 					break;
 				}

+ 34 - 0
src/openhmd.c

@@ -88,6 +88,8 @@ ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index)
 		ohmd_driver* driver = (ohmd_driver*)desc->driver_ptr;
 		ohmd_device* device = driver->open_device(driver, desc);
 
+		device->rotation_correction.w = 1;
+
 		if (device == NULL)
 			return NULL;
 
@@ -126,6 +128,9 @@ int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, f
 			vec3f point = {{0, 0, 0}};
 			quatf rot;
 			device->getf(device, OHMD_ROTATION_QUAT, (float*)&rot);
+			quatf tmp = device->rotation_correction;
+			oquatf_mult_me(&tmp, &rot);
+			rot = tmp;
 			mat4x4f orient, world_shift, result;
 			omat4x4f_init_look_at(&orient, &rot, &point);
 			omat4x4f_init_translate(&world_shift, +(device->properties.ipd / 2.0f), 0, 0);
@@ -137,6 +142,7 @@ int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, f
 			vec3f point = {{0, 0, 0}};
 			quatf rot;
 			device->getf(device, OHMD_ROTATION_QUAT, (float*)&rot);
+			oquatf_mult_me(&rot, &device->rotation_correction);
 			mat4x4f orient, world_shift, result;
 			omat4x4f_init_look_at(&orient, &rot, &point);
 			omat4x4f_init_translate(&world_shift, -(device->properties.ipd / 2.0f), 0, 0);
@@ -184,6 +190,21 @@ int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, f
 	case OHMD_PROJECTION_ZNEAR:
 		*out = device->properties.znear;
 		return 0;
+
+	case OHMD_ROTATION_QUAT:
+	{
+		int ret = device->getf(device, OHMD_ROTATION_QUAT, out);
+
+		if(ret != 0)
+			return ret;
+
+		oquatf_mult_me((quatf*)out, &device->rotation_correction);
+		quatf tmp = device->rotation_correction;
+		oquatf_mult_me(&tmp, (quatf*)out);
+		*(quatf*)out = tmp;
+		return 0;
+	}
+		
 	default:
 		return device->getf(device, type, out);
 	}
@@ -201,6 +222,19 @@ int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, f
 	case OHMD_PROJECTION_ZNEAR:
 		device->properties.znear = *in;
 		return 0;
+	case OHMD_ROTATION_QUAT:
+		{
+			// adjust rotation correction
+			quatf q;
+			int ret = device->getf(device, OHMD_ROTATION_QUAT, (float*)&q);
+
+			if(ret != 0){
+				return ret;
+			}
+
+			oquatf_diff(&q, (quatf*)in, &device->rotation_correction);
+			return 0;
+		}
 	default:
 		return -1;
 	}

+ 3 - 0
src/openhmdi.h

@@ -72,6 +72,9 @@ typedef struct {
 struct ohmd_device {
 	ohmd_device_properties properties;
 
+	quatf rotation_correction;
+	vec3f position_correction;
+
 	int (*getf)(ohmd_device* device, ohmd_float_value type, float* out);
 	void (*update)(ohmd_device* device);
 	void (*close)(ohmd_device* device);