// 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 drawSlide() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(0, 0, -3.0) gl.Begin(gl.QUADS) //top left gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 0) //top right gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 0) //bottom right gl.TexCoord2f(1, 1) gl.Vertex3f(1, -1, 0) //bottom left gl.TexCoord2f(0, 1) gl.Vertex3f(-1, -1, 0) gl.End() }
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 (e *Element) Render() { gl.Color4f(e.color.R, e.color.G, e.color.B, e.color.A) gl.Begin(gl.QUADS) gl.Vertex3f(e.x, e.y, 0) gl.Vertex3f(e.x, e.y+e.height, 0) gl.Vertex3f(e.x+e.width, e.y+e.height, 0) gl.Vertex3f(e.x+e.width, e.y, 0) gl.End() }
func (ctx *DrawContext) drawGrid() { gl.Disable(gl.DEPTH_TEST) defer gl.Enable(gl.DEPTH_TEST) for i := float32(-500); i <= 500; i += 5 { gl.Begin(gl.LINES) gl.Color3f(0.2, 0.2, 0.2) gl.Vertex3f(-500, i, 0) gl.Vertex3f(500, i, 0) gl.Vertex3f(i, -500, 0) gl.Vertex3f(i, 500, 0) gl.End() } }
// 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 drawScene(w *glfw.Window) { width, height := w.GetFramebufferSize() ratio := float32(width) / float32(height) var x1, x2, y1, y2 float32 if ratio > 1 { x1, x2, y1, y2 = -ratio, ratio, -1, 1 } else { x1, x2, y1, y2 = -1, 1, -1/ratio, 1/ratio } gl.Viewport(0, 0, int32(width), int32(height)) gl.Clear(gl.COLOR_BUFFER_BIT) // Applies subsequent matrix operations to the projection matrix stack gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() // replace the current matrix with the identity matrix gl.Ortho(float64(x1), float64(x2), float64(y1), float64(y2), 1, -1) // multiply the current matrix with an orthographic matrix // Applies subsequent matrix operations to the modelview matrix stack gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.LineWidth(1) gl.Begin(gl.LINE) // delimit the vertices of a primitive or a group of like primitives gl.Color3f(0, 0, 0) // set the current color gl.Vertex3f(0, y1, 0) gl.Vertex3f(0, y2, 0) gl.Vertex3f(x1, 0, 0) gl.Vertex3f(x2, 0, 0) gl.End() gl.Rotatef(float32(glfw.GetTime()*50), 0, 0, 1) // multiply the current matrix by a rotation matrix s := float32(.95) gl.Begin(gl.TRIANGLES) gl.Color3f(1, 0, 0) // set the current color gl.Vertex3f(0, s, 0) // specify a vertex gl.Color3f(0, 1, 0) gl.Vertex3f(s*.866, s*-0.5, 0) gl.Color3f(0, 0, 1) gl.Vertex3f(s*-.866, s*-0.5, 0) gl.End() gl.LineWidth(5) gl.Begin(gl.LINE_LOOP) for i := float64(0); i < 2*math.Pi; i += .05 { r, g, b := hsb2rgb(float32(i/(2*math.Pi)), 1, 1) gl.Color3f(r, g, b) gl.Vertex3f(s*float32(math.Sin(i)), s*float32(math.Cos(i)), 0) } gl.End() }
func (o openGLCanvas) FillRect(what color.Color, where image.Rectangle) { return //this does not work at all... makes the whole image red drawX := float32(where.Min.X) * o.scaleX drawY := float32(where.Min.Y) * o.scaleY endX := float32(where.Max.X) * o.scaleX endY := float32(where.Max.Y) * o.scaleY r, g, b, a := what.RGBA() gl.Color4f(float32(r)/0xFF, float32(g)/0xFF, float32(b)/0xFF, float32(a)/0xFF) gl.Begin(gl.QUADS) { gl.Vertex3f(drawX, drawY, 0) gl.Vertex3f(drawX, endX, 0) gl.Vertex3f(endX, endY, 0) gl.Vertex3f(endX, drawY, 0) } gl.End() }
// drawCircle draws a circle for the specified radius, rotation angle, and the specified number of sides func drawCircle(radius float64, sides int) { gl.Begin(gl.LINE_LOOP) for a := 0.0; a < 2*math.Pi; a += (2 * math.Pi / float64(sides)) { gl.Vertex2d(math.Sin(a)*radius, math.Cos(a)*radius) } gl.Vertex3f(0, 0, 0) gl.End() }
// drawCircle draws a circle for the specified radius, rotation angle, and the specified number of sides func drawCircle(radius float64, sides int) { gl.Begin(gl.TRIANGLE_FAN) for a := 0.0; a < 2*math.Pi; a += (2 * math.Pi / float64(70)) { gl.Vertex2d(math.Sin(a)*radius, math.Cos(a)*radius) } gl.Vertex3f(0, 0, 0) gl.End() }
func (o *openGLCanvas) Draw(tile image.Rectangle, where image.Rectangle, f tmx.FlipMode, tileset string) { if _, ok := o.sets[tileset]; !ok { o.sets[tileset] = newChipset(tileset, tile.Max.X-tile.Min.X) } c := o.sets[tileset] gl.BindTexture(gl.TEXTURE_2D, c.handle) // Texture coords fts := float32(c.tilesize) tileWidthPixels := fts / float32(c.width) tileHeightPixels := fts / float32(c.height) startX := (float32(tile.Min.X) / fts) * tileWidthPixels startY := (float32(tile.Min.Y) / fts) * tileHeightPixels endX := startX + tileWidthPixels endY := startY + tileHeightPixels // Draw coords drawX := float32(where.Min.X) drawY := float32(where.Min.Y) gl.Begin(gl.QUADS) { gl.TexCoord2f(startX, startY) gl.Vertex3f(drawX*o.scaleX, drawY*o.scaleY, 0) gl.TexCoord2f(startX, endY) gl.Vertex3f(drawX*o.scaleX, (drawY+fts)*o.scaleY, 0) gl.TexCoord2f(endX, endY) gl.Vertex3f((drawX+fts)*o.scaleX, (drawY+fts)*o.scaleY, 0) gl.TexCoord2f(endX, startY) gl.Vertex3f((drawX+fts)*o.scaleX, (drawY)*o.scaleY, 0) } gl.End() }
func renderTile(id int, x, y float32) { var tx, ty, w, h float32 = 0, 0, 0.0625, 0.0625 tilex := id % 16 tiley := (id - tilex) / 16 tx = float32(tilex) ty = float32(tiley) gl.TexCoord2f(tx/16.0, ty/16.0+h) gl.Vertex3f(x, y, 0) gl.TexCoord2f(tx/16.0, ty/16.0) gl.Vertex3f(x, y+1, 0) gl.TexCoord2f(tx/16.0+w, ty/16.0+h) gl.Vertex3f(x+1, y, 0) gl.TexCoord2f(tx/16.0+w, ty/16.0) gl.Vertex3f(x+1, y+1, 0) gl.TexCoord2f(tx/16.0+w, ty/16.0+h) gl.Vertex3f(x+1, y, 0) gl.TexCoord2f(tx/16.0, ty/16.0) gl.Vertex3f(x, y+1, 0) }
// creates the box vector func box(h float32, w float32, d float32) { gl.ShadeModel(gl.SMOOTH) gl.Normal3d(1.0, 1.0, 1.0) //red := [...]float32{0.8, 0.1, 0.0, 1.0} //green := [...]float32{0.0, 0.8, 0.2, 1.0} //blue := [...]float32{0.2, 0.2, 1.0, 1.0} var red float32 = 1.0 var green float32 = 1.0 var blue float32 = 1.0 gl.Materialfv(gl.FRONT, gl.AMBIENT_AND_DIFFUSE, &red) var delta float32 = 0 // left wall gl.Begin(gl.POLYGON) gl.Color3f(1.0, 0.0, 0.0) gl.Vertex3f(delta, delta, delta) gl.Vertex3f(delta, h, delta) gl.Vertex3f(w, h, delta) gl.Vertex3f(w, delta, delta) gl.End() //right wall gl.Begin(gl.POLYGON) gl.Color3f(0.0, 0.0, 1.0) gl.Vertex3f(delta, delta, d) gl.Vertex3f(delta, h, d) gl.Vertex3f(w, h, d) gl.Vertex3f(w, delta, d) gl.End() gl.Materialfv(gl.FRONT, gl.AMBIENT_AND_DIFFUSE, &blue) //top wall gl.Begin(gl.POLYGON) gl.Color3f(0.0, 1.0, 0.0) gl.Vertex3f(delta, delta, d) gl.Vertex3f(w, delta, d) gl.Vertex3f(w, delta, delta) gl.Vertex3f(delta, delta, delta) gl.End() //bottom wall gl.Begin(gl.POLYGON) gl.Color3f(0.5, 0.5, 0.0) gl.Vertex3f(delta, h, d) gl.Vertex3f(w, h, d) gl.Vertex3f(w, h, delta) gl.Vertex3f(delta, h, delta) gl.End() gl.Materialfv(gl.FRONT, gl.AMBIENT_AND_DIFFUSE, &green) //forward wall gl.Begin(gl.POLYGON) gl.Color3f(0.0, 0.0, 1.0) gl.Vertex3f(delta, delta, delta) gl.Vertex3f(delta, delta, d) gl.Vertex3f(delta, h, d) gl.Vertex3f(delta, h, delta) gl.End() // //back wall gl.Begin(gl.POLYGON) gl.Color3f(0.0, 0.0, 1.0) gl.Vertex3f(w, delta, delta) gl.Vertex3f(w, delta, d) gl.Vertex3f(w, h, d) gl.Vertex3f(w, h, delta) gl.End() }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(0, 0, -3.0) gl.Rotatef(rotationX, 1, 0, 0) gl.Rotatef(rotationY, 0, 1, 0) rotationX += 0.5 rotationY += 0.5 gl.BindTexture(gl.TEXTURE_2D, texture) gl.Color4f(1, 1, 1, 1) gl.Begin(gl.QUADS) gl.Normal3f(0, 0, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) gl.Normal3f(0, 0, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) gl.Normal3f(0, 1, 0) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.Normal3f(0, -1, 0) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.Normal3f(1, 0, 0) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.Normal3f(-1, 0, 0) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() }
// 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 (ctx *DrawContext) drawSphere(p vector.V3, r float64, c colorful.Color) { /* TODO: - decrease sphere detail if it's further away - only draw spheres that would be visible inside the frustum: - (no small spheres near the far plane) */ if ctx.cam.SphereInFrustum(p, r) == OUTSIDE { return } gl.Color3f(float32(c.R), float32(c.G), float32(c.B)) gl.MatrixMode(gl.MODELVIEW) gl.PushMatrix() defer gl.PopMatrix() slices := int(math.Max(10, 5*math.Log(r+1))) gl.Translated(p.X, p.Y, p.Z) gl.Scaled(r, r, r) l, ok := uint32(0), false if ctx.wireframe { l, ok = ctx.spheresWireframe[slices] } else { l, ok = ctx.spheresSolid[slices] } if !ok { ctx.listId++ // XXX: atomic? l = ctx.listId gl.NewList(l, gl.COMPILE) for i := 0; i <= slices; i++ { lat0 := math.Pi * (-0.5 + float64(i-1)/float64(slices)) z0 := math.Sin(lat0) zr0 := math.Cos(lat0) lat1 := math.Pi * (-0.5 + float64(i)/float64(slices)) z1 := math.Sin(lat1) zr1 := math.Cos(lat1) if ctx.wireframe { gl.Begin(gl.LINES) } else { gl.Begin(gl.QUAD_STRIP) } for j := 0; j <= slices; j++ { lng := 2 * math.Pi * (float64(j-1) / float64(slices)) x := math.Cos(lng) y := math.Sin(lng) gl.Normal3f(float32(x*zr0), float32(y*zr0), float32(z0)) gl.Vertex3f(float32(x*zr0), float32(y*zr0), float32(z0)) gl.Normal3f(float32(x*zr1), float32(y*zr1), float32(z1)) gl.Vertex3f(float32(x*zr1), float32(y*zr1), float32(z1)) } gl.End() } gl.EndList() if ctx.wireframe { ctx.spheresWireframe[slices] = l } else { ctx.spheresSolid[slices] = l } } gl.CallList(l) }