summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-10-31 22:43:41 -0500
committersanine-a <sanine.not@pm.me>2020-10-31 22:43:41 -0500
commit42d42c9ba3b741d167eaa2196c686962559686f1 (patch)
tree46f9d9c51d1d4fe1ccbacf1d1f73c066e93dfc32 /demo
parentad75604ec79d70d328595f114e65bac80db9999f (diff)
add basic framebuffer operations
Diffstat (limited to 'demo')
-rw-r--r--demo/ScreenQuad.lua47
-rw-r--r--demo/main.lua37
2 files changed, 78 insertions, 6 deletions
diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua
new file mode 100644
index 0000000..938674c
--- /dev/null
+++ b/demo/ScreenQuad.lua
@@ -0,0 +1,47 @@
+local ScreenQuad = {}
+
+ScreenQuad.quad = honey.primitives.plane(2,2)
+
+local vertexShader = [[
+#version 330 core
+
+layout(location = 0) in vec3 position;
+layout(location = 1) in vec3 normal;
+layout(location = 2) in vec2 uv;
+
+out vec2 UV;
+
+void main()
+{
+ gl_Position = vec4(position.xy, 0, 1) - vec4(1,1,0,0);
+ UV = uv;
+} ]]
+
+local fragmentShader = [[
+#version 330 core
+
+in vec2 UV;
+
+uniform sampler2D tex;
+
+out vec4 color;
+
+void main()
+{
+ color = vec4(texture(tex, UV));
+} ]]
+
+ScreenQuad.shader = honey.shader.new(vertexShader, fragmentShader)
+
+ScreenQuad.texture = honey.texture.new()
+honey.texture.create(ScreenQuad.texture, 'rgba', 640, 480);
+--honey.texture.load(ScreenQuad.texture, 'checkerboard.png', false)
+
+ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.texture, nil, 640, 480)
+
+ScreenQuad.draw = function(self)
+ honey.texture.use(self.texture, 0)
+ honey.mesh.draw(self.quad, self.shader)
+end
+
+return ScreenQuad
diff --git a/demo/main.lua b/demo/main.lua
index 3c32b6f..5474609 100644
--- a/demo/main.lua
+++ b/demo/main.lua
@@ -1,6 +1,7 @@
local Vector = require('Vector')
local Matrix = require('Matrix')
local FPSCamera = require('FPSCamera')
+local ScreenQuad = require('ScreenQuad')
FPSCamera.movement_speed = 5
local model = Matrix.Mat4.eye()
@@ -10,9 +11,11 @@ print(model)
honey.input.key.bind(honey.input.key.escape, honey.exit)
+local buffer = false
+honey.input.key.bind(honey.input.key.f, function(action) if action == 1 then buffer = not buffer end end)
+
local tex = honey.texture.new()
honey.texture.load(tex, 'checkerboard.png', false)
-honey.texture.use(tex, 0)
local vertex_shader = [[
#version 330 core
@@ -49,12 +52,13 @@ uniform sampler2D tex;
out vec4 color;
void main() {
- vec2 texture_coords = UV + (time * vec2(100,100));
- color = vec4(texture(tex, texture_coords).xyz, 1);
+ //vec2 texture_coords = UV + (time * vec2(100,100));
+ color = vec4(texture(tex, UV).xyz, 1);
} ]]
local shader = honey.shader.new(vertex_shader, fragment_shader)
-local plane = honey.mesh.load('Suzanne.obj')[1]
+local suzanne = honey.mesh.load('Suzanne.obj')[1]
+local plane = honey.primitives.plane(4,4)
local color1 = Vector.Vec4.new{1,0,0,1}
local color2 = Vector.Vec4.new{0,0,1,1}
@@ -73,11 +77,32 @@ function honey.update(dt)
end
end
-function honey.draw()
- total_frames = total_frames + 1
+function draw_suzanne()
+ honey.texture.use(tex, 0)
honey.shader.set_mat4(shader, 'model', model.array)
honey.shader.set_mat4(shader, 'view', FPSCamera.view.array)
honey.shader.set_mat4(shader, 'projection', FPSCamera.projection.array)
honey.shader.set_float(shader, 'time', total_time)
+ honey.mesh.draw(suzanne, shader)
honey.mesh.draw(plane, shader)
end
+
+function honey.draw()
+ total_frames = total_frames + 1
+
+ if buffer then
+ honey.set_framebuffer(ScreenQuad.fb)
+ honey.enable_depth_test(true)
+ honey.clear_color(Vector.Vec4.new().array, true, true, false)
+ draw_suzanne()
+
+ honey.set_framebuffer(0)
+ honey.enable_depth_test(true)
+ honey.clear_color(Vector.Vec4.new{0,0,1,1}.array, true, true, false)
+ ScreenQuad:draw()
+ else
+ honey.clear_color(Vector.Vec4.new{1,1,0,1}.array, true, true, false)
+ honey.enable_depth_test(true)
+ draw_suzanne()
+ end
+end