forked from rainliu/gocl
/
main.go
150 lines (122 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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 app that draws a blue triangle on a red background.
package main
import (
"encoding/binary"
"log"
"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"
"golang.org/x/mobile/cl"
)
var (
numPlatforms cl.CL_uint
program gl.Program
position gl.Attrib
offset gl.Uniform
color gl.Uniform
buf gl.Buffer
blue float32
touchLoc geom.Point
)
func main() {
app.Run(app.Callbacks{
Draw: draw,
Touch: touch,
})
}
// TODO(crawshaw): Need an easier way to do GL-dependent initialization.
func initGL() {
var err error
program, err = glutil.CreateProgram(vertexShader, fragmentShader)
if err != nil {
log.Printf("error creating GL program: %v", err)
return
}
buf = gl.GenBuffer()
gl.BindBuffer(gl.ARRAY_BUFFER, buf)
gl.BufferData(gl.ARRAY_BUFFER, gl.STATIC_DRAW, triangleData)
position = gl.GetAttribLocation(program, "position")
color = gl.GetUniformLocation(program, "color")
offset = gl.GetUniformLocation(program, "offset")
touchLoc = geom.Point{geom.Width / 2, geom.Height / 2}
}
/*
func initCL(){
var errNum cl.CL_int
//var context cl.CL_context
// First, query the total number of platforms
errNum = cl.CLGetPlatformIDs(0, nil, &numPlatforms)
if errNum != cl.CL_SUCCESS || numPlatforms <= 0 {
log.Printf("Failed to find any OpenCL platform.")
return
}
// Next, allocate memory for the installed plaforms, and qeury
// to get the list.
platformIds = make([]cl.CL_platform_id, numPlatforms)
// First, query the total number of platforms
errNum = cl.CLGetPlatformIDs(numPlatforms, platformIds, nil)
if errNum != cl.CL_SUCCESS {
log.Printf("Failed to find any OpenCL platforms.")
return
}
log.Printf("Number of platforms: \t%d\n", numPlatforms)
}*/
func touch(t event.Touch) {
touchLoc = t.Loc
}
func draw() {
if program.Value == 0 {
initGL()
initCL()
}
if numPlatforms == 0{
gl.ClearColor(1, 0, 0, 1)
}else{
gl.ClearColor(0, 1, 0, 1)
}
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.UseProgram(program)
blue += 0.01
if blue > 1 {
blue = 0
}
gl.Uniform4f(color, 0, 0, blue, 1)
gl.Uniform2f(offset, float32(touchLoc.X/geom.Width), float32(touchLoc.Y/geom.Height))
gl.BindBuffer(gl.ARRAY_BUFFER, buf)
gl.EnableVertexAttribArray(position)
gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
gl.DrawArrays(gl.TRIANGLES, 0, vertexCount)
gl.DisableVertexAttribArray(position)
debug.DrawFPS()
}
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
)
const (
coordsPerVertex = 3
vertexCount = 3
)
const vertexShader = `#version 100
uniform vec2 offset;
attribute vec4 position;
void main() {
// 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;
}`
const fragmentShader = `#version 100
precision mediump float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`