| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Go Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 // An app that draws a green triangle on a red background. | |
| 6 package main | |
| 7 | |
| 8 import ( | |
| 9 "encoding/binary" | |
| 10 "log" | |
| 11 | |
| 12 "golang.org/x/mobile/app" | |
| 13 "golang.org/x/mobile/app/debug" | |
| 14 "golang.org/x/mobile/event" | |
| 15 "golang.org/x/mobile/f32" | |
| 16 "golang.org/x/mobile/geom" | |
| 17 "golang.org/x/mobile/gl" | |
| 18 "golang.org/x/mobile/gl/glutil" | |
| 19 ) | |
| 20 | |
| 21 var ( | |
| 22 program gl.Program | |
| 23 position gl.Attrib | |
| 24 offset gl.Uniform | |
| 25 color gl.Uniform | |
| 26 buf gl.Buffer | |
| 27 | |
| 28 green float32 | |
| 29 touchLoc geom.Point | |
| 30 ) | |
| 31 | |
| 32 func main() { | |
| 33 app.Run(app.Callbacks{ | |
| 34 Start: start, | |
| 35 Stop: stop, | |
| 36 Draw: draw, | |
| 37 Touch: touch, | |
| 38 }) | |
| 39 } | |
| 40 | |
| 41 func start() { | |
| 42 var err error | |
| 43 program, err = glutil.CreateProgram(vertexShader, fragmentShader) | |
| 44 if err != nil { | |
| 45 log.Printf("error creating GL program: %v", err) | |
| 46 return | |
| 47 } | |
| 48 | |
| 49 buf = gl.GenBuffer() | |
| 50 gl.BindBuffer(gl.ARRAY_BUFFER, buf) | |
| 51 gl.BufferData(gl.ARRAY_BUFFER, gl.STATIC_DRAW, triangleData) | |
| 52 | |
| 53 position = gl.GetAttribLocation(program, "position") | |
| 54 color = gl.GetUniformLocation(program, "color") | |
| 55 offset = gl.GetUniformLocation(program, "offset") | |
| 56 touchLoc = geom.Point{geom.Width / 2, geom.Height / 2} | |
| 57 | |
| 58 // TODO(crawshaw): the debug package needs to put GL state init here | |
| 59 } | |
| 60 | |
| 61 func stop() { | |
| 62 gl.DeleteProgram(program) | |
| 63 gl.DeleteBuffer(buf) | |
| 64 } | |
| 65 | |
| 66 func touch(t event.Touch) { | |
| 67 touchLoc = t.Loc | |
| 68 } | |
| 69 | |
| 70 func draw() { | |
| 71 gl.ClearColor(1, 0, 0, 1) | |
| 72 gl.Clear(gl.COLOR_BUFFER_BIT) | |
| 73 | |
| 74 gl.UseProgram(program) | |
| 75 | |
| 76 green += 0.01 | |
| 77 if green > 1 { | |
| 78 green = 0 | |
| 79 } | |
| 80 gl.Uniform4f(color, 0, green, 0, 1) | |
| 81 | |
| 82 gl.Uniform2f(offset, float32(touchLoc.X/geom.Width), float32(touchLoc.Y/
geom.Height)) | |
| 83 | |
| 84 gl.BindBuffer(gl.ARRAY_BUFFER, buf) | |
| 85 gl.EnableVertexAttribArray(position) | |
| 86 gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) | |
| 87 gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) | |
| 88 gl.DisableVertexAttribArray(position) | |
| 89 | |
| 90 debug.DrawFPS() | |
| 91 } | |
| 92 | |
| 93 var triangleData = f32.Bytes(binary.LittleEndian, | |
| 94 0.0, 0.4, 0.0, // top left | |
| 95 0.0, 0.0, 0.0, // bottom left | |
| 96 0.4, 0.0, 0.0, // bottom right | |
| 97 ) | |
| 98 | |
| 99 const ( | |
| 100 coordsPerVertex = 3 | |
| 101 vertexCount = 3 | |
| 102 ) | |
| 103 | |
| 104 const vertexShader = `#version 100 | |
| 105 uniform vec2 offset; | |
| 106 | |
| 107 attribute vec4 position; | |
| 108 void main() { | |
| 109 // offset comes in with x/y values between 0 and 1. | |
| 110 // position bounds are -1 to 1. | |
| 111 vec4 offset4 = vec4(2.0*offset.x-1.0, 1.0-2.0*offset.y, 0, 0); | |
| 112 gl_Position = position + offset4; | |
| 113 }` | |
| 114 | |
| 115 const fragmentShader = `#version 100 | |
| 116 precision mediump float; | |
| 117 uniform vec4 color; | |
| 118 void main() { | |
| 119 gl_FragColor = color; | |
| 120 }` | |
| OLD | NEW |