-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.go
63 lines (48 loc) · 1.23 KB
/
texture.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
package ghetty
import (
"image"
"log"
"math"
"os"
)
const (
R = 0
G = 1
B = 2
A = 3
RGBToFloat = 1. / 255.
)
type Texture struct {
Width, Height int
Data []byte
}
func LoadTexture(directory string) Texture {
file, err := os.Open(directory)
if err != nil {
log.Fatal(err)
}
defer file.Close()
image, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
var texture Texture = Texture{Width: image.Bounds().Dx(), Height: image.Bounds().Dy()}
for y := 0; y < texture.Height; y++ {
for x := 0; x < texture.Width; x++ {
r, g, b, a := image.At(x, y).RGBA()
texture.Data = append(texture.Data, byte(r), byte(g), byte(b), byte(a))
}
}
return texture
}
func (texture *Texture) ConvertPosition(uv *Vertex) int {
var tx int = int((*uv)[X] * float32(texture.Width))
var ty int = int((1 - (*uv)[Y]) * float32(texture.Height))
return int(math.Abs(float64((ty*texture.Width+tx)*4))) % len(texture.Data)
}
func (texture *Texture) Get(position int) (r, g, b, a float32) {
return float32(texture.Data[position+R]) * RGBToFloat,
float32(texture.Data[position+G]) * RGBToFloat,
float32(texture.Data[position+B]) * RGBToFloat,
float32(texture.Data[position+A]) * RGBToFloat
}