-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.go
97 lines (81 loc) · 3.3 KB
/
engine.go
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
// Encapsulate all the Futhark stuff.
package main
// #include "engine.h"
// #include "stdlib.h"
// #cgo !darwin LDFLAGS: -lOpenCL -lm
// #cgo darwin LDFLAGS: -framework OpenCL
import "C"
import (
"os"
"unsafe"
)
type Engine struct {
cfg *C.struct_futhark_context_config
ctx *C.struct_futhark_context
world *C.struct_futhark_opaque_world
Frame unsafe.Pointer
screenX C.int32_t
screenY C.int32_t
}
func NewEngine(screenX, screenY int) Engine {
cfg := C.futhark_context_config_new()
C.futhark_context_config_set_device(cfg, C.CString(os.Getenv("OPENCL_DEVICE")))
ctx := C.futhark_context_new(cfg)
var world *C.struct_futhark_opaque_world
C.futhark_entry_empty_world(ctx, &world)
frame := C.malloc(C.ulong(screenX * screenY * 4))
return Engine{
cfg, ctx, world, frame, C.int32_t(screenX), C.int32_t(screenY),
}
}
func (g *Engine) Free() {
C.free(g.Frame)
C.futhark_context_config_free(g.cfg)
C.futhark_free_opaque_world(g.ctx, g.world)
C.futhark_context_free(g.ctx)
}
func (g *Engine) AddSphere(x, y, z, radius float32, colour uint32, shine float32) {
defer C.futhark_free_opaque_world(g.ctx, g.world)
var i C.int32_t
C.futhark_entry_add_sphere(g.ctx, &g.world, &i, g.world, C.float(x), C.float(y), C.float(z), C.float(radius), C.int32_t(colour), C.float(shine))
}
func (g *Engine) SetSphereRadius(i int32, radius float32) {
defer C.futhark_free_opaque_world(g.ctx, g.world)
C.futhark_entry_set_sphere_radius(g.ctx, &g.world, g.world, C.int32_t(i), C.float(radius))
}
func (g *Engine) SetSpherePositions(xs []float32, ys []float32, zs []float32) {
defer C.futhark_free_opaque_world(g.ctx, g.world)
xs_fut := C.futhark_new_f32_1d(g.ctx, (*C.float)(unsafe.Pointer(&xs[0])), C.int32_t(len(xs)))
defer C.futhark_free_f32_1d(g.ctx, xs_fut)
ys_fut := C.futhark_new_f32_1d(g.ctx, (*C.float)(unsafe.Pointer(&ys[0])), C.int32_t(len(ys)))
defer C.futhark_free_f32_1d(g.ctx, ys_fut)
zs_fut := C.futhark_new_f32_1d(g.ctx, (*C.float)(unsafe.Pointer(&zs[0])), C.int32_t(len(zs)))
defer C.futhark_free_f32_1d(g.ctx, zs_fut)
C.futhark_entry_set_sphere_positions(g.ctx, &g.world,
g.world, xs_fut, ys_fut, zs_fut)
}
func (g *Engine) AddPlane(pos_x, pos_y, pos_z, norm_x, norm_y, norm_z float32, colour uint32, shine float32) {
defer C.futhark_free_opaque_world(g.ctx, g.world)
var i C.int32_t
C.futhark_entry_add_plane(g.ctx, &g.world, &i,
g.world,
C.float(pos_x), C.float(pos_y), C.float(pos_z),
C.float(norm_x), C.float(norm_y), C.float(norm_z),
C.int32_t(colour), C.float(shine))
}
func (g *Engine) AddLight(x, y, z float32, colour uint32, intensity float32) {
defer C.futhark_free_opaque_world(g.ctx, g.world)
C.futhark_entry_add_light(g.ctx, &g.world, g.world,
C.float(x), C.float(y), C.float(z), C.int32_t(colour),
C.float(intensity))
}
func (g *Engine) Render(fov int, eye_pos_x, eye_pos_y, eye_pos_z, eye_dir_a, eye_dir_b float32, ambient uint32, ambient_intensity float32, limit int) {
var frame_fut *C.struct_futhark_i32_2d
C.futhark_entry_render(g.ctx, &frame_fut, g.world,
g.screenX, g.screenY, C.int32_t(fov),
C.float(eye_pos_x), C.float(eye_pos_y), C.float(eye_pos_z),
C.float(eye_dir_a), C.float(eye_dir_b),
C.int32_t(ambient), C.float(ambient_intensity), C.int32_t(limit))
defer C.futhark_free_i32_2d(g.ctx, frame_fut)
C.futhark_values_i32_2d(g.ctx, frame_fut, (*C.int32_t)(g.Frame))
}