Skip to content

Commit b3eb587

Browse files
committed
Fix trackball/camera and opengl rendering
1 parent 6b4c866 commit b3eb587

9 files changed

Lines changed: 62 additions & 70 deletions

File tree

examples/cuda/dynamic_geometry.cu

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ extern "C" __global__ void __raygen__rg()
101101
static_cast< float >( idx.y ) / static_cast< float >( dim.y )
102102
) - 1.0f;
103103

104-
if (idx.x == 0 && idx.y == 0 && idx.z == 0) {
105-
printf("ix=%u/%u, iy=%u/%u, width=%u, height=%u\n", idx.x, dim.x-1, idx.y, dim.y-1, params.width, params.height);
106-
printf("U: %f, %f, %f V: %f, %f, %f, W: %f, %f, %f, eye: %f, %f, %f\n",
107-
U.x, U.y, U.z, V.x, V.y, V.z, W.x, W.y, W.z, eye.x, eye.y, eye.z);
108-
printf("frame_buffer=%p, trav_handle=%p, subframe_index=%u\n", params.frame_buffer, params.trav_handle, params.subframe_index);
109-
}
110-
111-
112104
const float3 direction = normalize( d.x * U + d.y * V + W );
113105
float3 payload_rgb = make_float3( 0.5f, 0.5f, 0.5f );
114106

examples/dynamic_geometry.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class DynamicGeometryState:
5858
__slots__ = ['params', 'time', 'ctx', 'module', 'pipeline', 'pipeline_opts',
5959
'raygen_grp', 'miss_grp', 'hit_grp', 'sbt',
6060
'generate_vertices_kernel', 'd_temp_vertices', 'last_exploding_sphere_rebuild_time',
61-
'static_gas_handle', 'deforming_gas_handle', 'exploding_gas_handle', 'ias',
61+
'static_gas', 'deforming_gas', 'exploding_gas', 'ias',
6262
'trackball', 'camera_changed', 'mouse_button', 'resize_dirty', 'minimized']
6363

6464
def __init__(self):
@@ -78,7 +78,7 @@ def camera(self):
7878

7979
@property
8080
def dimensions(self):
81-
return (int(self.params.height), int(self.params.width))
81+
return (int(self.params.width), int(self.params.height))
8282

8383
class AnimationMode(enum.Enum):
8484
NONE = 0
@@ -101,17 +101,17 @@ class AnimationMode(enum.Enum):
101101
INST_COUNT = g_diffuse_colors.shape[0]
102102

