Skip to content

Commit

Permalink
Complete desktop webgl path.
Browse files Browse the repository at this point in the history
Depends on ajhager/webgl#2 being merged.
  • Loading branch information
dmitshur committed Dec 26, 2014
1 parent 9d0fe92 commit 6398c5a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 14 deletions.
118 changes: 115 additions & 3 deletions 58/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand Down
31 changes: 20 additions & 11 deletions 58/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
`
Expand All @@ -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

Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

1 comment on commit 6398c5a

@dmitshur
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on ajhager/webgl#2 being merged.

Please sign in to comment.