diff options
-rw-r--r-- | demo/Camera.lua | 15 | ||||
-rw-r--r-- | demo/FPSCamera.lua | 22 | ||||
-rw-r--r-- | demo/MeshInstance.lua | 6 | ||||
-rw-r--r-- | demo/ScreenQuad.lua | 2 | ||||
-rw-r--r-- | demo/Shader.lua | 53 | ||||
-rw-r--r-- | demo/SpatialShader.lua | 42 | ||||
-rw-r--r-- | demo/main.lua | 100 | ||||
-rw-r--r-- | src/glm_bindings.c | 13 | ||||
-rw-r--r-- | src/glm_bindings.h | 4 | ||||
-rw-r--r-- | src/glm_mat4_bindings.c | 41 | ||||
-rw-r--r-- | src/glm_vec3_bindings.c | 27 | ||||
-rw-r--r-- | src/honey.c | 18 | ||||
-rw-r--r-- | src/input.c | 17 | ||||
-rw-r--r-- | src/mesh.c | 12 | ||||
-rw-r--r-- | src/primitives.c | 4 | ||||
-rw-r--r-- | src/scene_tree_node.c | 103 | ||||
-rw-r--r-- | src/shader.c | 15 | ||||
-rw-r--r-- | src/texture.c | 18 |
18 files changed, 320 insertions, 192 deletions
diff --git a/demo/Camera.lua b/demo/Camera.lua index de14b6c..fda23f2 100644 --- a/demo/Camera.lua +++ b/demo/Camera.lua @@ -1,15 +1,11 @@ -local Vector = require('Vector') -local Matrix = require('Matrix') -local Node = require('Node') - local Camera = {} Camera.prototype = {} -setmetatable(Camera.prototype, { __index = Node.prototype }) +setmetatable(Camera.prototype, { __index = honey.nodeMetatable.__index }) Camera.prototype.updateView = function(self) self.basis = self.transform:basis() - Matrix.Mat4.look(self.position, self.basis.z, self.basis.y, self.view) + self.view:look(self.position, self.basis.z, self.basis.y) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -20,12 +16,13 @@ Camera.mt.__index = Camera.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Camera.new = function(parent, position, rotation, scale, fov, aspect, near, far) - local camera = Node.new(parent, position, rotation, scale) + local camera = honey.node(parent, position, rotation, scale) setmetatable(camera, Camera.mt) - camera.view = Matrix.Mat4.new() + camera.view = honey.glm.mat4() camera:updateView() - camera.projection = Matrix.Mat4.perspective(fov, aspect, near, far) + camera.projection = honey.glm.mat4() + camera.projection:perspective(fov, aspect, near, far) return camera end diff --git a/demo/FPSCamera.lua b/demo/FPSCamera.lua index 5a9fabf..a1ab00f 100644 --- a/demo/FPSCamera.lua +++ b/demo/FPSCamera.lua @@ -1,11 +1,9 @@ -local Vector = require('Vector') -local Matrix = require('Matrix') local Camera = require('Camera') local camera = Camera.new(nil, - Vector.Vec3.new(), - Vector.Vec3.new(), - Vector.Vec3.new{1,1,1}, + honey.glm.vec3(), + honey.glm.vec3(), + honey.glm.vec3{1,1,1}, math.rad(90), 640/480, 0.1, 1000) camera.pitch = 0 @@ -16,7 +14,7 @@ camera.sensitivity = 0.1 camera.movement_speed = 1 function camera:update(dt) - movement = Vector.Vec3.new() + movement = honey.glm.vec3() if honey.input.key.is_down(honey.input.key.w) then movement:add(self.basis.z, movement) end @@ -30,14 +28,14 @@ function camera:update(dt) movement:sub(self.basis.x, movement) end - movement:setAt(1, 0) + movement:set(1, 0) movement:normalize() if honey.input.key.is_down(honey.input.key.left_shift) then - movement:add(Vector.Vec3.Y_UNIT, movement) + movement:add(honey.glm.UNIT_Y, movement) end if honey.input.key.is_down(honey.input.key.left_control) then - movement:sub(Vector.Vec3.Y_UNIT, movement) + movement:sub(honey.glm.UNIT_Y, movement) end movement:muls(self.movement_speed*dt, movement) self:translate(movement) @@ -63,11 +61,11 @@ honey.input.mouse.bind_movement( if camera.pitch > 89.9 then camera.pitch = 89.9 end if camera.pitch < -89.9 then camera.pitch = -89.9 end - camera.rotation:setAt(0, math.rad(camera.pitch)) - camera.rotation:setAt(1, math.rad(camera.yaw)) + camera.rotation:set(0, math.rad(camera.pitch)) + camera.rotation:set(1, math.rad(camera.yaw)) end ) -honey.input.mouse.set_mode(honey.input.mouse.mode.disabled) +honey.input.mouse.set_mode(honey.input.mouse.mode.captured) return camera diff --git a/demo/MeshInstance.lua b/demo/MeshInstance.lua index b12edba..eceec5d 100644 --- a/demo/MeshInstance.lua +++ b/demo/MeshInstance.lua @@ -1,9 +1,7 @@ -local Node = require('Node') - local MeshInstance = {} MeshInstance.prototype = {} -setmetatable(MeshInstance.prototype, { __index = Node.prototype}) +setmetatable(MeshInstance.prototype, { __index = honey.nodeMetatable.__index }) MeshInstance.prototype.draw = function(self, camera, shader) local shader = shader or self.shader @@ -20,7 +18,7 @@ MeshInstance.mt.__index = MeshInstance.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MeshInstance.new = function(parent, position, rotation, scale, mesh, shader) - local meshinstance = Node.new(parent, position, rotation, scale) + local meshinstance = honey.node(parent, position, rotation, scale) meshinstance.mesh = mesh meshinstance.shader = shader diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua index ba9c27e..4cc247f 100644 --- a/demo/ScreenQuad.lua +++ b/demo/ScreenQuad.lua @@ -31,7 +31,7 @@ void main() color = vec4(texture(tex, UV).xyz, 1); } ]] -ScreenQuad.shader = honey.shader.new(vertexShader, fragmentShader) +ScreenQuad.shader = honey.shader(vertexShader, fragmentShader) ScreenQuad.tex = honey.texture.new() ScreenQuad.depth = honey.texture.new() diff --git a/demo/Shader.lua b/demo/Shader.lua deleted file mode 100644 index 494bd53..0000000 --- a/demo/Shader.lua +++ /dev/null @@ -1,53 +0,0 @@ -local Shader = {} - -Shader.prototype = {} - -Shader.prototype.setInteger = function(self, uniform, integer) - honey.shader.set_int(self.program, uniform, integer) -end - -Shader.prototype.setFloat = function(self, uniform, float) - honey.shader.set_float(self.program, uniform, float) -end - -Shader.prototype.setVec3 = function(self, uniform, vector) - honey.shader.set_vec3(self.program, uniform, vector.array) -end - -Shader.prototype.setVec4 = function(self, uniform, vector) - honey.shader.set_vec4(self.program, uniform, vector.array) -end - -Shader.prototype.setMat3 = function(self, uniform, matrix) - honey.shader.set_mat3(self.program, uniform, matrix.array) -end - -Shader.prototype.setMat4 = function(self, uniform, matrix) - honey.shader.set_mat4(self.program, uniform, matrix.array) -end - -Shader.prototype.drawMesh = function(self, mesh) - honey.mesh.draw(mesh.mesh, self.program) -end - -Shader.prototype.delete = function(self) - honey.shader.delete(self.program) -end - --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Shader.mt = {} -Shader.mt.__index = Shader.prototype - --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Shader.new = function(vertexCode, fragmentCode) - local shader = {} - shader.program = honey.shader.new(vertexCode, fragmentCode) - - setmetatable(shader, Shader.mt) - - return shader -end - -return Shader diff --git a/demo/SpatialShader.lua b/demo/SpatialShader.lua index 4a9f979..5f83e59 100644 --- a/demo/SpatialShader.lua +++ b/demo/SpatialShader.lua @@ -1,5 +1,3 @@ -local Shader = require('Shader') - local VertexCode = [[ #version 330 core @@ -42,17 +40,44 @@ void main() local SpatialShader = {} SpatialShader.prototype = {} -setmetatable(SpatialShader.prototype, { __index = Shader.prototype }) + +SpatialShader.prototype.use = function(self) + self.shader:use() +end + +SpatialShader.prototype.setInteger = function(self, name, value) + self.shader:setInteger(name, value) +end + +SpatialShader.prototype.setFloat = function(self, name, value) + self.shader:setFloat(name, value) +end + +SpatialShader.prototype.setVec3 = function(self, name, value) + self.shader:setVec3(name, value) +end + +SpatialShader.prototype.setVec4 = function(self, name, value) + self.shader:setVec4(name, value) +end + +SpatialShader.prototype.setMat3 = function(self, name, value) + self.shader:setMat3(name, value) +end + +SpatialShader.prototype.setMat4 = function(self, name, value) + self.shader:setMat4(name, value) +end SpatialShader.prototype.setCamera = function(self, camera) - self:setMat4('view', camera.view) - self:setMat4('projection', camera.projection) + self.shader:setMat4('view', camera.view) + self.shader:setMat4('projection', camera.projection) end SpatialShader.prototype.drawMesh = function(self, mesh) - self:setMat4('model', mesh.transform) + self.shader:setMat4('model', mesh.transform) honey.texture.use(self.albedo, 0) - honey.mesh.draw(mesh.mesh, self.program) + honey.mesh.draw(mesh.mesh, self.shader) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -63,7 +88,8 @@ SpatialShader.mt.__index = SpatialShader.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SpatialShader.new = function(albedo) - local spatialshader = Shader.new(VertexCode, FragmentCode) + local spatialshader = {} + spatialshader.shader = honey.shader(VertexCode, FragmentCode) spatialshader.albedo = albedo setmetatable(spatialshader, SpatialShader.mt) diff --git a/demo/main.lua b/demo/main.lua index ec0bac1..3382d8d 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -1,22 +1,90 @@ -local root = honey.node(nil, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{1,1,1}) +local FPSCamera = require('FPSCamera') +local SpatialShader = require('SpatialShader') +local ScreenQuad = require('ScreenQuad') +local MeshInstance = require('MeshInstance') +FPSCamera.movement_speed = 5 -local child = honey.node(root, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{1,1,1}) +honey.input.key.bind(honey.input.key.escape, honey.exit) -local v = honey.glm.vec3{1, 2, 3} +local buffer = false +honey.input.key.bind(honey.input.key.f, function(action) if action == 1 then buffer = not buffer end end) -local M = honey.glm.mat4() -M:set(1,3, 1) +local tex = honey.texture.new() +honey.texture.load(tex, 'checkerboard.png', false) -print(M:mul(M)) +local sceneRoot = honey.node(nil, + honey.glm.vec3(), + honey.glm.vec3(), + honey.glm.vec3{1,1,1}) -print(v) -print(honey.glm.vec3{2, 0.001, 0}) -print(honey.glm.vec4{100, 0, 0, 20.44}) +local shader = SpatialShader.new(tex) +local lightDirection = honey.glm.vec3{1,1,1} +lightDirection:normalize() +shader:setVec3('directional_lights[0].direction', lightDirection) +shader:setVec3('directional_lights[0].color', honey.glm.vec3{0,1,0}) +local meshes = honey.mesh.load('Suzanne.obj') +local suzanne = MeshInstance.new(sceneRoot, + honey.glm.vec3{0,0,3}, + honey.glm.vec3{0,math.pi,0}, + honey.glm.vec3{0.5,1,0.5}, + meshes[1], + shader) +local plane = MeshInstance.new(suzanne, + honey.glm.vec3{1,0,0}, + honey.glm.vec3{0,0,0}, + honey.glm.vec3{1,1,1}, + honey.primitives.plane(4,4), + shader) +local plane2 = MeshInstance.new(suzanne, + honey.glm.vec3{5,0,0}, + honey.glm.vec3{0,math.pi,0}, + honey.glm.vec3{1,1,1}, + honey.primitives.plane(4,4), + shader) -v.dot(v, v) +suzanne.update = function(self, dt) + self:rotate('y', dt) +end + +local total_frames = 0 +local total_time = 0 + +honey.window.set_size(640, 480) + +function honey.update(dt) + total_time = total_time + dt + FPSCamera:update(dt) + sceneRoot:updateCascade(dt) + if total_time > 1 then + print('FPS: '..tostring(total_frames/total_time)) + total_time = 0 + total_frames = 0 + end +end + +function draw_suzanne() + sceneRoot:drawCascade(FPSCamera) +end + +function honey.draw() + total_frames = total_frames + 1 + + if buffer then + honey.set_framebuffer(ScreenQuad.fb) + honey.set_viewport_size(480,640) + honey.clear_color(honey.glm.vec4(), true, true, false) + honey.enable_depth_test(true) + draw_suzanne() + + honey.set_framebuffer(0) + honey.set_viewport_size(640, 480) + honey.enable_depth_test(false) + honey.clear_color(honey.glm.vec4{0,0,1,1}, true, false, false) + ScreenQuad:draw() + else + honey.clear_color(honey.glm.vec4{1,1,0,1}, true, true, false) + honey.set_viewport_size(640, 480) + honey.enable_depth_test(true) + draw_suzanne() + end +end diff --git a/src/glm_bindings.c b/src/glm_bindings.c index 6893534..aa40fff 100644 --- a/src/glm_bindings.c +++ b/src/glm_bindings.c @@ -10,8 +10,8 @@ int honey_glm_UNIT_Y_ref = LUA_NOREF; int honey_glm_UNIT_Z_ref = LUA_NOREF; static void create_vec3(lua_State* L, - int x, int y, int z, - int* ref) + int x, int y, int z, + int* ref) { lua_createtable(L, 3, 0); @@ -32,12 +32,14 @@ static void create_vec3(lua_State* L, lua_pop(L, 1); } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + void honey_setup_glm(lua_State* L) { /* vec3 metatable */ honey_lua_create_table (L, 3, - HONEY_TABLE, "__index", 23, + HONEY_TABLE, "__index", 24, HONEY_FUNCTION, "get", honey_glm_array_vec_get, HONEY_FUNCTION, "set", honey_glm_array_vec_set, HONEY_FUNCTION, "copyTo", honey_glm_vec3_copy, @@ -53,6 +55,7 @@ void honey_setup_glm(lua_State* L) HONEY_FUNCTION, "sub", honey_glm_vec3_sub, HONEY_FUNCTION, "subs", honey_glm_vec3_subs, HONEY_FUNCTION, "mul", honey_glm_vec3_mul, + HONEY_FUNCTION, "muls", honey_glm_vec3_muls, HONEY_FUNCTION, "scale", honey_glm_vec3_scale, HONEY_FUNCTION, "scaleAs", honey_glm_vec3_scale_as, HONEY_FUNCTION, "div", honey_glm_vec3_div, @@ -124,7 +127,7 @@ void honey_setup_glm(lua_State* L) honey_lua_create_table (L, 3, - HONEY_TABLE, "__index", 24, + HONEY_TABLE, "__index", 25, HONEY_FUNCTION, "get", honey_glm_array_mat_get, HONEY_FUNCTION, "set", honey_glm_array_mat_set, HONEY_FUNCTION, "copyTo", honey_glm_mat4_copy, @@ -136,6 +139,7 @@ void honey_setup_glm(lua_State* L) HONEY_FUNCTION, "scale", honey_glm_mat4_scale, HONEY_FUNCTION, "det", honey_glm_mat4_det, HONEY_FUNCTION, "inv", honey_glm_mat4_inv, + HONEY_FUNCTION, "basis", honey_glm_mat4_basis, HONEY_FUNCTION, "translate", honey_glm_translate, HONEY_FUNCTION, "translateX", honey_glm_translate_x, HONEY_FUNCTION, "translateY", honey_glm_translate_y, @@ -442,7 +446,6 @@ int honey_glm_array_destroy(lua_State* L) { honey_glm_array* array; honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &array); - printf("DESTROY GLM ARRAY %d", array->type); free(array->data); return 0; } diff --git a/src/glm_bindings.h b/src/glm_bindings.h index b04016e..21d92b9 100644 --- a/src/glm_bindings.h +++ b/src/glm_bindings.h @@ -76,6 +76,8 @@ int honey_glm_vec3_sub(lua_State* L); int honey_glm_vec3_subs(lua_State* L); int honey_glm_vec3_mul(lua_State* L); + +int honey_glm_vec3_muls(lua_State* L); int honey_glm_vec3_scale(lua_State* L); @@ -186,6 +188,8 @@ int honey_glm_mat4_det(lua_State* L); int honey_glm_mat4_inv(lua_State* L); +int honey_glm_mat4_basis(lua_State* L); + int honey_glm_translate(lua_State* L); int honey_glm_translate_x(lua_State* L); diff --git a/src/glm_mat4_bindings.c b/src/glm_mat4_bindings.c index b082a4b..8320dcb 100644 --- a/src/glm_mat4_bindings.c +++ b/src/glm_mat4_bindings.c @@ -197,6 +197,47 @@ int honey_glm_mat4_inv(lua_State* L) return 0; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_glm_mat4_basis(lua_State* L) +{ + honey_glm_array* self; + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &self); + + + lua_createtable(L, 0, 3); + honey_glm_array *x, *y, *z; + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + x = lua_touserdata(L, -1); + lua_setfield(L, -2, "x"); + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + y = lua_touserdata(L, -1); + lua_setfield(L, -2, "y"); + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + z = lua_touserdata(L, -1); + lua_setfield(L, -2, "z"); + + x->data[0] = self->data[0]; + x->data[1] = self->data[1]; + x->data[2] = self->data[2]; + + y->data[0] = self->data[4]; + y->data[1] = self->data[5]; + y->data[2] = self->data[6]; + + z->data[0] = self->data[8]; + z->data[1] = self->data[9]; + z->data[2] = self->data[10]; + + return 1; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Affine Transforms diff --git a/src/glm_vec3_bindings.c b/src/glm_vec3_bindings.c index a8157f2..e6ea407 100644 --- a/src/glm_vec3_bindings.c +++ b/src/glm_vec3_bindings.c @@ -247,6 +247,33 @@ int honey_glm_vec3_mul(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +int honey_glm_vec3_muls(lua_State* L) +{ + honey_glm_array *self, *dest; + float s; + int choice = honey_lua_parse_arguments + (L, 2, + 2, HONEY_USERDATA, &self, HONEY_NUMBER, &s, + 3, HONEY_USERDATA, &self, HONEY_NUMBER, &s, HONEY_USERDATA, &dest); + + if (choice == 0) + dest = self; + else { + if (dest->type != VEC3) + honey_lua_throw_error + (L, "destination vector must be of type VEC3 (%d); got %d instead", + VEC3, dest->type); + } + + dest->data[0] = s * self->data[0]; + dest->data[1] = s * self->data[1]; + dest->data[2] = s * self->data[2]; + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + int honey_glm_vec3_scale(lua_State* L) { honey_glm_array *self, *dest; diff --git a/src/honey.c b/src/honey.c index f58be3e..5f83544 100644 --- a/src/honey.c +++ b/src/honey.c @@ -57,17 +57,17 @@ bool honey_parse_options(honey_options* options, int argc, char** argv) static int honey_lua_clear_color(lua_State* L) { - float* color_array; + honey_glm_array* color_array; bool color, depth, stencil; - honey_lua_parse_arguments(L, 4, + honey_lua_parse_arguments(L, 1, 4, HONEY_USERDATA, &color_array, HONEY_BOOLEAN, &color, HONEY_BOOLEAN, &depth, HONEY_BOOLEAN, &stencil); - float r = color_array[0]; - float g = color_array[1]; - float b = color_array[2]; - float a = color_array[3]; + float r = color_array->data[0]; + float g = color_array->data[1]; + float b = color_array->data[2]; + float a = color_array->data[3]; int clear_flags = 0; if (color) @@ -87,7 +87,7 @@ static int honey_lua_clear_color(lua_State* L) int honey_lua_enable_depth_test(lua_State* L) { bool enable; - honey_lua_parse_arguments(L, 1, HONEY_BOOLEAN, &enable); + honey_lua_parse_arguments(L, 1, 1, HONEY_BOOLEAN, &enable); if (enable) glEnable(GL_DEPTH_TEST); else @@ -98,7 +98,7 @@ int honey_lua_enable_depth_test(lua_State* L) int honey_lua_set_viewport_size(lua_State* L) { int width, height; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_INTEGER, &width, HONEY_INTEGER, &height); glViewport(0,0,width,height); @@ -249,7 +249,7 @@ int honey_get_callback(lua_State* L, char* callback) int honey_set_framebuffer(lua_State* L) { int framebuffer; - honey_lua_parse_arguments(L, 1, HONEY_INTEGER, &framebuffer); + honey_lua_parse_arguments(L, 1, 1, HONEY_INTEGER, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); return 0; } diff --git a/src/input.c b/src/input.c index 3b0b286..ca5fc05 100644 --- a/src/input.c +++ b/src/input.c @@ -194,8 +194,8 @@ int honey_key_bind(lua_State* L) int choice = honey_lua_parse_arguments (L, 2, - 2, HONEY_INTEGER, &key, HONEY_FUNCTION, - 3, HONEY_INTEGER, &key, HONEY_FUNCTION, HONEY_ANY); + 2, HONEY_INTEGER, &key, HONEY_FUNCTION, NULL, + 3, HONEY_INTEGER, &key, HONEY_FUNCTION, NULL, HONEY_ANY, NULL); lua_pushvalue(L, 2); int callback = luaL_ref(L, LUA_REGISTRYINDEX); @@ -282,16 +282,19 @@ int honey_mouse_set_mode(lua_State* L) int honey_mouse_movement_bind(lua_State* L) { - honey_lua_parse_arguments - (L, 1, - 2, HONEY_FUNCTION, HONEY_ANY); + int choice = honey_lua_parse_arguments + (L, 2, + 1, HONEY_FUNCTION, NULL, + 2, HONEY_FUNCTION, NULL, HONEY_ANY, NULL); honey_mouse_movement_unbind(L); /* avoid memory leaks! */ lua_pushvalue(L, 1); honey_mouse_movement_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - honey_mouse_movement_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + if (choice == 1) { + lua_pushvalue(L, 2); + honey_mouse_movement_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + } return 0; } @@ -3,18 +3,18 @@ static int honey_mesh_lua_draw(lua_State* L) { honey_mesh* mesh; - int shader; - honey_lua_parse_arguments(L, 2, + int* shader; + honey_lua_parse_arguments(L, 1, 2, HONEY_USERDATA, &mesh, - HONEY_INTEGER, &shader); - honey_mesh_draw(*mesh, shader); + HONEY_USERDATA, &shader); + honey_mesh_draw(*mesh, *shader); return 0; } static int honey_mesh_lua_delete(lua_State* L) { honey_mesh* mesh; - honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh); + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &mesh); honey_mesh_delete(*mesh); return 0; } @@ -123,7 +123,7 @@ static void process_nodes_recursively(lua_State* L, int honey_mesh_load(lua_State* L) { char* filename; - honey_lua_parse_arguments(L, 1, HONEY_STRING, &filename); + honey_lua_parse_arguments(L, 1, 1, HONEY_STRING, &filename); int n_meshes = 1; diff --git a/src/primitives.c b/src/primitives.c index c2f6c9e..ee68f6a 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -3,7 +3,7 @@ static int honey_mesh_lua_plane(lua_State* L) { float width, height; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_NUMBER, &width, HONEY_NUMBER, &height); @@ -18,7 +18,7 @@ static int honey_mesh_lua_plane(lua_State* L) static int honey_mesh_lua_cube(lua_State* L) { float width, height, depth; - honey_lua_parse_arguments(L, 3, + honey_lua_parse_arguments(L, 1, 3, HONEY_NUMBER, &width, HONEY_NUMBER, &height, HONEY_NUMBER, &depth); diff --git a/src/scene_tree_node.c b/src/scene_tree_node.c index a3ca27e..87618e6 100644 --- a/src/scene_tree_node.c +++ b/src/scene_tree_node.c @@ -16,6 +16,9 @@ void honey_setup_scene_tree(lua_State* L) HONEY_FUNCTION, "scale", honey_node_scale); honey_node_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_node_mt_ref); + lua_setfield(L, -2, "nodeMetatable"); + lua_pushcfunction(L, honey_node_new); lua_setfield(L, -2, "node"); } @@ -42,18 +45,19 @@ int honey_node_new(lua_State* L) lua_rawgeti(L, LUA_REGISTRYINDEX, honey_node_mt_ref); lua_setmetatable(L, -2); - lua_pushvalue(L, 1); - if (!lua_isnil(L, -1)) { + if (lua_istable(L, 1)) { /* add self to parent.children */ - lua_getfield(L, -1, "children"); + lua_getfield(L, 1, "children"); int length = lua_objlen(L, -1); lua_pushinteger(L, length+1); - lua_pushvalue(L, 5); + lua_pushvalue(L, -3); lua_settable(L, -3); lua_pop(L, 1); + + lua_pushvalue(L, 1); + lua_setfield(L, -2, "parent"); } - lua_setfield(L, -2, "parent"); if (position->type != VEC3) honey_lua_throw_error @@ -105,46 +109,35 @@ int honey_node_update_transform(lua_State* L) { honey_lua_parse_arguments(L, 1, 1, HONEY_TABLE, NULL); - /* self.transform:eye() */ - lua_pushcfunction(L, honey_glm_mat4_eye); - lua_getfield(L, 1, "transform"); - lua_pushvalue(L, -1); - honey_lua_pcall(L, 1, 0); + honey_glm_array *transform, *position, *rotation, *parent_transform; - /* self.transform:translate(self.position) */ - lua_pushcfunction(L, honey_glm_translate); - lua_pushvalue(L, 2); - lua_getfield(L, 1, "position"); - honey_lua_pcall(L, 2, 0); + lua_getfield(L, 1, "transform"); + transform = lua_touserdata(L, -1); + lua_pop(L, 1); - /* self.transform:rotateZ(self.rotation:at(2)) */ - lua_pushcfunction(L, honey_glm_rotate_z); - lua_pushvalue(L, 2); lua_getfield(L, 1, "position"); - honey_glm_array* position = lua_touserdata(L, -1); + position = lua_touserdata(L, -1); + lua_pop(L, 1); + + lua_getfield(L, 1, "rotation"); + rotation = lua_touserdata(L, -1); lua_pop(L, 1); - lua_pushnumber(L, position->data[2]); - honey_lua_pcall(L, 2, 0); - /* self.transform:rotateY(self.rotation:at(1)) */ - lua_pushcfunction(L, honey_glm_rotate_y); - lua_pushvalue(L, 2); - lua_pushnumber(L, position->data[1]); - honey_lua_pcall(L, 2, 0); + glm_mat4_identity(transform->data); - /* self.transform:rotateX(self.rotation:at(0)) */ - lua_pushcfunction(L, honey_glm_rotate_x); - lua_pushvalue(L, 2); - lua_pushnumber(L, position->data[0]); - honey_lua_pcall(L, 2, 0); + glm_translate(transform->data, position->data); + + glm_rotate_z(transform->data, rotation->data[2], transform->data); + glm_rotate_y(transform->data, rotation->data[1], transform->data); + glm_rotate_x(transform->data, rotation->data[0], transform->data); lua_getfield(L, 1, "parent"); - if (!lua_isnil(L, -1)) { - lua_pushcfunction(L, honey_glm_mat4_mul); - lua_getfield(L, -2, "transform"); - lua_pushvalue(L, 2); - lua_pushvalue(L, -1); - honey_lua_pcall(L, 3, 0); + if (lua_istable(L, -1)) { + lua_getfield(L, -1, "transform"); + parent_transform = lua_touserdata(L, -1); + lua_pop(L, 1); + + glm_mat4_mul(parent_transform->data, transform->data, transform->data); } lua_pop(L, 1); @@ -191,13 +184,23 @@ int honey_node_update_cascade(lua_State* L) int honey_node_draw_cascade(lua_State* L) { - honey_lua_parse_arguments(L, 1, 1, HONEY_TABLE, NULL); + int* shader; + int choice = honey_lua_parse_arguments + (L, 2, + 2, HONEY_TABLE, NULL, HONEY_TABLE, NULL, + 3, HONEY_TABLE, NULL, HONEY_TABLE, NULL, HONEY_USERDATA, &shader); /* call self.draw if it exists */ lua_getfield(L, 1, "draw"); if (!lua_isnil(L, -1)) { lua_pushvalue(L, 1); - honey_lua_pcall(L, 1, 0); + lua_pushvalue(L, 2); + if (choice == 1) { + lua_pushvalue(L, 3); + honey_lua_pcall(L, 3, 0); + } + else + honey_lua_pcall(L, 2, 0); } else lua_pop(L, 1); @@ -209,7 +212,13 @@ int honey_node_draw_cascade(lua_State* L) lua_rawgeti(L, -1, i+1); lua_pushcfunction(L, honey_node_draw_cascade); lua_pushvalue(L, -2); - honey_lua_pcall(L, 1, 0); + lua_pushvalue(L, 2); + if (choice == 1) { + lua_pushvalue(L, 3); + honey_lua_pcall(L, 3, 0); + } + else + honey_lua_pcall(L, 2, 0); lua_pop(L, 1); } @@ -220,15 +229,17 @@ int honey_node_draw_cascade(lua_State* L) int honey_node_translate(lua_State* L) { - honey_glm_array* v; + honey_glm_array* position, *v; honey_lua_parse_arguments(L, 1, 2, HONEY_TABLE, NULL, HONEY_USERDATA, &v); - lua_pushcfunction(L, honey_glm_vec3_add); - lua_getfield(L, 1, "transform"); - lua_pushvalue(L, 2); - lua_pushvalue(L, -2); - honey_lua_pcall(L, 3, 0); + lua_getfield(L, 1, "position"); + position = lua_touserdata(L, -1); + lua_pop(L, 1); + position->data[0] += v->data[0]; + position->data[1] += v->data[1]; + position->data[2] += v->data[2]; + return 0; } diff --git a/src/shader.c b/src/shader.c index a05bd0c..048cf87 100644 --- a/src/shader.c +++ b/src/shader.c @@ -10,17 +10,20 @@ void honey_setup_shader(lua_State* L) /* honey.shader.prototype */ HONEY_FUNCTION, "use", honey_shader_use, - HONEY_FUNCTION, "set_int", honey_shader_set_int, - HONEY_FUNCTION, "set_float", honey_shader_set_float, - HONEY_FUNCTION, "set_vec3", honey_shader_set_vec3, - HONEY_FUNCTION, "set_vec4", honey_shader_set_vec4, - HONEY_FUNCTION, "set_mat3", honey_shader_set_mat3, - HONEY_FUNCTION, "set_mat4", honey_shader_set_mat4, + HONEY_FUNCTION, "setInteger", honey_shader_set_int, + HONEY_FUNCTION, "setFloat", honey_shader_set_float, + HONEY_FUNCTION, "setVec3", honey_shader_set_vec3, + HONEY_FUNCTION, "setVec4", honey_shader_set_vec4, + HONEY_FUNCTION, "setMat3", honey_shader_set_mat3, + HONEY_FUNCTION, "setMat4", honey_shader_set_mat4, HONEY_FUNCTION, "__gc", honey_shader_delete); honey_shader_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_shader_mt_ref); + lua_setfield(L, -2, "shaderMetatable"); + lua_pushcfunction(L, honey_shader_new); lua_setfield(L, -2, "shader"); } diff --git a/src/texture.c b/src/texture.c index 7388d50..cdd36c4 100644 --- a/src/texture.c +++ b/src/texture.c @@ -11,7 +11,7 @@ static int honey_lua_texture_create(lua_State* L) honey_texture* texture; int width, height; char* type; - honey_lua_parse_arguments(L, 4, + honey_lua_parse_arguments(L, 1, 4, HONEY_USERDATA, &texture, HONEY_STRING, &type, HONEY_INTEGER, &width, @@ -41,7 +41,7 @@ static int honey_lua_texture_load(lua_State* L) { honey_texture* texture; char* texture_path; - honey_lua_parse_arguments(L, 3, + honey_lua_parse_arguments(L, 1, 3, HONEY_USERDATA, &texture, HONEY_STRING, &texture_path); enum honey_texture_result result = honey_texture_load(texture, texture_path); @@ -62,7 +62,7 @@ static int honey_lua_texture_use(lua_State* L) { honey_texture* texture; int texture_unit; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_USERDATA, &texture, HONEY_INTEGER, &texture_unit); honey_texture_use(*texture, texture_unit); @@ -72,6 +72,13 @@ static int honey_lua_texture_use(lua_State* L) static int honey_lua_framebuffer_new(lua_State* L) { honey_texture* draw, *depth; + int width, height; + honey_lua_parse_arguments(L, 1, 4, + HONEY_ANY, NULL, + HONEY_ANY, NULL, + HONEY_INTEGER, &width, + HONEY_INTEGER, &height); + if (lua_isuserdata(L, 1)) draw = lua_touserdata(L, 1); else @@ -82,11 +89,6 @@ static int honey_lua_framebuffer_new(lua_State* L) else depth = NULL; - int width, height; - honey_lua_parse_arguments(L, 4, HONEY_ANY, HONEY_ANY, - HONEY_INTEGER, &width, - HONEY_INTEGER, &height); - unsigned int framebuffer; honey_texture_framebuffer_object_new(&framebuffer, draw, depth, |