-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample01.py
133 lines (98 loc) · 3.61 KB
/
sample01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import matplotlib.pyplot as plt
import numpy as np
import sys, getopt
import PIL
#from pynari import *
import pynari as anari
width = 1024
height = 768
cam_pos = [0.0, 0.0, 0.0]
cam_up = [0.0, 1.0, 0.0]
cam_view = [0.1, 0.0, 1.0]
vertex = np.array([
-1.0, -1.0, 3.0,
-1.0, 1.0, 3.0,
1.0, -1.0, 3.0,
0.1, 0.1, 0.3
], dtype = np.float32)
color = np.array([
0.9, 0.5, 0.5, 1.0,
0.8, 0.8, 0.8, 1.0,
0.8, 0.8, 0.8, 1.0,
0.5, 0.9, 0.5, 1.0
], dtype = np.float32)
index = np.array([
0, 1, 2,
1, 2, 3
], dtype = np.uint32)
#library = anariLoadLibrary('default', status_handle)
device = anari.newDevice('default')
camera = device.newCamera('perspective')
#anariSetParameter(device, camera, 'aspect', ANARI_FLOAT32, width/height)
camera.setParameter('aspect', anari.FLOAT32, width/height)
#anariSetParameter(device, camera, 'position', ANARI_FLOAT32_VEC3, cam_pos)
camera.setParameter('position',anari.FLOAT32_VEC3, cam_pos)
#anariSetParameter(device, camera, 'direction', ANARI_FLOAT32_VEC3, cam_view)
camera.setParameter('direction',anari.float3,cam_view)
#anariSetParameter(device, camera, 'up', ANARI_FLOAT32_VEC3, cam_up)
camera.setParameter('up',anari.float3,cam_up)
#anariCommitParameters(device, camera)
camera.commitParameters()
world = device.newWorld()
mesh = device.newGeometry('triangle')
array = device.newArray(anari.FLOAT32_VEC3,vertex)
mesh.setParameter('vertex.position', anari.ARRAY, array)
array = device.newArray(anari.FLOAT32_VEC4, color)
mesh.setParameter('vertex.color', anari.ARRAY, array)
array = device.newArray(anari.UINT32_VEC3 , index)
mesh.setParameter('primitive.index', anari.ARRAY, array)
mesh.commitParameters()
material = device.newMaterial('matte')
material.commitParameters()
surface = device.newSurface()
surface.setParameter('geometry', anari.GEOMETRY, mesh)
surface.setParameter('material', anari.MATERIAL, material)
surface.commitParameters()
world.setParameterArray('surface', anari.SURFACE, [ surface ])
light = device.newLight('directional')
light.setParameter('direction', anari.float3, (0,0,1))
light.setParameter('irradiance', anari.float, 1)
light.commitParameters()
array = device.newArray(anari.LIGHT, [light])
world.setParameter('light', anari.ARRAY1D, array)
world.commitParameters()
# background gradient: use an image of 1 pixel wide and 2 pixels high
bg_values = np.array(((.9,.9,.9,1.),(.15,.25,.8,1.)), dtype=np.float32).reshape((4,1,2))
bg_gradient = device.newArray(anari.float4, bg_values)
renderer = device.newRenderer('default')
renderer.setParameter('pixelSamples', anari.INT32, 128)
renderer.setParameter('background', anari.ARRAY, bg_gradient)
renderer.setParameter('ambientRadiance',anari.FLOAT32, .3)
renderer.commitParameters()
frame = device.newFrame()
frame.setParameter('size', anari.uint2, [width, height])
frame.setParameter('channel.color', anari.DATA_TYPE, anari.UFIXED8_VEC4)
#frame.setParameter('channel.color', anari.DATA_TYPE, anari.FLOAT32_VEC4)
frame.setParameter('renderer', anari.OBJECT, renderer)
frame.setParameter('camera', anari.OBJECT, camera)
frame.setParameter('world', anari.OBJECT, world)
frame.commitParameters()
frame.render()
fb_color = frame.get('channel.color')
pixels = np.array(fb_color)#.reshape([height, width, 4])
out_file_name = ''
args = sys.argv[1:]
opts, args = getopt.getopt(args,"ho:",["help","output="])
for opt,arg in opts:
if opt == '-h':
printf('sample01.py [-o outfile.jpg]')
sys.exit(0)
elif opt == '-o':
out_file_name = arg
if out_file_name == '':
plt.imshow(pixels)
plt.gca().invert_yaxis()
plt.show()
else:
im = PIL.Image.fromarray(pixels)
im.convert('RGB').save(out_file_name)