02-01-2016 05:40 AM
rc = ovrRenderAPIConfig()
header = rc.Header;
header.API = ovrRenderAPI_OpenGL
header.BackBufferSize = self.hmdDesc.Resolution #ovr.ovrSizei(1920, 1080);
header.Multisample = 1
for i in range(0, 8):
rc.PlatformData[i] = ctypes.cast(0, ctypes.POINTER(ctypes.c_uint)) #ctypes.cast(x, ctypes.POINTER(ctypes.c_ulong))
result = [ ovrEyeRenderDesc(), ovrEyeRenderDesc() ]
out_arr = (ovrEyeRenderDesc * 2)(*result)
in_arr = (ovrFovPort * 2)(*self.fovPorts)
ovrHmd_ConfigureRendering(self.hmd, rc, (ovrDistortionCap_Chromatic |
ovrDistortionCap_TimeWarp |
ovrDistortionCap_Vignette), in_arr, out_arr)
self.eyeRenderDescs = out_arr
import numpy as np
import pygame
import pygame.locals as pgl
from OpenGL.GL import *
from cgkit.cgtypes import mat4, vec3, quat
from ctypes import *
import sys
sys.path.append('/home/ehrssonlab/python-ovrsdk/oculusvr/')
#from oculusvr import Hmd, ovrGLTexture, ovrPosef, ovrVector3f
#import oculusvr as ovr
from ovrsdk import * #by wwwtyro
#from ovrsdk import ovrHmd_GetDesc, ovrHmd_Create
class RiftApp():
def __init__(self):
ovr_Initialize()
self.hmd = ovrHmd_Create(0)
if(ovrHmd_Detect() == 0):
print "not detected"
else:
print "detected"
self.hmdDesc = ovrHmdDesc()
ovrHmd_GetDesc(self.hmd,byref(self.hmdDesc))
#self.hmdDesc = cast(self.hmd.hmd,POINTER(ovr.ovrHmdDesc)).contents
self.frame = 0
print self.hmdDesc.Resolution.w
# Workaround for a race condition bug in the SDK
import time
time.sleep(0.1)
#self.hmd.configure_tracking()
self.fovPorts = (
self.hmdDesc.DefaultEyeFov[0],
self.hmdDesc.DefaultEyeFov[1]
)
projections = map(
lambda fovPort:
(ovrMatrix4f_Projection(fovPort, 0.01, 1000, '\x01' if True else '\x00')),
#ovr.Hmd.get_perspective(fovPort, 0.01, 1000, True)),
self.fovPorts
)
self.w = 1
self.x = 0
self.y = 0
self.z = 0
self.projections = map(
lambda pr:
(self.w, self.x, self.y, self.z),
projections)
self.eyeTextures = [ ovrGLTexture(), ovrGLTexture() ]
for eye in range(0, 2):
#size = self.hmd.get_fov_texture_size(eye, self.fovPorts[eye])
size = ovrHmd_GetFovTextureSize(self.hmd,eye,self.hmdDesc.DefaultEyeFov[eye],1.0);
eyeTexture = self.eyeTextures[eye]
eyeTexture.API = ovrRenderAPI_OpenGL
header = eyeTexture.Texture.Header;
header.TextureSize = size
vp = header.RenderViewport;
vp.Size = size
vp.Pos.x = 0
vp.Pos.y = 0
def close(self):
glDeleteFramebuffers(2, self.fbo)
glDeleteTextures(self.color)
glDeleteRenderbuffers(2, self.depth)
self.hmd.destroy()
self.hmd = None
ovr.Hmd.shutdown()
def create_window(self):
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (
self.hmdDesc.WindowsPos.x,
self.hmdDesc.WindowsPos.y)
pygame.init()
pygame.display.set_mode(
(
self.hmdDesc.Resolution.w,
self.hmdDesc.Resolution.h
),
pgl.HWSURFACE | pgl.OPENGL | pgl.DOUBLEBUF | pgl.NOFRAME)
window_info = pygame.display.get_wm_info()
self.window = c_void_p(window_info['window'])
#ovr.ovrHmd_AttachToWindow(self.hmd.hmd, self.window, 0, 0)
print self.hmdDesc.Resolution.w
def init_gl(self):
self.fbo = glGenFramebuffers(2)
self.color = glGenTextures(2)
self.depth = glGenRenderbuffers(2)
for eye in range(0, 2):
self.build_framebuffer(eye)
self.eyeTextures[eye].OGL.TexId = np.asscalar(self.color[eye])
rc = ovrRenderAPIConfig()
header = rc.Header;
header.API = ovrRenderAPI_OpenGL
header.BackBufferSize = self.hmdDesc.Resolution #ovr.ovrSizei(1920, 1080);
header.Multisample = 1
for i in range(0, 8):
rc.PlatformData[i] = ctypes.cast(0, ctypes.POINTER(ctypes.c_uint)) #ctypes.cast(x, ctypes.POINTER(ctypes.c_ulong))
print "test2A"
result = [ ovrEyeRenderDesc(), ovrEyeRenderDesc() ]
out_arr = (ovrEyeRenderDesc * 2)(*result)
in_arr = (ovrFovPort * 2)(*self.fovPorts)
ovrHmd_ConfigureRendering(self.hmd, rc, (ovrDistortionCap_Chromatic |
ovrDistortionCap_TimeWarp |
ovrDistortionCap_Vignette), in_arr, out_arr)
self.eyeRenderDescs = out_arr
print "test2B"
self.eyeOffsets = [ ovrVector3f(), ovrVector3f() ]
for eye in range(0, 2):
self.eyeOffsets[eye] = self.eyeRenderDescs[eye].HmdToEyeViewOffset
# Bug in the SDK leaves a program bound, so clear it
glUseProgram(0)
def build_framebuffer(self, eye):
size = self.eyeTextures[eye].Texture.Header.TextureSize
# Set up the color attachement texture
glBindTexture(GL_TEXTURE_2D, self.color[eye])
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
size.w, size.h, 0, GL_RGB,
GL_UNSIGNED_BYTE, None)
glBindTexture(GL_TEXTURE_2D, 0)
# Set up the depth attachment renderbuffer
glBindRenderbuffer(GL_RENDERBUFFER, self.depth[eye])
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
size.w, size.h)
glBindRenderbuffer(GL_RENDERBUFFER, 0)
# Set up the framebuffer proper
glBindFramebuffer(GL_FRAMEBUFFER, self.fbo[eye])
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
self.color[eye], 0)
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
self.depth[eye])
fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER)
if (GL_FRAMEBUFFER_COMPLETE != fboStatus):
raise Exception("Bad framebuffer setup")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
def render_frame(self):
self.frame += 1
# Fetch the head pose
poses = self.hmd.get_eye_poses(self.frame, self.eyeOffsets)
self.hmd.begin_frame(self.frame)
for i in range(0, 2):
eye = self.hmdDesc.EyeRenderOrder[i]
glMatrixMode(GL_PROJECTION)
glLoadMatrixf(self.projections[eye])
self.eyeview = mat4(1.0)
# Apply the head orientation
rot = poses[eye].Orientation
# Convert the OVR orientation (a quaternion
# structure) to a cgkit quaternion class, and
# from there to a mat4 Coordinates are camera
# coordinates
rot = quat(rot.toList())
rot = rot.toMat4()
# Apply the head position
pos = poses[eye].Position
# Convert the OVR position (a vector3 structure)
# to a cgcit vector3 class. Position is in camera /
# Rift coordinates
pos = vec3(pos.toList())
pos = mat4(1.0).translate(pos)
pose = pos * rot
# apply it to the eyeview matrix
self.eyeview = pose;
# The subclass is responsible for taking eyeview
# and applying it to whatever camera or modelview
# coordinate system it uses before rendering the
# scene
# Active the offscreen framebuffer and render the scene
glBindFramebuffer(GL_FRAMEBUFFER, self.fbo[eye])
size = self.eyeTextures[eye].Texture.Header.RenderViewport.Size
glViewport(0, 0, size.w, size.h)
self.render_scene()
glBindFramebuffer(GL_FRAMEBUFFER, 0)
self.hmd.end_frame(poses, self.eyeTextures)
glGetError()
def update(self):
for event in pygame.event.get():
self.on_event(event)
def on_event(self, event):
if event.type == pgl.QUIT:
self.running = False
return True
if event.type == pgl.KEYUP and event.key == pgl.K_ESCAPE:
self.running = False
return True
return False
def run(self):
self.create_window()
self.init_gl()
self.running = True
#start = ovr.Hmd.get_time_in_seconds()
last = start
while self.running:
self.update()
self.render_frame()
#pygame.display.flip()
#now = ovr.Hmd.get_time_in_seconds()
#if (now - last > 10):
#interval = now - start
#fps = self.frame / interval
#print "%f" % fps
#last = now
self.close()
pygame.quit()
if __name__ == '__main__':
app=RiftApp()
app.create_window()
app.init_gl()
Program received signal SIGSEGV, Segmentation fault.
0x00007fffe39c7a30 in OVR::CAPI::HMDRenderState::setupRenderDesc(ovrEyeRenderDesc_*, ovrFovPort_ const*) () from /usr/local/lib/python2.7/dist-packages/ovrsdk/linux/libovr.so
(gdb) backtrace
#0 0x00007fffe39c7a30 in OVR::CAPI::HMDRenderState::setupRenderDesc(ovrEyeRenderDesc_*, ovrFovPort_ const*) () from /usr/local/lib/python2.7/dist-packages/ovrsdk/linux/libovr.so
#1 0x00007fffe39c8e7b in OVR::CAPI::HMDState::ConfigureRendering(ovrEyeRenderDesc_*, ovrFovPort_ const*, ovrRenderAPIConfig_ const*, unsigned int) ()
from /usr/local/lib/python2.7/dist-packages/ovrsdk/linux/libovr.so
#2 0x00007fffef9a4adc in ffi_call_unix64 () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#3 0x00007fffef9a440c in ffi_call () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#4 0x00007fffefbbb5fe in _ctypes_callproc ()
from /usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so
#5 0x00007fffefbbcf9e in ?? () from /usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so
#6 0x0000000000499be5 in PyEval_EvalFrameEx ()
#7 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#8 0x00000000004a1634 in ?? ()
#9 0x000000000044e4a5 in PyRun_FileExFlags ()
#10 0x000000000044ec9f in PyRun_SimpleFileExFlags ()
#11 0x000000000044f904 in Py_Main ()
#12 0x00007ffff74e8ec5 in __libc_start_main (main=0x44f9c2 <main>, argc=2, argv=0x7fffffffde68,
init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>,
stack_end=0x7fffffffde58) at libc-start.c:287
#13 0x0000000000578c4e in _start ()