func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } // int width, height // glfw.GetFramebufferSize(window, &width, &height) for !window.ShouldClose() { // Do OpenGL stuff. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Color3f(1, 1, 0) gl.Rectf(-0.75, 0.75, 0.75, -0.75) gl.Color3f(1, 0, 1) gl.Rectf(-0.5, 0.5, 0.5, -0.5) window.SwapBuffers() glfw.PollEvents() } }
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 drawgl() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Begin(gl.TRIANGLES) gl.Color3f(1.0, 0.0, 0.0) gl.Vertex2f(0.5, 0.0) gl.Color3f(0.0, 1.0, 0.0) gl.Vertex2f(-0.5, -0.5) gl.Color3f(0.0, 0.0, 1.0) gl.Vertex2f(-0.5, 0.5) 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() 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 (ctx *DrawContext) drawHud(o *orrery.Orrery, frametime time.Duration) { txt, size, err := ctx.createHudTexture(o, frametime) if err != nil { log.Fatalf(`can't create texture from text surface: %s`, err) } defer gl.DeleteTextures(1, &txt) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() gl.Ortho(0.0, float64(ctx.width), float64(ctx.height), 0.0, -1.0, 1.0) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Clear(gl.DEPTH_BUFFER_BIT) gl.BindTexture(gl.TEXTURE_2D, txt) gl.Enable(gl.TEXTURE_2D) defer gl.Disable(gl.TEXTURE_2D) gl.Color3f(1, 1, 1) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2f(0.0, 0.0) gl.TexCoord2f(1, 0) gl.Vertex2f(float32(size[0]), 0.0) gl.TexCoord2f(1, 1) gl.Vertex2f(float32(size[0]), float32(size[1])) gl.TexCoord2f(0, 1) gl.Vertex2f(0.0, float32(size[1])) gl.End() gl.PopMatrix() }
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 draw(screen [64][32]uint8) { for i := 0; i < 64; i++ { for j := 0; j < 32; j++ { color := float32(screen[i][j]) gl.Color3f(color, color, color) gl.Rectf(float32(i), float32(j), float32(i+1), float32(j+1)) } } window.SwapBuffers() }
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 drawThumbnail(x, y, tx, ty, tw, th float32) { sx := x + 4 sy := y + 4 gl.Disable(gl.TEXTURE_2D) gl.Color3f(0.2, 0.2, 0.2) gl.Begin(gl.QUADS) gl.Vertex2f(sx, sy) gl.Vertex2f(sx+256, sy) gl.Vertex2f(sx+256, sy+240) gl.Vertex2f(sx, sy+240) gl.End() gl.Enable(gl.TEXTURE_2D) gl.Color3f(1, 1, 1) gl.Begin(gl.QUADS) gl.TexCoord2f(tx, ty) gl.Vertex2f(x, y) gl.TexCoord2f(tx+tw, ty) gl.Vertex2f(x+256, y) gl.TexCoord2f(tx+tw, ty+th) gl.Vertex2f(x+256, y+240) gl.TexCoord2f(tx, ty+th) gl.Vertex2f(x, y+240) gl.End() }
// 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 asetaVäri(väri Väri) { gl.Color3f(väri[0], väri[1], väri[2]) }
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) }