From ced4c513947132cae08c6627fd2030b4345eb39d Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 21 Aug 2022 18:50:27 -0500 Subject: add demo script --- demo/honey.lua | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 demo/honey.lua (limited to 'demo') 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() -- cgit v1.2.1 From f84ee69cdf694a1f64f2536efed4b7d0dd75553a Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 09:12:09 -0500 Subject: make hello triangle work --- demo/honey.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 489f022..15acbf2 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -75,7 +75,7 @@ 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) +gl.bufferData(gl.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.bufferUsage.staticDraw) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end gl.vertexAttribPointer(0, 3, false, 3, 0) -- cgit v1.2.1 From 65ad3a58adcfe21690f379ab894a104b3852e3e9 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 10:31:56 -0500 Subject: refactor: move shader functions into separate file --- demo/honey.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 15acbf2..0a4758a 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -46,19 +46,19 @@ void main() } ]] -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 vertexShader = gl.shader.create(gl.shader.type.vertexShader) +gl.shader.setSource(vertexShader, vertexShaderSource) +gl.shader.compile(vertexShader) +local fragmentShader = gl.shader.create(gl.shader.type.fragmentShader) +gl.shader.setSource(fragmentShader, fragmentShaderSource) +gl.shader.compile(fragmentShader) -local shader = gl.createProgram() -gl.programAttachShader(shader, vertexShader) -gl.programAttachShader(shader, fragmentShader) -gl.linkProgram(shader) -gl.deleteShader(vertexShader) -gl.deleteShader(fragmentShader) +local shader = gl.shader.createProgram() +gl.shader.attachShader(shader, vertexShader) +gl.shader.attachShader(shader, fragmentShader) +gl.shader.link(shader) +gl.shader.delete(vertexShader) +gl.shader.delete(fragmentShader) local vertices = { @@ -90,7 +90,7 @@ 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.shader.use(shader) gl.bindVertexArray(vertexArray) gl.drawArrays(gl.primitiveType.triangles, 0, 3) -- cgit v1.2.1 From dccc9bef56d51c5cad13c2568c5a3b97f637fd6e Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 10:45:52 -0500 Subject: refactor: move drawing code into separate file --- demo/honey.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 0a4758a..fce264c 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -87,12 +87,12 @@ 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.draw.setClearColor(0.2, 0.3, 0.3, 1.0) + gl.draw.clear(gl.draw.bufferMask.colorBuffer); gl.shader.use(shader) gl.bindVertexArray(vertexArray) - gl.drawArrays(gl.primitiveType.triangles, 0, 3) + gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) window.swapBuffers(w) window.pollEvents() -- cgit v1.2.1 From a2c162f12337142c054c2e1f01ad508ee56963d2 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 11:00:42 -0500 Subject: refactor: move data functions into separate file --- demo/honey.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index fce264c..5e14461 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -67,23 +67,23 @@ local vertices = { 0.0, 0.5, 0.0 } -local vertexArray = gl.createVertexArray() +local vertexArray = gl.data.createVertexArray() -local vertexBuffer = gl.createBuffer() -gl.bindVertexArray(vertexArray) +local vertexBuffer = gl.data.createBuffer() +gl.data.bindVertexArray(vertexArray) -gl.bindBuffer(gl.bufferTarget.arrayBuffer, vertexBuffer) +gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer) local err = gl.getError() if err ~= gl.errorType.noError then error(gl.errorName(err)) end -gl.bufferData(gl.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.bufferUsage.staticDraw) +gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end -gl.vertexAttribPointer(0, 3, false, 3, 0) +gl.data.vertexAttribPointer(0, 3, false, 3, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end -gl.vertexArrayEnableAttrib(0) +gl.data.vertexArrayEnableAttrib(0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end -gl.bindBuffer(gl.bufferTarget.arrayBuffer, 0) +gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end while not window.shouldClose(w) do @@ -91,7 +91,7 @@ while not window.shouldClose(w) do gl.draw.clear(gl.draw.bufferMask.colorBuffer); gl.shader.use(shader) - gl.bindVertexArray(vertexArray) + gl.data.bindVertexArray(vertexArray) gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) window.swapBuffers(w) -- cgit v1.2.1 From 249833947ff691dd79453c4d817387a6922236d6 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 11:38:26 -0500 Subject: add element buffers --- demo/honey.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 5e14461..2b1759f 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -62,14 +62,20 @@ gl.shader.delete(fragmentShader) local vertices = { - -0.5, -0.5, 0.0, - 0.5, -0.5, 0.0, - 0.0, 0.5, 0.0 -} + 0.5, 0.5, 0.0, + 0.5, -0.5, 0.0, + -0.5, -0.5, 0.0, + -0.5, 0.5, 0.0 +} +local indices = { + 0, 1, 3, + 1, 2, 3 +} local vertexArray = gl.data.createVertexArray() local vertexBuffer = gl.data.createBuffer() +local elementBuffer = gl.data.createBuffer() gl.data.bindVertexArray(vertexArray) gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer) @@ -78,6 +84,9 @@ if err ~= gl.errorType.noError then error(gl.errorName(err)) end gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) +gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) + gl.data.vertexAttribPointer(0, 3, false, 3, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end gl.data.vertexArrayEnableAttrib(0) @@ -92,7 +101,8 @@ while not window.shouldClose(w) do gl.shader.use(shader) gl.data.bindVertexArray(vertexArray) - gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) + --gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) + gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) window.swapBuffers(w) window.pollEvents() -- cgit v1.2.1 From b2c0dd12d2a39e74f8e6d63bffec073ef0871ea7 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 12:39:51 -0500 Subject: add uniforms --- demo/honey.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 2b1759f..470880d 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -26,23 +26,21 @@ 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; +uniform vec4 color; out vec4 FragColor; void main() { - FragColor = vec4(pos.x+0.5, 0.0f, 2*(pos.y+pos.x), 1.0f); + FragColor = color; } ]] @@ -100,8 +98,12 @@ while not window.shouldClose(w) do gl.draw.clear(gl.draw.bufferMask.colorBuffer); gl.shader.use(shader) + local time = window.getTime() + local greenValue = (math.sin(time) / 2) + 0.5 + local colorLocation = gl.shader.getUniformLocation(shader, 'color') + gl.shader.uniform4f(colorLocation, 0, greenValue, 0, 1) + gl.data.bindVertexArray(vertexArray) - --gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) window.swapBuffers(w) -- cgit v1.2.1 From 6f06255d2ae0337d9aea5e5330502581e32dab09 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 13:29:20 -0500 Subject: add multiple vertex attributes --- demo/honey.lua | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 470880d..7f02006 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -26,21 +26,26 @@ end) local vertexShaderSource = [[ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 pColor; void main() { + pColor = aColor; gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } ]] local fragmentShaderSource = [[ #version 330 core +in vec3 pColor; uniform vec4 color; out vec4 FragColor; void main() { - FragColor = color; + FragColor = vec4(pColor.rgb, 1); } ]] @@ -60,10 +65,11 @@ gl.shader.delete(fragmentShader) local vertices = { - 0.5, 0.5, 0.0, - 0.5, -0.5, 0.0, - -0.5, -0.5, 0.0, - -0.5, 0.5, 0.0 + -- position color + 0.5, 0.5, 0.0, 0, 0, 0, + 0.5, -0.5, 0.0, 1, 0, 0, + -0.5, -0.5, 0.0, 0, 1, 0, + -0.5, 0.5, 0.0, 0, 0, 1 } local indices = { 0, 1, 3, @@ -85,10 +91,10 @@ if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) -gl.data.vertexAttribPointer(0, 3, false, 3, 0) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.data.vertexAttribPointer(0, 3, false, 6, 0) gl.data.vertexArrayEnableAttrib(0) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.data.vertexAttribPointer(1, 3, false, 6, 3) +gl.data.vertexArrayEnableAttrib(1) gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end -- cgit v1.2.1 From 1f47b685f35455afcc7441389cdc60018f66d159 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 15:55:44 -0500 Subject: add textures --- demo/container.jpg | Bin 0 -> 184939 bytes demo/honey.lua | 80 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 demo/container.jpg (limited to 'demo') diff --git a/demo/container.jpg b/demo/container.jpg new file mode 100644 index 0000000..d07bee4 Binary files /dev/null and b/demo/container.jpg differ diff --git a/demo/honey.lua b/demo/honey.lua index 7f02006..6d2cdfc 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -24,28 +24,35 @@ end) local vertexShaderSource = [[ + #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; -out vec3 pColor; +out vec3 ourColor; +out vec2 TexCoord; void main() { - pColor = aColor; - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = aTexCoord; } ]] local fragmentShaderSource = [[ #version 330 core -in vec3 pColor; -uniform vec4 color; out vec4 FragColor; + +in vec3 ourColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; void main() { - FragColor = vec4(pColor.rgb, 1); + FragColor = texture(ourTexture, TexCoord); } ]] @@ -64,12 +71,35 @@ gl.shader.delete(vertexShader) gl.shader.delete(fragmentShader) +---------------------------------------------------------------- + +local texture = gl.texture.create() +gl.texture.bind(gl.texture.bindTarget.texture2d, texture) +err = gl.getError() +if err ~= gl.errorType.noError then error(gl.errorName(err)) end + + +local image, width, height, channels = honey.image.load('container.jpg', 0) +gl.texture.bufferImage2d( + gl.texture.bindTarget.texture2d, 0, + gl.texture.format.rgb, + width, height, + gl.texture.format.rgb, gl.dataType.uchar, + image +) +gl.texture.generateMipmaps(gl.texture.bindTarget.texture2d) +err = gl.getError() +if err ~= gl.errorType.noError then error(gl.errorName(err)) end + +honey.image.destroy(image) + + local vertices = { - -- position color - 0.5, 0.5, 0.0, 0, 0, 0, - 0.5, -0.5, 0.0, 1, 0, 0, - -0.5, -0.5, 0.0, 0, 1, 0, - -0.5, 0.5, 0.0, 0, 0, 1 + -- position color uvs + 0.5, 0.5, 0.0, 0, 0, 0, 1, 1, + 0.5, -0.5, 0.0, 1, 0, 0, 1, 0, + -0.5, -0.5, 0.0, 0, 1, 0, 0, 0, + -0.5, 0.5, 0.0, 0, 0, 1, 0, 1 } local indices = { 0, 1, 3, @@ -91,24 +121,38 @@ if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) -gl.data.vertexAttribPointer(0, 3, false, 6, 0) +gl.data.vertexAttribPointer(0, 3, false, 8, 0) gl.data.vertexArrayEnableAttrib(0) -gl.data.vertexAttribPointer(1, 3, false, 6, 3) +gl.data.vertexAttribPointer(1, 3, false, 8, 3) gl.data.vertexArrayEnableAttrib(1) +gl.data.vertexAttribPointer(2, 2, false, 8, 6) +gl.data.vertexArrayEnableAttrib(2) gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.shader.use(shader) +local textureLocation = gl.shader.getUniformLocation(shader, "ourTexture") +err = gl.getError() +if err ~= gl.errorType.noError then error(gl.errorName(err)) end +gl.shader.uniform1i(textureLocation, 0) +err = gl.getError() +if err ~= gl.errorType.noError then error(gl.errorName(err)) end + + while not window.shouldClose(w) do gl.draw.setClearColor(0.2, 0.3, 0.3, 1.0) gl.draw.clear(gl.draw.bufferMask.colorBuffer); - gl.shader.use(shader) - local time = window.getTime() - local greenValue = (math.sin(time) / 2) + 0.5 - local colorLocation = gl.shader.getUniformLocation(shader, 'color') - gl.shader.uniform4f(colorLocation, 0, greenValue, 0, 1) + gl.texture.setActiveUnit(0) + err = gl.getError() + if err ~= gl.errorType.noError then error(gl.errorName(err)) end + gl.texture.bind(gl.texture.bindTarget.texture2d, texture) + err = gl.getError() + if err ~= gl.errorType.noError then error(gl.errorName(err)) end + + gl.shader.use(shader) gl.data.bindVertexArray(vertexArray) gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) -- cgit v1.2.1 From c52e1b30391d730cd6d20f65ec75d61884355c5c Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 23:58:00 -0500 Subject: refactor: rename opengl functions to match their C counterparts --- demo/honey.lua | 169 +++++++++++++++++++++++++++------------------------------ 1 file changed, 81 insertions(+), 88 deletions(-) (limited to 'demo') diff --git a/demo/honey.lua b/demo/honey.lua index 6d2cdfc..b0b3ec4 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -1,21 +1,20 @@ 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() +--====== initialize opengl ======-- + +gl.Init() window.setHint(window.hintType.contextVersionMajor, 3) window.setHint(window.hintType.contextVersionMinor, 3) window.setHint(window.hintType.openGlProfile, window.profileType.openGlCoreProfile) + +--====== create window ======-- + local w = window.create(640, 480, 'hello, world!') window.makeContextCurrent(w) -gl.initGlad() +gl.InitGlad() window.setFramebufferSizeCallback(w, function(_, width, height) print(string.format("resize: (%d, %d)", width, height)) @@ -23,8 +22,9 @@ window.setFramebufferSizeCallback(w, function(_, width, height) end) -local vertexShaderSource = [[ +--====== compile shaders ======-- +local vertexShaderSource = [[ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; @@ -56,108 +56,101 @@ void main() } ]] -local vertexShader = gl.shader.create(gl.shader.type.vertexShader) -gl.shader.setSource(vertexShader, vertexShaderSource) -gl.shader.compile(vertexShader) -local fragmentShader = gl.shader.create(gl.shader.type.fragmentShader) -gl.shader.setSource(fragmentShader, fragmentShaderSource) -gl.shader.compile(fragmentShader) +-- vertex shader +local vertexShader = gl.CreateShader(gl.VERTEX_SHADER) +gl.ShaderSource(vertexShader, vertexShaderSource) +gl.CompileShader(vertexShader) -local shader = gl.shader.createProgram() -gl.shader.attachShader(shader, vertexShader) -gl.shader.attachShader(shader, fragmentShader) -gl.shader.link(shader) -gl.shader.delete(vertexShader) -gl.shader.delete(fragmentShader) +-- 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 texture = gl.texture.create() -gl.texture.bind(gl.texture.bindTarget.texture2d, texture) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end - - -local image, width, height, channels = honey.image.load('container.jpg', 0) -gl.texture.bufferImage2d( - gl.texture.bindTarget.texture2d, 0, - gl.texture.format.rgb, - width, height, - gl.texture.format.rgb, gl.dataType.uchar, - image -) -gl.texture.generateMipmaps(gl.texture.bindTarget.texture2d) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end - -honey.image.destroy(image) +--====== set up vertex data ======-- local vertices = { - -- position color uvs - 0.5, 0.5, 0.0, 0, 0, 0, 1, 1, - 0.5, -0.5, 0.0, 1, 0, 0, 1, 0, - -0.5, -0.5, 0.0, 0, 1, 0, 0, 0, - -0.5, 0.5, 0.0, 0, 0, 1, 0, 1 +-- positions colors uvs + 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, -- top right + 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, -- bottom right + -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -- bottom let + -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0 -- top let } local indices = { - 0, 1, 3, - 1, 2, 3 + 0, 1, 3, -- first triangle + 1, 2, 3 -- second triangle } -local vertexArray = gl.data.createVertexArray() +-- buffers +local vertexArray = gl.GenVertexArrays() +local vertexBuffer = gl.GenBuffers() +local elementBuffer = gl.GenBuffers() -local vertexBuffer = gl.data.createBuffer() -local elementBuffer = gl.data.createBuffer() -gl.data.bindVertexArray(vertexArray) +gl.BindVertexArray(vertexArray) -gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer) -local err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end -gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer) +gl.BufferData(gl.ARRAY_BUFFER, gl.FLOAT, vertices, gl.STATIC_DRAW) -gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) -gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) +gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer) +gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.UNSIGNED_INT, indices, gl.STATIC_DRAW) -gl.data.vertexAttribPointer(0, 3, false, 8, 0) -gl.data.vertexArrayEnableAttrib(0) -gl.data.vertexAttribPointer(1, 3, false, 8, 3) -gl.data.vertexArrayEnableAttrib(1) -gl.data.vertexAttribPointer(2, 2, false, 8, 6) -gl.data.vertexArrayEnableAttrib(2) +-- position +gl.VertexAttribPointer(0, 3, false, 8, 0) +gl.EnableVertexAttribArray(0) -gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0) -if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +-- color +gl.VertexAttribPointer(1, 3, false, 8, 3) +gl.EnableVertexAttribArray(1) -gl.shader.use(shader) -local textureLocation = gl.shader.getUniformLocation(shader, "ourTexture") -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end -gl.shader.uniform1i(textureLocation, 0) -err = gl.getError() -if err ~= gl.errorType.noError then error(gl.errorName(err)) end +-- uv +gl.VertexAttribPointer(2, 2, false, 8, 6) +gl.EnableVertexAttribArray(2) -while not window.shouldClose(w) do - gl.draw.setClearColor(0.2, 0.3, 0.3, 1.0) - gl.draw.clear(gl.draw.bufferMask.colorBuffer); +--====== load texture ======-- + +local texture = gl.GenTextures() +gl.BindTexture(gl.TEXTURE_2D, texture) - gl.texture.setActiveUnit(0) - err = gl.getError() - if err ~= gl.errorType.noError then error(gl.errorName(err)) end - gl.texture.bind(gl.texture.bindTarget.texture2d, texture) - err = gl.getError() - if err ~= gl.errorType.noError then error(gl.errorName(err)) end +local image, width, height = honey.image.load('container.jpg', 3) +gl.TexImage2D( + gl.TEXTURE_2D, 0, + gl.RGB, width, height, + gl.RGB, gl.UNSIGNED_BYTE, + image +) +gl.GenerateMipmap(gl.TEXTURE_2D) +honey.image.destroy(image) + +-- connect shader samplers to texture units +gl.UseProgram(shader) +gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0) + + +--====== main loop ======-- + +while not window.shouldClose(w) do + gl.ClearColor(0.2, 0.3, 0.3, 1.0) + gl.Clear(gl.COLOR_BUFFER_BIT) + gl.ActiveTexture(0) + gl.BindTexture(gl.TEXTURE_2D, texture) - gl.shader.use(shader) - gl.data.bindVertexArray(vertexArray) - gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) + gl.UseProgram(shader) + gl.BindVertexArray(vertexArray) + gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) window.swapBuffers(w) window.pollEvents() end window.destroy(w) -gl.terminate() +gl.Terminate() -- cgit v1.2.1 From 711f530f7afe3358c674cf418ee067e7a4501699 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 Aug 2022 00:31:14 -0500 Subject: add gl.TexParameteri() --- demo/gl-window | 0 demo/honey.lua | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 demo/gl-window (limited to 'demo') diff --git a/demo/gl-window b/demo/gl-window new file mode 100644 index 0000000..e69de29 diff --git a/demo/honey.lua b/demo/honey.lua index b0b3ec4..3388b52 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -121,6 +121,12 @@ gl.EnableVertexAttribArray(2) local texture = gl.GenTextures() gl.BindTexture(gl.TEXTURE_2D, texture) +gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) +gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) + +gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) +gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) + local image, width, height = honey.image.load('container.jpg', 3) gl.TexImage2D( gl.TEXTURE_2D, 0, -- cgit v1.2.1 From ae4e17fc743ca0344af818ab767db7311ea7829c Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 Aug 2022 13:28:01 -0500 Subject: add basic transforms --- demo/gl-window | 0 demo/honey.lua | 12 +++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) delete mode 100644 demo/gl-window (limited to 'demo') diff --git a/demo/gl-window b/demo/gl-window deleted file mode 100644 index e69de29..0000000 diff --git a/demo/honey.lua b/demo/honey.lua index 3388b52..822877f 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -30,12 +30,14 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; +uniform mat4 transform; + out vec3 ourColor; out vec2 TexCoord; void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = transform * vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; } @@ -144,6 +146,8 @@ gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0) --====== main loop ======-- +local transform = honey.glm.mat4() + while not window.shouldClose(w) do gl.ClearColor(0.2, 0.3, 0.3, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) @@ -152,6 +156,12 @@ while not window.shouldClose(w) do gl.BindTexture(gl.TEXTURE_2D, texture) gl.UseProgram(shader) + honey.glm.mat4_identity(transform) + local time = window.getTime() + honey.glm.rotate_z(transform, time, transform) + local transformLocation = gl.GetUniformLocation(shader, 'transform') + gl.UniformMatrix4fv(transformLocation, false, transform) + gl.BindVertexArray(vertexArray) gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) -- cgit v1.2.1