2014-09-10 00:25:47 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// An android app that draws a green triangle on a red background.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"code.google.com/p/go.mobile/app"
|
2014-09-28 19:17:34 +00:00
|
|
|
"code.google.com/p/go.mobile/app/debug"
|
2014-09-11 23:14:12 +00:00
|
|
|
"code.google.com/p/go.mobile/event"
|
|
|
|
"code.google.com/p/go.mobile/geom"
|
2014-09-10 00:25:47 +00:00
|
|
|
"code.google.com/p/go.mobile/gl"
|
2014-09-11 23:14:12 +00:00
|
|
|
"code.google.com/p/go.mobile/gl/glutil"
|
2014-09-10 00:25:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-09-11 23:14:12 +00:00
|
|
|
program gl.Program
|
|
|
|
position gl.Attrib
|
|
|
|
offset gl.Uniform
|
|
|
|
color gl.Uniform
|
|
|
|
buf gl.Buffer
|
|
|
|
|
|
|
|
green float32
|
|
|
|
touchLoc geom.Point
|
2014-09-10 00:25:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-09-28 19:17:34 +00:00
|
|
|
log.Printf("basic example starting")
|
2014-09-10 00:25:47 +00:00
|
|
|
app.Run(app.Callbacks{
|
2014-09-11 23:14:12 +00:00
|
|
|
Draw: draw,
|
|
|
|
Touch: touch,
|
2014-09-10 00:25:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(crawshaw): Need an easier way to do GL-dependent initialization.
|
|
|
|
|
|
|
|
func initGL() {
|
|
|
|
var err error
|
2014-09-11 23:14:12 +00:00
|
|
|
program, err = glutil.CreateProgram(vertexShader, fragmentShader)
|
2014-09-10 00:25:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating GL program: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-28 19:17:34 +00:00
|
|
|
buf = gl.GenBuffer()
|
2014-09-10 00:25:47 +00:00
|
|
|
gl.BindBuffer(gl.ARRAY_BUFFER, buf)
|
|
|
|
gl.BufferData(gl.ARRAY_BUFFER, gl.STATIC_DRAW, triangleData)
|
|
|
|
|
2014-09-11 23:14:12 +00:00
|
|
|
position = gl.GetAttribLocation(program, "position")
|
|
|
|
color = gl.GetUniformLocation(program, "color")
|
|
|
|
offset = gl.GetUniformLocation(program, "offset")
|
|
|
|
touchLoc = geom.Point{geom.Width / 2, geom.Height / 2}
|
2014-09-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 23:14:12 +00:00
|
|
|
func touch(t event.Touch) {
|
|
|
|
touchLoc = t.Loc
|
|
|
|
}
|
|
|
|
|
|
|
|
func draw() {
|
2014-09-28 19:17:34 +00:00
|
|
|
if program.Value == 0 {
|
2014-09-10 00:25:47 +00:00
|
|
|
initGL()
|
|
|
|
log.Printf("example/basic rendering initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.ClearColor(1, 0, 0, 1)
|
|
|
|
gl.Clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT)
|
|
|
|
|
|
|
|
gl.UseProgram(program)
|
|
|
|
|
|
|
|
green += 0.01
|
|
|
|
if green > 1 {
|
|
|
|
green = 0
|
|
|
|
}
|
2014-09-11 23:14:12 +00:00
|
|
|
gl.Uniform4f(color, 0, green, 0, 1)
|
|
|
|
|
|
|
|
gl.Uniform2f(offset, float32(touchLoc.X/geom.Width), float32(touchLoc.Y/geom.Height))
|
2014-09-10 00:25:47 +00:00
|
|
|
|
|
|
|
gl.BindBuffer(gl.ARRAY_BUFFER, buf)
|
2014-09-11 23:14:12 +00:00
|
|
|
gl.EnableVertexAttribArray(position)
|
|
|
|
gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
|
2014-09-10 00:25:47 +00:00
|
|
|
gl.DrawArrays(gl.TRIANGLES, 0, vertexCount)
|
2014-09-11 23:14:12 +00:00
|
|
|
gl.DisableVertexAttribArray(position)
|
2014-09-28 19:17:34 +00:00
|
|
|
|
|
|
|
debug.DrawFPS()
|
2014-09-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var triangleData []byte
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := binary.Write(buf, binary.LittleEndian, triangleCoords); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
triangleData = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
const coordsPerVertex = 3
|
|
|
|
|
|
|
|
var triangleCoords = []float32{
|
2014-09-11 23:14:12 +00:00
|
|
|
0, 0.4, 0, // top left
|
|
|
|
0, 0, 0, // bottom left
|
|
|
|
0.4, 0, 0, // bottom right
|
2014-09-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
var vertexCount = len(triangleCoords) / coordsPerVertex
|
|
|
|
|
2014-10-28 01:08:26 +00:00
|
|
|
const vertexShader = `#version 100
|
2014-09-11 23:14:12 +00:00
|
|
|
uniform vec2 offset;
|
|
|
|
|
|
|
|
attribute vec4 position;
|
2014-09-10 00:25:47 +00:00
|
|
|
void main() {
|
2014-09-11 23:14:12 +00:00
|
|
|
// offset comes in with x/y values between 0 and 1.
|
|
|
|
// position bounds are -1 to 1.
|
|
|
|
vec4 offset4 = vec4(2.0*offset.x-1.0, 1.0-2.0*offset.y, 0, 0);
|
|
|
|
gl_Position = position + offset4;
|
2014-09-10 00:25:47 +00:00
|
|
|
}`
|
|
|
|
|
2014-10-28 01:08:26 +00:00
|
|
|
const fragmentShader = `#version 100
|
2014-09-10 00:25:47 +00:00
|
|
|
precision mediump float;
|
2014-09-11 23:14:12 +00:00
|
|
|
uniform vec4 color;
|
2014-09-10 00:25:47 +00:00
|
|
|
void main() {
|
2014-09-11 23:14:12 +00:00
|
|
|
gl_FragColor = color;
|
2014-09-10 00:25:47 +00:00
|
|
|
}`
|