mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Add opengl.Context.NewBuffer
This commit is contained in:
parent
7597268e85
commit
f88154f380
@ -27,7 +27,7 @@ const (
|
||||
FilterLinear
|
||||
)
|
||||
|
||||
func glFilter(c *opengl.Context, filter Filter) opengl.Filter {
|
||||
func glFilter(c *opengl.Context, filter Filter) opengl.FilterType {
|
||||
switch filter {
|
||||
case FilterNearest:
|
||||
return c.Nearest
|
||||
|
@ -41,8 +41,6 @@ type TextureQuads interface {
|
||||
|
||||
var initialized = false
|
||||
|
||||
const size = 10000
|
||||
|
||||
// TODO: Use unsafe.SizeOf?
|
||||
const uint16Size = 2
|
||||
const float32Size = 4
|
||||
@ -54,31 +52,13 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4]
|
||||
if err := initialize(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vertexBuffer := gl.GenBuffer()
|
||||
vertexBuffer.Bind(gl.ARRAY_BUFFER)
|
||||
s := float32Size * stride * size
|
||||
gl.BufferData(gl.ARRAY_BUFFER, s, nil, gl.DYNAMIC_DRAW)
|
||||
|
||||
indexBuffer := gl.GenBuffer()
|
||||
indexBuffer.Bind(gl.ELEMENT_ARRAY_BUFFER)
|
||||
indices := make([]uint16, 6*size)
|
||||
for i := uint16(0); i < size; i++ {
|
||||
indices[6*i+0] = 4*i + 0
|
||||
indices[6*i+1] = 4*i + 1
|
||||
indices[6*i+2] = 4*i + 2
|
||||
indices[6*i+3] = 4*i + 1
|
||||
indices[6*i+4] = 4*i + 2
|
||||
indices[6*i+5] = 4*i + 3
|
||||
}
|
||||
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, uint16Size*len(indices), indices, gl.STATIC_DRAW)
|
||||
|
||||
initialized = true
|
||||
}
|
||||
|
||||
if quads.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Check performance
|
||||
program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color)
|
||||
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
var programColorMatrix opengl.Program
|
||||
|
||||
func initialize(c *opengl.Context) error {
|
||||
const size = 10000
|
||||
|
||||
var err error
|
||||
shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source)
|
||||
if err != nil {
|
||||
@ -39,6 +41,22 @@ func initialize(c *opengl.Context) error {
|
||||
shaders[shaderColorMatrix].native,
|
||||
}
|
||||
programColorMatrix, err = c.NewProgram(shaders)
|
||||
|
||||
const stride = 4 * 4
|
||||
s := float32Size * stride * size
|
||||
c.NewBuffer(c.ArrayBuffer, s, nil, c.DynamicDraw)
|
||||
|
||||
indices := make([]uint16, 6*size)
|
||||
for i := uint16(0); i < size; i++ {
|
||||
indices[6*i+0] = 4*i + 0
|
||||
indices[6*i+1] = 4*i + 1
|
||||
indices[6*i+2] = 4*i + 2
|
||||
indices[6*i+3] = 4*i + 1
|
||||
indices[6*i+4] = 4*i + 2
|
||||
indices[6*i+5] = 4*i + 3
|
||||
}
|
||||
c.NewBuffer(c.ElementArrayBuffer, uint16Size*len(indices), indices, c.StaticDraw)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ func (t *Texture) Size() (width, height int) {
|
||||
return t.width, t.height
|
||||
}
|
||||
|
||||
func NewTexture(c *opengl.Context, width, height int, filter opengl.Filter) (*Texture, error) {
|
||||
func NewTexture(c *opengl.Context, width, height int, filter opengl.FilterType) (*Texture, error) {
|
||||
w := internal.NextPowerOf2Int(width)
|
||||
h := internal.NextPowerOf2Int(height)
|
||||
if w < 4 {
|
||||
@ -70,7 +70,7 @@ func NewTexture(c *opengl.Context, width, height int, filter opengl.Filter) (*Te
|
||||
return &Texture{native, width, height}, nil
|
||||
}
|
||||
|
||||
func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filter) (*Texture, error) {
|
||||
func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.FilterType) (*Texture, error) {
|
||||
origSize := img.Bounds().Size()
|
||||
if origSize.X < 4 {
|
||||
return nil, errors.New("width must be equal or more than 4.")
|
||||
|
@ -20,19 +20,10 @@ import (
|
||||
"github.com/go-gl/gl"
|
||||
)
|
||||
|
||||
type Filter int
|
||||
|
||||
const (
|
||||
filterNearest Filter = gl.NEAREST
|
||||
filterLinear = gl.LINEAR
|
||||
)
|
||||
|
||||
type FilterType int
|
||||
type ShaderType int
|
||||
|
||||
const (
|
||||
shaderTypeVertex ShaderType = gl.VERTEX_SHADER
|
||||
shaderTypeFragment = gl.FRAGMENT_SHADER
|
||||
)
|
||||
type BufferType int
|
||||
type BufferUsageType int
|
||||
|
||||
type Texture gl.Texture
|
||||
|
||||
@ -127,18 +118,26 @@ func (u UniformLocation) Uniform1i(v int) {
|
||||
}
|
||||
|
||||
type Context struct {
|
||||
Nearest Filter
|
||||
Linear Filter
|
||||
VertexShader ShaderType
|
||||
FragmentShader ShaderType
|
||||
Nearest FilterType
|
||||
Linear FilterType
|
||||
VertexShader ShaderType
|
||||
FragmentShader ShaderType
|
||||
ArrayBuffer BufferType
|
||||
ElementArrayBuffer BufferType
|
||||
DynamicDraw BufferUsageType
|
||||
StaticDraw BufferUsageType
|
||||
}
|
||||
|
||||
func NewContext() *Context {
|
||||
c := &Context{
|
||||
Nearest: filterNearest,
|
||||
Linear: filterLinear,
|
||||
VertexShader: shaderTypeVertex,
|
||||
FragmentShader: shaderTypeFragment,
|
||||
Nearest: gl.NEAREST,
|
||||
Linear: gl.LINEAR,
|
||||
VertexShader: gl.VERTEX_SHADER,
|
||||
FragmentShader: gl.FRAGMENT_SHADER,
|
||||
ArrayBuffer: gl.ARRAY_BUFFER,
|
||||
ElementArrayBuffer: gl.ELEMENT_ARRAY_BUFFER,
|
||||
DynamicDraw: gl.DYNAMIC_DRAW,
|
||||
StaticDraw: gl.STATIC_DRAW,
|
||||
}
|
||||
c.init()
|
||||
return c
|
||||
@ -152,7 +151,7 @@ func (c *Context) init() {
|
||||
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
||||
}
|
||||
|
||||
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
|
||||
func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterType) (Texture, error) {
|
||||
t := gl.GenTexture()
|
||||
if t < 0 {
|
||||
return 0, errors.New("glGenTexture failed")
|
||||
@ -216,3 +215,8 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
||||
}
|
||||
return Program(p), nil
|
||||
}
|
||||
|
||||
func (c *Context) NewBuffer(bufferType BufferType, size int, ptr interface{}, bufferUsageType BufferUsageType) {
|
||||
gl.GenBuffer().Bind(gl.GLenum(bufferType))
|
||||
gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user