// OpenGL draw function func draw() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() gl.Begin(gl.LINES) gl.Color3f(.2, .2, .2) for i := range staticLines { x := staticLines[i].GetAsSegment().A.X y := staticLines[i].GetAsSegment().A.Y gl.Vertex3f(float32(x), float32(y), 0) x = staticLines[i].GetAsSegment().B.X y = staticLines[i].GetAsSegment().B.Y gl.Vertex3f(float32(x), float32(y), 0) } gl.End() gl.Color4f(.3, .3, 1, .8) // draw balls for _, ball := range balls { gl.PushMatrix() pos := ball.Body.Position() rot := ball.Body.Angle() * chipmunk.DegreeConst gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) gl.Rotatef(float32(rot), 0, 0, 1) drawCircle(float64(ballRadius), 60) gl.PopMatrix() } }
func (c *Context) init() { if err := gl.Init(); err != nil { panic(err) } // Textures' pixel formats are alpha premultiplied. gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() fp, err := os.Open("example.tmx") if err != nil { panic(err) } m, err := tmx.NewMap(fp) if err != nil { panic(err) } var monitor *glfw.Monitor window, err := glfw.CreateWindow(screenWidth, screenHeight, "Map Renderer", monitor, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } width, height := window.GetFramebufferSize() gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Viewport(0, 0, int32(width), int32(height)) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(width), float64(height), 0, -1, 1) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) canvas := newOpenGLCanvas(width, height, float32(width)/float32(screenWidth), float32(height)/float32(screenHeight)) renderer := tmx.NewRenderer(*m, canvas) fps := 0 startTime := time.Now().UnixNano() timer := tmx.CreateTimer() timer.Start() for !window.ShouldClose() { elapsed := float64(timer.GetElapsedTime()) / (1000 * 1000) renderer.Render(int64(math.Ceil(elapsed))) fps++ if time.Now().UnixNano()-startTime > 1000*1000*1000 { log.Println(fps) startTime = time.Now().UnixNano() fps = 0 } window.SwapBuffers() glfw.PollEvents() timer.UpdateTime() } }
func (c *Context) BlendFunc(mode CompositeMode) { _ = c.runOnContextThread(func() error { if c.lastCompositeMode == mode { return nil } c.lastCompositeMode = mode s, d := operations(mode) gl.BlendFunc(uint32(s), uint32(d)) return nil }) }
func draw() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() //Transform screen to keep player in middle. Added intentation to make obvious the push matrix is like a block gl.PushMatrix() // gl.Translatef((1280/2)-float32(player.x), 0, 0.0) // gl.Begin(gl.LINES) // gl.Color3f(.2, .5, .2) // for i := range staticLines { // x := staticLines[i].GetAsSegment().A.X // y := staticLines[i].GetAsSegment().A.Y // gl.Vertex3f(float32(x), float32(y), 0) // x = staticLines[i].GetAsSegment().B.X // y = staticLines[i].GetAsSegment().B.Y // gl.Vertex3f(float32(x), float32(y), 0) // } // gl.End() gl.Color4f(player.color_r, player.color_g, player.color_b, player.color_a) //Draw Player gl.PushMatrix() rot := player.rot pos_x := player.x pos_y := player.y gl.Translatef(pos_x, pos_y, 0.0) gl.Rotatef(float32(rot), 0, 0, 1) drawCircle(float64(BALL_RADIUS), 20) gl.PopMatrix() //Draw the grapple gl.PushMatrix() gl.Translatef(player.hook.x_end, player.hook.y_end, 0.0) drawCircle(float64(5), 5) gl.PopMatrix() //Grapple Line gl.LineWidth(2.5) gl.Color3f(1.0, 0.0, 0.0) gl.Begin(gl.LINES) gl.Vertex3f(player.x, player.y, 0.0) gl.Vertex3f(player.hook.x_end, player.hook.y_end, 0) gl.End() //Second Pop gl.PopMatrix() }
func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() glfw.SwapInterval(1) gl.Init() data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf")) if err != nil { panic(err) } gl.Enable(gl.TEXTURE_2D) tmpBitmap := make([]byte, 512*512) cdata, err, _, tmpBitmap := truetype.BakeFontBitmap(data, 0, 32, tmpBitmap, 512, 512, 32, 96) var ftex uint32 gl.GenTextures(1, &ftex) gl.BindTexture(gl.TEXTURE_2D, ftex) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 512, 512, 0, gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tmpBitmap[0])) gl.ClearColor(0.3, 0.3, 0.32, 1.) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 800, 600, 0, 0, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Disable(gl.DEPTH_TEST) gl.Color4ub(255, 255, 255, 255) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) my_print(100, 100, "The quick brown fox jumps over the fence", ftex, cdata) window.SwapBuffers() glfw.PollEvents() } }
func setModelViewOptions() { gl.Enable(gl.TEXTURE_2D) gl.Disable(gl.LIGHTING) gl.Disable(gl.DITHER) gl.Enable(gl.CULL_FACE) gl.CullFace(gl.FRONT) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.BLEND) gl.Enable(gl.ALPHA_TEST) gl.DepthFunc(gl.LEQUAL) gl.Disable(gl.DEPTH_TEST) }
func reshape(window *glfw.Window, w, h int) { gl.ClearColor(1, 1, 1, 1) //fmt.Println(gl.GetString(gl.EXTENSIONS)) gl.Viewport(0, 0, int32(w), int32(h)) /* Establish viewing area to cover entire window. */ gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */ gl.LoadIdentity() /* Reset project matrix. */ gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */ gl.Scalef(1, -1, 1) /* Invert Y axis so increasing Y goes down. */ gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */ gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Disable(gl.DEPTH_TEST) width, height = w, h }
// OpenGL draw function func draw() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() player := game.Player //Transform screen. gl.PushMatrix() gl.Translatef((1280/2)-float32((player.Body.Position().X)), 0, 0.0) gl.Begin(gl.LINES) gl.Color3f(.2, .5, .2) for _, segment := range game.Level.GetChipmunkSegments() { x := segment.GetAsSegment().A.X y := segment.GetAsSegment().A.Y gl.Vertex3f(float32(x), float32(y), 0) x = segment.GetAsSegment().B.X y = segment.GetAsSegment().B.Y gl.Vertex3f(float32(x), float32(y), 0) } gl.End() gl.Color4f(.9, .1, 1, .9) // draw balls for _, enemy := range game.Enemies { gl.PushMatrix() pos := enemy.Body.Position() rot := enemy.Body.Angle() * game.DegreeConst gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) gl.Rotatef(float32(rot), 0, 0, 1) drawCircle(float64(enemy.Radius), 60) gl.PopMatrix() } gl.Color4f(.3, .3, 1, .8) //Draw Player gl.PushMatrix() pos := player.Body.Position() rot := player.Body.Angle() * game.DegreeConst gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) gl.Rotatef(float32(rot), 0, 0, 1) drawCircle(float64(player.Radius), 60) gl.PopMatrix() gl.PopMatrix() }
func onDisplay(program uint32, coords uint32) { gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.UseProgram(program) gl.EnableVertexAttribArray(coords) gl.VertexAttribPointer(coords, 2, gl.FLOAT, false, 0, nil) gl.DrawArrays(gl.TRIANGLES, 0, 3) gl.DisableVertexAttribArray(coords) }
func (atlas *FontAtlas) Draw(text string, b Bounds) { atlas.LoadGlyphs(text) gl.Enable(gl.BLEND) defer gl.Disable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.TEXTURE_2D) defer gl.Disable(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, atlas.Texture) x := b.Min.X + atlas.drawPadding y := (b.Max.Y+b.Min.Y)/2 + (ceilPxf(atlas.maxBounds.Min.Y)+ceilPxf(atlas.maxBounds.Max.Y))/2 p := rune(0) for _, r := range text { glyph := atlas.Rendered[r] dx := float32(glyph.Loc.Dx()) dy := float32(glyph.Loc.Dy()) px := x + ceilPxf(glyph.Bounds.Min.X) - glyphPadding py := y + ceilPxf(glyph.Bounds.Min.Y) - glyphPadding // this is not the ideal way of positioning the letters // will create positioning artifacts // but it the result is more px = float32(math.Trunc(float64(px))) py = float32(math.Trunc(float64(py))) gl.Begin(gl.QUADS) { gl.TexCoord2f(glyph.RelLoc.Min.X, glyph.RelLoc.Min.Y) gl.Vertex2f(px, py) gl.TexCoord2f(glyph.RelLoc.Max.X, glyph.RelLoc.Min.Y) gl.Vertex2f(px+dx, py) gl.TexCoord2f(glyph.RelLoc.Max.X, glyph.RelLoc.Max.Y) gl.Vertex2f(px+dx, py+dy) gl.TexCoord2f(glyph.RelLoc.Min.X, glyph.RelLoc.Max.Y) gl.Vertex2f(px, py+dy) } gl.End() k := atlas.Face.Kern(p, r) p = r x += ceilPxf(glyph.Advance + k) } }
func (atlas *FontAtlas) draw(rendered *image.RGBA, b Bounds) { var texture uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rendered.Bounds().Dx()), int32(rendered.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rendered.Pix)) gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.Begin(gl.QUADS) { gl.TexCoord2f(0, 0) gl.Vertex2f(b.Min.X, b.Min.Y) gl.TexCoord2f(1, 0) gl.Vertex2f(b.Max.X, b.Min.Y) gl.TexCoord2f(1, 1) gl.Vertex2f(b.Max.X, b.Max.Y) gl.TexCoord2f(0, 1) gl.Vertex2f(b.Min.X, b.Max.Y) } gl.End() gl.Disable(gl.BLEND) gl.DeleteTextures(1, &texture) gl.Disable(gl.TEXTURE_2D) }
func (g *graphics) init() error { if err := g.loadImages(); err != nil { return err } gl.ClearColor(0, 0, 0.7, 1) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) g.fontStash = fontstash.New(512, 512) fontID, err := g.fontStash.AddFont(resourcePath("MorrisRoman-Black.ttf")) if err != nil { return err } g.fontStash.SetYInverted(true) g.font = NewGLFont(g.fontStash, fontID, 45, [4]float32{0, 0, 0, 1}) return nil }
func List(width, height int, list *draw.List) { gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.SCISSOR_TEST) defer gl.Disable(gl.SCISSOR_TEST) gl.EnableClientState(gl.VERTEX_ARRAY) defer gl.DisableClientState(gl.VERTEX_ARRAY) gl.EnableClientState(gl.COLOR_ARRAY) defer gl.DisableClientState(gl.COLOR_ARRAY) gl.EnableClientState(gl.TEXTURE_COORD_ARRAY) defer gl.DisableClientState(gl.TEXTURE_COORD_ARRAY) gl.VertexPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].P))) gl.TexCoordPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].UV))) gl.ColorPointer(4, gl.UNSIGNED_BYTE, vertexStride, unsafe.Pointer(&(list.Vertices[0].Color))) offset := 0 for _, cmd := range list.Commands { if cmd.Count == 0 { continue } if cmd.Texture == 0 { gl.Disable(gl.TEXTURE_2D) } else { gl.Enable(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, uint32(cmd.Texture)) } x, y, w, h := cmd.Clip.AsInt32() gl.Scissor(x, int32(height)-y-h, w, h) gl.DrawElements(gl.TRIANGLES, int32(cmd.Count), indexType, gl.Ptr(list.Indicies[offset:])) offset += int(cmd.Count) } }
func onDisplay(program uint32) { coords := uint32(attributeCoord2d) vcolor := uint32(attributeVColor) gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) gl.UseProgram(program) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Uniform1f(uniformFade, curFade) gl.EnableVertexAttribArray(coords) gl.EnableVertexAttribArray(vcolor) gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle) gl.VertexAttribPointer(coords, 2, gl.FLOAT, false, 5*floatSize, nil) gl.VertexAttribPointer(vcolor, 3, gl.FLOAT, false, 5*floatSize, gl.PtrOffset(2*floatSize)) gl.DrawArrays(gl.TRIANGLES, 0, 3) gl.DisableVertexAttribArray(vcolor) gl.DisableVertexAttribArray(coords) }
func (c *Context) BlendFunc(src, dst int) { gl.BlendFunc(uint32(src), uint32(dst)) }
func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() glfw.SwapInterval(1) gl.Init() stash := fontstash.New(512, 512) clearSansRegular, err := stash.AddFont(filepath.Join("..", "ClearSans-Regular.ttf")) if err != nil { panic(err) } clearSansItalic, err := stash.AddFont(filepath.Join("..", "ClearSans-Italic.ttf")) if err != nil { panic(err) } clearSansBold, err := stash.AddFont(filepath.Join("..", "ClearSans-Bold.ttf")) if err != nil { panic(err) } droidJapanese, err := stash.AddFont(filepath.Join("..", "DroidSansJapanese.ttf")) if err != nil { panic(err) } gl.ClearColor(0.3, 0.3, 0.32, 1.) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 800, 0, 600, -1, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Disable(gl.DEPTH_TEST) gl.Color4ub(255, 255, 255, 255) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Disable(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.Vertex2i(0, -5) gl.Vertex2i(5, -5) gl.Vertex2i(5, -11) gl.Vertex2i(0, -11) gl.End() sx := float64(100) sy := float64(250) stash.BeginDraw() dx := sx dy := sy dx = stash.DrawText(clearSansRegular, 24, dx, dy, "The quick ", [4]float32{0, 0, 0, 1}) dx = stash.DrawText(clearSansItalic, 48, dx, dy, "brown ", [4]float32{1, 1, 0.5, 1}) dx = stash.DrawText(clearSansRegular, 24, dx, dy, "fox ", [4]float32{0, 1, 0.5, 1}) _, _, lh := stash.VMetrics(clearSansItalic, 24) dx = sx dy -= lh * 1.2 dx = stash.DrawText(clearSansItalic, 24, dx, dy, "jumps over ", [4]float32{0, 1, 1, 1}) dx = stash.DrawText(clearSansBold, 24, dx, dy, "the lazy ", [4]float32{1, 0, 1, 1}) dx = stash.DrawText(clearSansRegular, 24, dx, dy, "dog.", [4]float32{0, 1, 0, 1}) dx = sx dy -= lh * 1.2 dx = stash.DrawText(clearSansRegular, 12, dx, dy, "Now is the time for all good men to come to the aid of the party.", [4]float32{0, 0, 1, 1}) _, _, lh = stash.VMetrics(clearSansItalic, 12) dx = sx dy -= lh * 1.2 * 2 dx = stash.DrawText(clearSansItalic, 18, dx, dy, "Ég get etið gler án þess að meiða mig.", [4]float32{1, 0, 0, 1}) _, _, lh = stash.VMetrics(clearSansItalic, 18) dx = sx dy -= lh * 1.2 stash.DrawText(droidJapanese, 18, dx, dy, "どこかに置き忘れた、サングラスと打ち明け話。", [4]float32{1, 1, 1, 1}) stash.EndDraw() gl.Enable(gl.DEPTH_TEST) window.SwapBuffers() glfw.PollEvents() } }
func main() { if err := glfw.Init(); err != nil { fmt.Println("glfw.Init():", err) return } defer glfw.Terminate() glfw.WindowHint(glfw.Decorated, glfw.True) glfw.WindowHint(glfw.ContextVersionMajor, 1) glfw.WindowHint(glfw.ContextVersionMinor, 0) glfw.WindowHint(glfw.Resizable, glfw.True) window, err := glfw.CreateWindow(10, 10, "Settlers", nil, nil) if err != nil { fmt.Println("glfw.CreateWindow():", err) return } window.MakeContextCurrent() if err := gl.Init(); err != nil { fmt.Println("gl.Init():", err) return } stash := fontstash.New(512, 512) fontID, err := stash.AddFont(resourcePath("MorrisRoman-Black.ttf")) if err != nil { fmt.Println(err) return } stash.SetYInverted(true) font := &font{stash, fontID, 35} gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() rand.Seed(time.Now().UnixNano()) g := game.New([]game.Color{game.Red, game.White, game.Blue}, rand.Int()) var lines []string window.SetCharCallback(func(_ *glfw.Window, r rune) { if len(lines) == 0 { lines = []string{""} } lines[len(lines)-1] += string(r) }) window.SetCharModsCallback(func(_ *glfw.Window, r rune, _ glfw.ModifierKey) { }) window.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, _ int, action glfw.Action, _ glfw.ModifierKey) { if action == glfw.Release { return } if key == glfw.KeyEscape { window.SetShouldClose(true) } if key == glfw.Key1 { g.CurrentPlayer = 0 } if key == glfw.Key2 { g.CurrentPlayer = 1 } if key == glfw.Key3 { g.CurrentPlayer = 2 } if key == glfw.Key4 { g.CurrentPlayer = 3 } if key == glfw.KeyR { state = buildingRoad } if key == glfw.KeyS { state = buildingSettlement } if key == glfw.KeyKP2 { g.DealResources(2) } if key == glfw.KeyKP3 { g.DealResources(3) } if key == glfw.KeyKP4 { g.DealResources(4) } if key == glfw.KeyKP5 { g.DealResources(5) } if key == glfw.KeyKP6 { g.DealResources(6) } if key == glfw.KeyKP8 { g.DealResources(8) } if key == glfw.KeyKP9 { g.DealResources(9) } if key == glfw.KeyKP0 { g.DealResources(10) } if key == glfw.KeyKP1 { g.DealResources(11) } if key == glfw.KeyKP7 { g.DealResources(12) } if len(lines) > 0 && key == glfw.KeyBackspace && (action == glfw.Press || action == glfw.Repeat) { if len(lines[len(lines)-1]) == 0 { lines = lines[:len(lines)-1] } else { lines[len(lines)-1] = removeLastRune(lines[len(lines)-1]) } } if (action == glfw.Press || action == glfw.Repeat) && (key == glfw.KeyEnter || key == glfw.KeyKPEnter) { lines = append(lines, "") } }) window.SetInputMode(glfw.CursorMode, glfw.CursorHidden) window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) window.SetSizeCallback(func(_ *glfw.Window, w, h int) { windowW, windowH = w, h cam.WindowWidth, cam.WindowHeight = w, h gl.Viewport(0, 0, int32(w), int32(h)) }) if err := loadImages(); err != nil { fmt.Println(err) return } var background image.Image var backImg *glImage go func() { gameW, gameH := 7*200, 7*tileYOffset+tileSlopeHeight back := image.NewRGBA(image.Rect(0, 0, gameW, gameH)) clearToTransparent(back) drawGameBackgroundIntoImage(back, g) background = back }() gameColorToString := func(c game.Color) string { switch c { case game.Red: return "red" case game.Orange: return "orange" case game.Blue: return "blue" default: return "white" } } roadImage := func(pos game.TileEdge, c game.Color) *glImage { color := gameColorToString(c) dir := "up" if isEdgeVertical(pos) { dir = "vertical" } if isEdgeGoingDown(pos) { dir = "down" } return glImages["road_"+color+"_"+dir] } settlementImage := func(c game.Color) *glImage { return glImages["settlement_"+gameColorToString(c)] } cityImage := func(c game.Color) *glImage { return glImages["city_"+gameColorToString(c)] } // TODO this is for showing the road/settlement being built currently var movingSettlementCorner game.TileCorner var movingSettlementVisible bool var movingRoadEdge game.TileEdge var movingRoadVisible bool window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) { mouseX, mouseY = x, y gameX, gameY := cam.screenToGame(x, y) movingSettlementCorner, movingSettlementVisible = screenToCorner(gameX, gameY) movingRoadEdge, movingRoadVisible = screenToEdge(gameX, gameY) }) window.SetCursorEnterCallback(func(_ *glfw.Window, entered bool) { mouseIn = entered if entered == false { movingSettlementVisible = false movingRoadVisible = true } }) ////////////////////////////////////////////////////////////////// drawGame := func() { if backImg == nil && background != nil { backImg, _ = NewGLImageFromImage(background) } if backImg != nil { backImg.DrawAtXY(0, 0) for _, p := range g.GetPlayers() { for _, r := range p.GetBuiltRoads() { x, y := edgeToScreen(r.Position) img := roadImage(r.Position, p.Color) img.DrawAtXY(x-img.Width/2, y-img.Height/2) } for _, s := range p.GetBuiltSettlements() { x, y := cornerToScreen(s.Position) img := settlementImage(p.Color) img.DrawAtXY(x-img.Width/2, y-(5*img.Height/8)) } for _, c := range p.GetBuiltCities() { x, y := cornerToScreen(c.Position) img := cityImage(p.Color) img.DrawAtXY(x-img.Width/2, y-(5*img.Height/8)) } } if state == buildingSettlement && movingSettlementVisible && mouseIn { color := [4]float32{1, 1, 1, 1} x, y := cornerToScreen(movingSettlementCorner) if !movingSettlementVisible || !g.CanBuildSettlementAt(movingSettlementCorner) { color = [4]float32{0.7, 0.7, 0.7, 0.7} x, y = cam.screenToGame(mouseX, mouseY) } img := settlementImage(g.GetCurrentPlayer().Color) // TODO duplicate code: img.DrawColoredAtXY(x-img.Width/2, y-(5*img.Height/8), color) } if state == buildingRoad && movingRoadVisible && mouseIn { color := [4]float32{1, 1, 1, 1} x, y := edgeToScreen(movingRoadEdge) if !(!movingRoadVisible || !g.CanBuildRoadAt(movingRoadEdge)) { img := roadImage(movingRoadEdge, g.GetCurrentPlayer().Color) // TODO duplicate code: img.DrawColoredAtXY(x-img.Width/2, y-(5*img.Height/8), color) } } x, y, w, h := tileToScreen(g.Robber.Position) robber := glImages["robber"] robber.DrawAtXY(x+(w-robber.Width)/2, y+(h-robber.Height)/2) } } buildMenu := &menu{color{0.5, 0.4, 0.8, 0.9}, rect{0, 500, 800, 250}} showCursor := 0 start := time.Now() frames := 0 window.SetSize(windowW, windowH) for !window.ShouldClose() { glfw.PollEvents() gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() const controlsHeight = 0 // TODO reserve are for stats and menus gameW, gameH := 7.0*200, 7.0*tileYOffset+tileSlopeHeight+controlsHeight gameRatio := gameW / gameH windowRatio := float64(windowW) / float64(windowH) var left, right, bottom, top float64 if windowRatio > gameRatio { // window is wider than game => borders left and right border := (windowRatio*gameH - gameW) / 2 left, right = -border, border+gameW bottom, top = gameH, 0 } else { // window is higher than game => borders on top and bottom border := (gameW/windowRatio - gameH) / 2 left, right = 0, gameW bottom, top = gameH+border, -border } gl.Ortho(left, right, bottom, top, -1, 1) cam.Left, cam.Right, cam.Bottom, cam.Top = left, right, bottom, top gl.ClearColor(0, 0, 1, 1) gl.Clear(gl.COLOR_BUFFER_BIT) drawGame() lines = make([]string, g.PlayerCount*6) for i, player := range g.GetPlayers() { lines[i*6+0] = fmt.Sprintf("player %v has %v ore", i+1, player.Resources[game.Ore]) lines[i*6+1] = fmt.Sprintf("player %v has %v grain", i+1, player.Resources[game.Grain]) lines[i*6+2] = fmt.Sprintf("player %v has %v lumber", i+1, player.Resources[game.Lumber]) lines[i*6+3] = fmt.Sprintf("player %v has %v wool", i+1, player.Resources[game.Wool]) lines[i*6+4] = fmt.Sprintf("player %v has %v brick", i+1, player.Resources[game.Brick]) } if len(lines) > 0 { stash.BeginDraw() const fontSize = 35 const cursorText = "" //"_" cursor := "" if showCursor > 60 { cursor = cursorText } for i, line := range lines { output := line if i == len(lines)-1 { output += cursor } font.Write(output, 0, float64(i+1)*fontSize) } if len(lines) == 0 { font.Write(cursor, 0, fontSize) } stash.EndDraw() } buildMenu.draw() window.SwapBuffers() showCursor = (showCursor + 1) % 120 frames++ if time.Now().Sub(start).Seconds() >= 1.0 { fmt.Println(frames, "fps") frames = 0 start = time.Now() } } }
func (*backend) BlendFunc(sfactor, dfactor gg.Enum) { gl.BlendFunc(uint32(sfactor), uint32(dfactor)) }
func main() { if err := glfw.Init(nopContextWatcher{}); err != nil { panic(err) } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 8) // Anti-aliasing. window, err := glfw.CreateWindow(400, 400, "", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } glfw.SwapInterval(1) // Vsync. InitFont() defer DeinitFont() framebufferSizeCallback := func(w *glfw.Window, framebufferSize0, framebufferSize1 int) { gl.Viewport(0, 0, int32(framebufferSize0), int32(framebufferSize1)) var windowSize [2]int windowSize[0], windowSize[1] = w.GetSize() // Update the projection matrix. gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(windowSize[0]), float64(windowSize[1]), 0, -1, 1) gl.MatrixMode(gl.MODELVIEW) } { var framebufferSize [2]int framebufferSize[0], framebufferSize[1] = window.GetFramebufferSize() framebufferSizeCallback(window, framebufferSize[0], framebufferSize[1]) } window.SetFramebufferSizeCallback(framebufferSizeCallback) var inputEventQueue []events.InputEvent mousePointer = &events.Pointer{VirtualCategory: events.POINTING} window.SetMouseMovementCallback(func(w *glfw.Window, xpos, ypos, xdelta, ydelta float64) { inputEvent := events.InputEvent{ Pointer: mousePointer, EventTypes: map[events.EventType]struct{}{events.SLIDER_EVENT: {}}, InputId: 0, Buttons: nil, Sliders: []float64{xdelta, ydelta}, } if w.GetInputMode(glfw.CursorMode) != glfw.CursorDisabled { inputEvent.EventTypes[events.AXIS_EVENT] = struct{}{} inputEvent.Axes = []float64{xpos, ypos} } inputEventQueue = events.EnqueueInputEvent(inputEventQueue, inputEvent) }) window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) { inputEvent := events.InputEvent{ Pointer: mousePointer, EventTypes: map[events.EventType]struct{}{events.BUTTON_EVENT: {}}, InputId: uint16(button), Buttons: []bool{action != glfw.Release}, Sliders: nil, Axes: nil, ModifierKey: uint8(mods), } inputEventQueue = events.EnqueueInputEvent(inputEventQueue, inputEvent) }) go func() { <-time.After(5 * time.Second) log.Println("trigger!") boxUpdated = true glfw.PostEmptyEvent() }() gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // For font. gl.ClearColor(247.0/255, 247.0/255, 247.0/255, 1) var spinner int var widgets []events.Widgeter widgets = append(widgets, NewButtonWidget(mgl64.Vec2{50, 200}, func() { fmt.Println("button triggered") })) for !window.ShouldClose() && glfw.Press != window.GetKey(glfw.KeyEscape) { glfw.WaitEvents() // Process Input. inputEventQueue = events.ProcessInputEventQueue(inputEventQueue, widgets[0]) gl.Clear(gl.COLOR_BUFFER_BIT) gl.LoadIdentity() for _, widget := range widgets { widget.Render() } drawSpinner(spinner) spinner++ drawBox() gl.Color3d(1, 0, 0) NewOpenGlStream(mgl64.Vec2{50, 300}).PrintText(` !"#$%&'()*+,-./ 0123456789:;<=>? @ABCDEFGHIJKLMNO PQRSTUVWXYZ[\]^_ ` + "`" + `abcdefghijklmno pqrstuvwxyz{|}~`) window.SwapBuffers() log.Println("swapped buffers") //runtime.Gosched() } }
// OpenGL draw function func draw(window *glfw.Window) { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() gl.PushMatrix() gl.Disable(gl.LIGHTING) width, height := window.GetSize() x := float64(width) y := float64(height) h := 0 gl.Color4f(.1, .1, .1, .8) gl.LineWidth(1.0) // x方向 var x0, x1, y0, y1 float64 var deltaX, deltaY float64 d := width / 2 x0 = -x x1 = -x y0 = -y y1 = y deltaX = ((2 * x) / float64(d)) for i := 0; i < d; i++ { x0 = x0 + deltaX gl.Begin(gl.LINES) gl.Vertex3f(float32(x0), float32(y0), float32(h)) gl.Vertex3f(float32(x0), float32(y1), float32(h)) gl.End() } // y方向 x0 = -x x1 = x deltaY = ((2 * y) / float64(d)) for i := 0; i < d; i++ { y0 = y0 + deltaY gl.Begin(gl.LINES) gl.Vertex3f(float32(x0), float32(y0), float32(h)) gl.Vertex3f(float32(x1), float32(y0), float32(h)) gl.End() } gl.PopMatrix() // draw boxes for _, room := range rooms { gl.PushMatrix() rot := room.Box.Body.Angle() * chipmunk.DegreeConst gl.Rotatef(float32(rot), 0, 0, 1.0) x := roundm(float64(room.Box.Body.Position().X), 4.0) y := roundm(float64(room.Box.Body.Position().Y), 4.0) gl.Translated(x, y, 0.0) drawRoom(room) gl.PopMatrix() } }
func main() { fmt.Printf(`WolfenGo v%s, Copyright (C) 2016 gdm85 https://github.com/gdm85/wolfengo WolfenGo comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under GNU/GPLv2 license.`+"\n", version) if err := glfw.Init(); err != nil { fatalError(err) } defer glfw.Terminate() // create a context and activate it glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.DoubleBuffer, glfw.True) var err error Window, err = glfw.CreateWindow(800, 600, "WolfenGo", nil, nil) if err != nil { fatalError(err) } Window.MakeContextCurrent() // gl.Init() should be called after context is current if err := gl.Init(); err != nil { fatalError(err) } glfw.SwapInterval(1) // enable vertical refresh fmt.Println(gl.GoStr(gl.GetString(gl.VERSION))) if debugGL { gl.DebugMessageCallback(debugCb, unsafe.Pointer(nil)) gl.Enable(gl.DEBUG_OUTPUT) } // init graphics gl.ClearColor(0.0, 0.0, 0.0, 0.0) gl.FrontFace(gl.CW) gl.CullFace(gl.BACK) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.DEPTH_CLAMP) gl.Enable(gl.TEXTURE_2D) isRunning := false // load all assets _, err = getBasicShader() if err != nil { fatalError(err) } err = _defaultMedkit.initMedkit() if err != nil { fatalError(err) } err = _defaultMonster.initMonster() if err != nil { fatalError(err) } getDoorMesh() initPlayer() err = initGun() if err != nil { fatalError(err) } G, err = NewGame() if err != nil { fatalError(err) } // GAME STARTS isRunning = true var frames uint64 var frameCounter time.Duration frameTime := time.Duration(1000/frameCap) * time.Millisecond lastTime := time.Now() var unprocessedTime time.Duration for isRunning { var render bool startTime := time.Now() passedTime := startTime.Sub(lastTime) lastTime = startTime unprocessedTime += passedTime frameCounter += passedTime for unprocessedTime > frameTime { render = true unprocessedTime -= frameTime if Window.ShouldClose() { isRunning = false } G.timeDelta = frameTime.Seconds() glfw.PollEvents() err := G.input() if err != nil { fatalError(err) } err = G.update() if err != nil { fatalError(err) } if frameCounter >= time.Second { if printFPS { fmt.Printf("%d FPS\n", frames) } frames = 0 frameCounter -= time.Second } } if render { //TODO: Stencil Buffer gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) G.render() frames++ Window.SwapBuffers() } else { time.Sleep(time.Millisecond) } } Window.Destroy() }
func (w *Window) Show() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.True) window, err := glfw.CreateWindow(w.W, w.H, w.Title, nil, nil) if err != nil { panic(err) } if err := gl.Init(); err != nil { panic(err) } window.MakeContextCurrent() if len(w.Icon) == 1 { window.SetIcon(w.Icon) } if w.RestrictionsEnabled { window.SetSizeLimits(w.MinW, w.MinH, w.MaxW, w.MaxH) } if w.Blend { gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) } else if w.Smooth { gl.Enable(gl.SMOOTH) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.Enable(gl.POLYGON_SMOOTH) gl.Enable(gl.RELATIVE_SMOOTH_CUBIC_CURVE_TO_NV) gl.Enable(gl.RELATIVE_SMOOTH_QUADRATIC_CURVE_TO_NV) } else if w.Multisample { gl.Enable(gl.MULTISAMPLE) } reshape(w.W, w.H) window.SetRefreshCallback(func(g *glfw.Window) { reshape(w.W, w.H) }) window.SetSizeCallback(func(g *glfw.Window, width int, height int) { reshape(width, height) w.W = width w.H = height }) version := gl.GoStr(gl.GetString(gl.VERSION)) if !silentMode { fmt.Println("OpenGL version", version) } for !window.ShouldClose() { //Reset? gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) if w.DrawFunc != nil { w.DrawFunc() } // Maintenance gl.Flush() window.SwapBuffers() glfw.PollEvents() } }
// BlendFunc sets the pixel blending factors. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glBlendFunc.xhtml func BlendFunc(sfactor, dfactor Enum) { gl.BlendFunc(uint32(sfactor), uint32(dfactor)) }
// Printf draws the given string at the specified coordinates. // It expects the string to be a single line. Line breaks are not // handled as line breaks and are rendered as glyphs. // // In order to render multi-line text, it is up to the caller to split // the text up into individual lines of adequate length and then call // this method for each line seperately. func (f *Font) Printf(x, y float32, fs string, argv ...interface{}) error { indices := []rune(fmt.Sprintf(fs, argv...)) if len(indices) == 0 { return nil } // Runes form display list indices. // For this purpose, they need to be offset by -FontConfig.Low low := f.config.Low for i := range indices { indices[i] -= low } var vp [4]int32 gl.GetIntegerv(gl.VIEWPORT, &vp[0]) gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() gl.Ortho(float64(vp[0]), float64(vp[2]), float64(vp[1]), float64(vp[3]), 0, 1) gl.PopAttrib() gl.PushAttrib(gl.LIST_BIT | gl.CURRENT_BIT | gl.ENABLE_BIT | gl.TRANSFORM_BIT) { gl.MatrixMode(gl.MODELVIEW) gl.Disable(gl.LIGHTING) gl.Disable(gl.DEPTH_TEST) gl.Enable(gl.BLEND) gl.Enable(gl.TEXTURE_2D) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BindTexture(gl.TEXTURE_2D, f.texture) gl.ListBase(f.listbase) var mv [16]float32 gl.GetFloatv(gl.MODELVIEW_MATRIX, &mv[0]) gl.PushMatrix() { gl.LoadIdentity() mgw := float32(f.maxGlyphWidth) mgh := float32(f.maxGlyphHeight) switch f.config.Dir { case LeftToRight, TopToBottom: gl.Translatef(x, float32(vp[3])-y-mgh, 0) case RightToLeft: gl.Translatef(x-mgw, float32(vp[3])-y-mgh, 0) } gl.MultMatrixf(&mv[0]) gl.CallLists(int32(len(indices)), gl.UNSIGNED_INT, unsafe.Pointer(&indices[0])) } gl.PopMatrix() gl.BindTexture(gl.TEXTURE_2D, 0) } gl.PopAttrib() gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.PopAttrib() return checkGLError() }