From 89572c1648128456822cf2dda07b13e78cfc2813 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 13 Mar 2023 02:42:16 -0500 Subject: render all shapes --- main.lua | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 120 insertions(+), 4 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 991e8d1..95068eb 100644 --- a/main.lua +++ b/main.lua @@ -1,10 +1,126 @@ require 'honey.std' -honey.glfw.Init() -local window = honey.Window(640, 480, "geometry") +local glfw = honey.glfw +local gl = honey.gl +local Vec3 = honey.Vec3 +local Mat4 = honey.Mat4 + + +local window = honey.init() +gl.Enable(gl.DEPTH_TEST) + + +local model = Mat4() +model:identity() + +local view = Mat4() +view:identity() +view:translate(Vec3{0, 0, -3}) + +local projection = Mat4() +projection:perspective(math.rad(45), 640/480, 0.1, 100) + + +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); +} +]] + +-- vertex shader +local vertexShader = gl.CreateShader(gl.VERTEX_SHADER) +gl.ShaderSource(vertexShader, vertexShaderSource) +gl.CompileShader(vertexShader) + +-- fragment shader +local fragmentShader = gl.CreateShader(gl.FRAGMENT_SHADER) +gl.ShaderSource(fragmentShader, fragmentShaderSource) +gl.CompileShader(fragmentShader) + +-- link +local shader = gl.CreateProgram() +gl.AttachShader(shader, vertexShader) +gl.AttachShader(shader, fragmentShader) +gl.LinkProgram(shader) +-- clean up +gl.DeleteShader(vertexShader) +gl.DeleteShader(fragmentShader) + + +local mesh = honey.mesh.loadFile("assets/tetrahedron.obj")[1] +--local mesh = honey.mesh.Mesh( +-- { +-- 0, 0, 0, 0, 0, 1, 0, 0, +-- 1, 0, 0, 0, 0, 1, 1, 0, +-- 0, 1, 0, 0, 0, 1, 0, 1, +-- }, +-- { 0, 2, 1 } +--) + + while not window:shouldClose() do - print(window:getSize()) - honey.glfw.PollEvents() + local time = glfw.GetTime() + model:identity() + model:rotateY(0.5 * math.pi * time) + model:rotateX(0.05 * math.pi * time) + --model:scale(0.2 * Vec3{1, 1, 1}) + + gl.ClearColor(0.2, 0.4, 1.0, 1.0) + gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) + + gl.UseProgram(shader) + + local modelLocation = gl.GetUniformLocation(shader, "model") + local viewLocation = gl.GetUniformLocation(shader, "view") + local projectionLocation = gl.GetUniformLocation(shader, "projection") + + gl.UniformMatrix4fv(modelLocation, false, model.data) + gl.UniformMatrix4fv(viewLocation, false, view.data) + gl.UniformMatrix4fv(projectionLocation, false, projection.data) + + mesh:drawElements() + + + window:swapBuffers() + glfw.PollEvents() end + +honey.terminate() -- cgit v1.2.1