Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
Archivo: gg.go Proyecto: dmac/gg
func (*backend) GetAttribLocation(p *gg.Program, name string) (*gg.Attribute, error) {
	a := gl.GetAttribLocation(p.Value.(uint32), gl.Str(name+"\x00"))
	if a < 0 {
		return nil, fmt.Errorf("gg: no attribute named " + name)
	}
	return &gg.Attribute{Value: uint32(a)}, nil
}
Ejemplo n.º 3
0
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation {
	attrib := attribLocation(gl.GetAttribLocation(uint32(p), gl.Str(location+"\x00")))
	if attrib == -1 {
		panic("opengl: invalid attrib location: " + location)
	}
	return attrib
}
Ejemplo n.º 4
0
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation {
	uniform := uniformLocation(gl.GetUniformLocation(uint32(p), gl.Str(location+"\x00")))
	if uniform == -1 {
		panic("opengl: invalid uniform location: " + location)
	}
	return uniform
}
Ejemplo n.º 5
0
Archivo: gg.go Proyecto: dmac/gg
func (*backend) CreateShader(src []byte, typ gg.Enum) (*gg.Shader, error) {
	csrc := gl.Str(string(append([]byte(src), 0)))
	shader := gl.CreateShader(uint32(typ))
	gl.ShaderSource(shader, 1, &csrc, nil)
	gl.CompileShader(shader)
	var status int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
	if status == gl.TRUE {
		return &gg.Shader{Value: shader}, nil
	}
	var logLength int32
	gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
	log := strings.Repeat("\x00", int(logLength+1))
	gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
	return nil, fmt.Errorf("compile shader: %s%s", src, log)
}
Ejemplo n.º 6
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
}
Ejemplo n.º 7
0
func (c *Context) GetUniformLocation(p Program, location string) UniformLocation {
	u := UniformLocation(gl.GetUniformLocation(uint32(p), gl.Str(location+"\x00")))
	if u == -1 {
		panic("invalid uniform location: " + location)
	}
	return u
}
Ejemplo n.º 8
0
func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
	a := AttribLocation(gl.GetAttribLocation(uint32(p), gl.Str(location+"\x00")))
	if a == -1 {
		panic("invalid attrib location: " + location)
	}
	return a
}
Ejemplo n.º 9
0
Archivo: gg.go Proyecto: dmac/gg
func (*backend) GetUniformLocation(p *gg.Program, name string) (*gg.Uniform, error) {
	u := gl.GetUniformLocation(p.Value.(uint32), gl.Str(name+"\x00"))
	if u < 0 {
		return nil, fmt.Errorf("gg: no uniform named " + name)
	}
	return &gg.Uniform{Value: u}, nil
}
Ejemplo n.º 10
0
func (program *Program) Attrib(attrname string) Attrib {
	location := gl.GetAttribLocation(program.id, gl.Str(attrname+"\x00"))
	return Attrib{
		name:     attrname,
		location: location,
	}
}
Ejemplo n.º 11
0
func (s *Shader) getShaderInfoLog(shader uint32, context string) error {
	var logLength int32
	gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

	log := strings.Repeat("\x00", int(logLength+1))
	gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
	return fmt.Errorf("%s: %s", context, log)
}
Ejemplo n.º 12
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))
}
Ejemplo n.º 13
0
// GetActiveUniform returns details about an active uniform variable.
// A value of 0 for index selects the first active uniform variable.
// Permissible values for index range from 0 to the number of active
// uniform variables minus 1.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetActiveUniform.xhtml
func GetActiveUniform(p Program, index uint32) (name string, size int, ty Enum) {
	var length, si int32
	var typ uint32
	name = strings.Repeat("\x00", 256)
	cname := gl.Str(name)
	gl.GetActiveUniform(p.Value, uint32(index), int32(len(name)-1), &length, &si, &typ, cname)
	name = name[:strings.IndexRune(name, 0)]
	return name, int(si), Enum(typ)
}
Ejemplo n.º 14
0
// PrintLog prints the error log for an object
func PrintLog(object uint32) {
	var logLength int32
	if gl.IsShader(object) {
		gl.GetShaderiv(object, gl.INFO_LOG_LENGTH, &logLength)
	} else if gl.IsProgram(object) {
		gl.GetProgramiv(object, gl.INFO_LOG_LENGTH, &logLength)
	} else {
		log.Fatal("PrintLog: not a shader or program")
	}

	infoLog := strings.Repeat("\x00", int(logLength+1))
	if gl.IsShader(object) {
		gl.GetShaderInfoLog(object, logLength, nil, gl.Str(infoLog))
	} else if gl.IsProgram(object) {
		gl.GetProgramInfoLog(object, logLength, nil, gl.Str(infoLog))
	}
	log.Fatal(infoLog)
}
Ejemplo n.º 15
0
func (s *Shader) addUniform(uniformName string) error {
	uniformLocation := gl.GetUniformLocation(s.program, gl.Str(uniformName+"\000"))

	if uniformLocation == -1 {
		return fmt.Errorf("could not find uniform: %s", uniformName)
	}

	s.uniforms[uniformName] = uniformLocation
	return nil
}
Ejemplo n.º 16
0
func initResources() (uint32, int32) {
	vSrc0 := fixupSrc(vShaderSrc)
	vSrc := gl.Str(vSrc0)
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(vs, 1, &vSrc, nil)
	gl.CompileShader(vs)
	var vok int32
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &vok)
	if vok == 0 {
		log.Fatal("error in vertex shader")

	}

	fSrc := gl.Str(fShaderSrc)
	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	gl.ShaderSource(fs, 1, &fSrc, nil)
	gl.CompileShader(fs)
	var fok int32
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &fok)
	if fok == 0 {
		log.Fatal("error in 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")
	}

	attribName := fixupSrc("coord2d")
	attribCoord2d := gl.GetAttribLocation(program, gl.Str(attribName))
	if attribCoord2d == -1 {
		log.Fatal("failed to bind attribute")
	}
	return program, attribCoord2d
}
Ejemplo n.º 17
0
Archivo: gg.go Proyecto: dmac/gg
func (*backend) LinkProgram(p *gg.Program) error {
	pv := p.Value.(uint32)
	gl.LinkProgram(pv)
	var status int32
	gl.GetProgramiv(pv, gl.LINK_STATUS, &status)
	if status == gl.TRUE {
		return nil
	}
	var logLength int32
	gl.GetProgramiv(pv, gl.INFO_LOG_LENGTH, &logLength)
	log := strings.Repeat("\x00", int(logLength+1))
	gl.GetProgramInfoLog(pv, logLength, nil, gl.Str(log))
	return fmt.Errorf("link program: %s", log)
}
Ejemplo n.º 18
0
func (s *Shader) addProgram(text string, typ uint32) error {
	shader := gl.CreateShader(typ)
	if shader == 0 {
		return errors.New("could not find valid memory location when adding shader")
	}
	cStr := gl.Str(text + "\000")
	gl.ShaderSource(shader, 1, &cStr, nil)
	gl.CompileShader(shader)

	var result int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &result)
	if result == 0 {
		return s.getShaderInfoLog(shader, "get shader error")
	}

	gl.AttachShader(s.program, shader)
	return nil
}
Ejemplo n.º 19
0
func loadShader(shaderType uint32, source string) uint32 {
	shader := gl.CreateShader(shaderType)
	if err := gl.GetError(); err != gl.NO_ERROR {
		panic(fmt.Errorf("gl error: %v", err))
	}

	csource := gl.Str(source)
	gl.ShaderSource(shader, 1, &csource, nil)
	gl.CompileShader(shader)

	var status int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
	if status != gl.TRUE {
		//log := shader.GetInfoLog()
		panic(fmt.Errorf("Failed to compile shader:  shader: %v", source))
	}

	return shader
}
Ejemplo n.º 20
0
// CreateShader creates a shader object from a source file.
func CreateShader(filename string, xtype uint32) uint32 {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatal(err)
	}

	dataString := "#version 120\n" + string(data) + "\x00"
	dataSrc := gl.Str(dataString)

	res := gl.CreateShader(xtype)
	gl.ShaderSource(res, 1, &dataSrc, nil)
	gl.CompileShader(res)

	var compileOk int32
	gl.GetShaderiv(res, gl.COMPILE_STATUS, &compileOk)
	if compileOk == gl.FALSE {
		PrintLog(res)
		gl.DeleteShader(res)
	}
	return res
}
Ejemplo n.º 21
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
}
Ejemplo n.º 22
0
// GetAttribLocation returns the location of an attribute variable.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetAttribLocation.xhtml
func GetAttribLocation(p Program, name string) Attrib {
	return Attrib{Value: uint(gl.GetAttribLocation(p.Value, gl.Str(name+"\x00")))}
}
Ejemplo n.º 23
0
// ShaderSource sets the source code of s to the given source code.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glShaderSource.xhtml
func ShaderSource(s Shader, src string) {
	glsource := gl.Str(src + "\x00")
	gl.ShaderSource(s.Value, 1, &glsource, nil)
}
Ejemplo n.º 24
0
func (c *Context) GetUniformLocation(program *Program, name string) *UniformLocation {
	return &UniformLocation{gl.GetUniformLocation(program.uint32, gl.Str(name+"\x00"))}
}
Ejemplo n.º 25
0
// BindAttribLocation binds a vertex attribute index with a named
// variable.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glBindAttribLocation.xhtml
func BindAttribLocation(p Program, a Attrib, name string) {
	gl.BindAttribLocation(p.Value, uint32(a.Value), gl.Str(name+"\x00"))
}
Ejemplo n.º 26
0
// Str takes a null-terminated Go string and returns its GL-compatible address.
// This function reaches into Go string storage in an unsafe way so the caller
// must ensure the string is not garbage collected.
func (c *Context) Str(str string) *uint8 {
	return gl.Str(str)
}
Ejemplo n.º 27
0
// GetUniformLocation returns the location of a uniform variable.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetUniformLocation.xhtml
func GetUniformLocation(p Program, name string) Uniform {
	return Uniform{Value: gl.GetUniformLocation(p.Value, gl.Str(name+"\x00"))}
}
Ejemplo n.º 28
0
func (c *Context) GetAttribLocation(program *Program, name string) int {
	return int(gl.GetAttribLocation(program.uint32, gl.Str(name+"\x00")))
}