diff options
author | xengineering <me@xengineering.eu> | 2022-03-16 20:18:18 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2022-03-16 20:18:18 +0100 |
commit | 0340cb664f19ab8f14d893c8d82b6dead705a794 (patch) | |
tree | b311ee5d1040a2beabda5aab25372b0145e89ea2 /graphics.go | |
parent | b38538bfd5a3ad3d55473a5c3970f0a46bc63970 (diff) | |
download | stlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.tar stlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.tar.zst stlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.zip |
Reduce code width to <= 80 characters
Diffstat (limited to 'graphics.go')
-rw-r--r-- | graphics.go | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/graphics.go b/graphics.go index fc195eb..a952e40 100644 --- a/graphics.go +++ b/graphics.go @@ -20,37 +20,43 @@ const ( // vertex shader to draw points VERTEX_SHADER = ` - #version 410 +#version 410 - in vec3 vp_model; // vertex position in model coordinate system - in vec3 vn_model; // vertex normal in model coordinate system +in vec3 vp_model; // vertex position in model coordinate system +in vec3 vn_model; // vertex normal in model coordinate system - uniform mat4 trafo; // one single transformation matrix +uniform mat4 trafo; // one single transformation matrix - out vec3 vn_eye; // vertex normal in eye coordinate system +out vec3 vn_eye; // vertex normal in eye coordinate system - void main() { - vn_eye = vec3(trafo * vec4(vn_model, 0.0)); // 0.0 because translation is ignored - gl_Position = trafo * vec4(vp_model, 1.0); - } - ` + "\x00" +void main() { + + // 0.0 because translation is ignored + vn_eye = vec3(trafo * vec4(vn_model, 0.0)); + + gl_Position = trafo * vec4(vp_model, 1.0); + +} + ` + "\x00" // fragment shader to draw surfaces FRAGMENT_SHADER = ` - #version 410 +#version 410 - in vec3 vn_eye; // vertex normal in eye coordinate system +in vec3 vn_eye; // vertex normal in eye coordinate system - out vec4 frag_colour; +out vec4 frag_colour; - void main() { - vec3 ambient = vec3(0.3, 0.3, 0.3); // ambient colour is static - vec3 light_vector_eye = vec3(1.0, 0.1, -1.0); - vec3 diffuse = (vec3(1, 1, 1) - ambient) * max(dot(normalize(vn_eye), normalize(light_vector_eye)), 0.0); - vec3 color = (ambient + diffuse) * vec3(0, 0, 1); // hard-coded vector is color in RGB format - frag_colour = vec4(color, 1.0); // RGBA color format - } - ` + "\x00" +void main() { + vec3 ambient = vec3(0.3, 0.3, 0.3); // ambient colour is static + vec3 light_vector_eye = vec3(1.0, 0.1, -1.0); + vec3 diffuse = (vec3(1, 1, 1) - ambient) * max(dot(normalize(vn_eye), + normalize(light_vector_eye)), 0.0); + // hard-coded vector is color in RGB format: + vec3 color = (ambient + diffuse) * vec3(0, 0, 1); + frag_colour = vec4(color, 1.0); // RGBA color format +} + ` + "\x00" ) type Graphics struct { @@ -83,7 +89,8 @@ func newGraphics() Graphics { if err != nil { log.Fatal(err) } - graphics.fragmentShader, err = compileShader(FRAGMENT_SHADER, gl.FRAGMENT_SHADER) + graphics.fragmentShader, err = compileShader(FRAGMENT_SHADER, + gl.FRAGMENT_SHADER) if err != nil { log.Fatal(err) } @@ -99,8 +106,6 @@ func newGraphics() Graphics { graphics.vao = makeVao(vertices, vertex_normals) // create transformation matrix - //graphics.trafo = mgl32.HomogRotate3D(float32(glfw.GetTime()) * OMEGA, mgl32.Vec3{ROT_X, ROT_Y, ROT_Z}) - //graphics.trafoUniform = gl.GetUniformLocation(graphics.program, gl.Str("trafo\x00")) gl.UniformMatrix4fv(graphics.trafoUniform, 1, false, &graphics.trafo[0]) return graphics @@ -110,11 +115,13 @@ func (graphics Graphics) draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(graphics.program) - //graphics.trafo = mgl32.HomogRotate3D(float32(glfw.GetTime()) * OMEGA, mgl32.Vec3{ROT_X, ROT_Y, ROT_Z}) gl.UniformMatrix4fv(graphics.trafoUniform, 1, false, &graphics.trafo[0]) gl.BindVertexArray(graphics.vao) - gl.DrawArrays(gl.TRIANGLES, 0, int32(len(vertices)/3)) // POINTS, LINES, LINE_STRIP, LINE_LOOP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN + + // or alternatively POINTS, LINES, LINE_STRIP, LINE_LOOP, TRIANGLES, + // TRIANGLE_STRIP, TRIANGLE_FAN + gl.DrawArrays(gl.TRIANGLES, 0, int32(len(vertices)/3)) } // makeVao initializes and returns a vertex array from the points provided. @@ -125,13 +132,15 @@ func makeVao(points []float32, normals []float32) uint32 { var position_vbo uint32 // VBO ID gl.GenBuffers(1, &position_vbo) gl.BindBuffer(gl.ARRAY_BUFFER, position_vbo) - gl.BufferData(gl.ARRAY_BUFFER, 4*len(points), gl.Ptr(points), gl.STATIC_DRAW) + gl.BufferData(gl.ARRAY_BUFFER, 4*len(points), gl.Ptr(points), + gl.STATIC_DRAW) // vertex buffer object (VBO) for normals: var normal_vbo uint32 // VBO ID gl.GenBuffers(1, &normal_vbo) gl.BindBuffer(gl.ARRAY_BUFFER, normal_vbo) - gl.BufferData(gl.ARRAY_BUFFER, 4*len(normals), gl.Ptr(normals), gl.STATIC_DRAW) + gl.BufferData(gl.ARRAY_BUFFER, 4*len(normals), gl.Ptr(normals), + gl.STATIC_DRAW) // vertex array objects (VAO) to combine VBOs: var vao uint32 // VAO ID @@ -140,12 +149,14 @@ func makeVao(points []float32, normals []float32) uint32 { // connect position_vbo to vao gl.BindBuffer(gl.ARRAY_BUFFER, position_vbo) - gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) // tell GL to use 3D float vectors + // tell GL to use 3D float vectors: + gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(0) // connect normal_vbo to vao gl.BindBuffer(gl.ARRAY_BUFFER, normal_vbo) - gl.VertexAttribPointer(1, 3, gl.FLOAT, false, 0, nil) // tell GL to use 3D float vectors + // tell GL to use 3D float vectors: + gl.VertexAttribPointer(1, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(1) return vao |