Skip to content

Commit

Permalink
Fix examples/gopher/gopher.go couldn't load PNG - issue chsc#36
Browse files Browse the repository at this point in the history
  • Loading branch information
Traun Leyden committed Feb 9, 2014
1 parent 99c9a42 commit 6c35ac6
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions examples/gopher/gopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"image"
"image/png"
"io"
"io/ioutil"
"os"
)

Expand All @@ -19,11 +20,11 @@ const (
)

var (
texture gl.Uint
rotx, roty gl.Float
ambient []gl.Float = []gl.Float{0.5, 0.5, 0.5, 1}
diffuse []gl.Float = []gl.Float{1, 1, 1, 1}
lightpos []gl.Float = []gl.Float{-5, 5, 10, 0}
texture gl.Uint
rotx, roty gl.Float
ambient []gl.Float = []gl.Float{0.5, 0.5, 0.5, 1}
diffuse []gl.Float = []gl.Float{1, 1, 1, 1}
lightpos []gl.Float = []gl.Float{-5, 5, 10, 0}
)

func main() {
Expand Down Expand Up @@ -78,12 +79,12 @@ func createTexture(r io.Reader) (textureId gl.Uint, err error) {

// flip image: first pixel is lower left corner
imgWidth, imgHeight := img.Bounds().Dx(), img.Bounds().Dy()
data := make([]byte, imgWidth * imgHeight * 4)
data := make([]byte, imgWidth*imgHeight*4)
lineLen := imgWidth * 4
dest := len(data)-lineLen
for src := 0; src < len(rgbaImg.Pix); src+=rgbaImg.Stride {
dest := len(data) - lineLen
for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride {
copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride])
dest-=lineLen
dest -= lineLen
}
gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))

Expand All @@ -96,6 +97,7 @@ func createTextureFromBytes(data []byte) (gl.Uint, error) {
}

func initScene() (err error) {

gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.LIGHTING)
Expand All @@ -116,7 +118,12 @@ func initScene() (err error) {
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()

texture, err = createTextureFromBytes(gopher_png[:])
gopher_png, err := ioutil.ReadFile("gopher.png")
if err != nil {
panic("Could not load png")
}

texture, err = createTextureFromBytes(gopher_png)
return
}

Expand Down

0 comments on commit 6c35ac6

Please sign in to comment.