diff options
author | sanine <sanine.not@pm.me> | 2022-08-21 18:50:27 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-21 18:50:27 -0500 |
commit | ced4c513947132cae08c6627fd2030b4345eb39d (patch) | |
tree | 0dfa5f1c3d963f02f3b17e37756f6d083bb0d677 /demo | |
parent | bd5ae222cc1d2a24b55bd8aa8c72dd63f73cb35f (diff) |
add demo script
Diffstat (limited to 'demo')
-rw-r--r-- | demo/honey.lua | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/demo/honey.lua b/demo/honey.lua new file mode 100644 index 0000000..489f022 --- /dev/null +++ b/demo/honey.lua @@ -0,0 +1,101 @@ +local gl = honey.gl +local window = honey.window + +gl.errorName = function(errorCode) + for name, code in pairs(gl.errorType) do + if code == errorCode then return name end + end + return 'unknown' +end + +gl.init() +window.setHint(window.hintType.contextVersionMajor, 3) +window.setHint(window.hintType.contextVersionMinor, 3) +window.setHint(window.hintType.openGlProfile, window.profileType.openGlCoreProfile) + +local w = window.create(640, 480, 'hello, world!') +window.makeContextCurrent(w) +gl.initGlad() + +window.setFramebufferSizeCallback(w, function(_, width, height) + print(string.format("resize: (%d, %d)", width, height)) + gl.setViewport(0, 0, width, height) +end) + + +local vertexShaderSource = [[ +#version 330 core +layout (location = 0) in vec3 aPos; +out vec3 pos; + +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + pos = aPos; +} +]] + +local fragmentShaderSource = [[ +#version 330 core +in vec3 pos; +out vec4 FragColor; + +void main() +{ + FragColor = vec4(pos.x+0.5, 0.0f, 2*(pos.y+pos.x), 1.0f); +} +]] + +local vertexShader = gl.createShader(gl.shaderType.vertexShader) +gl.setShaderSource(vertexShader, vertexShaderSource) +gl.compileShader(vertexShader) +local fragmentShader = gl.createShader(gl.shaderType.fragmentShader) +gl.setShaderSource(fragmentShader, fragmentShaderSource) +gl.compileShader(fragmentShader) + +local shader = gl.createProgram() +gl.programAttachShader(shader, vertexShader) +gl.programAttachShader(shader, fragmentShader) +gl.linkProgram(shader) +gl.deleteShader(vertexShader) +gl.deleteShader(fragmentShader) + + +local vertices = { + -0.5, -0.5, 0.0, + 0.5, -0.5, 0.0, + 0.0, 0.5, 0.0 +} + +local vertexArray = gl.createVertexArray() + +local vertexBuffer = gl.createBuffer() +gl.bindVertexArray(vertexArray) + +gl.bindBuffer(gl.bufferTarget.arrayBuffer, vertexBuffer) +local err = gl.getError() +if err ~= gl.errorType.noError then error(gl.errorName(err)) end +gl.bufferData(gl.bufferTarget.arrayBuffer, vertices, gl.bufferUsage.staticDraw) +if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end + +gl.vertexAttribPointer(0, 3, false, 3, 0) +if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.vertexArrayEnableAttrib(0) +if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end + +gl.bindBuffer(gl.bufferTarget.arrayBuffer, 0) +if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end + +while not window.shouldClose(w) do + gl.setClearColor(0.2, 0.3, 0.3, 1.0) + gl.clear(gl.bufferMask.colorBuffer); + + gl.useProgram(shader) + gl.bindVertexArray(vertexArray) + gl.drawArrays(gl.primitiveType.triangles, 0, 3) + + window.swapBuffers(w) + window.pollEvents() +end +window.destroy(w) +gl.terminate() |