func NewButton(ctx gl.Context, x, y float32, w, h float32) *Button { btn := &Button{r: 1, g: 1, b: 1, a: 1} btn.x = (-(-1 - x)) / 2 btn.y = (1 - y) / 2 btn.w, btn.h = (w)/2, (-h)/2 btn.verts = []float32{ x, y, 0, x + w, y, 0, x, y + h, 0, x, y + h, 0, x + w, y, 0, x + w, y + h, 0, } btn.data = make([]byte, len(btn.verts)*4) var err error btn.program, err = glutil.CreateProgram(ctx, vertexShader, fragmentShader) if err != nil { panic(fmt.Errorf("error creating GL program: %v", err)) } // create and alloc hw buf btn.buf = ctx.CreateBuffer() ctx.BindBuffer(gl.ARRAY_BUFFER, btn.buf) ctx.BufferData(gl.ARRAY_BUFFER, make([]byte, len(btn.verts)*4), gl.STATIC_DRAW) btn.position = ctx.GetAttribLocation(btn.program, "position") btn.color = ctx.GetUniformLocation(btn.program, "color") return btn }
// TODO just how many samples do we want/need to display something useful? func NewWaveform(ctx gl.Context, n int, in Sound) (*Waveform, error) { wf := &Waveform{Sound: in} wf.outs = make([][]float64, n) for i := range wf.outs { wf.outs[i] = make([]float64, in.BufferLen()*in.Channels()) } wf.samples = make([]float64, in.BufferLen()*in.Channels()*n) // wf.aligned = make([]float64, in.BufferLen()*in.Channels()*n) wf.verts = make([]float32, len(wf.samples)*3) wf.data = make([]byte, len(wf.verts)*4) if ctx == nil { return wf, nil } var err error wf.program, err = glutil.CreateProgram(ctx, vertexShader, fragmentShader) if err != nil { return nil, fmt.Errorf("error creating GL program: %v", err) } // create and alloc hw buf wf.buf = ctx.CreateBuffer() ctx.BindBuffer(gl.ARRAY_BUFFER, wf.buf) ctx.BufferData(gl.ARRAY_BUFFER, make([]byte, len(wf.samples)*12), gl.STREAM_DRAW) wf.position = ctx.GetAttribLocation(wf.program, "position") wf.color = ctx.GetUniformLocation(wf.program, "color") return wf, nil }
func onPaint(glctx gl.Context, sz size.Event) { glctx.ClearColor(1, 1, 1, 1) // クリアする色 glctx.Clear(gl.COLOR_BUFFER_BIT) // 塗りつぶし? now := clock.Time(time.Since(startTime) * 60 / time.Second) // 60FramePerSecで計算し、現在のフレームを計算する game.Update(now) eng.Render(scene, now, sz) }
func onPaint(glctx gl.Context, sz size.Event) { glctx.ClearColor(1, 1, 1, 1) // white background glctx.Clear(gl.COLOR_BUFFER_BIT) now := clock.Time(time.Since(startTime) * 60 / time.Second) eng.Render(scene, now, sz) fps.Draw(sz) }
func onPaint(glctx gl.Context, sz size.Event) { glctx.ClearColor(0, 0, 0, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) now := clock.Time(time.Since(startTime) * 60 / time.Second) eng.Render(scene, now, sz) log.Draw(sz) }
func onPaint(glctx gl.Context, sz size.Event) { glctx.ClearColor(1, 1, 1, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) now := clock.Time(time.Since(startTime) * 60 / time.Second) game.Update(now) eng.Render(scene, now, sz) }
func onPaint(glctx gl.Context, sz size.Event) { glctx.ClearColor(236, 240, 241, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) columnOffset := (sz.WidthPt / 2) - ((geom.Pt(gridColumnsCount) * tileSize) / 2) rowOffset := (sz.HeightPt / 2) - ((geom.Pt(gridrowsCount) * tileSize) / 2) for c := 0; c < gridColumnsCount; c++ { for r := 0; r < gridrowsCount; r++ { t := &grid[((c+1)*(r+1))-1] t.columnOffset = columnOffset t.rowOffset = rowOffset t.x = geom.Pt(c) * tileSize t.y = geom.Pt(r) * tileSize t.Draw(sz) } } }
func (tex Texture) Bind(ctx gl.Context, options ...func(gl.Context, Texture)) { ctx.ActiveTexture(gl.Enum(uint32(gl.TEXTURE0) + tex.Value - 1)) ctx.BindTexture(gl.TEXTURE_2D, tex.Texture) for _, opt := range options { opt(ctx, tex) } }
func onPaint(ctx gl.Context) { ctx.ClearColor(material.BlueGrey100.RGBA()) ctx.Clear(gl.COLOR_BUFFER_BIT) env.Draw(ctx) now := time.Now() fps = int(time.Second / now.Sub(lastpaint)) lastpaint = now }
func (prg Program) Mat4(ctx gl.Context, dst gl.Uniform, src f32.Mat4) { ctx.UniformMatrix4fv(dst, []float32{ src[0][0], src[1][0], src[2][0], src[3][0], src[0][1], src[1][1], src[2][1], src[3][1], src[0][2], src[1][2], src[2][2], src[3][2], src[0][3], src[1][3], src[2][3], src[3][3], }) }
func (prg Program) Mat4(ctx gl.Context, dst gl.Uniform, src f32.Mat4) { m := make([]float32, 16) for i, v := range src { for j, x := range v { m[i*4+j] = x } } ctx.UniformMatrix4fv(dst, m) }
func onStop(glctx gl.Context) { glctx.DeleteProgram(program) glctx.DeleteBuffer(buf) fps.Release() if statusPainter != nil { statusPainter.Release() } images.Release() }
func (vs *viewState) draw(glctx gl.Context, sz size.Event) { now := time.Now() diff := now.Sub(vs.lastDraw) vs.percentColor -= float32(diff.Seconds() * 0.5) if vs.percentColor < .25 { vs.percentColor = .25 } vs.lastDraw = now select { case change := <-vs.tc: vs.touchChange = change default: } switch vs.touchChange.Type { case touch.TypeMove: case touch.TypeBegin: now := time.Now() if vs.lastTouch.Add(time.Millisecond * 1000).Before(now) { vs.lastTouch = now vs.percentColor = 0.5 select { default: case vs.toggle <- now: } } case touch.TypeEnd: } var pingOk bool var connErr error vs.mu.RLock() pingOk = vs.pingOk connErr = vs.connErr vs.mu.RUnlock() var r, g, b = vs.percentColor, vs.percentColor, vs.percentColor if pingOk { r, b = 0, 0 } else { g, b = 0, 0 } glctx.ClearColor(r, g, b, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) msg := "Tap to toggle garage door" if connErr != nil { msg = connErr.Error() } vs.sq.Draw(glctx, sz, "Garage door opener", msg) }
// TODO pass in an actual Texture ... func NewTextureBuffer(ctx gl.Context, width, height int) *TextureFramebuffer { buf := &TextureFramebuffer{w: width, h: height} buf.fbo = Framebuffer{ctx.CreateFramebuffer()} buf.tex = Texture{ctx.CreateTexture()} buf.def = TextureDef(0, width, height, gl.RGBA, nil) buf.filter = TextureFilter(gl.LINEAR, gl.LINEAR) buf.wrap = TextureWrap(gl.REPEAT, gl.REPEAT) buf.withtex = FramebufferWithTex(buf.tex, 0, buf.def, buf.filter, buf.wrap) return buf }
// NewWhiteKey is a constructor that creates and returns a WhiteKey func NewWhiteKey(glctx gl.Context, note util.KeyNote, sz size.Event, count int) *WhiteKey { newWhiteKey := new(WhiteKey) // Create the coloring and sound for the white key. newWhiteKey.PianoKey = newPianoKey(glctx, whiteKeyRGBColor, note) // Create coordinates for this specific white key newWhiteKey.openGLCoords, newWhiteKey.keyOutline, newWhiteKey.keyOuterBoundary = makeWhiteKeyVector(float32(sz.WidthPx), count) glctx.BufferData(gl.ARRAY_BUFFER, newWhiteKey.openGLCoords, gl.STATIC_DRAW) return newWhiteKey }
// LoadProgram reads shader sources from the asset repository, compiles, and // links them into a program. func LoadProgram(glctx gl.Context, vertexAsset, fragmentAsset string) (program gl.Program, err error) { log.Println("LoadProgram:", vertexAsset, fragmentAsset) program = glctx.CreateProgram() if program.Value == 0 { return gl.Program{}, fmt.Errorf("glutil: no programs available") } err = LoadShaders(glctx, program, vertexAsset, fragmentAsset) return }
// Use this to exit your game safely // It will automatically unload all your assets and dispose the stage // Schedule an exit from the application. On android, this will cause a call to pause() and dispose() some time in the future, // it will not immediately finish your application. // On iOS this should be avoided in production as it breaks Apples guidelines func appStop(glctx gl.Context) { println("Exiting") running = false if currentScene.OnPause != nil { currentScene.OnPause(currentScene) soundsPlayer.Close() } glctx.DeleteProgram(program) glctx.DeleteBuffer(buf) fps.Release() images.Release() }
// NewBlackKey is a constructor that creates and returns a BlackKey func NewBlackKey(leftWhiteKey Key, glctx gl.Context, note util.KeyNote, sz size.Event) *BlackKey { newBlackKey := new(BlackKey) // Create the coloring and sound for the black key. newBlackKey.PianoKey = newPianoKey(glctx, blackKeyRGBColor, note) // Create coordinates for this specific black key newBlackKey.openGLCoords, newBlackKey.keyOutline, newBlackKey.keyOuterBoundary = makeBlackKeyVector(leftWhiteKey) glctx.BufferData(gl.ARRAY_BUFFER, newBlackKey.openGLCoords, gl.STATIC_DRAW) return newBlackKey }
// writeAff3 must only be called while holding windowImpl.glctxMu. func writeAff3(glctx gl.Context, u gl.Uniform, a f64.Aff3) { var m [9]float32 m[0*3+0] = float32(a[0*3+0]) m[0*3+1] = float32(a[1*3+0]) m[0*3+2] = 0 m[1*3+0] = float32(a[0*3+1]) m[1*3+1] = float32(a[1*3+1]) m[1*3+2] = 0 m[2*3+0] = float32(a[0*3+2]) m[2*3+1] = float32(a[1*3+2]) m[2*3+2] = 1 glctx.UniformMatrix3fv(u, m[:]) }
// writeAffine writes the contents of an Affine to a 3x3 matrix GL uniform. func writeAffine(glctx gl.Context, u gl.Uniform, a *f32.Affine) { var m [9]float32 m[0*3+0] = a[0][0] m[0*3+1] = a[1][0] m[0*3+2] = 0 m[1*3+0] = a[0][1] m[1*3+1] = a[1][1] m[1*3+2] = 0 m[2*3+0] = a[0][2] m[2*3+1] = a[1][2] m[2*3+2] = 1 glctx.UniformMatrix3fv(u, m[:]) }
func (buf *TextureFramebuffer) StartSample(ctx gl.Context) { buf.fbo.Bind(ctx, buf.withtex) ctx.GetIntegerv(int32v4, gl.VIEWPORT) ctx.Viewport(0, 0, buf.w, buf.h) ctx.ClearColor(0, 0, 0, 0) ctx.Clear(gl.COLOR_BUFFER_BIT) }
func onStart(ctx gl.Context) { env.SetPalette(material.Palette{ Primary: material.BlueGrey500, Dark: material.BlueGrey700, Light: material.BlueGrey100, Accent: material.DeepOrangeA200, }) quits = []chan struct{}{} sig = make(snd.Discrete, len(material.ExpSig)) copy(sig, material.ExpSig) rsig := make(snd.Discrete, len(material.ExpSig)) copy(rsig, material.ExpSig) rsig.UnitInverse() sig = append(sig, rsig...) sig.NormalizeRange(0, 1) ctx.Enable(gl.BLEND) ctx.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) ctx.Enable(gl.CULL_FACE) ctx.CullFace(gl.BACK) env.Load(ctx) for i := range boxes { boxes[i] = env.NewMaterial(ctx) boxes[i].SetColor(material.BlueGrey200) } }
// newPianoKey creates a PianoKey with color and sound. func newPianoKey(glctx gl.Context, keyColor util.RGBColor, note util.KeyNote) *PianoKey { key := new(PianoKey) key.keyColor = keyColor // Create buffer key.glBuf = glctx.CreateBuffer() glctx.BindBuffer(gl.ARRAY_BUFFER, key.glBuf) // Generate sound _ = al.OpenDevice() key.soundBuffers = al.GenBuffers(1) key.soundSources = al.GenSources(1) key.soundBuffers[0].BufferData(al.FormatStereo8, audio.GenSound(note), audio.SampleRate) key.soundSources[0].QueueBuffers(key.soundBuffers) return key }
func onPaint(glctx gl.Context, sz size.Event, u *uistate.UIState) { if u.CurView == uistate.None { u.ScanChan = make(chan bool) go sync.ScanForSG(u.Ctx, u.ScanChan, u) view.LoadDiscoveryView(u) } glctx.ClearColor(1, 1, 1, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) now := clock.Time(time.Since(u.StartTime) * 60 / time.Second) u.Eng.Render(u.Scene, now, sz) if u.Debug { fps.Draw(sz) } }
func unproject(glctx gl.Context, x, y float32) (mgl32.Vec3, mgl32.Vec3) { var wx, wy float32 var viewport [4]int32 glctx.GetIntegerv(viewport[:], gl.VIEWPORT) wx = x wy = float32(viewport[3]) - y posStart, err := mgl32.UnProject(mgl32.Vec3{wx, wy, 0}, arcball.getMtx(), projectionMtx, int(viewport[0]), int(viewport[1]), int(viewport[2]), int(viewport[3])) posEnd, err := mgl32.UnProject(mgl32.Vec3{wx, wy, 1}, arcball.getMtx(), projectionMtx, int(viewport[0]), int(viewport[1]), int(viewport[2]), int(viewport[3])) if err != nil { log.Printf("unable to unproject: %+v", err) } return posStart, posEnd }
func (buf *uintBuffer) Update(ctx gl.Context, data []uint32) { buf.count = len(data) subok := len(buf.bin) > 0 && len(data)*4 <= len(buf.bin) if !subok { buf.bin = make([]byte, len(data)*4) } for i, u := range data { buf.bin[4*i+0] = byte(u >> 0) buf.bin[4*i+1] = byte(u >> 8) buf.bin[4*i+2] = byte(u >> 16) buf.bin[4*i+3] = byte(u >> 24) } if subok { ctx.BufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, buf.bin) } else { ctx.BufferData(gl.ELEMENT_ARRAY_BUFFER, buf.bin, buf.usage) } }
func onStart(ctx gl.Context) { ctx.Enable(gl.BLEND) ctx.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) ctx.Enable(gl.CULL_FACE) ctx.CullFace(gl.BACK) env.Load(ctx) env.LoadGlyphs(ctx) t112 = env.NewButton(ctx) t112.SetTextColor(material.White) t112.SetText("AAAH`e_llo |jJ go 112px") t112.BehaviorFlags = material.DescriptorFlat t56 = env.NewButton(ctx) t56.SetTextColor(material.White) t56.SetText("Hello go 56px") t56.BehaviorFlags = material.DescriptorFlat t45 = env.NewButton(ctx) t45.SetTextColor(material.White) t45.SetText("Hello go 45px") t45.BehaviorFlags = material.DescriptorFlat t34 = env.NewButton(ctx) t34.SetTextColor(material.White) t34.SetText("Hello go 34px") t34.BehaviorFlags = material.DescriptorFlat t24 = env.NewButton(ctx) t24.SetTextColor(material.White) t24.SetText("Hello go 24px") t24.BehaviorFlags = material.DescriptorFlat t20 = env.NewButton(ctx) t20.SetTextColor(material.White) t20.SetText("Hello go 20px") t20.BehaviorFlags = material.DescriptorFlat t16 = env.NewButton(ctx) t16.SetTextColor(material.White) t16.SetText("Hello go 16px") t16.BehaviorFlags = material.DescriptorFlat t14 = env.NewButton(ctx) t14.SetTextColor(material.White) t14.SetText("Hello go 14px") t14.BehaviorFlags = material.DescriptorFlat t12 = env.NewButton(ctx) t12.SetTextColor(material.White) t12.SetText("Hello go 12px") t12.BehaviorFlags = material.DescriptorFlat }
func onTouch(glctx gl.Context, e touch.Event) { touchX = e.X touchY = e.Y // When touch occurs we need to figure out which key is pressed. // Using color picking for this. // That is, scene is redrawn with each key given a unique color. // And then the color is read on the touched pixel to figure out which key // is pressed or if a key is pressed at all. glctx.ClearColor(0.73, 0.5, 0.75, 0.5) glctx.Clear(gl.DEPTH_BUFFER_BIT) glctx.Clear(gl.COLOR_BUFFER_BIT) glctx.UseProgram(program) board.DrawI() // Draw the board with each key in a unique color c := make([]byte, 12, 12) glctx.ReadPixels(c, int(e.X), int(e.Y), 1, 1, gl.RGB, gl.UNSIGNED_BYTE) // gl.RGB, gl.UNSIGNED_BYTE is the combination that is preffered by my Android // phone. And is said to be the one preffered by many. r := (float32(c[0]) / 255) * 100 // Convert byte to float out := float32(math.Floor(float64(r)+0.5)) / 100 // and round up key, ok := board.idColorKey[out] if !ok { curKey, ok := keystate[e.Sequence] //stop already playing sound if ok { StopSound(curKey) } return } if e.Type == touch.TypeBegin { keystate[e.Sequence] = key PlaySound(key) } else if e.Type == touch.TypeEnd { delete(keystate, e.Sequence) StopSound(key) } else if e.Type == touch.TypeMove { if keystate[e.Sequence] != key { // Drag has moved out of initial key curKey, ok := keystate[e.Sequence] //stop already playing sound if ok { StopSound(curKey) } PlaySound(key) // play new key's sound keystate[e.Sequence] = key } } }
func onDraw(glctx gl.Context, sz size.Event) { select { case <-determined: if ok { glctx.ClearColor(0, 1, 0, 1) } else { glctx.ClearColor(1, 0, 0, 1) } default: glctx.ClearColor(0, 0, 0, 1) } glctx.Clear(gl.COLOR_BUFFER_BIT) }
func (m *Module) draw(glctx gl.Context, sz size.Event, images *glutil.Images) { serverResponse := m.Network.Response() glctx.ClearColor(1, 1, 1, 1) glctx.Clear(gl.COLOR_BUFFER_BIT) var display render.Display display.Loaded = m.loaded if len(serverResponse.Predictions) > 0 { display.NextOK = true display.NextTrainMinutes = serverResponse.Predictions[0].Minutes display.TransitRouteName = fmt.Sprintf("%s (%s)", serverResponse.Stop.Route, serverResponse.Stop.Direction) if len(serverResponse.Predictions) > 1 { display.NextNextOK = true display.NextNextTrainMinutes = serverResponse.Predictions[1].Minutes } display.UpdatedSecondsAgo = int(time.Now().Sub(serverResponse.LastRefresh).Seconds()) display.PredictionSource = serverResponse.Predictions[0].Source } m.Render.Display(display, sz, glctx, images) }