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.
|
|
|
|
|
2014-10-28 22:19:38 +00:00
|
|
|
// An app that draws a green triangle on a red background.
|
2015-04-21 21:51:13 +00:00
|
|
|
//
|
|
|
|
// Note: This demo is an early preview of Go 1.5. In order to build this
|
2015-05-01 19:30:20 +00:00
|
|
|
// program as an Android APK using the gomobile tool.
|
2015-04-21 21:51:13 +00:00
|
|
|
//
|
2015-05-01 19:30:20 +00:00
|
|
|
// See http://godoc.org/golang.org/x/mobile/cmd/gomobile to install gomobile.
|
2015-04-21 21:51:13 +00:00
|
|
|
//
|
|
|
|
// Get the basic example and use gomobile to build or install it on your device.
|
|
|
|
//
|
|
|
|
// $ go get -d golang.org/x/mobile/example/basic
|
|
|
|
// $ gomobile build golang.org/x/mobile/example/basic # will build an APK
|
|
|
|
//
|
|
|
|
// # plug your Android device to your computer or start an Android emulator.
|
|
|
|
// # if you have adb installed on your machine, use gomobile install to
|
|
|
|
// # build and deploy the APK to an Android target.
|
|
|
|
// $ gomobile install golang.org/x/mobile/example/basic
|
|
|
|
//
|
|
|
|
// Switch to your device or emulator to start the Basic application from
|
|
|
|
// the launcher.
|
|
|
|
// You can also run the application on your desktop by running the command
|
|
|
|
// below. (Note: It currently doesn't work on Windows.)
|
|
|
|
// $ go install golang.org/x/mobile/example/basic && basic
|
2014-09-10 00:25:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"log"
|
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/mobile/app"
|
|
|
|
"golang.org/x/mobile/app/debug"
|
|
|
|
"golang.org/x/mobile/event"
|
|
|
|
"golang.org/x/mobile/f32"
|
|
|
|
"golang.org/x/mobile/geom"
|
|
|
|
"golang.org/x/mobile/gl"
|
|
|
|
"golang.org/x/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() {
|
|
|
|
app.Run(app.Callbacks{
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
Start: start,
|
|
|
|
Stop: stop,
|
|
|
|
Draw: draw,
|
|
|
|
Touch: touch,
|
|
|
|
Config: config,
|
2014-09-10 00:25:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-19 15:28:05 +00:00
|
|
|
func start() {
|
2014-09-10 00:25:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-24 03:18:30 +00:00
|
|
|
buf = gl.CreateBuffer()
|
2014-09-10 00:25:47 +00:00
|
|
|
gl.BindBuffer(gl.ARRAY_BUFFER, buf)
|
2015-03-24 03:18:30 +00:00
|
|
|
gl.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)
|
2014-09-10 00:25:47 +00:00
|
|
|
|
2014-09-11 23:14:12 +00:00
|
|
|
position = gl.GetAttribLocation(program, "position")
|
|
|
|
color = gl.GetUniformLocation(program, "color")
|
|
|
|
offset = gl.GetUniformLocation(program, "offset")
|
2014-12-19 15:28:05 +00:00
|
|
|
|
|
|
|
// TODO(crawshaw): the debug package needs to put GL state init here
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
// Can this be an event.Register call now??
|
2014-12-19 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stop() {
|
|
|
|
gl.DeleteProgram(program)
|
|
|
|
gl.DeleteBuffer(buf)
|
2014-09-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
func config(new, old event.Config) {
|
|
|
|
touchLoc = geom.Point{new.Width / 2, new.Height / 2}
|
|
|
|
}
|
|
|
|
|
|
|
|
func touch(t event.Touch, c event.Config) {
|
2014-09-11 23:14:12 +00:00
|
|
|
touchLoc = t.Loc
|
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
func draw(c event.Config) {
|
2014-09-10 00:25:47 +00:00
|
|
|
gl.ClearColor(1, 0, 0, 1)
|
2014-10-28 22:19:38 +00:00
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT)
|
2014-09-10 00:25:47 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
gl.Uniform2f(offset, float32(touchLoc.X/c.Width), float32(touchLoc.Y/c.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
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 06:41:48 +00:00
|
|
|
debug.DrawFPS(c)
|
2014-09-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 22:19:38 +00:00
|
|
|
var triangleData = f32.Bytes(binary.LittleEndian,
|
|
|
|
0.0, 0.4, 0.0, // top left
|
|
|
|
0.0, 0.0, 0.0, // bottom left
|
|
|
|
0.4, 0.0, 0.0, // bottom right
|
|
|
|
)
|
2014-09-10 00:25:47 +00:00
|
|
|
|
2014-10-28 22:19:38 +00:00
|
|
|
const (
|
|
|
|
coordsPerVertex = 3
|
|
|
|
vertexCount = 3
|
|
|
|
)
|
2014-09-10 00:25:47 +00:00
|
|
|
|
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
|
|
|
}`
|