diff options
author | sanine-a <sanine.not@pm.me> | 2023-03-22 12:46:23 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-03-22 12:46:23 -0500 |
commit | 92803015adf19848c3f3a30caea889006ad05999 (patch) | |
tree | d3a3a325664ebb3f2b8c214217dce31746a1ae9a | |
parent | 16bcc6daab84373cac0f4125c1580d3cb1261baf (diff) |
tidy up main.lua
-rw-r--r-- | fragment.glsl | 15 | ||||
-rw-r--r-- | honey/init.lua | 22 | ||||
-rw-r--r-- | honey/shader.lua | 15 | ||||
-rw-r--r-- | honey/std.lua | 1 | ||||
-rw-r--r-- | main.lua | 88 | ||||
-rw-r--r-- | vertex.glsl | 21 |
6 files changed, 86 insertions, 76 deletions
diff --git a/fragment.glsl b/fragment.glsl new file mode 100644 index 0000000..27dbbfa --- /dev/null +++ b/fragment.glsl @@ -0,0 +1,15 @@ +#version 410 core
+out vec4 FragColor;
+
+in vec3 position;
+in vec3 normal;
+in vec2 tex;
+
+uniform sampler2D ourTexture;
+
+void main()
+{
+ FragColor = vec4(normal, 1.0f);
+ //FragColor = vec4(tex, 1.0f, 1.0f);
+ //FragColor = texture(ourTexture, TexCoord);
+}
diff --git a/honey/init.lua b/honey/init.lua index c3dda37..6afa324 100644 --- a/honey/init.lua +++ b/honey/init.lua @@ -2,9 +2,9 @@ local glfw = honey.glfw local gl = honey.gl local window = require 'honey.window' -local hinit = {} -setmetatable(hinit, {__index=_G}) -setfenv(1, hinit) +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) function init(width, height, title) @@ -16,14 +16,28 @@ function init(width, height, title) local window = honey.Window(width, height, title) glfw.MakeContextCurrent(window.win) gl.InitGlad() + gl.Enable(gl.DEPTH_TEST) return window end +function loop(window, update) + local prevTime = 0 + while not window:shouldClose() do + local time = glfw.GetTime() + local dt = time - prevTime + prevTime = time + update(dt) + window:swapBuffers() + glfw.PollEvents() + end +end + + function terminate() glfw.Terminate() end -return hinit +return module diff --git a/honey/shader.lua b/honey/shader.lua index 1fb9f5b..083b260 100644 --- a/honey/shader.lua +++ b/honey/shader.lua @@ -15,11 +15,26 @@ local function compileShader(source, type) return shader end +local function readFile(filename) + local f, err = io.open(filename) + if not f then error(err) end + local str = f:read("*a") + f:close() + return str +end + function Shader.new(_, sources) local self = {} self.locations = {} self.links = {} + if sources.vertexFile then + sources.vertex = readFile(sources.vertexFile) + end + if sources.fragmentFile then + sources.fragment = readFile(sources.fragmentFile) + end + local shaders = {} if sources.vertex then table.insert(shaders, compileShader(sources.vertex, gl.VERTEX_SHADER)) diff --git a/honey/std.lua b/honey/std.lua index 028a2a8..7cb7695 100644 --- a/honey/std.lua +++ b/honey/std.lua @@ -1,6 +1,7 @@ local init = require 'honey.init' honey.init = init.init +honey.loop = init.loop honey.terminate = init.terminate honey.ecs = require 'honey.ecs' @@ -1,6 +1,5 @@ require 'honey.std' - local glfw = honey.glfw local gl = honey.gl local Vec3 = honey.Vec3 @@ -9,86 +8,41 @@ local ecs = honey.ecs local systems = honey.standardSystems --- camera matrices +-- initialize honey +local window = honey.init() + +-- create camera matrices local camera = { view=Mat4():identity():translate(Vec3{0, 0, -60}), projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), } - - +-- setup ecs local level = ecs.Level() level:addSystem(systems.transformCascade) level:addSystem(systems.renderCam(camera)) level:addSystem(systems.update) - - -local window = honey.init() -gl.Enable(gl.DEPTH_TEST) - - - -local vertexShaderSource = [[ -#version 410 core -layout (location = 0) in vec3 in_position; -layout (location = 1) in vec3 in_normal; -layout (location = 2) in vec2 in_tex; - -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 = vec3(model * vec4(in_normal, 1.0f)); - normal = in_normal; - tex = in_tex; -} -]] - -local fragmentShaderSource = [[ -#version 410 core -out vec4 FragColor; - -in vec3 position; -in vec3 normal; -in vec2 tex; - -uniform sampler2D ourTexture; - -void main() -{ - FragColor = vec4(normal, 1.0f); - //FragColor = vec4(tex, 1.0f, 1.0f); - //FragColor = texture(ourTexture, TexCoord); -} -]] - +-- create shader local shader = honey.Shader{ - vertex = vertexShaderSource, - fragment = fragmentShaderSource, + vertexFile = "vertex.glsl", + fragmentFile = "fragment.glsl", } +-- load models local tetra = honey.mesh.loadFile("assets/tetrahedron.obj")[1] local cube = honey.mesh.loadFile("assets/cube.obj")[1] local octa = honey.mesh.loadFile("assets/octahedron.obj")[1] local dodeca = honey.mesh.loadFile("assets/dodecahedron.obj")[1] local icosa = honey.mesh.loadFile("assets/icosahedron.obj")[1] - +-- update function for each entity function updateTransform(self, dt) self.transform:rotateY(0.3 * math.pi * dt) self.transform:rotateX(0.1 * math.pi * dt) end - +-- create entities function growLine(prev, depth) if depth == 0 then return prev end @@ -104,7 +58,6 @@ function growLine(prev, depth) return growLine(entity, depth-1) end - local leaf = { transform=Mat4():identity():translate(Vec3{2, 0, 0}), parent=false, @@ -114,28 +67,19 @@ local leaf = { local root = growLine(leaf, 24) level:addEntity(root) - +-- close window on ESCAPE key window:setKeyCallback(function(_, key) if key == glfw.KEY_ESCAPE then window:setShouldClose(true) end end) - - -local prevTime = 0 -while not window:shouldClose() do - local time = glfw.GetTime() - local dt = time - prevTime - prevTime = time - +-- main loop +honey.loop(window, function(dt) gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) - level:update(dt) +end) - window:swapBuffers() - glfw.PollEvents() -end - +-- clean up honey.terminate() diff --git a/vertex.glsl b/vertex.glsl new file mode 100644 index 0000000..1e9fb67 --- /dev/null +++ b/vertex.glsl @@ -0,0 +1,21 @@ +#version 410 core
+layout (location = 0) in vec3 in_position;
+layout (location = 1) in vec3 in_normal;
+layout (location = 2) in vec2 in_tex;
+
+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 = vec3(model * vec4(in_normal, 1.0f));
+ normal = in_normal;
+ tex = in_tex;
+}
|