Exemplo n.º 1
0
func (img *glImage) DrawColoredAtXY(x, y int, color [4]float32) {
	// TODO have the state knwon globally somewhere so this does not need to be
	// called all the time
	gl.Enable(gl.TEXTURE_2D)
	gl.BindTexture(gl.TEXTURE_2D, img.id)

	gl.Begin(gl.QUADS)

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.top)
	gl.Vertex2i(int32(x), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.top)
	gl.Vertex2i(int32(x+img.Width), int32(y))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.right, img.bottom)
	gl.Vertex2i(int32(x+img.Width), int32(y+img.Height))

	gl.Color4f(color[0], color[1], color[2], color[3])
	gl.TexCoord2f(img.left, img.bottom)
	gl.Vertex2i(int32(x), int32(y+img.Height))

	gl.End()
}
Exemplo n.º 2
0
func MakePolygon(points ...Vertex) Polygon {
	gl.Begin(gl.POLYGON)
	for _, p := range points {
		gl.Vertex2i(p.X, p.Y)
	}
	return Polygon{}
}
Exemplo n.º 3
0
func InitFont() {
	LoadTexture(filepath.Join("data", "Font.png"))

	oFontBase = gl.GenLists(32 + 96*4)
	for i := 0; i < 96*4; i++ {
		const shiftX, shiftY = float64(1.0 / 16), float64(1.0 / 6 / 4)

		indexX, indexY := i%16, i/16

		gl.NewList(oFontBase+uint32(i+32), gl.COMPILE)
		gl.Begin(gl.QUADS)
		gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY)*shiftY)
		gl.Vertex2i(0, 0)
		gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY)*shiftY)
		gl.Vertex2i(fontWidth, 0)
		gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY+1)*shiftY)
		gl.Vertex2i(fontWidth, fontHeight)
		gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY+1)*shiftY)
		gl.Vertex2i(0, fontHeight)
		gl.End()
		gl.Translated(fontWidth, 0.0, 0.0)
		gl.EndList()
	}

	oFontBackground = gl.GenLists(1)
	gl.NewList(oFontBackground, gl.COMPILE)
	gl.Begin(gl.QUADS)
	gl.Vertex2i(0, 0)
	gl.Vertex2i(0, fontHeight)
	gl.Vertex2i(fontWidth, fontHeight)
	gl.Vertex2i(fontWidth, 0)
	gl.End()
	gl.Translated(fontWidth, 0.0, 0.0)
	gl.EndList()

	CheckGLError()
}
Exemplo n.º 4
0
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()
	}
}
Exemplo n.º 5
0
func MakeRect(X, Y, Width, Height int32) Rect {
	rect := Rect{X, Y, Width, Height, MakeColor(255, 255, 255), NumPropertySet{0, 0, 0, 0}}
	x := rect.X
	y := rect.Y
	width := rect.X2
	height := rect.Y2
	radius := rect.BorderRadius.LEFT

	//TODO edit corner radius builder to use NumPropertySet for each corner
	if radius > 0 {
		gl.Color4f(1, 0, 0, 0.5)
		gl.Begin(gl.QUADS)

		gl.Vertex2i(x+radius, y+height)         //4
		gl.Vertex2i(x+(width-radius), y+height) //5

		gl.Vertex2i(x+(width-radius), y) //10
		gl.Vertex2i(x+radius, y)         //11

		gl.End()

		gl.Begin(gl.QUADS)
		gl.Vertex2i(x, y+radius)                 //1
		gl.Vertex2i(x, y+(height-radius))        //2
		gl.Vertex2i(x+radius, y+(height-radius)) //3
		gl.Vertex2i(x+radius, y+radius)          //12
		gl.End()

		gl.Begin(gl.QUADS)
		gl.Vertex2i(x+(width-radius), y+(height-radius)) //6
		gl.Vertex2i(x+width, y+(height-radius))          //7
		gl.Vertex2i(x+width, y+radius)                   //8
		gl.Vertex2i(x+(width-radius), y+radius)          //9
		gl.End()

		gl.Begin(gl.TRIANGLE_FAN)
		orginx := x
		orginy := y

		x = orginx + radius
		y = orginy + radius

		//gl.Translatef(float32(-x), float32(-y), 0)
		var triangleAmount = float64(48.0) //# of triangles used to draw circle
		//GLfloat radius = 0.8f; //radius
		twicePi := float64(0.5) * float64(3.14)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(144); i >= triangleAmount+48; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}

		gl.End()

		y = orginy + height - radius

		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(96); i >= triangleAmount; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()

		x = orginx + width - radius
		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(0); i <= triangleAmount; i++ {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()

		y = orginy + radius

		gl.Begin(gl.TRIANGLE_FAN)
		gl.Vertex2f(float32(x), float32(y)) // center of circle
		for i := float64(192); i >= triangleAmount+96; i-- {

			gl.Vertex2f(float32(float64(x)+(float64(radius)*Cos(i*twicePi/triangleAmount))), float32(float64(y)+(float64(radius)*Sin(i*twicePi/triangleAmount))))
		}
		gl.End()
	} else {
		gl.Begin(gl.QUADS)
		gl.Vertex2i(x, y)
		gl.Vertex2i(x, y+height)
		gl.Vertex2i(x+width, y+height)
		gl.Vertex2i(x+width, y)
		gl.End()
	}

	// if rect.Stroke.BOTTOM.Width < 1 || rect.Stroke.LEFT.Width < 1 || rect.Stroke.RIGHT.Width < 1 || rect.Stroke.TOP.Width < 1 {
	// 	//BOTTOM
	// 	if rect.Stroke.BOTTOM.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.BOTTOM.Fill.R)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.G)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.B)*0.0039), float32(float32(rect.Stroke.BOTTOM.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y+height)
	// 		gl.Vertex2i(x+width, y+height)
	// 		gl.End()
	// 	} else if rect.Stroke.LEFT.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.LEFT.Fill.R)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.G)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.B)*0.0039), float32(float32(rect.Stroke.LEFT.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y)
	// 		gl.Vertex2i(x, y+height)
	// 		gl.End()
	// 	} else if rect.Stroke.TOP.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.TOP.Fill.R)*0.0039), float32(float32(rect.Stroke.TOP.Fill.G)*0.0039), float32(float32(rect.Stroke.TOP.Fill.B)*0.0039), float32(float32(rect.Stroke.TOP.Fill.A)*0.0039))
	// 		gl.Vertex2i(x, y)
	// 		gl.Vertex2i(x+width, y)
	// 		gl.End()
	// 	} else if rect.Stroke.RIGHT.Width < 1 {
	// 		gl.Begin(gl.LINE)
	// 		gl.Color4f(float32(float32(rect.Stroke.RIGHT.Fill.R)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.G)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.B)*0.0039), float32(float32(rect.Stroke.RIGHT.Fill.A)*0.0039))
	// 		gl.Vertex2i(x+width, y)
	// 		gl.Vertex2i(x+width, y+height)
	// 		gl.End()
	// 	}

	// 	//LEFT

	// 	//TOP

	// 	//RIGHT
	// 	gl.Begin(gl.LINES)
	// 	gl.Vertex2i(x, y)
	// 	gl.Vertex2i(x, y+height)
	// 	gl.Vertex2i(x+width, y+height)
	// 	gl.Vertex2i(x+width, y)
	// }

	return rect
}