예제 #1
0
파일: sprite.go 프로젝트: velovix/paunch
// NewSprite creates a new Sprite object using the given data, which is
// expected to be in RGBA format. If you use PNG image files, you can use the
// NewSpriteFromImage shortcut function instead.
func NewSprite(x, y, width, height float64, data []byte, clip int) (*Sprite, error) {

	verticies := []float64{
		x, y,
		x + width, y,
		x, y + height,

		x + width, y + height,
		x + width, y,
		x, y + height}

	shape, err := NewShape(gl.TRIANGLES, verticies)
	if err != nil {
		return nil, err
	}

	sprite := &Sprite{texcoordBuffer: 0, texture: nil, shape: shape}

	texCoords := []float32{
		0, 0,
		1, 0,
		0, 1,

		1, 1,
		1, 0,
		0, 1}

	gl.GenBuffers(1, &sprite.texcoordBuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(sprite.texcoordBuffer))
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(texCoords)*4), gl.Pointer(&texCoords[0]), gl.STREAM_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	sprite.texture = make([]gl.Uint, clip)
	gl.GenTextures(gl.Sizei(clip), &sprite.texture[0])

	clips := make([][]byte, clip)
	for i := range clips {
		clips[i] = data[i*(len(data)/len(clips)) : (i+1)*(len(data)/len(clips))]
		gl.BindTexture(gl.TEXTURE_2D, sprite.texture[len(clips)-1-i])
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
			gl.Sizei(width), gl.Sizei(height),
			0, gl.RGBA, gl.UNSIGNED_BYTE,
			gl.Pointer(&clips[i][0]))
	}

	gl.BindTexture(gl.TEXTURE_2D, 0)

	return sprite, checkForErrors()
}
예제 #2
0
파일: gomandel.go 프로젝트: nsf/gomandel
func uploadTexture_RGBA32(w, h int, data []byte) gl.Uint {
	var id gl.Uint
	gl.GenTextures(1, &id)
	gl.BindTexture(gl.TEXTURE_2D, id)
	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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0, gl.RGBA,
		gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))

	if gl.GetError() != gl.NO_ERROR {
		gl.DeleteTextures(1, &id)
		panic("Failed to load a texture")
	}
	return id
}
예제 #3
0
파일: backend.go 프로젝트: ajhager/rog
func (w *glfwBackend) Open(width, height, zoom int, fs bool, font *FontData) {
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	w.font = font
	w.zoom = zoom
	w.width = width
	w.height = height

	var fwidth = width * font.CellWidth * zoom
	var fheight = height * font.CellHeight * zoom
	var twidth = fwidth
	var theight = fheight

	flag := glfw.Windowed
	if fs {
		flag = glfw.Fullscreen
		dm := glfw.DesktopMode()
		twidth = dm.W
		theight = dm.H
	}

	glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE)
	err := glfw.OpenWindow(twidth, theight, 8, 8, 8, 8, 0, 0, flag)
	if err != nil {
		panic(err)
	}

	w.key = NOKEY
	glfw.SetWindowCloseCallback(func() int { w.Close(); return 0 })
	glfw.SetKeyCallback(func(key, state int) { w.setKey(key, state) })
	glfw.SetCharCallback(func(key, state int) { w.setKey(key, state) })
	glfw.Enable(glfw.KeyRepeat)

	w.mouse = new(MouseData)
	glfw.Enable(glfw.MouseCursor)
	glfw.SetMousePosCallback(func(x, y int) { w.mouseMove(x, y) })
	glfw.SetMouseButtonCallback(func(but, state int) { w.mousePress(but, state) })
	glfw.Enable(glfw.MouseCursor)

	xoff := float32(twidth-fwidth) / 2.0
	yoff := float32(theight-fheight) / 2.0

	fc := float32(font.CellWidth * zoom)
	fch := float32(font.CellHeight * zoom)
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			cx := xoff + float32(x)*fc
			cy := yoff + float32(y)*fch
			w.verts = append(w.verts, cx, cy, cx, cy+fch, cx+fc, cy+fch, cx+fc, cy)
		}
	}

	runtime.LockOSThread()
	glInit(twidth, theight)

	m := font.Image.(*image.RGBA)
	w.s = float32(font.CellWidth) / float32(m.Bounds().Max.X)
	w.t = float32(font.CellHeight) / float32(m.Bounds().Max.Y)
	textures = make([]gl.Uint, 2)
	gl.GenTextures(2, &textures[0])

	gl.BindTexture(gl.TEXTURE_2D, textures[0])
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0]))

	m = image.NewRGBA(image.Rect(0, 0, font.CellWidth, font.CellHeight))
	draw.Draw(m, m.Bounds(), &image.Uniform{White}, image.ZP, draw.Src)
	gl.BindTexture(gl.TEXTURE_2D, textures[1])
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0]))

	w.open = true
}