commit 9b4b3e71ac8ec7c6a4d3b52944d0739155182460
parent e273ad0ac66480f6fb58a1ff0fc3cbc917e21025
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 23 Oct 2024 11:11:43 +0900
flip image
Diffstat:
3 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl
@@ -5,6 +5,6 @@ uniform sampler2D texture1;
uniform sampler2D texture2;
out vec4 fcol;
void main() {
- fcol = mix(texture(texture1, texCoord), texture(texture2, texCoord), 0.5);
+ fcol = mix(texture(texture1, texCoord), texture(texture2, texCoord), 0.2);
}
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -103,7 +103,7 @@ func main() {
log.Fatalf("set texture: %v", err)
}
- texture2, err := draw.NewTexture(texpath2)
+ texture2, err := draw.NewTextureFlip(texpath2, true, false)
if err != nil {
log.Fatalf("create texture: %v", err)
}
diff --git a/texture.go b/texture.go
@@ -32,6 +32,10 @@ type Texture struct {
var nextUnit uint32 = gl.TEXTURE0
func NewTexture(name string) (*Texture, error) {
+ return NewTextureFlip(name, false, false)
+}
+
+func NewTextureFlip(name string, v bool, h bool) (*Texture, error) {
var id uint32
gl.GenTextures(1, &id)
gl.BindTexture(gl.TEXTURE_2D, id)
@@ -44,6 +48,12 @@ func NewTexture(name string) (*Texture, error) {
return nil, fmt.Errorf("load texture: %v", err)
}
rgba := image.NewRGBA(img.Bounds())
+ if v {
+ img = flipV(img)
+ }
+ if h {
+ img = flipH(img)
+ }
idraw.Draw(rgba, rgba.Bounds(), img, image.ZP, idraw.Src)
if rgba.Stride != rgba.Rect.Dx()*4 {
return nil, fmt.Errorf("data should be packed dense")
@@ -61,3 +71,27 @@ func (t *Texture) Bind() {
gl.ActiveTexture(t.unit)
gl.BindTexture(gl.TEXTURE_2D, t.id)
}
+
+func flipV(img image.Image) image.Image {
+ dst := image.NewRGBA(img.Bounds())
+ xmin, ymin := dst.Bounds().Min.X, dst.Bounds().Min.Y
+ xmax, ymax := dst.Bounds().Max.X, dst.Bounds().Max.Y
+ for y := ymin; y < ymax; y++ {
+ for x := xmin; x < xmax; x++ {
+ dst.Set(x, ymax + ymin - y, img.At(x, y))
+ }
+ }
+ return dst
+}
+
+func flipH(img image.Image) image.Image {
+ dst := image.NewRGBA(img.Bounds())
+ xmin, ymin := dst.Bounds().Min.X, dst.Bounds().Min.Y
+ xmax, ymax := dst.Bounds().Max.X, dst.Bounds().Max.Y
+ for y := ymin; y < ymax; y++ {
+ for x := xmin; x < xmax; x++ {
+ dst.Set(xmax + xmin - x, y, img.At(x, y))
+ }
+ }
+ return dst
+}