Skip to content

Instantly share code, notes, and snippets.

/OpenHMD.pod Secret

Created October 13, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4758dcf51b250c1b734a to your computer and use it in GitHub Desktop.
Save anonymous/4758dcf51b250c1b734a to your computer and use it in GitHub Desktop.

NAME

OpenHMD - Perl binding for OpenHMD C library

SYNOPSIS

# This synopsis is available as examples/simple.pl
##############################################################################
# This code is a port of OpenHMD's examples/simple.c
# It is not representitive of "best practice" Perl.
##############################################################################

use OpenHMD qw(:all);

sub ohmd_sleep
{
    select(undef, undef, undef, shift);
}

sub print_infof
{
    my ($hmd, $name, $len, $val) = @_;

    my $f = pack 'f*', (0) x $len;
    ohmd_device_getf($hmd, $val, $f);
    printf("%-20s", $name);
    my @f = unpack 'f*', $f;
    for(my $i = 0; $i < $len; $i++)
        { printf("%f ", $f[$i]); }
    printf("\n");
}

my $ctx = ohmd_ctx_create();

my $num_devices = ohmd_ctx_probe($ctx);
if ($num_devices < 0) {
    printf('failed to probe devices: %s', ohmd_ctx_get_error($ctx));
    exit;
}

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

for (my $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));
}

my $hmd = ohmd_list_open_device($ctx, 0);
if (!$hmd) {
    printf('failed to open device: %s', ohmd_ctx_get_error($ctx));
    exit;
}

my @ivals = (pack 'i1') x 2;
ohmd_device_geti($hmd, $OHMD_SCREEN_HORIZONTAL_RESOLUTION, $ivals[0]);
ohmd_device_geti($hmd, $OHMD_SCREEN_VERTICAL_RESOLUTION, $ivals[1]);
printf "resolution:         %i x %i\n", unpack 'i*', join '', @ivals;

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";

for (my $i = 0; $i < 10000; $i++) {
    ohmd_ctx_update($ctx);
    print_infof($hmd, "rotation quat:", 4, $OHMD_ROTATION_QUAT);
    ohmd_sleep(.01);
}

ohmd_ctx_destroy($ctx);

DESCRIPTION

OpenHMD is a 1:1 mapping for the OpenHMD C library.

FUNCTIONS

OpenHMD implements the following functions, which can be imported individually or using the :functions tag.

ohmd_ctx_create

my $context = ohmd_ctx_create();

Create a new OpenHMD context.

ohmd_ctx_destroy

ohmd_ctx_destroy($context);

Destroy an OpenHMD context.

ohmd_ctx_get_error

my $error = ohmd_ctx_get_error($context);

Gets the latest human readable error message.

ohmd_ctx_probe

my $device_count = ohmd_ctx_probe($context);

Probes for supported devices and returns the count of found devices.

ohmd_ctx_update

ohmd_ctx_update($context);

Updates an OpenHMD context.

ohmd_device_getf

my $status = ohmd_device_getf($device, $type, $out);

Populate @values with the float values for $type.

# Get the rotation quaternion
my $out = pack 'f4';
my $status = ohmd_device_getf($device, $OHMD_ROTATION_QUAT, $out);
my @quaternion = unpack 'f*', $out;
# @quaternion = (0, 0, 0, 1);

ohmd_device_geti

my $status = ohmd_device_geti($device, $type, $out);

Populate @values with the integer values for $type.

# Get the horizontal resolution
my $out = pack 'i1';
my $status = ohmd_device_geti(
    $device, $OHMD_SCREEN_HORIZONTAL_RESOLUTION, $out
);
my $horizontal = unpack 'i*', $out;
# $horizontal = 1280

ohmd_device_setf

my $status = ohmd_device_setf($device, $type, $in);

Sets float values for $type.

# Set the IPD to 0.60
my $in = pack 'f1', 0.6;
my $status = ohmd_device_getf($device, $OHMD_EYE_IPD, $in);

ohmd_list_gets

my $string = ohmd_list_gets($context, $index, $type);

Gets the string value of $type for device at $index.

# Get vendor of device at index 1
my $vendor = ohmd_list_gets($context, 1, $OHMD_VENDOR);
# $vendor = 'OpenHMD'

ohmd_list_open_device

my $device = ohmd_list_open_device($context, $index);

Opens the device at $index.

# Open the default device
my $device = ohmd_list_open_device($context, 0);

# Open the last device
my $device = ohmd_list_open_device($context, $device_count - 1);

CONSTANTS

OpenHMD provides the following constants, which can be imported individually or using the :constants tag.

String
$OHMD_VENDOR
$OHMD_PRODUCT
$OHMD_PATH
Status
$OHMD_S_OK
$OHMD_S_UNKNOWN_ERROR
$OHMD_S_INVALID_PARAMETER
$OHMD_S_USER_RESERVED
Integer
$OHMD_SCREEN_HORIZONTAL_RESOLUTION
$OHMD_SCREEN_VERTICAL_RESOLUTION
Float
$OHMD_ROTATION_QUAT
$OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX
$OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX
$OHMD_LEFT_EYE_GL_PROJECTION_MATRIX
$OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX
$OHMD_POSITION_VECTOR
$OHMD_SCREEN_HORIZONTAL_SIZE
$OHMD_SCREEN_VERTICAL_SIZE
$OHMD_LENS_HORIZONTAL_SEPARATION
$OHMD_LENS_VERTICAL_POSITION
$OHMD_LEFT_EYE_FOV
$OHMD_LEFT_EYE_ASPECT_RATIO
$OHMD_RIGHT_EYE_FOV
$OHMD_RIGHT_EYE_ASPECT_RATIO
$OHMD_EYE_IPD
$OHMD_PROJECTION_ZFAR
$OHMD_PROJECTION_ZNEAR
$OHMD_DISTORTION_K

SEE ALSO

OpenHMD

OpenHMD's website.

#openhmd @ freenode

Official IRC channel for OpenHMD

DEPENDENCIES

OpenHMD depends on the following modules.

Const::Fast

Exportable, read-only constants.

Exporter

Exporting constants and functions.

Inline::C

Access to C functions.

ACKNOWLEDGEMENTS

Fredrick Hultin and Joey Ferwerda

For their help with the C part of this module, answering questions, implementing suggested functionality into OpenHMD and generally being very encouraging and supportive of perl-openhmd.

NefariousMoogle

Reviewed documentation as someone relatively inexperienced with Perl.

AUTHOR

CandyAngel (candyangel@electricjungle.org)

LICENSE AND COPYRIGHT

Copyright (c) 2015 CandyAngel (candyangel@electricjungle.org). All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment