From 6398c5aa60a530d12de8b8616c649ff88161c45a Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Thu, 25 Dec 2014 20:19:06 -0800 Subject: [PATCH] Complete desktop webgl path. Depends on ajhager/webgl#2 being merged. --- 58/desktop.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++++-- 58/script.go | 31 ++++++++----- 2 files changed, 135 insertions(+), 14 deletions(-) diff --git a/58/desktop.go b/58/desktop.go index 56bfd3a..fe9dbb0 100644 --- a/58/desktop.go +++ b/58/desktop.go @@ -3,14 +3,109 @@ package main import ( + "fmt" "runtime" "github.com/ajhager/webgl" + "github.com/go-gl/mathgl/mgl32" glfw "github.com/shurcooL/glfw3" ) var gl *webgl.Context +const ( + vertexSource = `#version 120 + +attribute vec3 aVertexPosition; + +uniform mat4 uMVMatrix; +uniform mat4 uPMatrix; + +void main() { + gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); +} +` + fragmentSource = `#version 120 + +void main() { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} +` +) + +var program *webgl.Program +var pMatrixUniform *webgl.UniformLocation +var mvMatrixUniform *webgl.UniformLocation + +var mvMatrix mgl32.Mat4 +var pMatrix mgl32.Mat4 + +var itemSize int +var numItems int + +func initShaders() error { + vertexShader := gl.CreateShader(gl.VERTEX_SHADER) + gl.ShaderSource(vertexShader, vertexSource) + gl.CompileShader(vertexShader) + defer gl.DeleteShader(vertexShader) + + fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER) + gl.ShaderSource(fragmentShader, fragmentSource) + gl.CompileShader(fragmentShader) + defer gl.DeleteShader(fragmentShader) + + program = gl.CreateProgram() + gl.AttachShader(program, vertexShader) + gl.AttachShader(program, fragmentShader) + gl.LinkProgram(program) + + /*if !gl.GetProgramParameterb(program, gl.LINK_STATUS) { + return errors.New("LINK_STATUS") + }*/ + + gl.ValidateProgram(program) + /*if !gl.GetProgramParameterb(program, gl.VALIDATE_STATUS) { + return errors.New("VALIDATE_STATUS") + }*/ + + gl.UseProgram(program) + + pMatrixUniform = gl.GetUniformLocation(program, "uPMatrix") + mvMatrixUniform = gl.GetUniformLocation(program, "uMVMatrix") + + if glError := gl.GetError(); glError != 0 { + return fmt.Errorf("gl.GetError: %v", glError) + } + + return nil +} + +func createVbo() error { + triangleVertexPositionBuffer := gl.CreateBuffer() + gl.BindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer) + vertices := []float32{ + 0, 0, 0, + 300, 100, 0, + 0, 100, 0, + } + gl.BufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) + itemSize = 3 + numItems = 3 + + vertexPositionAttribute := gl.GetAttribLocation(program, "aVertexPosition") + gl.EnableVertexAttribArray(vertexPositionAttribute) + gl.VertexAttribPointer(vertexPositionAttribute, itemSize, gl.FLOAT, false, 0, 0) + + if glError := gl.GetError(); glError != 0 { + return fmt.Errorf("gl.GetError: %v", glError) + } + + return nil +} + +const viewportWidth = 400 +const viewportHeight = 400 + func init() { runtime.LockOSThread() } @@ -24,19 +119,36 @@ func main() { gl = webgl.NewContext() - window, err := glfw.CreateWindow(400, 400, "Testing", nil, nil) + window, err := glfw.CreateWindow(viewportWidth, viewportHeight, "Testing", nil, nil) if err != nil { panic(err) } - window.MakeContextCurrent() + err = initShaders() + if err != nil { + panic(err) + } + err = createVbo() + if err != nil { + panic(err) + } + gl.ClearColor(0.8, 0.3, 0.01, 1) for !mustBool(window.ShouldClose()) { - // Do OpenGL stuff gl.Clear(gl.COLOR_BUFFER_BIT) + gl.Viewport(0, 0, viewportWidth, viewportHeight) + + pMatrix = mgl32.Ortho2D(0, float32(viewportWidth), float32(viewportHeight), 0) + + mvMatrix = mgl32.Translate3D(50, 100, 0) + + gl.UniformMatrix4fv(pMatrixUniform, false, pMatrix[:]) + gl.UniformMatrix4fv(mvMatrixUniform, false, mvMatrix[:]) + gl.DrawArrays(gl.TRIANGLES, 0, numItems) + window.SwapBuffers() glfw.PollEvents() } diff --git a/58/script.go b/58/script.go index dc3fc0d..9258ff5 100644 --- a/58/script.go +++ b/58/script.go @@ -6,11 +6,10 @@ import ( "errors" "fmt" - "honnef.co/go/js/dom" - "github.com/ajhager/webgl" "github.com/go-gl/mathgl/mgl32" "github.com/gopherjs/gopherjs/js" + "honnef.co/go/js/dom" ) var gl *webgl.Context @@ -23,13 +22,13 @@ attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; -void main(void) { +void main() { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); } ` fragmentSource = `#version 100 -void main(void) { +void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } ` @@ -39,10 +38,6 @@ var program *webgl.Program var pMatrixUniform *webgl.UniformLocation var mvMatrixUniform *webgl.UniformLocation -/*var program js.Object -var pMatrixUniform js.Object -var mvMatrixUniform js.Object*/ - var mvMatrix mgl32.Mat4 var pMatrix mgl32.Mat4 @@ -79,10 +74,14 @@ func initShaders() error { pMatrixUniform = gl.GetUniformLocation(program, "uPMatrix") mvMatrixUniform = gl.GetUniformLocation(program, "uMVMatrix") + if glError := gl.GetError(); glError != 0 { + return fmt.Errorf("gl.GetError: %v", glError) + } + return nil } -func createVbo() { +func createVbo() error { triangleVertexPositionBuffer := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer) vertices := []float32{ @@ -97,6 +96,12 @@ func createVbo() { vertexPositionAttribute := gl.GetAttribLocation(program, "aVertexPosition") gl.EnableVertexAttribArray(vertexPositionAttribute) gl.VertexAttribPointer(vertexPositionAttribute, itemSize, gl.FLOAT, false, 0, 0) + + if glError := gl.GetError(); glError != 0 { + return fmt.Errorf("gl.GetError: %v", glError) + } + + return nil } const viewportWidth = 400 @@ -132,13 +137,17 @@ func main() { if err != nil { panic(err) } - createVbo() + err = createVbo() + if err != nil { + panic(err) + } gl.ClearColor(0.8, 0.3, 0.01, 1) - gl.Clear(gl.COLOR_BUFFER_BIT) // Draw scene. { + gl.Clear(gl.COLOR_BUFFER_BIT) + gl.Viewport(0, 0, viewportWidth, viewportHeight) pMatrix = mgl32.Ortho2D(0, float32(viewportWidth), float32(viewportHeight), 0)