Exemplo n.º 1
0
func (m Mesh) draw() {
	gl.EnableVertexAttribArray(0)
	gl.EnableVertexAttribArray(1)
	gl.EnableVertexAttribArray(2)

	if m.vbo == 0 {
		panic("attempt to set array buffer with VBO=0")
	}

	gl.BindBuffer(gl.ARRAY_BUFFER, m.vbo)
	gl.VertexAttribPointer(0, 3, gl.FLOAT, false, VertexSize*4, gl.PtrOffset(0))
	gl.VertexAttribPointer(1, 2, gl.FLOAT, false, VertexSize*4, gl.PtrOffset(12))
	gl.VertexAttribPointer(2, 3, gl.FLOAT, false, VertexSize*4, gl.PtrOffset(20))

	if m.vbo == 0 {
		panic("attempt to set element array buffer with VBO=0")
	}

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, m.ibo)

	if m.size == 0 {
		panic("attempt to draw elements with mesh size = 0")
	}

	gl.DrawElements(gl.TRIANGLES, m.size, gl.UNSIGNED_INT, gl.PtrOffset(0))

	gl.DisableVertexAttribArray(0)
	gl.DisableVertexAttribArray(1)
	gl.DisableVertexAttribArray(2)
}
Exemplo n.º 2
0
func (r *Renderer) Render() {
	gl.EnableClientState(gl.VERTEX_ARRAY)

	rVbo := r.Vbos[0]
	gl.BindBuffer(gl.ARRAY_BUFFER, rVbo.Id)

	for _, c := range r.Layer.Flatten() {
		renderable := c.GetRenderable()
		transform := c.GetTransform()

		if renderable == nil || transform == nil || !c.Visible() {
			continue
		}

		if rVbo != renderable.VboPosition.Vbo {
			rVbo = renderable.VboPosition.Vbo
			gl.BindBuffer(gl.ARRAY_BUFFER, rVbo.Id)
		}

		gl.VertexPointer(ValsInVertex, gl.FLOAT, 0, nil)

		if transform.NeedsUpdate {
			transform.Update()
		}

		gl.LoadMatrixf(&(transform.Matrix[0]))
		gl.DrawArrays(gl.QUADS, int32(renderable.VboPosition.Index/ValsInVertex),
			int32(renderable.Length/ValsInVertex))
	}

	gl.DisableClientState(gl.VERTEX_ARRAY)
}
Exemplo n.º 3
0
func (c *Context) BindBuffer(target int, buffer *Buffer) {
	if buffer == nil {
		gl.BindBuffer(uint32(target), 0)
		return
	}
	gl.BindBuffer(uint32(target), buffer.uint32)
}
Exemplo n.º 4
0
func (buffer *Buffer) Update(offset int, idata interface{}) {
	data, err := resolveData(idata)
	if err != nil {
		panic(err)
	}
	if data.typ != buffer.data.typ {
		panic(fmt.Errorf("buffer data type mismatch: %04X and %04X", buffer.data.typ, data.typ))
	}
	gl.BindBuffer(buffer.bindpoint, buffer.id)
	defer gl.BindBuffer(buffer.bindpoint, 0)
	gl.BufferSubData(buffer.bindpoint, offset*data.siz, data.length*data.siz, data.ptr)
}
Exemplo n.º 5
0
func (buffer *Buffer) Upload(idata interface{}) {
	data, err := resolveData(idata)
	if err != nil {
		panic(err)
	}
	if data.typ != buffer.data.typ {
		panic(fmt.Errorf("buffer data type mismatch: %04X and %04X", buffer.data.typ, data.typ))
	}
	gl.BindBuffer(buffer.bindpoint, buffer.id)
	defer gl.BindBuffer(buffer.bindpoint, 0)
	gl.BufferData(buffer.bindpoint, data.siz*data.length, data.ptr, buffer.usage)
}
Exemplo n.º 6
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.º 7
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.º 8
0
func (w *MultitouchTestBoxWidget) Render() {
	colors := [...]mgl64.Vec3{
		{0 / 255.0, 140 / 255.0, 0 / 255.0},
		{0 / 255.0, 98 / 255.0, 140 / 255.0},
		{194 / 255.0, 74 / 255.0, 0 / 255.0},
		{89 / 255.0, 0 / 255.0, 140 / 255.0},
		{191 / 255.0, 150 / 255.0, 0 / 255.0},
		{140 / 255.0, 0 / 255.0, 0 / 255.0},
	}

	backgroundColor := colors[w.color]

	//borderColor := backgroundColor

	switch 1 {
	case 0:
		DrawBorderlessBox(w.pos, mgl64.Vec2{200, 200}, backgroundColor)
	case 1:
		gl.PushMatrix()
		gl.Translated(w.pos[0], w.pos[1], 0)
		gl.Color3dv(&backgroundColor[0])

		gl.EnableClientState(gl.VERTEX_ARRAY)
		gl.BindBuffer(gl.ARRAY_BUFFER, w.buffer)
		gl.VertexPointer(2, gl.FLOAT, 0, nil)

		gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)

		gl.PopMatrix()
	}
}
Exemplo n.º 9
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.º 10
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.º 11
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.º 12
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.º 13
0
func (r *Renderer) Render() {
	gl.EnableClientState(gl.VERTEX_ARRAY)

	for _, g := range r.GameObjects {
		gl.BindBuffer(gl.ARRAY_BUFFER, g.VboPosition.Vbo.Id)
		gl.VertexPointer(2, gl.FLOAT, 0, nil)
		gl.DrawArrays(gl.QUADS, 0, int32(4))
	}

	gl.DisableClientState(gl.VERTEX_ARRAY)
}
Exemplo n.º 14
0
Arquivo: vao.go Projeto: PieterD/crap
func (vao *VAO) Enable(elements int, buffer *Buffer, attrib Attrib, opts ...VAOOption) {
	opt := vaoOption{
		stride:    0,
		offset:    0,
		normalize: false,
	}
	for _, o := range opts {
		o(&opt)
	}
	gl.BindVertexArray(vao.id)
	defer gl.BindVertexArray(0)
	gl.BindBuffer(gl.ARRAY_BUFFER, buffer.id)
	defer gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	gl.EnableVertexAttribArray(attrib.Location())
	gl.VertexAttribPointer(
		attrib.Location(),
		int32(elements),
		buffer.data.typ,
		opt.normalize,
		int32(opt.stride*buffer.data.siz),
		gl.PtrOffset(opt.offset*buffer.data.siz))
}
Exemplo n.º 15
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.º 16
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.º 17
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.º 18
0
func onDisplay(program uint32) {
	coords := uint32(attributeCoord2d)
	vcolor := uint32(attributeVColor)

	gl.ClearColor(1.0, 1.0, 1.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	gl.UseProgram(program)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Uniform1f(uniformFade, curFade)

	gl.EnableVertexAttribArray(coords)
	gl.EnableVertexAttribArray(vcolor)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle)
	gl.VertexAttribPointer(coords, 2, gl.FLOAT, false, 5*floatSize, nil)
	gl.VertexAttribPointer(vcolor, 3, gl.FLOAT, false, 5*floatSize, gl.PtrOffset(2*floatSize))

	gl.DrawArrays(gl.TRIANGLES, 0, 3)

	gl.DisableVertexAttribArray(vcolor)
	gl.DisableVertexAttribArray(coords)
}
Exemplo n.º 19
0
func CreateQuad(opts QuadOpts) *GameObject {
	halfWidth := opts.Width / 2
	halfHeight := opts.Height / 2

	verts := []float32{
		-halfWidth, -halfHeight,
		-halfWidth, halfHeight,
		halfWidth, halfHeight,
		halfWidth, -halfHeight,
	}

	g := &GameObject{
		Transform:   NewTransform(nil),
		VboPosition: G.Renderer.GetNewVboPosition(verts),
		Vertices:    verts,
	}

	gl.BindBuffer(gl.ARRAY_BUFFER, g.VboPosition.Vbo.Id)
	gl.BufferSubData(gl.ARRAY_BUFFER, int(g.VboPosition.Index*4), len(g.Vertices)*4, gl.Ptr(g.Vertices))

	G.Renderer.GameObjects = append(G.Renderer.GameObjects, g)
	return g
}
Exemplo n.º 20
0
// BindBuffer binds a buffer.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBindBuffer.xhtml
func BindBuffer(target Enum, b Buffer) {
	gl.BindBuffer(uint32(target), b.Value)
}
Exemplo n.º 21
0
func (c *Context) BindElementArrayBuffer(b Buffer) {
	_ = c.runOnContextThread(func() error {
		gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
		return nil
	})
}
Exemplo n.º 22
0
Arquivo: gg.go Projeto: dmac/gg
func (*backend) BindBuffer(typ gg.Enum, b *gg.Buffer) {
	gl.BindBuffer(uint32(typ), b.Value.(uint32))
}
Exemplo n.º 23
0
func (buffer *Buffer) Use() {
	gl.BindBuffer(buffer.bindpoint, buffer.id)
}
Exemplo n.º 24
0
func (c *Context) BindElementArrayBuffer(b Buffer) {
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
}