/
image.go
84 lines (66 loc) · 1.87 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package vuelto
import (
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"vuelto.me/internal/gl"
)
type Image struct {
Texture uint32
X, Y float32
Width, Height float32
}
var ImageArray []uint32
// Loads a new image and returns a Image struct. Can be later drawn using the Draw() method
func (r *Renderer2D) LoadImage(imagePath string, x, y, width, height float32) *Image {
file, err := os.Open(imagePath)
if err != nil {
log.Fatalln("Failed to open image: ", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
log.Fatalln("Failed to decode image: ", err)
}
rgba := image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, image.Point{}, draw.Over)
var textureID uint32
gl.GenTextures(1, &textureID)
gl.BindTexture(gl.TEXTURE_2D, textureID)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, rgba.Rect.Size().X, rgba.Rect.Size().Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, rgba.Pix)
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_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
ImageArray = append(ImageArray, textureID)
return &Image{
Texture: textureID,
X: x,
Y: y,
Width: width,
Height: height,
}
}
// Draws the image that's loaded before.
func (img *Image) Draw() {
gl.BindTexture(gl.TEXTURE_2D, img.Texture)
defer gl.BindTexture(gl.TEXTURE_2D, 0)
gl.Begin(gl.QUADS)
defer gl.End()
gl.TexCoord2f(0.0, 0.0)
gl.Vertex2f(img.X, img.Y)
gl.TexCoord2f(1.0, 0.0)
gl.Vertex2f(img.X+img.Width, img.Y)
gl.TexCoord2f(1.0, 1.0)
gl.Vertex2f(img.X+img.Width, img.Y+img.Height)
gl.TexCoord2f(0.0, 1.0)
gl.Vertex2f(img.X, img.Y+img.Height)
}
func cleanTex() {
for _, i := range ImageArray {
gl.DeleteTextures(1, &i)
}
}