summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-23 22:16:24 -0500
committersanine <sanine.not@pm.me>2022-08-23 22:16:24 -0500
commit709e1b6e1ce86f8da4fc136747fcefbc6c6057bd (patch)
tree8f4da8c27526fb056a340a9d2948fdace8341ece
parent3afbf2a13b2dada445fb667bf25600407fea480a (diff)
add basic 3d perspective
-rw-r--r--demo/.honey.lua.swobin0 -> 20480 bytes
-rw-r--r--demo/honey.lua42
-rw-r--r--src/glm/CMakeLists.txt2
-rw-r--r--src/glm/camera.c30
-rw-r--r--src/glm/glm.c26
-rw-r--r--src/glm/glm.h9
-rw-r--r--src/glm/mat4.c26
-rw-r--r--src/glm/transform.c29
-rw-r--r--src/glm/vec3.c50
-rw-r--r--src/logging/logging.c1
10 files changed, 203 insertions, 12 deletions
diff --git a/demo/.honey.lua.swo b/demo/.honey.lua.swo
new file mode 100644
index 0000000..73c5d1d
--- /dev/null
+++ b/demo/.honey.lua.swo
Binary files differ
diff --git a/demo/honey.lua b/demo/honey.lua
index 822877f..9d007e5 100644
--- a/demo/honey.lua
+++ b/demo/honey.lua
@@ -30,14 +30,17 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
-uniform mat4 transform;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
- gl_Position = transform * vec4(aPos, 1.0);
+ gl_Position = projection * view * model * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}
@@ -144,6 +147,28 @@ gl.UseProgram(shader)
gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0)
+--====== matrices ======--
+
+local model = honey.glm.mat4()
+honey.glm.mat4_identity(model)
+local axis = honey.glm.vec3()
+honey.glm.vec3_set(axis, 0, 1.0)
+honey.glm.vec3_set(axis, 1, 0.0)
+honey.glm.vec3_set(axis, 2, 0.0)
+honey.glm.rotate(model, math.rad(-55), axis)
+
+local view = honey.glm.mat4()
+honey.glm.mat4_identity(view)
+local translation = honey.glm.vec3()
+honey.glm.vec3_set(translation, 0, 0.0)
+honey.glm.vec3_set(translation, 1, 0.0)
+honey.glm.vec3_set(translation, 2, -3.0)
+honey.glm.translate(view, translation)
+
+local projection = honey.glm.mat4()
+honey.glm.perspective(math.rad(45), 800/600, 0.1, 100, projection)
+
+
--====== main loop ======--
local transform = honey.glm.mat4()
@@ -156,11 +181,14 @@ while not window.shouldClose(w) do
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.UseProgram(shader)
- honey.glm.mat4_identity(transform)
- local time = window.getTime()
- honey.glm.rotate_z(transform, time, transform)
- local transformLocation = gl.GetUniformLocation(shader, 'transform')
- gl.UniformMatrix4fv(transformLocation, false, transform)
+
+ local modelL = gl.GetUniformLocation(shader, 'model')
+ local viewL = gl.GetUniformLocation(shader, 'view')
+ local projectionL = gl.GetUniformLocation(shader, 'projection')
+
+ gl.UniformMatrix4fv(modelL, false, model)
+ gl.UniformMatrix4fv(viewL, false, view)
+ gl.UniformMatrix4fv(projectionL, false, projection)
gl.BindVertexArray(vertexArray)
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0)
diff --git a/src/glm/CMakeLists.txt b/src/glm/CMakeLists.txt
index 962dbbd..3f0dada 100644
--- a/src/glm/CMakeLists.txt
+++ b/src/glm/CMakeLists.txt
@@ -2,6 +2,8 @@ project(honey_engine)
target_sources(honey PUBLIC
${CMAKE_CURRENT_LIST_DIR}/glm.c
+ ${CMAKE_CURRENT_LIST_DIR}/vec3.c
${CMAKE_CURRENT_LIST_DIR}/mat4.c
${CMAKE_CURRENT_LIST_DIR}/transform.c
+ ${CMAKE_CURRENT_LIST_DIR}/camera.c
)
diff --git a/src/glm/camera.c b/src/glm/camera.c
new file mode 100644
index 0000000..7180b77
--- /dev/null
+++ b/src/glm/camera.c
@@ -0,0 +1,30 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#include <cglm/cglm.h>
+#include "util/util.h"
+
+
+int perspective(lua_State *L);
+
+
+void setup_camera(lua_State *L, int glm_tbl)
+{
+ int tbl = hs_create_table(L,
+ hs_str_cfunc("perspective", perspective),
+ );
+
+ append_table(L, glm_tbl, tbl);
+ lua_pop(L, 1);
+}
+
+
+int perspective(lua_State *L)
+{
+ lua_Number fov, aspect, near, far;
+ void *dest_ptr;
+ hs_parse_args(L, hs_num(fov), hs_num(aspect), hs_num(near), hs_num(far), hs_user(dest_ptr));
+ mat4 *dest = dest_ptr;
+
+ glm_perspective(fov, aspect, near, far, *dest);
+ return 0;
+}
diff --git a/src/glm/glm.c b/src/glm/glm.c
index 786fcf0..525a029 100644
--- a/src/glm/glm.c
+++ b/src/glm/glm.c
@@ -5,9 +5,29 @@
void setup_glm(lua_State *L, int honey_index)
{
lua_createtable(L, 0, 1);
- int glm_index = lua_gettop(L);
+ int glm_tbl = lua_gettop(L);
+
+ setup_vec3(L, glm_tbl);
+ setup_mat4(L, glm_tbl);
+
+ setup_transform(L, glm_tbl);
+ setup_camera(L, glm_tbl);
- setup_mat4(L, glm_index);
- setup_transform(L, glm_index);
lua_setfield(L, honey_index, "glm");
}
+
+
+void array_get(lua_State *L, int max, float *array, int index)
+{
+ if (index < 0 || index >= max)
+ hs_throw_error(L, "index %d is out of range [0-%d]", index, max-1);
+ lua_pushnumber(L, array[index]);
+}
+
+
+void array_set(lua_State *L, int max, float *array, int index, float value)
+{
+ if (index < 0 || index >= max)
+ hs_throw_error(L, "index %d is out of range [0-%d]", index, max-1);
+ array[index] = value;
+}
diff --git a/src/glm/glm.h b/src/glm/glm.h
index 2bf803b..0667179 100644
--- a/src/glm/glm.h
+++ b/src/glm/glm.h
@@ -4,7 +4,12 @@
#include <lua.h>
void setup_glm(lua_State *L, int honey_index);
-void setup_mat4(lua_State *L, int glm_index);
-void setup_transform(lua_State *L, int glm_index);
+void setup_vec3(lua_State *L, int glm_tbl);
+void setup_mat4(lua_State *L, int glm_tbl);
+void setup_transform(lua_State *L, int glm_tbl);
+void setup_camera(lua_State *L, int glm_tbl);
+
+void array_get(lua_State *L, int max, float *array, int index);
+void array_set(lua_State *L, int max, float *array, int index, float value);
#endif
diff --git a/src/glm/mat4.c b/src/glm/mat4.c
index f98cdfb..02ef047 100644
--- a/src/glm/mat4.c
+++ b/src/glm/mat4.c
@@ -2,14 +2,19 @@
#include <honeysuckle.h>
#include <cglm/cglm.h>
#include "util/util.h"
+#include "glm.h"
int mat4_create(lua_State *L);
+int mat4_set(lua_State *L);
+int mat4_get(lua_State *L);
int mat4_identity(lua_State *L);
void setup_mat4(lua_State *L, int glm_tbl)
{
int tbl = hs_create_table(L,
hs_str_cfunc("mat4", mat4_create),
+ hs_str_cfunc("mat4_set", mat4_set),
+ hs_str_cfunc("mat4_get", mat4_get),
hs_str_cfunc("mat4_identity", mat4_identity),
);
@@ -25,6 +30,27 @@ int mat4_create(lua_State *L)
}
+int mat4_set(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index), hs_num(value));
+ array_set(L, 16, ptr, index, value);
+ return 0;
+}
+
+
+int mat4_get(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ hs_parse_args(L, hs_user(ptr), hs_int(index));
+ array_get(L, 16, ptr, index);
+ return 1;
+}
+
+
int mat4_identity(lua_State *L)
{
void *ptr;
diff --git a/src/glm/transform.c b/src/glm/transform.c
index 4dce595..daf75a5 100644
--- a/src/glm/transform.c
+++ b/src/glm/transform.c
@@ -4,12 +4,16 @@
#include "util/util.h"
+int translate(lua_State *L);
+int rotate(lua_State *L);
int rotate_z(lua_State *L);
void setup_transform(lua_State *L, int glm_tbl)
{
int tbl = hs_create_table(L,
+ hs_str_cfunc("translate", translate),
+ hs_str_cfunc("rotate", rotate),
hs_str_cfunc("rotate_z", rotate_z),
);
@@ -18,6 +22,31 @@ void setup_transform(lua_State *L, int glm_tbl)
}
+int translate(lua_State *L)
+{
+ void *m_ptr, *v_ptr;
+ hs_parse_args(L, hs_user(m_ptr), hs_user(v_ptr));
+ mat4 *m = m_ptr;
+ vec3 *v = v_ptr;
+
+ glm_translate(*m, *v);
+ return 0;
+}
+
+
+int rotate(lua_State *L)
+{
+ void *m_ptr, *axis_ptr;
+ lua_Number angle;
+ hs_parse_args(L, hs_user(m_ptr), hs_num(angle), hs_user(axis_ptr));
+ mat4 *m = m_ptr;
+ vec3 *axis = axis_ptr;
+
+ glm_rotate(*m, angle, *axis);
+ return 0;
+}
+
+
int rotate_z(lua_State *L)
{
void *src_ptr, *dest_ptr;
diff --git a/src/glm/vec3.c b/src/glm/vec3.c
new file mode 100644
index 0000000..483f5bd
--- /dev/null
+++ b/src/glm/vec3.c
@@ -0,0 +1,50 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#include <cglm/cglm.h>
+#include "util/util.h"
+#include "glm.h"
+
+int vec3_create(lua_State *L);
+int vec3_set(lua_State *L);
+int vec3_get(lua_State *L);
+
+void setup_vec3(lua_State *L, int glm_tbl)
+{
+ int tbl = hs_create_table(L,
+ hs_str_cfunc("vec3", vec3_create),
+ hs_str_cfunc("vec3_set", vec3_set),
+ hs_str_cfunc("vec3_get", vec3_get),
+ );
+
+ append_table(L, glm_tbl, tbl);
+ lua_pop(L, 1);
+}
+
+
+int vec3_create(lua_State *L)
+{
+ lua_newuserdata(L, 3*sizeof(float));
+ return 1;
+}
+
+
+int vec3_set(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index), hs_num(value));
+ array_set(L, 3, ptr, index, value);
+ return 0;
+}
+
+
+int vec3_get(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index));
+ array_get(L, 3, ptr, index);
+ return 1;
+}
diff --git a/src/logging/logging.c b/src/logging/logging.c
index 0dcf47b..111d524 100644
--- a/src/logging/logging.c
+++ b/src/logging/logging.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdarg.h>
+#include <lua.h>
#include "logging/logging.h"
int _honey_log_level = HONEY_WARN;