Exemplo n.º 1
0
func resolveData(idata interface{}) (iData, error) {
	var d iData
	switch data := idata.(type) {
	case []float32:
		d.typ = gl.FLOAT
		d.siz = 4
		d.length = len(data)
		d.ptr = gl.Ptr(data)
	case []uint8:
		d.typ = gl.UNSIGNED_BYTE
		d.siz = 1
		d.length = len(data)
		d.ptr = gl.Ptr(data)
	case []uint16:
		d.typ = gl.UNSIGNED_SHORT
		d.siz = 2
		d.length = len(data)
		d.ptr = gl.Ptr(data)
	case []uint32:
		d.typ = gl.UNSIGNED_INT
		d.siz = 4
		d.length = len(data)
		d.ptr = gl.Ptr(data)
	default:
		return iData{}, fmt.Errorf("Unusable data type for buffer")
	}
	return d, nil
}
Exemplo n.º 2
0
func (c *Context) UniformFloats(p Program, location string, v []float32) {
	l := int32(GetUniformLocation(c, p, location))
	switch len(v) {
	case 4:
		gl.Uniform4fv(l, 1, (*float32)(gl.Ptr(v)))
	case 16:
		gl.UniformMatrix4fv(l, 1, false, (*float32)(gl.Ptr(v)))
	default:
		panic("not reach")
	}
}
Exemplo n.º 3
0
func (c *Context) UniformFloats(p Program, location string, v []float32) {
	_ = c.runOnContextThread(func() error {
		l := int32(c.locationCache.GetUniformLocation(c, p, location))
		switch len(v) {
		case 4:
			gl.Uniform4fv(l, 1, (*float32)(gl.Ptr(v)))
		case 16:
			gl.UniformMatrix4fv(l, 1, false, (*float32)(gl.Ptr(v)))
		default:
			panic("not reach")
		}
		return nil
	})
}
Exemplo n.º 4
0
func (m *Mesh) addVertices(vertices []*Vertex, indices []int32, calcNormals bool) {
	if calcNormals {
		m.calcNormals(vertices, indices)
	}

	m.size = int32(len(indices))
	fb := verticesAsFloats(vertices)

	gl.BindBuffer(gl.ARRAY_BUFFER, m.vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(fb)*4, gl.Ptr(fb), gl.STATIC_DRAW)

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, m.ibo)
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indices)*4, gl.Ptr(indices), gl.STATIC_DRAW)
}
Exemplo n.º 5
0
Arquivo: gc.go Projeto: stanim/draw2d
func (p *Painter) Flush() {
	if len(p.vertices) != 0 {
		gl.EnableClientState(gl.COLOR_ARRAY)
		gl.EnableClientState(gl.VERTEX_ARRAY)
		gl.ColorPointer(4, gl.UNSIGNED_BYTE, 0, gl.Ptr(p.colors))
		gl.VertexPointer(2, gl.INT, 0, gl.Ptr(p.vertices))

		// draw lines
		gl.DrawArrays(gl.LINES, 0, int32(len(p.vertices)/2))
		gl.DisableClientState(gl.VERTEX_ARRAY)
		gl.DisableClientState(gl.COLOR_ARRAY)
		p.vertices = p.vertices[0:0]
		p.colors = p.colors[0:0]
	}
}
Exemplo n.º 6
0
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
	var shader Shader
	if err := c.runOnContextThread(func() error {
		s := gl.CreateShader(uint32(shaderType))
		if s == 0 {
			return fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
		}
		cSources, free := gl.Strs(source + "\x00")
		gl.ShaderSource(uint32(s), 1, cSources, nil)
		free()
		gl.CompileShader(s)

		var v int32
		gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
		if v == gl.FALSE {
			log := []uint8{}
			gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
			if v != 0 {
				log = make([]uint8, int(v))
				gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
			}
			return fmt.Errorf("opengl: shader compile failed: %s", log)
		}
		shader = Shader(s)
		return nil
	}); err != nil {
		return 0, err
	}
	return shader, nil
}
Exemplo n.º 7
0
// Shouldn't have tabs nor newlines
func (o *OpenGlStream) PrintSegment(s string) {
	if s == "" {
		return
	}

	if o.BackgroundColor != nil && o.BorderColor == nil {
		gl.PushAttrib(gl.CURRENT_BIT)
		gl.Color3dv((*float64)(&o.BackgroundColor[0]))
		gl.PushMatrix()
		gl.Translated(float64(o.pos[0]), float64(o.pos[1]), 0)
		for range s {
			gl.CallList(oFontBackground)
		}
		gl.PopMatrix()
		gl.PopAttrib()
	}

	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_LOD_BIAS, float32(lodBias*0.01))

	gl.Enable(gl.BLEND)
	defer gl.Disable(gl.BLEND)
	gl.Enable(gl.TEXTURE_2D)
	defer gl.Disable(gl.TEXTURE_2D)

	gl.PushMatrix()
	gl.Translated(float64(o.pos[0]), float64(o.pos[1]), 0)
	gl.ListBase(oFontBase + uint32(o.FontOptions)*96)
	gl.CallLists(int32(len(s)), gl.UNSIGNED_BYTE, gl.Ptr(&[]byte(s)[0]))
	gl.PopMatrix()

	//CheckGLError()
}
Exemplo n.º 8
0
// TexImage2D writes a 2D texture image.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glTexImage2D.xhtml
func TexImage2D(target Enum, level int, width, height int, format Enum, ty Enum, data []byte) {
	p := unsafe.Pointer(nil)
	if len(data) > 0 {
		p = gl.Ptr(&data[0])
	}
	gl.TexImage2D(uint32(target), int32(level), int32(format), int32(width), int32(height), 0, uint32(format), uint32(ty), p)
}
Exemplo n.º 9
0
func initResources() uint32 {
	vs := CreateShader("triangle.v.glsl", gl.VERTEX_SHADER)
	fs := CreateShader("triangle.f.glsl", gl.FRAGMENT_SHADER)

	var linkOk int32
	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &linkOk)
	if linkOk == 0 {
		log.Fatal("gl.LinkProgram")
	}

	gl.GenBuffers(1, &vboTriangle)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle)
	gl.BufferData(gl.ARRAY_BUFFER, floatSize*len(triangleAttributes), gl.Ptr(triangleAttributes), gl.STATIC_DRAW)

	attributeCoord2d = gl.GetAttribLocation(program, gl.Str("coord2d\x00"))
	if attributeCoord2d == -1 {
		log.Fatal("failed to bind attribute")
	}

	attributeVColor = gl.GetAttribLocation(program, gl.Str("v_color\x00"))
	if attributeVColor == -1 {
		log.Fatal("could not bind attribute v_color")
	}

	uniformFade = gl.GetUniformLocation(program, gl.Str("fade\x00"))
	if uniformFade == -1 {
		log.Fatal("could not bind uniform fade")
	}

	return program
}
Exemplo n.º 10
0
func newChipset(file string, tilesize int) chipset {
	imgFile, err := os.Open(file)
	if err != nil {
		log.Fatalf("texture %q not found on disk: %v\n", file, err)
	}
	img, _, err := image.Decode(imgFile)
	if err != nil {
		panic(err)
	}

	rgba := image.NewRGBA(img.Bounds())
	draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src)

	var texture uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0, gl.RGBA,
		int32(rgba.Rect.Size().X),
		int32(rgba.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix))

	return chipset{width: rgba.Bounds().Dx(), height: rgba.Bounds().Dy(), handle: texture, tilesize: tilesize}
}
Exemplo n.º 11
0
func initResources() (uint32, int32) {
	vs := CreateShader("triangle.v.glsl", gl.VERTEX_SHADER)
	fs := CreateShader("triangle.f.glsl", gl.FRAGMENT_SHADER)

	var linkOk int32
	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &linkOk)
	if linkOk == 0 {
		log.Fatal("gl.LinkProgram")
	}

	gl.GenBuffers(1, &vboTriangle)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle)
	gl.BufferData(gl.ARRAY_BUFFER, 4*len(triangleVertices), gl.Ptr(triangleVertices), gl.STATIC_DRAW)

	attribName := "coord2d\x00"
	attribCoord2d := gl.GetAttribLocation(program, gl.Str(attribName))
	if attribCoord2d == -1 {
		log.Fatal("failed to bind attribute")
	}
	return program, attribCoord2d
}
Exemplo n.º 12
0
func loadTexture(fileName string) (uint32, error) {
	imgFile, err := os.Open("./res/textures/" + fileName)
	if err != nil {
		return 0, err
	}

	imgCfg, _, err := image.DecodeConfig(imgFile)
	if err != nil {
		return 0, err
	}
	_, err = imgFile.Seek(0, 0)
	if err != nil {
		return 0, err
	}

	w, h := int32(imgCfg.Width), int32(imgCfg.Height)

	img, _, err := image.Decode(imgFile)
	if err != nil {
		return 0, err
	}

	buffer := make([]byte, w*h*4)
	index := 0
	for y := 0; y < int(h); y++ {
		for x := 0; x < int(w); x++ {
			pixel := img.At(x, y).(color.NRGBA)
			buffer[index] = pixel.R
			buffer[index+1] = pixel.G
			buffer[index+2] = pixel.B
			buffer[index+3] = pixel.A

			index += 4
		}
	}

	var texture uint32

	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA8,
		w,
		h,
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(buffer))

	return texture, nil
}
Exemplo n.º 13
0
func (v *Video) initGL() {
	if err := gl.Init(); err != nil {
		panic(err)
	}

	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	v.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef)
	posAttrib := uint32(gl.GetAttribLocation(v.prog, gl.Str("vPosition"+"\x00")))
	texCoordAttr := uint32(gl.GetAttribLocation(v.prog, gl.Str("vTexCoord"+"\x00")))
	v.textureUni = gl.GetAttribLocation(v.prog, gl.Str("texture"+"\x00"))

	var texture uint32
	gl.GenTextures(1, &texture)
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

	gl.UseProgram(v.prog)
	gl.EnableVertexAttribArray(posAttrib)
	gl.EnableVertexAttribArray(texCoordAttr)
	//posAttrib.EnableArray()
	//texCoordAttr.EnableArray()

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	verts := []float32{-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(verts)*int(unsafe.Sizeof(verts[0])), gl.Ptr(verts), gl.STATIC_DRAW)

	var textCoorBuf uint32
	gl.GenBuffers(1, &textCoorBuf)
	gl.BindBuffer(gl.ARRAY_BUFFER, textCoorBuf)
	texVerts := []float32{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}
	gl.BufferData(gl.ARRAY_BUFFER, len(texVerts)*int(unsafe.Sizeof(texVerts[0])), gl.Ptr(texVerts), gl.STATIC_DRAW)

	gl.VertexAttribPointer(posAttrib, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	gl.VertexAttribPointer(texCoordAttr, 2, gl.FLOAT, false, 0, gl.PtrOffset(0))
	//posAttrib.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
	//texCoordAttr.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
}
Exemplo n.º 14
0
// GetShaderInfoLog is a method you can call to get the compilation logs of a shader
func (c *Context) GetShaderInfoLog(shader *Shader) string {
	var maxLength int32
	gl.GetShaderiv(shader.uint32, gl.INFO_LOG_LENGTH, &maxLength)

	errorLog := make([]byte, maxLength)
	gl.GetShaderInfoLog(shader.uint32, maxLength, &maxLength, (*uint8)(gl.Ptr(errorLog)))

	return string(errorLog)
}
Exemplo n.º 15
0
func NewRenderable(verts []float32) *Renderable {
	r := &Renderable{
		VboPosition: G.Renderer.NewVboPosition(verts),
		Length:      len(verts),
	}

	gl.BindBuffer(gl.ARRAY_BUFFER, r.VboPosition.Vbo.Id)
	gl.BufferSubData(gl.ARRAY_BUFFER, int(r.VboPosition.Index*4), len(verts)*4, gl.Ptr(verts))
	return r
}
Exemplo n.º 16
0
Arquivo: vbo.go Projeto: fmd/gogol
func NewVbo(length int) *Vbo {
	v := &Vbo{
		Vertices: make([]float32, length),
	}

	gl.GenBuffers(1, &v.Id)
	gl.BindBuffer(gl.ARRAY_BUFFER, v.Id)
	gl.BufferData(gl.ARRAY_BUFFER, length*FloatSize, gl.Ptr(v.Vertices), gl.STATIC_DRAW)
	return v
}
Exemplo n.º 17
0
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
	gl.Flush()

	gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))

	pixels := make([]uint8, 4*width*height)
	gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
	if e := gl.GetError(); e != gl.NO_ERROR {
		return nil, errors.New(fmt.Sprintf("glReadPixels: %d", e))
	}
	return pixels, nil
}
Exemplo n.º 18
0
Arquivo: gg.go Projeto: dmac/gg
func (*backend) TexImage2D(
	target gg.Enum, level int, internalFormat gg.Enum,
	width, height, border int,
	format, typ gg.Enum,
	data interface{},
) {
	gl.TexImage2D(
		gl.TEXTURE_2D, int32(level), gl.RGBA,
		int32(width), int32(height), int32(border),
		uint32(format), uint32(typ),
		gl.Ptr(data),
	)
}
Exemplo n.º 19
0
func newMultitouchTestBoxWidget(pos mgl64.Vec2, color int) MultitouchTestBoxWidget {
	var buffer uint32
	gl.GenBuffers(1, &buffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, buffer)
	vertices := []float32{
		0, 0,
		0, 200,
		200, 200,
		200, 0,
	}
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	return MultitouchTestBoxWidget{pos: pos, color: color, buffer: buffer}
}
Exemplo n.º 20
0
func (t *Texture) load(path string) int {
	index := t.lru()
	delete(t.lookup, t.reverse[index])
	t.mark(index)
	t.lookup[path] = index
	t.reverse[index] = path
	x := int32((index % textureDim) * 256)
	y := int32((index / textureDim) * 256)
	im := copyImage(t.loadThumbnail(path))
	size := im.Rect.Size()
	gl.TexSubImage2D(
		gl.TEXTURE_2D, 0, x, y, int32(size.X), int32(size.Y),
		gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(im.Pix))
	return index
}
Exemplo n.º 21
0
func onDisplay(program uint32, coords uint32) {
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	gl.UseProgram(program)
	gl.EnableVertexAttribArray(coords)
	triangleVertices := []float32{
		0.0, 0.8,
		-0.8, -0.8,
		0.8, -0.8}
	gl.VertexAttribPointer(coords, 2, gl.FLOAT, false, 0, gl.Ptr(triangleVertices))
	gl.DrawArrays(gl.TRIANGLES, 0, 3)
	gl.DisableVertexAttribArray(coords)

}
Exemplo n.º 22
0
func newTexture(rgba image.RGBA) *uint32 {
	var texture1 uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture1)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rgba.Rect.Size().X),
		int32(rgba.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix))

	return &texture1

}
Exemplo n.º 23
0
func (atlas *FontAtlas) draw(rendered *image.RGBA, b Bounds) {
	var texture uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &texture)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rendered.Bounds().Dx()),
		int32(rendered.Bounds().Dy()),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rendered.Pix))

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)

	gl.Begin(gl.QUADS)
	{
		gl.TexCoord2f(0, 0)
		gl.Vertex2f(b.Min.X, b.Min.Y)
		gl.TexCoord2f(1, 0)
		gl.Vertex2f(b.Max.X, b.Min.Y)
		gl.TexCoord2f(1, 1)
		gl.Vertex2f(b.Max.X, b.Max.Y)
		gl.TexCoord2f(0, 1)
		gl.Vertex2f(b.Min.X, b.Max.Y)
	}
	gl.End()

	gl.Disable(gl.BLEND)

	gl.DeleteTextures(1, &texture)
	gl.Disable(gl.TEXTURE_2D)
}
Exemplo n.º 24
0
func NewGLImageFromImage(img image.Image) (*glImage, error) {
	var rgba *image.RGBA
	if asRGBA, ok := img.(*image.RGBA); ok {
		rgba = asRGBA
	} else {
		rgba = image.NewRGBA(img.Bounds())
		if rgba.Stride != rgba.Rect.Size().X*4 {
			return nil, errors.New("unsupported stride")
		}
		draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
	}

	var tex uint32
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &tex)
	gl.BindTexture(gl.TEXTURE_2D, tex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rgba.Bounds().Dx()),
		int32(rgba.Bounds().Dy()),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix),
	)

	return &glImage{
		tex,
		img.Bounds().Dx(),
		img.Bounds().Dy(),
		0.0,
		0.0,
		1.0,
		1.0,
	}, nil
}
Exemplo n.º 25
0
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
	var buffer Buffer
	_ = c.runOnContextThread(func() error {
		var b uint32
		gl.GenBuffers(1, &b)
		gl.BindBuffer(uint32(bufferType), b)
		switch v := v.(type) {
		case int:
			gl.BufferData(uint32(bufferType), v, nil, uint32(bufferUsage))
		case []uint16:
			// TODO: What about the endianness?
			gl.BufferData(uint32(bufferType), 2*len(v), gl.Ptr(v), uint32(bufferUsage))
		default:
			panic("not reach")
		}
		buffer = Buffer(b)
		return nil
	})
	return buffer
}
Exemplo n.º 26
0
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
	var b uint32
	gl.GenBuffers(1, &b)
	gl.BindBuffer(uint32(bufferType), b)
	size := 0
	ptr := v
	switch v := v.(type) {
	case int:
		size = v
		ptr = nil
	case []uint16:
		size = 2 * len(v)
	case []float32:
		size = 4 * len(v)
	default:
		panic("not reach")
	}
	gl.BufferData(uint32(bufferType), size, gl.Ptr(ptr), uint32(bufferUsage))
	return Buffer(b)
}
Exemplo n.º 27
0
func List(width, height int, list *draw.List) {
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.Enable(gl.SCISSOR_TEST)
	defer gl.Disable(gl.SCISSOR_TEST)

	gl.EnableClientState(gl.VERTEX_ARRAY)
	defer gl.DisableClientState(gl.VERTEX_ARRAY)

	gl.EnableClientState(gl.COLOR_ARRAY)
	defer gl.DisableClientState(gl.COLOR_ARRAY)

	gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)
	defer gl.DisableClientState(gl.TEXTURE_COORD_ARRAY)

	gl.VertexPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].P)))
	gl.TexCoordPointer(2, gl.FLOAT, vertexStride, unsafe.Pointer(&(list.Vertices[0].UV)))
	gl.ColorPointer(4, gl.UNSIGNED_BYTE, vertexStride, unsafe.Pointer(&(list.Vertices[0].Color)))

	offset := 0
	for _, cmd := range list.Commands {
		if cmd.Count == 0 {
			continue
		}
		if cmd.Texture == 0 {
			gl.Disable(gl.TEXTURE_2D)
		} else {
			gl.Enable(gl.TEXTURE_2D)
			gl.BindTexture(gl.TEXTURE_2D, uint32(cmd.Texture))
		}

		x, y, w, h := cmd.Clip.AsInt32()
		gl.Scissor(x, int32(height)-y-h, w, h)
		gl.DrawElements(gl.TRIANGLES, int32(cmd.Count), indexType, gl.Ptr(list.Indicies[offset:]))
		offset += int(cmd.Count)
	}
}
Exemplo n.º 28
0
func NewTexture(img *image.RGBA, opts ...TextureOption) (*Texture, error) {
	if img.Stride != img.Rect.Size().X*4 {
		return nil, fmt.Errorf("unsupported stride in texture image")
	}
	opt := textureOption{
		filterMin: LINEAR,
		filterMag: LINEAR,
		wrap_s:    REPEAT,
		wrap_t:    REPEAT,
	}
	for _, o := range opts {
		o(&opt)
	}

	var id uint32
	gl.GenTextures(1, &id)
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, id)
	defer gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(opt.filterMin))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(opt.filterMag))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, int32(opt.wrap_s))
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, int32(opt.wrap_t))
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(img.Rect.Size().X),
		int32(img.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(img.Pix))
	return &Texture{
		id:   id,
		size: img.Rect.Size(),
	}, nil
}
Exemplo n.º 29
0
func (atlas *FontAtlas) upload() {
	if !atlas.Dirty {
		return
	}
	atlas.Dirty = false

	gl.Enable(gl.TEXTURE_2D)

	if atlas.Texture != 0 {
		gl.DeleteTextures(1, &atlas.Texture)
		atlas.Texture = 0
	}

	gl.GenTextures(1, &atlas.Texture)
	gl.BindTexture(gl.TEXTURE_2D, atlas.Texture)

	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)

	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(atlas.Image.Rect.Size().X),
		int32(atlas.Image.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(atlas.Image.Pix))

	if err := gl.GetError(); err != 0 {
		log.Println(err)
	}

	gl.Disable(gl.TEXTURE_2D)
}
Exemplo n.º 30
0
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
	s := gl.CreateShader(uint32(shaderType))
	if s == 0 {
		return 0, errors.New("glCreateShader failed")
	}

	glSource := gl.Str(source + "\x00")
	gl.ShaderSource(uint32(s), 1, &glSource, nil)
	gl.CompileShader(s)

	var v int32
	gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
	if v == gl.FALSE {
		log := []uint8{}
		gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
		if v != 0 {
			log = make([]uint8, int(v))
			gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
		}
		return 0, errors.New(fmt.Sprintf("shader compile failed: %s", string(log)))
	}
	return Shader(s), nil
}