From 759db92cf135aba21f0cbd17073d2f2e294fdf4d Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 12 May 2023 15:06:13 -0500 Subject: add nice builtin shaders --- honey/asset/shader.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ honey/ecs/script.lua | 22 ++++++++++++++++++++++ main.lua | 28 +++++++++++++++++++++++++--- palette.png | Bin 0 -> 36541 bytes starfield.png | Bin 0 -> 116815 bytes starfield2.png | Bin 0 -> 88326 bytes 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 palette.png create mode 100644 starfield.png create mode 100644 starfield2.png diff --git a/honey/asset/shader.lua b/honey/asset/shader.lua index a23c17b..d1fecc4 100644 --- a/honey/asset/shader.lua +++ b/honey/asset/shader.lua @@ -140,4 +140,47 @@ clearCache = function() end +--===== builtin shaders =====-- + +builtin["builtin.basic3d.vert"] = [[ + #version 410 core + layout (location = 0) in vec3 in_position; + layout (location = 1) in vec3 in_normal; + layout (location = 2) in vec2 in_texture; + + uniform mat4 model; + uniform mat4 view; + uniform mat4 projection; + + out vec3 position; + out vec3 normal; + out vec2 tex; + + void main() + { + gl_Position = projection * view * model * vec4(in_position, 1.0); + + position = in_position; + normal = in_normal; + tex = in_texture; + } +]] + +builtin["builtin.flat.frag"] = [[ + #version 410 core + + out vec4 frag_color; + + in vec3 position; + in vec3 normal; + in vec2 tex; + + uniform sampler2D surface; + + void main() + { + frag_color = texture(surface, tex); + } +]] + return module diff --git a/honey/ecs/script.lua b/honey/ecs/script.lua index 46af4fa..a9b7644 100644 --- a/honey/ecs/script.lua +++ b/honey/ecs/script.lua @@ -1,3 +1,5 @@ +local glfw = honey.glfw + local ecs = require 'honey.ecs.ecs' local node = require 'honey.ecs.node' @@ -27,6 +29,26 @@ dispatch = function(db, msg, data) end end +--===== bind window events to script handlers =====-- + +bindEvents = function(window, db) + glfw.SetFramebufferSizeCallback(window, function(w, width, height) + dispatch(db, "onFramebufferSize", {window=w, width=width, height=height}) + end) + + glfw.SetKeyCallback(window, function(w, key, scancode, action, mods) + dispatch(db, "onKey", {window=w, key=key, scancode=scancode, action=action, mods=mods}) + end) + + glfw.SetCursorPosCallback(window, function(w, xpos, ypos) + dispatch(db, "onCursorPos", {window=w, xpos=xpos, ypos=ypos}) + end) + + glfw.SetMouseButtonCallback(window, function(w, button, action, mods) + dispatch(db, "onMouseButton", {window=w, button=button, action=action, mods=mods}) + end) +end + --===== script system =====-- local script = ecs.System("script", function(db, dt, params) diff --git a/main.lua b/main.lua index c251071..60cc174 100644 --- a/main.lua +++ b/main.lua @@ -26,6 +26,26 @@ db:createEntityWithComponents{ }, } +-- skybox +db:createEntityWithComponents{ + node = { + matrix = Mat4() + :identity(), + }, + renderMesh = { + mesh = { + filename = "assets/skybox.obj", + index = 1, + }, + uniforms = { + textures = { + surface = { filename = "starfield2.png" }, + }, + }, + shader = { vertex = "builtin.basic3d.vert", fragment = "builtin.flat.frag" }, + }, +} + -- mesh db:createEntityWithComponents{ node = { @@ -37,15 +57,17 @@ db:createEntityWithComponents{ filename = "assets/dodecahedron.obj", index = 1, }, - textures = { - ourTexture = { filename = "assets/green-grass.jpg" }, + uniforms = { + textures = { + ourTexture = { filename = "assets/green-grass.jpg" }, + }, }, shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, }, } honey.loop(function(dt) - gl.ClearColor(0.2, 0.4, 1.0, 1.0) + gl.ClearColor(0xa/0xff, 0x10/0xff, 0x1f/0xff, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT + gl.STENCIL_BUFFER_BIT) systems:update(dt) end) diff --git a/palette.png b/palette.png new file mode 100644 index 0000000..52a1cbd Binary files /dev/null and b/palette.png differ diff --git a/starfield.png b/starfield.png new file mode 100644 index 0000000..f67fc93 Binary files /dev/null and b/starfield.png differ diff --git a/starfield2.png b/starfield2.png new file mode 100644 index 0000000..c6a4853 Binary files /dev/null and b/starfield2.png differ -- cgit v1.2.1