当前位置: 首页 > 工具软件 > OpenHMD > 使用案例 >

openHMD-simple代码分析(1/2)

金伟
2023-12-01

openHMD-simple代码分析(1/2)

以官方的simple例程,来对openHMD进行分析。

int main(int argc, char** argv)
{
    //创建ctx,填充设备驱动接口ctx->drivers
    //这里填充驱动接口时有个小问题,我已在github上提交,官方已确认修复。
    ohmd_context* ctx = ohmd_ctx_create();

    //根据HID设备的VID、PID进行枚举操作,判断设备是否存在
    int num_devices = ohmd_ctx_probe(ctx);
    if(num_devices < 0){
        printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
        return -1;
    }

    printf("num devices: %d\n\n", num_devices);

    //打印设备的厂商信息,路径(这些信息在枚举时已填充)
    for(int i = 0; i < num_devices; i++){
        printf("device %d\n", i);
        printf("  vendor:  %s\n", ohmd_list_gets(ctx, i, OHMD_VENDOR));
        printf("  product: %s\n", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
        printf("  path:    %s\n\n", ohmd_list_gets(ctx, i, OHMD_PATH));
    }

    //打开设备列表中第一个设备
    ohmd_device* hmd = ohmd_list_open_device(ctx, 0);

    if(!hmd){
        printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
        return -1;
    }

    //获取所打开设备的显示参数
    int ivals[2];
    ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
    ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
    printf("resolution:         %i x %i\n", ivals[0], ivals[1]);

    print_infof(hmd, "hsize:",            1, OHMD_SCREEN_HORIZONTAL_SIZE);
    print_infof(hmd, "vsize:",            1, OHMD_SCREEN_VERTICAL_SIZE);
    print_infof(hmd, "lens separation:",  1, OHMD_LENS_HORIZONTAL_SEPARATION);
    print_infof(hmd, "lens vcenter:",     1, OHMD_LENS_VERTICAL_POSITION);
    print_infof(hmd, "left eye fov:",     1, OHMD_LEFT_EYE_FOV);
    print_infof(hmd, "right eye fov:",    1, OHMD_RIGHT_EYE_FOV);
    print_infof(hmd, "left eye aspect:",  1, OHMD_LEFT_EYE_ASPECT_RATIO);
    print_infof(hmd, "right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
    print_infof(hmd, "distortion k:",     6, OHMD_DISTORTION_K);

    printf("\n");

    //循环获取数据,并处理成四元素和vector。这部分很重要,后面讲单独、详细分析
    for(int i = 0; i < 10000; i++){
        ohmd_ctx_update(ctx);

        float zero[] = {.0, .1, .2, 1};
        ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
        ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);

        print_infof(hmd, "rotation quat:", 4, OHMD_ROTATION_QUAT);
        ohmd_sleep(.01);
    }
    //退出
    ohmd_ctx_destroy(ctx);

    return 0;
}

simple的主函数过程比较简单,后文将以ocluars的DK1/DK2代码运行对具体操作进行详细分析。

 类似资料: