func display() { gl.Clear(gl.COLOR_BUFFER_BIT) bitmap_output(40, 35, "This is written in a GLUT bitmap font.", glut.BITMAP_TIMES_ROMAN_24) bitmap_output(30, 210, "More bitmap text is a fixed 9 by 15 font.", glut.BITMAP_9_BY_15) bitmap_output(70, 240, " Helvetica is yet another bitmap font.", glut.BITMAP_HELVETICA_18) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() glu.Perspective(40.0, 1.0, 0.1, 20.0) gl.MatrixMode(gl.MODELVIEW) gl.PushMatrix() glu.LookAt(0.0, 0.0, 4.0, /* eye is at (0,0,30) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.0) /* up is in postivie Y direction */ gl.PushMatrix() gl.Translatef(0, 0, -4) gl.Rotatef(50, 0, 1, 0) stroke_output(-2.5, 1.1, " This is written in a", glut.STROKE_ROMAN) stroke_output(-2.5, 0, " GLUT stroke font.", glut.STROKE_ROMAN) stroke_output(-2.5, -1.1, "using 3D perspective.", glut.STROKE_ROMAN) gl.PopMatrix() gl.MatrixMode(gl.MODELVIEW) gl.PopMatrix() gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.MatrixMode(gl.MODELVIEW) gl.Flush() }
func (w *Window) renderUpload() { /* for _, node := range w.nodes { node.gfxupload() } */ gl.Flush() }
func runGameLoop(window *glfw.Window) { for !window.ShouldClose() { // update objects updateObjects() // hit detection hitDetection() // --------------------------------------------------------------- // draw calls gl.Clear(gl.COLOR_BUFFER_BIT) drawCurrentScore() drawHighScore() if isGameWon() { drawWinningScreen() } else if isGameLost() { drawGameOverScreen() } // draw everything 9 times in a 3x3 grid stitched together for seamless clipping for x := -1.0; x < 2.0; x++ { for y := -1.0; y < 2.0; y++ { gl.MatrixMode(gl.MODELVIEW) gl.PushMatrix() gl.Translated(gameWidth*x, gameHeight*y, 0) drawObjects() gl.PopMatrix() } } gl.Flush() window.SwapBuffers() glfw.PollEvents() // switch resolution if altEnter { window.Destroy() fullscreen = !fullscreen var err error window, err = initWindow() if err != nil { panic(err) } altEnter = false gl.LineWidth(1) if fullscreen { gl.LineWidth(2) } } } }
func display() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Begin(gl.TRIANGLES) gl.Color3f(0.0, 0.0, 1.0) /* blue */ gl.Vertex2i(0, 0) gl.Color3f(0.0, 1.0, 0.0) /* green */ gl.Vertex2i(200, 200) gl.Color3f(1.0, 0.0, 0.0) /* red */ gl.Vertex2i(20, 200) gl.End() gl.Flush() /* Single buffered, so needs a flush. */ }
func (rw *RenderWindow) draw() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.LINE_SMOOTH) for chanIdx, channel := range rw.Channels { color := channel.GetColor() gl.Color4f(color[0], color[1], color[2], color[3]) gl.Begin(gl.LINES) lastY := int64(0) for i := 0; i < len(rw.renderBuffers[chanIdx]); i += 1 { y := rw.Height - (rw.renderBuffers[chanIdx][i] * rw.Height / 1024) gl.Vertex2i(i-1, int(lastY)) gl.Vertex2i(i, int(y)) lastY = y } gl.Flush() gl.End() } glfw.SwapBuffers() }
func display() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Begin(gl.TRIANGLES) l := t.units for e := l.Front(); e != nil; e = e.Next() { b := e.Value.(*Entity) //r := b.rot_matrix p := b.pos gl.Color3f(0.0, 0.0, 1.0) /* blue */ gl.Vertex2f(p[0], p[1]) gl.Color3f(0.0, 1.0, 0.0) /* green */ gl.Vertex2f(20+p[0], 20+p[1]) gl.Color3f(1.0, 0.0, 0.0) /* red */ gl.Vertex2f(p[0], 20+p[1]) } gl.End() gl.Flush() /* Single buffered, so needs a flush. */ glut.SwapBuffers() }
// Draws the contents of the framebuffer at the requested width and height. func (fb *Framebuffer) Draw(w int, h int) { gl.Viewport(0, 0, w, h) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 1, 0, 1, 1, -1) gl.Enable(gl.TEXTURE_2D) fb.Texture.Bind(gl.TEXTURE_2D) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Begin(gl.QUADS) gl.TexCoord2d(0, 0) gl.Vertex3f(0.0, 0.0, 0) gl.TexCoord2d(1, 0) gl.Vertex3f(1.0, 0.0, 0) gl.TexCoord2d(1, 1) gl.Vertex3f(1.0, 1.0, 0) gl.TexCoord2d(0, 1) gl.Vertex3f(0.0, 1.0, 0) gl.End() gl.Flush() }
func main() { sys := Make() sys.Startup() defer sys.Shutdown() // InitQueue() sys.CreateWindow(1024, 768, "Gexic") gl.ClearColor(1, 1, 1, 0.) initGL() prevSelectPos = []int{0, 0, 0} // PurgeQueue() // genHexMap() hexMap2 = GenHexMap() // for matches := checkHexMap(); len(matches) > 0; matches = checkHexMap() { // removeHexAndGenNew(matches) // } glfw.SetMouseButtonCallback(MouseButtonCallback) glfw.SetCharCallback(charCallback) glfw.SetMousePosCallback(MousePosCallback) currExX = -1 currExY = -1 for sys.CheckExitMainLoop() { start := glfw.Time() wait := float64(1) / float64(30) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) // renderHexMap() wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() hexMap2.Render() hexRotate.AnimateAndExecute() hexShrink.AnimateAndExecute() hexFall.AnimateAndExecute() if !mouse.locked { x, y := mouse.GetXY() hexMap2.CalcClosestCenter(x, y) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) sys.Refresh() diff := glfw.Time() - start if diff < wait { glfw.Sleep(wait - diff) } } }
func renderHexMap() { gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.DECAL) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.PushMatrix() gl.Translatef(80, 80, 0) for x := 0; x < 10; x++ { maxy := 8 topy := 19 if x%2 == 1 { maxy = 9 topy = 0 } starty := 0 for y := starty; y < maxy; y++ { if currExX > -1 && currExY > -1 && starRotate { if y == currExY && x == currExX || y == currExY+1 && x == currExX || y == currExY-1 && x == currExX || currExX%2 == 0 && ((currExX == x-1 || currExX == x+1) && currExY == y-1 || (currExX == x-1 || currExX == x+1) && currExY == y) || currExX%2 == 1 && ((currExX == x-1 || currExX == x+1) && currExY == y || (currExX == x-1 || currExX == x+1) && currExY == y+1) { continue } } else if timesToRotate > 0 { // if y == currExY && x == currExX || currExX%2 == 0 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY+1) || currExX%2 == 1 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY-1) { // continue // } found := false for _, v := range selectedHex { if y == v[1] && x == v[0] { found = true break } } if found { continue } } found := false for _, v := range currentMatches { if scale > 0 && v[0] == x && v[1] == y || scale <= 0 && v[0] == x && v[1] >= y { found = true break } } for _, v := range starMatches { if starAlpha > 0 && v[0] == x && v[1] == y || starAlpha <= 0 && v[0] == x && v[1] >= y { found = true break } } if found || len(currStarCenter) > 0 && currStarCenter[0] == x && currStarCenter[1] == y { continue } gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(x*33, topy+y*38, hexMap[x][y], 1) } } gl.PopMatrix() if len(currentMatches) > 0 || len(starMatches) > 0 { mouseLock = true if len(currentMatches) > 0 && scale > 0 { scale -= 0.1 for _, v := range currentMatches { gl.PushMatrix() topy := 19 if v[0]%2 == 1 { topy = 0 } gl.Translatef(float32(v[0]*33+102), float32(v[1]*38+topy+94), 0) gl.Scalef(scale, scale, 1) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v[0]][v[1]], 1) gl.PopMatrix() } } else if len(starMatches) > 0 && starAlpha > 0 { starAlpha -= 0.05 // starAlpha = 0.7 starScale += 0.05 // starScale = 1.4 gl.PushMatrix() topy := 19 pm := 0 if currStarCenter[0]%2 == 1 { topy = 0 pm = -1 } gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) drawHex(-22, -14, 6, 1) gl.PopMatrix() gl.PushMatrix() gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) gl.Scalef(starScale, starScale, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]-1], starAlpha) drawHex(-22, -20+HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]+1], starAlpha) drawHex(-52, -36, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm], starAlpha) drawHex(-52, -40+HEX_HEIGHT, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm+1], starAlpha) drawHex(8, -36, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm], starAlpha) drawHex(8, -40+HEX_HEIGHT, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm+1], starAlpha) gl.PopMatrix() } else { if fallticks == 0 { animateFall = make([]*freefall, 0) for x := 0; x < 10; x++ { topy := 19 if x%2 == 1 { topy = 0 } fromy := -1 for _, v := range currentMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } for _, v := range starMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } if fromy == -1 { continue } for y := fromy; y >= 0; y-- { found := false for _, v := range currentMatches { if v[0] == x && v[1] == y { found = true break } } for _, v := range starMatches { if v[0] == x && v[1] == y { found = true break } } if found { continue } animateFall = append(animateFall, &freefall{x, y, getFallTargetY(x, y), math.Pow(float64(y), 2)/16 + 0.5}) gl.PushMatrix() gl.Translatef(float32(x*33+102), float32(y*38+topy+94), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[x][y], 1) gl.PopMatrix() } } fallticks++ } else { stillFalling := 0 for _, v := range animateFall { topy := 19 if v.x%2 == 1 { topy = 0 } newy := v.accel * math.Pow(float64(fallticks), 2) / 2 gl.PushMatrix() gl.Translatef(float32(v.x*33+102), float32(math.Min(float64(v.y*38+topy+94)+newy, float64(v.targetY*38+topy+94))), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v.x][v.y], 1) gl.PopMatrix() if float64(v.y*38+topy+94)+newy < float64(v.targetY*38+topy+94) { stillFalling++ } } fallticks++ if stillFalling == 0 { starScale = 1 starAlpha = 0 removeHexAndGenNew(currentMatches) makeStarAndGenNew(starMatches) currentMatches = checkHexMap() starMatches = checkHexStar() currStarCenter = []int{} scale = 1 fallticks = 0 mouseLock = false fmt.Println("Mouse unlocked 1") animateFall = nil } } } } if currExX > -1 && currExY > -1 { gl.PushMatrix() topy := 19 if currExX%2 == 1 { topy = 0 } if starRotate { timesToRotate = 0 gl.Translatef(float32(currExX*33+102), float32(currExY*38+topy+94), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) drawHex(-22, -14, 6, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currExX][currExY-1], 1) drawHex(-22, -20+HEX_HEIGHT, hexMap[currExX][currExY+1], 1) pm := 0 if currExX%2 == 1 { pm = -1 } drawHex(-52, -36, hexMap[currExX-1][currExY+pm], 1) drawHex(-52, -40+HEX_HEIGHT, hexMap[currExX-1][currExY+pm+1], 1) drawHex(8, -36, hexMap[currExX+1][currExY+pm], 1) drawHex(8, -40+HEX_HEIGHT, hexMap[currExX+1][currExY+pm+1], 1) } else { // gl.Translatef(float32(currExX*33+HEX_WIDTH+80), float32(currExxY*38+topy+20+80), 0) gl.Translatef(float32(prevSelectPos[0]), float32(prevSelectPos[1]), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) for _, v := range selectedHex { switch v[2] { case 1: drawHex(-32, -34, hexMap[v[0]][v[1]], 1) case 2: drawHex(0, -17, hexMap[v[0]][v[1]], 1) case 3: drawHex(-32, 0, hexMap[v[0]][v[1]], 1) case 4: drawHex(-44, -19, hexMap[v[0]][v[1]], 1) case 5: drawHex(-12, -36, hexMap[v[0]][v[1]], 1) case 6: drawHex(-12, -2, hexMap[v[0]][v[1]], 1) } } // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY-1], 1) // } // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-44, -19, hexMap[currExX][currExY], 1) // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY+1], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY], 1) // } } gl.PopMatrix() if !starRotate && rotate < 120 { rotate += 15 // rotate = 15 } else if starRotate && rotate < 60 { rotate += 6 } else { if starRotate { if currExX%2 == 0 { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1], hexMap[currExX-1][currExY] = hexMap[currExX-1][currExY], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1] } else { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY], hexMap[currExX-1][currExY-1] = hexMap[currExX-1][currExY-1], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY] } } else { v2 := make([][]int, 3) for _, v := range selectedHex { idx := 0 switch v[2] { case 1, 4: idx = 0 case 2, 5: idx = 1 case 3, 6: idx = 2 } v2[idx] = []int{v[0], v[1]} fmt.Println(idx, v[0], v[1]) } hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]], hexMap[v2[2][0]][v2[2][1]] = hexMap[v2[2][0]][v2[2][1]], hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]] } starMatches = checkHexStar() if len(starMatches) > 6 { fmt.Println(starMatches) } if len(starMatches) >= 6 { timesToRotate = 0 rotate = 0 currExX = -1 currExY = -1 starScale = 1 starAlpha = 1 starRotate = false currStarCenter = getStarCenter(starMatches) hexMap[currStarCenter[0]][currStarCenter[1]] = 6 // makeStarAndGenNew(starMatches) } else { matches := checkHexMap() if len(matches) > 0 { currentMatches = matches scale = 1 currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false } else { if timesToRotate == 0 { currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false mouseLock = false fmt.Println("Mouse unlocked 3") } rotate = 0 timesToRotate-- } } } } if !mouseLock { prevSelectPos = calcClosestCenter(posX, posY) drawBorderAtXY(float32(prevSelectPos[0]), float32(prevSelectPos[1]), prevSelectPos[2]) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) }
func (d *DisplayEngine) drawFrame() { toDraw := make([][]*resources.Bitmap, d.config.MapW*d.config.MapH) drawPasses := 0 for x := 0; x < d.config.MapW; x++ { for y := 0; y < d.config.MapH; y++ { toDraw[x*d.config.MapW+y] = (*d.gameEngine).GetTile(x, y) length := len(toDraw[x*d.config.MapW+y]) if length > drawPasses { drawPasses = length } } } viewport := d.viewport font := allegro.CreateBuiltinFont() // Don't want anyone changing the viewport mid frame or any such highjinks d.Display.SetTargetBackbuffer() allegro.RunInThread(func() { r, g, b, a := d.config.BGColor.GetRGBA() gl.ClearColor( gl.GLclampf(r)/255.0, gl.GLclampf(g)/255.0, gl.GLclampf(b)/255.0, gl.GLclampf(a)/255.0) gl.Clear(gl.COLOR_BUFFER_BIT) viewport.SetupTransform() for p := 0; p < drawPasses; p++ { m := d.config.MapW n := d.config.MapH for s := 0; s < m+n; s++ { for x := 0; x < s; x++ { y := s - x - 1 if x >= m || y < 0 || y >= n { continue } if len(toDraw[x*d.config.MapW+y]) < p { continue } // Coordinates in terms of pixels px := (y - x) * d.config.TileW / 2 py := (x + y) * d.config.TileH / 2 bmp := toDraw[x*d.config.MapW+y][p] /* ox := bmp.OffX oy := bmp.OffY*/ bw, bh := bmp.W, bmp.H if viewport.OnScreen(px, py, bw, bh) { gl.Begin(gl.QUADS) bmp.Tex.Bind(gl.TEXTURE_2D) gl.TexCoord2f(0, 0) gl.Vertex3i(px, py, 0) gl.TexCoord2f(0, 1) gl.Vertex3i(px, py+bw, 0) gl.TexCoord2f(1, 1) gl.Vertex3i(px+bh, py+bw, 0) gl.TexCoord2f(1, 0) gl.Vertex3i(px+bh, py, 0) gl.End() } } } } gl.Flush() }) var trans allegro.Transform trans.Identity() trans.Use() font.Draw(allegro.CreateColor(0, 255, 0, 255), 0, 0, 0, fmt.Sprint(int(d.fps))) allegro.Flip() d.frameDrawing.Unlock() }
func main() { b, _ := ioutil.ReadFile("./roboto/roboto-light.ttf") font, ferr := tt.Parse(b) if ferr != nil { fmt.Println("can't parse font %v , len %v", ferr.Error(), len(b)) } fc := ft.NewContext() fc.SetFont(font) glfw.SetErrorCallback(errorCallback) if !glfw.Init() { panic("Can't init glfw!") } defer glfw.Terminate() window, err := glfw.CreateWindow(800, 600, "Testing", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() gl.Init() program := gl.CreateProgram() vertexShader := gl.CreateShader(gl.VERTEX_SHADER) vertexShader.Source(` attribute vec4 a_position; attribute vec2 a_coord; varying vec2 v_coord; void main() { gl_Position = a_position; v_coord = a_coord; } `) vertexShader.Compile() fmt.Printf("vertex: %v\n", vertexShader.GetInfoLog()) fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER) fragmentShader.Source(` varying vec2 v_coord; uniform sampler2D s_picture; uniform vec4 color; uniform bool has_picture; void main() { if(has_picture) { gl_FragColor = texture2D(s_picture, v_coord); } else { gl_FragColor = color; } } `) fragmentShader.Compile() fmt.Printf("fragment %v \n", fragmentShader.GetInfoLog()) program.AttachShader(vertexShader) program.AttachShader(fragmentShader) program.Link() // ini //gl.MatrixMode(gl.PROJECTION) //gl.Ortho(0, 640, 0, 480, 0, 1) gl.ClearColor(0.5, 0.5, 0.5, 0.0) root := widget.Widget{ Name: "Red", Rect: image.Rect(0, 0, 800, 600), Background: color.RGBA{255, 128, 128, 126}, OnClick: widget.ClickInner, OnDrag: widget.DragInner, OnHover: widget.HoverInner, OnResize: widget.ResizeItself, } blue := root.AddWidget(&widget.Widget{ Name: "Blue", Rect: image.Rect(100, 100, 200, 200), Image: LoadImage("./test.png"), Background: color.RGBA{128, 128, 255, 126}, OnClick: func(w *widget.Widget, p image.Point) { root.SetTop(w) fmt.Println("Clicked blue box") widget.ClickInner(w, p) }, OnDrag: func(w *widget.Widget, p image.Point, d image.Point) bool { widget.DragInner(w, p, d) widget.DragItself(w, p, d) return true }, OnResize: widget.ResizeItself, }) blue.AddWidget(&widget.Widget{ Name: "White", Rect: image.Rect(90, 90, 100, 100), Background: color.RGBA{250, 250, 250, 250}, OnDrag: func(w *widget.Widget, p image.Point, d image.Point) bool { widget.DragItself(w, p, d) blue.Resize(d) return true }, }) root.AddWidget(&widget.Widget{ Name: "Green", Rect: image.Rect(100, 300, 200, 400), Background: color.RGBA{128, 255, 128, 126}, OnClick: func(w *widget.Widget, p image.Point) { root.SetTop(w) w.Image = LoadImage("./test2.png") }, OnDrag: widget.DragItself, }) root.AddWidget(&widget.Widget{ Name: "Black", Rect: image.Rect(100, 400, 150, 450), Background: color.RGBA{0, 0, 0, 126}, OnHover: func(w *widget.Widget, p0 image.Point, p1 image.Point) { if p1.In(w.Rect) { w.Background = color.RGBA{255, 255, 255, 126} } else { w.Background = color.RGBA{0, 0, 0, 126} } }, }) white := root.AddWidget(&widget.Widget{ Name: "White", Text: "Меня зовут Светлана, я из города Иваново. «Единая Россия» очень много сделала достижений: они подняли экономик… экономику, мы стали более лучшие… одеваться, и не было того что щас — это очень большие достижения! В сельском хозяйстве очень хорошо. (Гладин: Что именно в сельском хозяйстве они сделали?) Стало больше… земель за-а… много, ну… я не знаю даже как сказать… засеивать больше земель… а-а-а вот, овощи там, рожь — вот это всё. Что еще сказать… Так как у нас страна многонациональная, у нас в Москве очень много людей, которые очень помогают нам… с других городов… (вопрос Гладина: Вы считаете это достижение «Единой России»?) Да, это большое достижение! Очень хорошее даже! Видите ну… да… Видите ну у нас в Иванове очень хорошая стала медицина… а…что ещё… благоустройство в городах хорошее… с жильём… никаких проблем. Люди подмогают очень хорошо", Rect: image.Rect(400, 200, 700, 500), Foreground: color.RGBA{0, 0, 0, 0}, Background: color.RGBA{255, 255, 255, 126}, OnDrag: func(w *widget.Widget, p image.Point, d image.Point) bool { root.SetTop(w) widget.DragInner(w, p, d) widget.DragItself(w, p, d) return true }, OnResize: widget.ResizeItself, Padding: image.Rect(20, 20, 20, 20), }) white.AddWidget(&widget.Widget{ Name: "White", Rect: image.Rect(290, 290, 300, 300), Background: color.RGBA{0, 0, 0, 250}, OnDrag: func(w *widget.Widget, p image.Point, d image.Point) bool { widget.DragItself(w, p, d) white.Resize(d) return true }, }) x0 := 0.0 y0 := 0.0 window.SetMouseButtonCallback(func(w *glfw.Window, but glfw.MouseButton, act glfw.Action, key glfw.ModifierKey) { xpos, ypos := w.GetCursorPosition() if act == glfw.Press { root.Click(image.Point{X: int(xpos), Y: int(ypos)}) x0, y0 = xpos, ypos } }) window.SetCursorPositionCallback(func(w *glfw.Window, xpos float64, ypos float64) { root.Hover(image.Point{X: int(x0), Y: int(y0)}, image.Point{X: int(xpos), Y: int(ypos)}) if w.GetMouseButton(glfw.MouseButtonLeft) == glfw.Press { root.Drag(image.Point{X: int(x0), Y: int(y0)}, image.Point{X: int(xpos - x0), Y: int(ypos - y0)}) x0, y0 = xpos, ypos } x0, y0 = xpos, ypos }) width0, height0 := window.GetSize() window.SetSizeCallback(func(w *glfw.Window, width int, height int) { gl.Viewport(0, 0, width, height) root.Rect.Max = image.Point{width, height} width0, height0 = width, height }) /*switch(color_type){ case PNG_COLOR_TYPE_GRAY: return GL_LUMINANCE; case PNG_COLOR_TYPE_GRAY_ALPHA: return GL_LUMINANCE_ALPHA; case PNG_COLOR_TYPE_RGB: return GL_RGB; case PNG_COLOR_TYPE_RGB_ALPHA: return GL_RGBA; */ /* here init texture pool texturePool := make([widget.Widget]texture) */ for !window.ShouldClose() { //Do OpenGL stuff program.Use() gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) root.DrawBy(image.Point{}, func(w *widget.Widget, corner image.Point) { var texture gl.Texture if w.Image != nil { texture = gl.GenTexture() texture.Bind(gl.TEXTURE_2D) gl.ActiveTexture(gl.TEXTURE0) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w.Image.Rect.Dx(), w.Image.Rect.Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, w.Image.Pix) } leftX := 2.0*float32(corner.X+w.Rect.Min.X)/float32(root.Rect.Dx()) - 1.0 leftY := 1.0 - 2.0*float32(corner.Y+w.Rect.Min.Y)/float32(root.Rect.Dy()) rightX := 2.0*float32(corner.X+w.Rect.Min.X)/float32(root.Rect.Dx()) - 1.0 rightY := 1.0 - 2.0*float32(corner.Y+w.Rect.Max.Y)/float32(root.Rect.Dy()) bottomX := 2.0*float32(corner.X+w.Rect.Max.X)/float32(root.Rect.Dx()) - 1.0 bottomY := 1.0 - 2.0*float32(corner.Y+w.Rect.Min.Y)/float32(root.Rect.Dy()) topX := 2.0*float32(corner.X+w.Rect.Max.X)/float32(root.Rect.Dx()) - 1.0 topY := 1.0 - 2.0*float32(corner.Y+w.Rect.Max.Y)/float32(root.Rect.Dy()) vertices := []float32{ leftX, leftY, rightX, rightY, bottomX, bottomY, topX, topY, } texturePoints := []float32{ 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, } s_picture := program.GetUniformLocation("s_picture") s_picture.Uniform1i(0) has_picture := program.GetUniformLocation("has_picture") if w.Image != nil { has_picture.Uniform1i(1) } else { has_picture.Uniform1i(0) } col := program.GetUniformLocation("color") r, g, b, a := w.Background.RGBA() col.Uniform4f(float32(r)/float32(0xFFFF), float32(g)/float32(0xFFFF), float32(b)/float32(0xFFFF), float32(a)/float32(0xFFFF)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, gl.UNSIGNED_BYTE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) a_position := program.GetAttribLocation("a_position") a_position.AttribPointer(2, gl.FLOAT, false, 0, vertices) a_position.EnableArray() a_coord := program.GetAttribLocation("a_coord") a_coord.AttribPointer(2, gl.FLOAT, false, 0, texturePoints) a_coord.EnableArray() gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.Flush() texture.Delete() if len(w.Text) > 0 { rct := w.Rect rct.Max = rct.Max.Sub(w.Rect.Min) rct.Min = rct.Min.Sub(w.Rect.Min) fg := image.NewRGBA(rct) fgu := image.NewUniform(color.RGBA{0, 16, 32, 255}) draw.Draw(fg, fg.Bounds(), fgu, image.ZP, draw.Src) bg := image.NewRGBA(rct) bgu := image.NewUniform(color.RGBA{255, 255, 255, 255}) draw.Draw(bg, bg.Bounds(), bgu, image.ZP, draw.Src) lineHeight := 20.0 fc.SetDPI(100.0) fc.SetFont(font) fc.SetFontSize(12.0) fc.SetClip(bg.Bounds()) fc.SetDst(bg) fc.SetSrc(fg) p0 := ft.Pt(w.Padding.Min.X, w.Padding.Min.Y) p := p0 for _, s := range w.Text { p, _ = fc.DrawString(string(s), p) if int(p.X>>8) > rct.Max.X-w.Padding.Max.X-w.Padding.Min.X { p.X = p0.X p.Y += raster.Fix32(lineHeight * 256) } } var texture gl.Texture if bg != nil { texture = gl.GenTexture() texture.Bind(gl.TEXTURE_2D) gl.ActiveTexture(gl.TEXTURE0) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, bg.Rect.Dx(), bg.Rect.Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, bg.Pix) } leftX := 2.0*float32(corner.X+w.Rect.Min.X)/float32(root.Rect.Dx()) - 1.0 leftY := 1.0 - 2.0*float32(corner.Y+w.Rect.Min.Y)/float32(root.Rect.Dy()) rightX := 2.0*float32(corner.X+w.Rect.Min.X)/float32(root.Rect.Dx()) - 1.0 rightY := 1.0 - 2.0*float32(corner.Y+w.Rect.Max.Y)/float32(root.Rect.Dy()) bottomX := 2.0*float32(corner.X+w.Rect.Max.X)/float32(root.Rect.Dx()) - 1.0 bottomY := 1.0 - 2.0*float32(corner.Y+w.Rect.Min.Y)/float32(root.Rect.Dy()) topX := 2.0*float32(corner.X+w.Rect.Max.X)/float32(root.Rect.Dx()) - 1.0 topY := 1.0 - 2.0*float32(corner.Y+w.Rect.Max.Y)/float32(root.Rect.Dy()) vertices := []float32{ leftX, leftY, rightX, rightY, bottomX, bottomY, topX, topY, } texturePoints := []float32{ 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, } s_picture := program.GetUniformLocation("s_picture") s_picture.Uniform1i(0) has_picture := program.GetUniformLocation("has_picture") if bg != nil { has_picture.Uniform1i(1) } else { has_picture.Uniform1i(0) } col := program.GetUniformLocation("color") r, g, b, a := w.Background.RGBA() col.Uniform4f(float32(r)/float32(0xFFFF), float32(g)/float32(0xFFFF), float32(b)/float32(0xFFFF), float32(a)/float32(0xFFFF)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, gl.UNSIGNED_BYTE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) a_position := program.GetAttribLocation("a_position") a_position.AttribPointer(2, gl.FLOAT, false, 0, vertices) a_position.EnableArray() a_coord := program.GetAttribLocation("a_coord") a_coord.AttribPointer(2, gl.FLOAT, false, 0, texturePoints) a_coord.EnableArray() gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.Flush() texture.Delete() } }) window.SwapBuffers() glfw.PollEvents() } }
func main() { var err error if err = glfw.Init(); err != nil { fmt.Fprintf(os.Stderr, "[e] %v\n", err) return } defer glfw.Terminate() w, h := 1980, 1080 // w, h := 1280, 768 if err = glfw.OpenWindow(w, h, 8, 8, 8, 16, 0, 32, glfw.Fullscreen); err != nil { fmt.Fprintf(os.Stderr, "[e] %v\n", err) return } defer glfw.CloseWindow() glfw.SetSwapInterval(1) glfw.SetWindowTitle("Debris") quadric = glu.NewQuadric() gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Enable(gl.NORMALIZE) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.ShadeModel(gl.SMOOTH) gl.Enable(gl.LIGHTING) var ( ambient = []float32{0.1, 0.3, 0.6, 1} diffuse = []float32{1, 1, 0.5, 1} specular = []float32{0.4, 0.4, 0.4, 1} light_position = []float32{1, 0, 0, 0} // mat_specular []float32 = []float32{1, 1, 0.5, 1} mat_specular = []float32{1, 1, 0.75, 1} mat_shininess = float32(120) // light_position []float32 = []float32{0.0, 0.0, 1.0, 0.0} ) const ( fov = 1.1 // degrees znear = 145 zfar = 155 camera_z_offset = -150 camera_x_rotation = 0 // degrees // camera_x_rotation = 20 // degrees starfield_fov = 45 faces = 1000 earth_radius = 1 ) gl.Lightfv(gl.LIGHT1, gl.AMBIENT, ambient) gl.Lightfv(gl.LIGHT1, gl.DIFFUSE, diffuse) gl.Lightfv(gl.LIGHT1, gl.SPECULAR, specular) gl.Lightfv(gl.LIGHT1, gl.POSITION, light_position) gl.Enable(gl.LIGHT1) mat_emission := []float32{0, 0, 0.1, 1} gl.Materialfv(gl.FRONT_AND_BACK, gl.EMISSION, mat_emission) gl.Materialfv(gl.FRONT_AND_BACK, gl.SPECULAR, mat_specular) gl.Materialf(gl.FRONT_AND_BACK, gl.SHININESS, mat_shininess) gl.ClearColor(0.02, 0.02, 0.02, 1) gl.ClearDepth(1) gl.ClearStencil(0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) b := createBuffer() planetoids := []*Planetoid{} for i := 0; i < 1000; i++ { p := &Planetoid{ apogee: 1.2 + rand.Float64()*0.7, perigee: 1.5, // inclination: 45, inclination: rand.Float64()*20 - 10, // inclination: 0, phase0: rand.Float64() * 360, rising_node: rand.Float64() * 10, phase: 0, // radius: rand.Float32()*0.05 + 0.01, //float32(r), radius: rand.Float32()*0.0125 + 0.005, //float32(r), // quadric: glu.NewQuadric(), circle: b, } planetoids = append(planetoids, p) } // Initial projection matrix: var aspect float64 glfw.SetWindowSizeCallback(func(w, h int) { gl.Viewport(0, 0, w, h) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() aspect = float64(w) / float64(h) glu.Perspective(fov, aspect, znear, zfar) }) d := float64(0) wireframe := false atmosphere := false polar := false rotating := false front := false earth := true cone := true shadowing := true tilt := false running := true glfw.SetKeyCallback(func(key, state int) { if state != glfw.KeyPress { // Don't act on key coming up return } switch key { case 'A': atmosphere = !atmosphere case 'C': cone = !cone case 'E': earth = !earth case 'R': rotating = !rotating case 'F': front = !front if front { gl.FrontFace(gl.CW) } else { gl.FrontFace(gl.CCW) } case 'S': shadowing = !shadowing case 'T': tilt = !tilt case 'W': wireframe = !wireframe method := gl.GLenum(gl.FILL) if wireframe { method = gl.LINE } gl.PolygonMode(gl.FRONT_AND_BACK, method) case glfw.KeyF2: println("Screenshot captured") // glh.CaptureToPng("screenshot.png") w, h := glh.GetViewportWH() im := image.NewRGBA(image.Rect(0, 0, w, h)) glh.ClearAlpha(1) gl.Flush() glh.CaptureRGBA(im) go func() { fd, err := os.Create("screenshot.png") if err != nil { panic("Unable to open file") } defer fd.Close() png.Encode(fd, im) }() case 'Q', glfw.KeyEsc: running = !running case glfw.KeySpace: polar = !polar } }) _ = rand.Float64 stars := glh.NewMeshBuffer( glh.RenderArrays, glh.NewPositionAttr(3, gl.DOUBLE, gl.STATIC_DRAW), glh.NewColorAttr(3, gl.DOUBLE, gl.STATIC_DRAW)) const Nstars = 50000 points := make([]float64, 3*Nstars) colors := make([]float64, 3*Nstars) for i := 0; i < Nstars; i++ { const R = 1 phi := rand.Float64() * 2 * math.Pi z := R * (2*rand.Float64() - 1) theta := math.Asin(z / R) points[i*3+0] = R * math.Cos(theta) * math.Cos(phi) points[i*3+1] = R * math.Cos(theta) * math.Sin(phi) points[i*3+2] = z const r = 0.8 v := rand.Float64()*r + (1 - r) colors[i*3+0] = v colors[i*3+1] = v colors[i*3+2] = v } stars.Add(points, colors) render_stars := func() { glh.With(glh.Attrib{gl.DEPTH_BUFFER_BIT | gl.ENABLE_BIT}, func() { gl.Disable(gl.LIGHTING) gl.PointSize(1) gl.Color4f(1, 1, 1, 1) gl.Disable(gl.DEPTH_TEST) gl.DepthMask(false) stars.Render(gl.POINTS) }) } render_scene := func() { // Update light position (sensitive to current modelview matrix) gl.Lightfv(gl.LIGHT1, gl.POSITION, light_position) gl.Lightfv(gl.LIGHT2, gl.POSITION, light_position) if earth { Sphere(earth_radius, faces) } unlit_points := glh.Compound(glh.Disable(gl.LIGHTING), glh.Primitive{gl.POINTS}) glh.With(unlit_points, func() { gl.Vertex3d(1, 0, 0) }) for _, p := range planetoids { const dt = 0.1 // TODO: Frame update p.Render(dt) } glh.With(glh.Disable(gl.LIGHTING), func() { // Atmosphere gl.Color4f(0.25, 0.25, 1, 0.1) if atmosphere && earth { Sphere(earth_radius*1.025, 100) } gl.PointSize(10) glh.With(glh.Primitive{gl.POINTS}, func() { gl.Color4f(1.75, 0.75, 0.75, 1) gl.Vertex3d(-1.04, 0, 0) }) }) } render_shadow_volume := func() { glh.With(glh.Attrib{ gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.ENABLE_BIT | gl.POLYGON_BIT | gl.STENCIL_BUFFER_BIT, }, func() { gl.Disable(gl.LIGHTING) if shadowing { // gl.Disable(gl.DEPTH_TEST) gl.DepthMask(false) gl.DepthFunc(gl.LEQUAL) gl.Enable(gl.STENCIL_TEST) gl.ColorMask(false, false, false, false) gl.StencilFunc(gl.ALWAYS, 1, 0xffffffff) } shadow_volume := func() { const sv_length = 2 const sv_granularity = 100 const sv_radius = earth_radius * 1.001 // Shadow cone glh.With(glh.Matrix{gl.MODELVIEW}, func() { gl.Rotatef(90, 1, 0, 0) gl.Rotatef(90, 0, -1, 0) gl.Color4f(0.5, 0.5, 0.5, 1) glu.Cylinder(quadric, sv_radius, sv_radius*1.05, sv_length, sv_granularity, 1) glu.Disk(quadric, 0, sv_radius, sv_granularity, 1) glh.With(glh.Matrix{gl.MODELVIEW}, func() { gl.Translated(0, 0, sv_length) glu.Disk(quadric, 0, sv_radius*1.05, sv_granularity, 1) }) }) for _, p := range planetoids { p.RenderShadowVolume() } } if cone { gl.FrontFace(gl.CCW) gl.StencilOp(gl.KEEP, gl.KEEP, gl.INCR) shadow_volume() gl.FrontFace(gl.CW) gl.StencilOp(gl.KEEP, gl.KEEP, gl.DECR) shadow_volume() } if shadowing { gl.StencilFunc(gl.NOTEQUAL, 0, 0xffffffff) gl.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP) gl.ColorMask(true, true, true, true) // gl.Disable(gl.STENCIL_TEST) gl.Disable(gl.DEPTH_TEST) gl.FrontFace(gl.CCW) // gl.Color4f(1, 0, 0, 0.75) gl.Color4f(0, 0, 0, 0.75) // gl.Color4f(1, 1, 1, 0.75) gl.LoadIdentity() gl.Translated(0, 0, camera_z_offset) // TODO: Figure out why this doesn't draw over the whole screen glh.With(glh.Disable(gl.LIGHTING), func() { glh.DrawQuadd(-10, -10, 20, 20) }) // gl.FrontFace(gl.CW) // gl.Enable(gl.LIGHTING) // gl.Disable(gl.LIGHT1) // render_scene() // gl.Enable(gl.LIGHT1) } }) } _ = render_stars for running { running = glfw.WindowParam(glfw.Opened) == 1 glfw.SwapBuffers() gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) rotation := func() { if tilt { gl.Rotated(20, 1, 0, 0) } if polar { gl.Rotated(90, 1, 0, 0) } gl.Rotated(d, 0, -1, 0) } // Star field glh.With(glh.Matrix{gl.PROJECTION}, func() { gl.LoadIdentity() glu.Perspective(starfield_fov, aspect, 0, 1) glh.With(glh.Matrix{gl.MODELVIEW}, func() { gl.LoadIdentity() rotation() render_stars() }) }) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translated(0, 0, camera_z_offset) rotation() if rotating { d += 0.2 } _ = render_scene render_scene() _ = render_shadow_volume render_shadow_volume() } }
func EndPaint() { gl.Flush() glfw.SwapBuffers() }