103103
g_instances = np.asarray([
104-
[1, 0, 0, -4.5,
105-
0, 1, 0, 0,
104+
[1, 0, 0, -4.5,
105+
0, 1, 0, 0,
106106
0, 0, 1, 0],
107-
[1, 0, 0, -1.5,
108-
0, 1, 0, 0,
107+
[1, 0, 0, -1.5,
108+
0, 1, 0, 0,
109109
0, 0, 1, 0],
110-
[1, 0, 0, 1.5,
111-
0, 1, 0, 0,
110+
[1, 0, 0, 1.5,
111+
0, 1, 0, 0,
112112
0, 0, 1, 0],
113-
[1, 0, 0, 4.5,
114-
0, 1, 0, 0,
113+
[1, 0, 0, 4.5,
114+
0, 1, 0, 0,
115115
0, 0, 1, 0],
116116
], dtype=np.float32).reshape(INST_COUNT, 3, 4)
117117

@@ -124,7 +124,7 @@ def mouse_button_callback(window, button, action, mods):
124124
(x, y) = glfw.get_cursor_pos(window)
125125
if action is glfw.PRESS:
126126
state.mouse_button = button
127-
state.trackball.start_tracking(int(x), int(y))
127+
state.trackball.start_tracking(x, y)
128128
else:
129129
state.mouse_button = -1
130130

@@ -163,7 +163,7 @@ def key_callback(window, key, scancode, action, mods):
163163

164164
def scroll_callback(window, xscroll, yscroll):
165165
state = glfw.get_window_user_pointer(window)
166-
if state.trackball.wheel_event(int(yscroll)):
166+
if state.trackball.wheel_event(yscroll):
167167
state.camera_changed = True
168168

169169
#------------------------------------------------------------------------------
@@ -202,11 +202,6 @@ def update_state(output_buffer, state):
202202

203203
def launch_subframe(output_buffer, state):
204204
state.params.frame_buffer = output_buffer.map()
205-
206-
print('LAUNCH SUBFRAME')
207-
print('state.dimensions', state.dimensions)
208-
print(f'state.output_buffer: width={output_buffer.width} height={output_buffer.height}')
209-
print(f'state.params\n{state.params}')
210205

211206
state.pipeline.launch(state.sbt, dimensions=state.dimensions,
212207
params=state.params.handle, stream=output_buffer.stream)
@@ -230,7 +225,7 @@ def init_camera_state(state):
230225
trackball = state.trackball
231226
trackball.move_speed = 10.0
232227
trackball.set_reference_frame([1,0,0], [0,0,1], [0,1,0])
233-
trackball.gimbal_lock = True
228+
trackball.reinitialize_orientation_from_camera()
234229

235230
def create_context(state):
236231
logger = ox.Logger(log)
@@ -257,13 +252,13 @@ def update_mesh_accel(state):
257252
def build_vertex_generation_kernel(state):
258253
cuda_source = os.path.join(script_dir, 'cuda', 'dynamic_geometry_vertex_generation.cu')
259254
example_include_path = os.path.dirname(cuda_source)
260-
255+
261256
build_flags = ox.module.get_default_nvrtc_compile_flags() + (f'-I{example_include_path}',)
262257

263258
with open(cuda_source, 'r') as f:
264259
code = f.read()
265260

266-
state.generate_vertices_kernel = cp.RawKernel(code=code, backend='nvrtc',
261+
state.generate_vertices_kernel = cp.RawKernel(code=code, backend='nvrtc',
267262
options=build_flags, name='generate_vertices')
268263

269264
def build_mesh_accel(state):
@@ -283,14 +278,14 @@ def build_mesh_accel(state):
283278
# Build an AS over the triangles.
284279
# We use un-indexed triangles so we can explode the sphere per triangle.
285280
build_input = ox.BuildInputTriangleArray(state.d_temp_vertices, flags=[ox.GeometryFlags.NONE])
286-
state.static_gas_handle = ox.AccelerationStructure(state.ctx, build_input,
281+
state.static_gas = ox.AccelerationStructure(state.ctx, build_input,
287282
compact=True, allow_update=True, random_vertex_access=True)
288283

289-
state.deforming_gas_handle = state.static_gas_handle
290-
state.exploding_gas_handle = state.static_gas_handle
284+
state.deforming_gas = state.static_gas
285+
state.exploding_gas = state.static_gas
291286

292-
traversables = [state.static_gas_handle, state.static_gas_handle,
293-
state.deforming_gas_handle, state.exploding_gas_handle]
287+
traversables = [state.static_gas, state.static_gas,
288+
state.deforming_gas, state.exploding_gas]
294289
instances = []
295290
for i in range(INST_COUNT):
296291
instance = ox.Instance(traversable=traversables[i], instance_id=0, flags=ox.InstanceFlags.NONE,

examples/dynamic_materials.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self, width, height):
6262

6363
@property
6464
def dimensions(self):
65-
return (int(self.params.image_height), int(self.params.image_width))
65+
return (int(self.params.image_width), int(self.params.image_height))
6666

6767

6868
class MaterialIndex:
@@ -154,14 +154,15 @@ def create_context(state):
154154

155155
def build_gas(state):
156156
aabb = np.asarray([[-1.5, -1.5, -1.5, 1.5, 1.5, 1.5]], dtype=np.float32)
157-
build_input = ox.BuildInputCustomPrimitiveArray(aabb_buffers=aabb, num_sbt_records=1)
158-
state.gas = ox.AccelerationStructure(state.ctx, build_input, compact=True)
157+
build_input = ox.BuildInputCustomPrimitiveArray(aabb_buffers=aabb, flags=[ox.GeometryFlags.DISABLE_ANYHIT])
158+
state.gas = ox.AccelerationStructure(state.ctx, build_input)
159159
state.params.radius = 1.5
160160

161161
def build_ias(state):
162+
return
162163
instances = []
163164
for i in range(transforms.shape[0]):
164-
instance = ox.Instance(traversable=state.gas, instance_id=0, flags=ox.InstanceFlags.NONE,
165+
instance = ox.Instance(traversable=state.gas, instance_id=0, flags=ox.InstanceFlags.DISABLE_ANYHIT,
165166
sbt_offset=sbt_offsets[i], transform=transforms[i])
166167
instances.append(instance)
167168
state.instances = instances
@@ -173,7 +174,7 @@ def build_ias(state):
173174
def create_module(state):
174175
pipeline_opts = ox.PipelineCompileOptions(
175176
uses_motion_blur=False,
176-
traversable_graph_flags=ox.TraversableGraphFlags.ALLOW_SINGLE_LEVEL_INSTANCING,
177+
traversable_graph_flags=ox.TraversableGraphFlags.ALLOW_SINGLE_GAS,
177178
uses_primitive_type_flags=ox.PrimitiveTypeFlags.CUSTOM,
178179
num_payload_values=3,
179180
num_attribute_values=3,
@@ -220,7 +221,7 @@ def create_pipeline(state):
220221
compile_options=state.pipeline_opts,
221222
link_options=link_opts,
222223
program_groups=program_grps,
223-
max_traversable_graph_depth=2)
224+
max_traversable_graph_depth=1)
224225

225226
pipeline.compute_stack_sizes(1, # max_trace_depth
226227
0, # max_cc_depth

optix/sutils/camera.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def _set_direction(self, value):
2929
direction = property(_get_direction, _set_direction)
3030

3131
def uvw_frame(self):
32+
# do not normalize W -- it implies focal length
3233
W = self.look_at - self.eye
3334
wlen = length(W)
35+
assert wlen > 0, (self.eye, self.look_at)
3436

3537
U = normalize(cross(W, self.up))
3638
V = normalize(cross(U, W))

optix/sutils/cuda_output_buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _reallocate_buffers(self):
119119
buffer_type = self.buffer_type
120120

121121
dtype = self.pixel_format
122-
shape = (self.width, self.height)
122+
shape = (self.height, self.width)
123123

124124
if buffer_type is CudaOutputBufferType.CUDA_DEVICE:
125125
self._host_buffer = np.empty(shape=shape, dtype=dtype)

optix/sutils/gl_display.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import ctypes
22
import numpy as np
33

44
import OpenGL.GL as gl
@@ -11,13 +11,13 @@ class GLDisplay:
1111
"""
1212
#version 330 core
1313
14-
layout(location = 0) in vec3 vertexPosition_modelspace;
14+
layout(location = 0) in vec3 position;
1515
out vec2 UV;
1616
1717
void main()
1818
{
19-
gl_Position = vec4(vertexPosition_modelspace,1);
20-
UV = (vec2(vertexPosition_modelspace.x, vertexPosition_modelspace.y)+vec2(1,1))/2.0;
19+
gl_Position = vec4(position, 1);
20+
UV = (vec2(position.x, position.y) + vec2(1,1)) / 2.0;
2121
}
2222
"""
2323

@@ -26,14 +26,13 @@ class GLDisplay:
2626
#version 330 core
2727
2828
in vec2 UV;
29-
out vec3 color;
29+
layout(location=0) out vec4 color;
3030
3131
uniform sampler2D render_tex;
32-
uniform bool correct_gamma;
3332
3433
void main()
3534
{
36-
color = texture(render_tex, UV).xyz;
35+
color = texture(render_tex, UV).xyzw;
3736
}
3837
"""
3938

@@ -90,6 +89,7 @@ def display(self, screen_res_x, screen_res_y, framebuf_res_x, framebuf_res_y, pb
9089
gl.glViewport(0, 0, framebuf_res_x, framebuf_res_y)
9190
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
9291
gl.glUseProgram(self._program)
92+
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
9393

9494
gl.glActiveTexture(gl.GL_TEXTURE0)
9595
gl.glBindTexture(gl.GL_TEXTURE_2D, self._render_tex)
@@ -109,7 +109,6 @@ def display(self, screen_res_x, screen_res_y, framebuf_res_x, framebuf_res_y, pb
109109

110110
image_format = self._image_format
111111
if(image_format == BufferImageFormat.UCHAR4):
112-
# input is assumed to be in srgb since it is only 1 byte per channel in size
113112
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA8, screen_res_x, screen_res_y,
114113
0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, None)
115114
convert_to_srgb = False
@@ -120,22 +119,21 @@ def display(self, screen_res_x, screen_res_y, framebuf_res_x, framebuf_res_y, pb
120119
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA32F, screen_res_x, screen_res_y,
121120
0, gl.GL_RGBA, gl.GL_FLOAT, None)
122121
else:
123-
raise NotImplementedError("Unknown buffer format.")
122+
raise NotImplementedError(f"Unknown image format {image_format}.")
123+
124+
if convert_to_srgb:
125+
gl.glEnable(gl.GL_FRAMEBUFFER_SRGB)
126+
else:
127+
gl.glDisable(gl.GL_FRAMEBUFFER_SRGB)
124128

125129
gl.glBindBuffer(gl.GL_PIXEL_UNPACK_BUFFER, 0)
126130
gl.glUniform1i(self._render_tex_uniforloc, 0)
127131

128132
# 1st attribute buffer : vertices
129133
gl.glEnableVertexAttribArray(0)
130134
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._quad_vertex_buffer)
131-
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
132-
133-
if convert_to_srgb:
134-
gl.glEnable(gl.GL_FRAMEBUFFER_SRGB)
135-
else:
136-
gl.glDisable(gl.GL_FRAMEBUFFER_SRGB)
137-
135+
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, ctypes.c_void_p(0))
138136
gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
139-
140137
gl.glDisableVertexAttribArray(0)
138+
141139
gl.glDisable(gl.GL_FRAMEBUFFER_SRGB)

optix/sutils/gui.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def init_ui(window_title, width, height):
3737
if not window:
3838
raise RuntimeError("Could not initialize Window")
3939

40+
glfw.swap_interval(0)
41+
4042
init_gl()
4143
impl = init_imgui(window)
4244

@@ -62,20 +64,20 @@ def display_stats(state_update_time, render_time, display_time):
6264

6365
cur_time = glfw.get_time()
6466

67+
display_stats.last_update_frames += 1
6568
last_update_time = display_stats.last_update_time or cur_time - 1e-7
6669
last_update_frames = display_stats.last_update_frames
6770
total_subframe_count = display_stats.total_subframe_count
6871

6972
dt = cur_time - last_update_time
70-
display_stats.last_update_frames += 1
7173

7274
do_update = (dt > display_update_min_interval_time) or (total_subframe_count == 0)
7375

7476
if do_update:
7577
fps = last_update_frames / dt
76-
state_ms = 1000.0 * state_update_time
77-
render_ms = 1000.0 * render_time
78-
display_ms = 1000.0 * display_time
78+
state_ms = 1000.0 * state_update_time / last_update_frames
79+
render_ms = 1000.0 * render_time / last_update_frames
80+
display_ms = 1000.0 * display_time / last_update_frames
7981

8082
display_stats.last_update_time = cur_time
8183
display_stats.last_update_frames = 0

optix/sutils/trackball.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44

5-
from optix.sutils.properties import get_member, set_bool, set_int, set_float, set_float3
5+
from optix.sutils.properties import get_member, set_bool, set_float, set_float3
66
from optix.sutils.vecmath import dot, length, normalize
77
from optix.sutils.camera import Camera
88

@@ -28,9 +28,8 @@ def __init__(self):
2828
roll_speed = property(get_member('_roll_speed'), set_float('_roll_speed', 0.5))
2929
latitude = property(get_member('_latitude'), set_float('_latitude', 0.0))
3030
longitude = property(get_member('_longitude'), set_float('_longitude', 0.0))
31-
32-
previous_position_x = property(get_member('_previous_position_x'), set_int('_previous_position_x', 0))
33-
previous_position_y = property(get_member('_previous_position_y'), set_int('_previous_position_y', 0))
31+
previous_position_x = property(get_member('_previous_position_x'), set_float('_previous_position_x', 0))
32+
previous_position_y = property(get_member('_previous_position_y'), set_float('_previous_position_y', 0))
3433

3534
gimbal_lock = property(get_member('_gimbal_lock'), set_bool('_gimbal_lock', False))
3635
perform_tracking = property(get_member('_perform_tracking'), set_bool('_perform_tracking', False))
@@ -64,7 +63,6 @@ def _set_camera(self, camera):
6463
camera = property(_get_camera, _set_camera)
6564

6665
def start_tracking(self, x, y):
67-
x, y = int(x), int(y)
6866
self.previous_position_x = x
6967
self.previous_position_y = y
7068
self.perform_tracking = True
@@ -73,10 +71,12 @@ def update_tracking(self, x, y, canvas_width, canvas_height):
7371
if not self._perform_tracking:
7472
return self.start_tracking(x, y)
7573

76-
x, y = int(x), int(y)
7774
delta_x = x - self.previous_position_x
7875
delta_y = y - self.previous_position_y
7976

77+
if delta_x == 0 and delta_y == 0:
78+
return
79+
8080
self.previous_position_x = x
8181
self.previous_position_y = y
8282

@@ -85,7 +85,7 @@ def update_tracking(self, x, y, canvas_width, canvas_height):
8585

8686
self._update_camera()
8787

88-
if self.gimbal_lock:
88+
if not self.gimbal_lock:
8989
self.reinitialize_orientation_from_camera()
9090
self.camera.up = self.w
9191

@@ -133,6 +133,7 @@ def set_reference_frame(self, u, v, w):
133133
self.v = v
134134
self.w = w
135135

136+
assert length(self.camera.look_at - self.camera.eye) != 0
136137
dir_ws = -normalize(self.camera.look_at - self.camera.eye)
137138

138139
dirx = dot(dir_ws, u)

optix/sutils/vecmath.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ def length(x):
1111
return np.sqrt(dot(x, x))
1212

1313
def normalize(x):
14-
return x/length(x)
15-
14+
l = length(x)
15+
assert l>0, x
16+
return x/l
1617

1718
def ctype_to_dtype(ctype):
1819
_ctype_to_dtype = {

0 commit comments

Comments
 (0)