/
texture.go
132 lines (120 loc) · 3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2014 Arne Roomann-Kurrik
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package twodee
import (
"bytes"
"fmt"
"github.com/go-gl/gl/v3.3-core/gl"
"image"
)
type TextureSmoothing int
const (
Nearest TextureSmoothing = gl.NEAREST
Linear TextureSmoothing = gl.LINEAR
)
type Texture struct {
Texture uint32
Width int
Height int
OriginalWidth int
OriginalHeight int
}
func LoadTexture(path string, smoothing TextureSmoothing) (texture *Texture, err error) {
var (
img image.Image
)
if img, err = loadPNG(path); err != nil {
return
}
return GetTexture(img, smoothing)
}
func GetTexture(img image.Image, smoothing TextureSmoothing) (texture *Texture, err error) {
var bounds = img.Bounds()
return GetTruncatedTexture(img, smoothing, bounds.Dx(), bounds.Dy())
}
func GetTruncatedTexture(img image.Image, smoothing TextureSmoothing, w, h int) (texture *Texture, err error) {
var (
bounds image.Rectangle
gltex uint32
)
img = getPow2Image(img)
bounds = img.Bounds()
if gltex, err = GetGLTexture(img, smoothing); err != nil {
return
}
texture = &Texture{
Texture: gltex,
Width: bounds.Dx(),
Height: bounds.Dy(),
OriginalWidth: w,
OriginalHeight: h,
}
return
}
func (t *Texture) Bind() {
gl.BindTexture(gl.TEXTURE_2D, t.Texture)
}
func (t *Texture) Unbind() {
gl.BindTexture(gl.TEXTURE_2D, 0)
}
func (t *Texture) Delete() {
if t.Texture != 0 {
gl.BindTexture(gl.TEXTURE_2D, 0)
gl.DeleteTextures(1, &t.Texture)
t.Texture = 0
}
}
func GetGLTexture(img image.Image, smoothing TextureSmoothing) (t uint32, err error) {
var (
data *bytes.Buffer
bounds image.Rectangle
width int
height int
)
if data, err = imageBytes(img); err != nil {
return
}
bounds = img.Bounds()
width = bounds.Max.X - bounds.Min.X
height = bounds.Max.Y - bounds.Min.Y
gl.GenTextures(1, &t)
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt1 ERROR: %s\n", e)
}
gl.BindTexture(gl.TEXTURE_2D, t)
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt2 ERROR: %s\n", e)
}
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(smoothing))
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt3 ERROR: %s\n", e)
}
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(smoothing))
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt4 ERROR: %s\n", e)
}
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, gl.Ptr(data.Bytes()))
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt5 ERROR: %s\n", e)
}
gl.GenerateMipmap(gl.TEXTURE_2D)
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt6 ERROR: %s\n", e)
}
gl.BindTexture(gl.TEXTURE_2D, 0)
if e := gl.GetError(); e != 0 {
fmt.Printf("ggt7 ERROR: %s\n", e)
}
return
}