tofu

Making something with OpenGL in Go
Log | Files | Refs

commit bdcf1967a838b2f2c0b5fc73c9a0c206c4e83195
parent 08820a0fe9c3eebac8fff31cbbbf55b4cdeaa958
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 18 Oct 2024 17:34:27 +0900

hello triangle

Diffstat:
Mcmd/sample/main.go | 24++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -76,6 +76,10 @@ var vertices = []float32{ 0.0, 0.5, 0.0, } +var indices = []uint32{ + 0, 1, 2, +} + const vertexShaderSource = `#version 330 core layout (location = 0) in vec3 aPos; void main() { @@ -123,22 +127,34 @@ func main() { if err != nil { log.Fatalf("link shaders: %v", err) } - gl.UseProgram(shaderProgram) gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) - window.SetFramebufferSizeCallback(framebufferSizeCallback) - - var VBO uint32 + var VBO, VAO, EBO uint32 + gl.GenVertexArrays(1, &VAO) gl.GenBuffers(1, &VBO) + gl.GenBuffers(1, &EBO) + gl.BindVertexArray(VAO) gl.BindBuffer(gl.ARRAY_BUFFER, VBO) gl.BufferData(gl.ARRAY_BUFFER, len(vertices) * 4, gl.Ptr(vertices), gl.STATIC_DRAW) + gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO) + gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indices) * 4, + gl.Ptr(indices), gl.STATIC_DRAW) + gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 3*4, nil) + gl.EnableVertexAttribArray(0) + gl.BindBuffer(gl.ARRAY_BUFFER, 0); + gl.BindVertexArray(0) + + window.SetFramebufferSizeCallback(framebufferSizeCallback) for !window.ShouldClose() { processInput(window) gl.ClearColor(0.2, 0.3, 0.3, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) + gl.UseProgram(shaderProgram) + gl.BindVertexArray(VAO) + gl.DrawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, nil) window.SwapBuffers() glfw.PollEvents() }