forked from smcameron/open-simplex-noise-in-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensimplex-lua.lua
More file actions
55 lines (45 loc) · 1.37 KB
/
opensimplex-lua.lua
File metadata and controls
55 lines (45 loc) · 1.37 KB
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
local ffi = require("ffi")
ffi.cdef[[
typedef struct osn_context osn_context;
// Cria e libera contexto
int open_simplex_noise(int64_t seed, osn_context **ctx);
void open_simplex_noise_free(osn_context *ctx);
// Funções de ruído
double open_simplex_noise2(const osn_context *ctx, double x, double y);
double open_simplex_noise3(const osn_context *ctx, double x, double y, double z);
double open_simplex_noise4(const osn_context *ctx, double x, double y, double z, double w);
]]
-- Ajuste o nome da lib conforme o SO
local lib = ffi.load("open_simplex") -- Ex: "libopen_simplex.so" no Linux
local M = {}
function M.new(seed)
local ctx_ptr = ffi.new("osn_context*[1]")
local ret = lib.open_simplex_noise(seed, ctx_ptr)
if ret ~= 0 then
error("Failed to create OpenSimplex context")
end
local obj = {
ctx = ctx_ptr[0]
}
setmetatable(obj, {
__index = M,
__gc = function(self)
if self.ctx ~= nil then
lib.open_simplex_noise_free(self.ctx)
self.ctx = nil
end
end
})
return obj
end
-- Funções de ruído
function M:noise2(x, y)
return lib.open_simplex_noise2(self.ctx, x, y)
end
function M:noise3(x, y, z)
return lib.open_simplex_noise3(self.ctx, x, y, z)
end
function M:noise4(x, y, z, w)
return lib.open_simplex_noise4(self.ctx, x, y, z, w)
end
return M