2014-12-30 19:04:52 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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 opengl
|
|
|
|
|
|
|
|
import (
|
2014-12-31 06:57:51 +01:00
|
|
|
"errors"
|
2014-12-30 19:04:52 +01:00
|
|
|
"github.com/go-gl/gl"
|
|
|
|
)
|
|
|
|
|
2014-12-31 06:57:51 +01:00
|
|
|
type Filter int
|
|
|
|
|
|
|
|
const (
|
|
|
|
filterNearest Filter = gl.NEAREST
|
|
|
|
filterLinear = gl.LINEAR
|
|
|
|
)
|
|
|
|
|
2014-12-30 19:04:52 +01:00
|
|
|
type Context struct {
|
2014-12-31 06:57:51 +01:00
|
|
|
Nearest Filter
|
|
|
|
Linear Filter
|
2014-12-30 19:04:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:57:51 +01:00
|
|
|
type Texture gl.Texture
|
|
|
|
|
2014-12-30 19:04:52 +01:00
|
|
|
func NewContext() *Context {
|
2014-12-31 06:57:51 +01:00
|
|
|
c := &Context{
|
|
|
|
Nearest: filterNearest,
|
|
|
|
Linear: filterLinear,
|
|
|
|
}
|
2014-12-30 19:04:52 +01:00
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) init() {
|
|
|
|
gl.Init()
|
|
|
|
gl.Enable(gl.TEXTURE_2D)
|
|
|
|
// Textures' pixel formats are alpha premultiplied.
|
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
|
|
|
}
|
2014-12-31 06:57:51 +01:00
|
|
|
|
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
|
|
|
|
t := gl.GenTexture()
|
|
|
|
if t < 0 {
|
|
|
|
return 0, errors.New("glGenTexture failed")
|
|
|
|
}
|
|
|
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
|
|
|
t.Bind(gl.TEXTURE_2D)
|
|
|
|
defer gl.Texture(0).Bind(gl.TEXTURE_2D)
|
|
|
|
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
|
|
|
|
|
|
|
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
|
|
|
|
|
|
|
return Texture(t), nil
|
|
|
|
}
|