Esempio n. 1
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)
}
Esempio n. 2
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
}
Esempio n. 3
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
}
Esempio n. 4
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))
}
Esempio n. 5
0
File: vbo.go Progetto: 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
}
Esempio n. 6
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
}
Esempio n. 7
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)
}
Esempio n. 8
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}
}
Esempio n. 9
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)
}
Esempio n. 10
0
// BufferInit creates a new unitialized data store for the bound buffer object.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBufferData.xhtml
func BufferInit(target Enum, size int, usage Enum) {
	gl.BufferData(uint32(target), size, nil, uint32(usage))
}
Esempio n. 11
0
// BufferData creates a new data store for the bound buffer object.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBufferData.xhtml
func BufferData(target Enum, size int, src unsafe.Pointer, usage uint32) {
	gl.BufferData(uint32(target), size, src, usage)
}
Esempio n. 12
0
// BufferData creates a new data store for the bound buffer object.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBufferData.xhtml
func BufferData(target Enum, src []byte, usage Enum) {
	gl.BufferData(uint32(target), int(len(src)), gl.Ptr(&src[0]), uint32(usage))
}
Esempio n. 13
0
func (c *Context) BufferData(target int, data interface{}, usage int) {
	s := uintptr(reflect.ValueOf(data).Len()) * reflect.TypeOf(data).Elem().Size()
	gl.BufferData(uint32(target), int(s), gl.Ptr(data), uint32(usage))
}
Esempio n. 14
0
File: gg.go Progetto: dmac/gg
func (*backend) BufferData(typ gg.Enum, src []byte, usage gg.Enum) {
	gl.BufferData(uint32(typ), len(src), gl.Ptr(src), uint32(usage))
}