diff options
author | sanine <sanine.not@pm.me> | 2023-03-13 02:42:16 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-03-13 02:42:16 -0500 |
commit | 89572c1648128456822cf2dda07b13e78cfc2813 (patch) | |
tree | 78cafed06eb4cb261e53d0de28fa7e170ce55d6f /main.lua | |
parent | bb8c38bffba86cda05e0e8e9b52fb68301286b53 (diff) |
render all shapes
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 124 |
1 files changed, 120 insertions, 4 deletions
@@ -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() |