Exemplo n.º 1
0
func main() {
	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.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(800, 600, "Cube", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	texture = newTexture("square.png")
	defer gl.DeleteTextures(1, &texture)

	setupScene()
	for !window.ShouldClose() {
		drawScene()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Exemplo n.º 2
0
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()
}
Exemplo n.º 3
0
func (c *Context) DeleteTexture(t Texture) {
	_ = c.runOnContextThread(func() error {
		tt := uint32(t)
		if !gl.IsTexture(tt) {
			return nil
		}
		if c.lastTexture == t {
			c.lastTexture = invalidTexture
		}
		gl.DeleteTextures(1, &tt)
		return nil
	})
}
Exemplo n.º 4
0
func setupScene() {

	gl.ClearColor(0, 0, 0, 0)
	if texDel {
		gl.DeleteTextures(1, tex1)
	}
	tex1 = newTexture(*slides[selCell].getImage(monWidth, monHeight))

	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(-1, 1, -1, 1, 1.0, 10.0)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	texDel = true

}
Exemplo n.º 5
0
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)
}
Exemplo n.º 6
0
func (atlas *FontAtlas) upload() {
	if !atlas.Dirty {
		return
	}
	atlas.Dirty = false

	gl.Enable(gl.TEXTURE_2D)

	if atlas.Texture != 0 {
		gl.DeleteTextures(1, &atlas.Texture)
		atlas.Texture = 0
	}

	gl.GenTextures(1, &atlas.Texture)
	gl.BindTexture(gl.TEXTURE_2D, atlas.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(atlas.Image.Rect.Size().X),
		int32(atlas.Image.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(atlas.Image.Pix))

	if err := gl.GetError(); err != 0 {
		log.Println(err)
	}

	gl.Disable(gl.TEXTURE_2D)
}
Exemplo n.º 7
0
// DeleteTexture deletes the given texture object.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glDeleteTextures.xhtml
func DeleteTexture(v Texture) {
	gl.DeleteTextures(1, &v.Value)
}
Exemplo n.º 8
0
//Creates a new engine and populates it with the core functions
func LoadGraphics(e *Engine) *Engine {

	e = add(e, "glfw.Init", NewCode("glfw.Init", 0, func(e *Engine, c *Thingy) *Engine {
		err := glfw.Init()
		if err != nil {
			panic(err)
		}
		return e
	}))

	e = add(e, "GLFWEVENTLOOP", NewCode("GLFWEVENTLOOP", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		window := el1._structVal.(*glfw.Window)
		window.MakeContextCurrent()

		if err := gl.Init(); err != nil {
			panic(err)
		}

		texture = newTexture("square.png")
		defer gl.DeleteTextures(1, &texture)

		setupScene()
		for !window.ShouldClose() {
			drawScene()
			window.SwapBuffers()
			glfw.PollEvents()
		}

		return e
	}))

	e = add(e, "wg.GetStringBounds", NewCode("wg.GetStringBounds", 0, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		left, top, right, bottom := gc.GetStringBounds(el1.getString())
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", bottom), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", right), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", top), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", left), c))
		return e
	}))

	e = add(e, "wg.SetFillColor", NewCode("wg.SetFillColor", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		col := el1._structVal.(color.RGBA)
		EnsureGC()
		gc.SetFillColor(col)
		return e
	}))

	e = add(e, "wg.SetStrokeColor", NewCode("wg.SetStrokeColor", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		col := el1._structVal.(color.RGBA)
		EnsureGC()
		gc.SetStrokeColor(col)
		return e
	}))

	e = add(e, "wg.CloseAndFill", NewCode("wg.CloseAndFill", 0, func(e *Engine, c *Thingy) *Engine {
		EnsureGC()
		gc.Close()
		gc.FillStroke()
		return e
	}))

	e = add(e, "wg.MoveTo", NewCode("wg.MoveTo", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		gc.MoveTo(float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "wg.LineTo", NewCode("wg.LineTo", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		gc.LineTo(float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "wg.RoundedRectangle", NewCode("wg.RoundedRectangle", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3, el4 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		el4, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		x3, _ := strconv.ParseFloat(el3.getString(), 32)
		x4, _ := strconv.ParseFloat(el4.getString(), 32)
		EnsureGC()
		draw2dkit.RoundedRectangle(gc, x1, x2, x3, x4, 10, 10)
		gc.FillStroke()
		return e
	}))

	e = add(e, "wg.FillStringAt", NewCode("wg.FillStringAt", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3 *Thingy
		el3, e.dataStack = popStack(e.dataStack)
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		// Set the font luximbi.ttf
		gc.SetFontData(draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic})
		// Set the fill text color to black
		gc.SetFillColor(image.White)
		gc.SetFontSize(15)
		gc.FillStringAt(el3.getString(), float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "WG.CLEAR", NewCode("WG.CLEAR", 1, func(e *Engine, c *Thingy) *Engine {
		var el3 *Thingy
		el3, e.dataStack = popStack(e.dataStack)
		col := el3._structVal.(color.RGBA)
		wg.ClearImage(col)
		return e
	}))

	e = add(e, "WG.TEST", NewCode("WG.TEST", 0, func(e *Engine, c *Thingy) *Engine {
		/*var el1, el2,el3 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt( el1.getString(), 10, 32 )
		x2, _ := strconv.ParseInt( el2.getString(), 10, 32 )
		col := el3._structVal.(color.RGBA) */
		EnsureGC()
		// Set some properties
		gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
		gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
		gc.SetLineWidth(5)

		// Draw a closed shape
		gc.MoveTo(100, 100) // should always be called first for a new path
		gc.LineTo(600, 150)
		gc.QuadCurveTo(600, 10, 10, 10)
		gc.Close()
		gc.FillStroke()
		return e
	}))

	e = add(e, "WG.SETPOINT", NewCode("WG.SETPOINT", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt(el1.getString(), 10, 32)
		x2, _ := strconv.ParseInt(el2.getString(), 10, 32)
		col := el3._structVal.(color.RGBA)
		wg.GetImage().Set(int(x1), int(x2), col)
		return e
	}))

	e = add(e, "RGBA", NewCode("RGBA", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3, el4 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		el4, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt(el1.getString(), 10, 32)
		x2, _ := strconv.ParseInt(el2.getString(), 10, 32)
		x3, _ := strconv.ParseInt(el3.getString(), 10, 32)
		x4, _ := strconv.ParseInt(el4.getString(), 10, 32)
		col := color.RGBA{uint8(x1), uint8(x2), uint8(x3), uint8(x4)}
		e.dataStack = append(e.dataStack, NewWrapper(col))
		return e
	}))

	e = add(e, "WG.GETKEY", NewCode("WG.GETKEY", 0, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		xx, _ := strconv.ParseInt(el1.getString(), 10, 32)
		var ret *Thingy
		if wg.GetKey(int(xx)) {
			ret = NewBool(1)
		} else {
			ret = NewBool(0)
		}
		e.dataStack = append(e.dataStack, ret)
		return e
	}))
	e = add(e, "WG.START", NewCode("WG.START", 1, func(ne *Engine, c *Thingy) *Engine {
		CallbackState = ne
		var port *Thingy
		port, ne.dataStack = popStack(ne.dataStack)
		portnum, _ := strconv.ParseInt(port.getString(), 10, 32)
		wg.Start(800, 600, int(portnum), WgCallback)
		return ne
	}))

	e = add(e, "glfw.CreateWindow", NewCode("glfw.CreateWindow", 2, func(ne *Engine, c *Thingy) *Engine {
		var x, y, title *Thingy

		x, ne.dataStack = popStack(ne.dataStack)
		y, ne.dataStack = popStack(ne.dataStack)
		title, ne.dataStack = popStack(ne.dataStack)

		xx, _ := strconv.ParseInt(x.getString(), 10, 32)
		yy, _ := strconv.ParseInt(y.getString(), 10, 32)

		glfw.WindowHint(glfw.Resizable, glfw.False)
		glfw.WindowHint(glfw.ContextVersionMajor, 2)
		glfw.WindowHint(glfw.ContextVersionMinor, 1)

		window, err := glfw.CreateWindow(int(xx), int(yy), title.getString(), nil, nil)
		if err != nil {
			panic(err)
		}

		ne.dataStack = pushStack(ne.dataStack, NewWrapper(window))
		return ne
	}))
	return e
}
Exemplo n.º 9
0
func (c *Context) DeleteTexture(t Texture) {
	tt := uint32(t)
	gl.DeleteTextures(1, &tt)
}
Exemplo n.º 10
0
// DeleteTexture will free the texture from the GPU memory
func (c *Context) DeleteTexture(texture *Texture) {
	gl.DeleteTextures(1, &[]uint32{texture.uint32}[0])
}
Exemplo n.º 11
0
func (texture *Texture) Delete() {
	gl.DeleteTextures(1, &texture.id)
}
Exemplo n.º 12
0
Arquivo: font.go Projeto: go-gl/gltext
// Release releases font resources.
// A font can no longer be used for rendering after this call completes.
func (f *Font) Release() {
	gl.DeleteTextures(1, &f.texture)
	gl.DeleteLists(f.listbase, int32(len(f.config.Glyphs)))
	f.config = nil
}