-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture2D.go
60 lines (47 loc) · 1.23 KB
/
Texture2D.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
package Texture
import (
"github.com/Adi146/goggle-engine/Core/Utils"
"image"
_ "image/jpeg"
_ "image/png"
"github.com/go-gl/gl/v4.3-core/gl"
)
const (
DiffuseTexture Type = "diffuse"
SpecularTexture Type = "specular"
EmissiveTexture Type = "emissive"
NormalsTexture Type = "normals"
)
type Texture2D struct {
Texture
}
func NewTextureFromRGBA(img *image.RGBA, textureType Type) (*Texture2D, error) {
texture := Texture{
Type: textureType,
Target: gl.TEXTURE_2D,
}
gl.GenTextures(1, &texture.ID)
if err := texture.Bind(); err != nil {
return nil, err
}
gl.TexParameteri(texture.Target, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(texture.Target, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
texture.SetWrapMode(ClampToEdge)
gl.TexImage2D(texture.Target, 0, gl.RGBA8, int32(img.Bounds().Dx()), int32(img.Bounds().Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, Utils.GlPtr(img.Pix))
texture.Unbind()
return &Texture2D{
Texture: texture,
}, nil
}
func ImportTexture(filename string, textureType Type) (*Texture2D, error) {
rgba, err := loadRGBA(filename)
if err != nil {
return nil, err
}
rgba = flipRGBA(rgba)
texture, err := NewTextureFromRGBA(rgba, textureType)
if err != nil {
return nil, err
}
return texture, nil
}