Skip to content

Commit 866f47c

Browse files
authored
added the core_vr_simulator to examples (#107)
1 parent 903747b commit 866f47c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

examples/core/core_vr_simulator.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import pyray
2+
GLSL_VERSION = 330
3+
4+
# Initialization
5+
screenWidth = 800
6+
screenHeight = 450
7+
8+
# NOTE: screenWidth/screenHeight should match VR device aspect ratio
9+
pyray.init_window(screenWidth, screenHeight, "raylib [core] example - vr simulator")
10+
11+
# VR device parameters definition
12+
device = pyray.VrDeviceInfo(
13+
# Oculus Rift CV1 parameters for simulator
14+
2160, # Horizontal resolution in pixels
15+
1200, # Vertical resolution in pixels
16+
0.133793, # Horizontal size in meters
17+
0.0669, # Vertical size in meters
18+
0.04678, # Screen center in meters
19+
0.041, # Distance between eye and display in meters
20+
0.07, # Lens separation distance in meters
21+
0.07, # IPD (distance between pupils) in meters
22+
23+
# NOTE: CV1 uses fresnel-hybrid-asymmetric lenses with specific compute shaders
24+
# Following parameters are just an approximation to CV1 distortion stereo rendering
25+
[1.0, 0.22, 0.24, 0.0], # Lens distortion constant parameters
26+
[0.996, -0.004, 1.014, 0.0] # Chromatic aberration correction parameters
27+
)
28+
29+
# Load VR stereo config for VR device parameters (Oculus Rift CV1 parameters)
30+
config = pyray.load_vr_stereo_config(device)
31+
32+
# Distortion shader (uses device lens distortion and chroma)
33+
distortion = pyray.load_shader(0, f"resources/distortion{GLSL_VERSION}.fs")
34+
35+
# Update distortion shader with lens and distortion-scale parameters
36+
pyray.set_shader_value(distortion, 2,"leftLensCenter", pyray.SHADER_UNIFORM_VEC2)
37+
pyray.set_shader_value(distortion, 2,"rightLensCenter", pyray.SHADER_UNIFORM_VEC2)
38+
pyray.set_shader_value(distortion, 2,"leftScreenCenter", pyray.SHADER_UNIFORM_VEC2)
39+
pyray.set_shader_value(distortion, 2,"rightScreenCenter", pyray.SHADER_UNIFORM_VEC2)
40+
41+
pyray.set_shader_value(distortion, 2,"scale", pyray.SHADER_UNIFORM_VEC2)
42+
pyray.set_shader_value(distortion, 2,"scaleIn", pyray.SHADER_UNIFORM_VEC2)
43+
pyray.set_shader_value(distortion, 4,"deviceWarpParam", pyray.SHADER_UNIFORM_VEC4)
44+
pyray.set_shader_value(distortion, 4,"chromaAbParam", pyray.SHADER_UNIFORM_VEC4)
45+
46+
# Initialize framebuffer for stereo rendering
47+
# NOTE: Screen size should match HMD aspect ratio
48+
target = pyray.load_render_texture(device.hResolution, device.vResolution)
49+
50+
# The target's height is flipped (in the source Rectangle), due to OpenGL reasons
51+
sourceRec = pyray.Rectangle(0.0, 0.0, target.texture.width, -target.texture.height)
52+
destRec = pyray.Rectangle(0.0, 0.0, pyray.get_screen_width(), pyray.get_screen_height())
53+
54+
# Define the camera to look into our 3D world
55+
camera = pyray.Camera3D(
56+
pyray.Vector3(5.0, 2.0, 5.0), # Camera position
57+
pyray.Vector3(0.0, 2.0, 0.0), # Camera looking at point
58+
pyray.Vector3(0.0, 1.0, 0.0), # Camera up vector
59+
60.0, # Camera field-of-view Y
60+
pyray.CAMERA_PERSPECTIVE # Camera projection type
61+
)
62+
63+
cubePosition = pyray.Vector3(0.0, 0.0, 0.0)
64+
65+
pyray.disable_cursor() # Limit cursor to relative movement inside the window
66+
67+
pyray.set_target_fps(90) # Set our game to run at 90 frames-per-second
68+
69+
# Main game loop
70+
while not pyray.window_should_close(): # Detect window close button or ESC key
71+
# Update
72+
pyray.update_camera(camera, pyray.CAMERA_FIRST_PERSON)
73+
74+
# Draw
75+
pyray.begin_texture_mode(target)
76+
pyray.clear_background(pyray.RAYWHITE)
77+
pyray.begin_vr_stereo_mode(config)
78+
pyray.begin_mode_3d(camera)
79+
80+
pyray.draw_cube(cubePosition, 2.0, 2.0, 2.0, pyray.RED)
81+
pyray.draw_cube_wires(cubePosition, 2.0, 2.0, 2.0, pyray.MAROON)
82+
pyray.draw_grid(40, 1.0)
83+
84+
pyray.end_mode_3d()
85+
pyray.end_vr_stereo_mode()
86+
pyray.end_texture_mode()
87+
88+
pyray.begin_drawing()
89+
pyray.clear_background(pyray.RAYWHITE)
90+
pyray.begin_shader_mode(distortion)
91+
pyray.draw_texture_pro(target.texture, sourceRec, destRec, pyray.Vector2(0.0, 0.0), 0.0, pyray.WHITE)
92+
pyray.end_shader_mode()
93+
pyray.draw_fps(10, 10)
94+
pyray.end_drawing()
95+
96+
# De-Initialization
97+
pyray.unload_vr_stereo_config(config) # Unload stereo config
98+
pyray.unload_render_texture(target) # Unload stereo render fbo
99+
pyray.unload_shader(distortion) # Unload distortion shader
100+
pyray.close_window() # Close window and OpenGL context

0 commit comments

Comments
 (0)