Пример #1
0
func (s *Shader) initShader(t gl.Enum, str string, shader *gl.Uint) {
	*shader = gl.CreateShader(t)
	if *shader == 0 {
		fmt.Println("error creating shader of type ", t)
	}

	src := gl.GLStringArray(str)
	defer gl.GLStringArrayFree(src)

	gl.ShaderSource(*shader, 1, &src[0], nil)
	gl.CompileShader(*shader)

	var (
		status      gl.Int
		info_length gl.Int
		message     *gl.Char
	)

	gl.GetShaderiv(*shader, gl.COMPILE_STATUS, &status)
	if status == gl.FALSE {
		fmt.Println("Error compiling shader")
		gl.GetShaderiv(*shader, gl.INFO_LOG_LENGTH, &info_length)
		message = gl.GLStringAlloc(gl.Sizei(info_length))
		gl.GetShaderInfoLog(*shader, gl.Sizei(info_length), nil, message)
		fmt.Println(gl.GoString(message))
		gl.GLStringFree(message)
	}

}
Пример #2
0
func (s *Shader) initWithString(vert_shader string, frag_shader string) {

	s.initShader(gl.VERTEX_SHADER, vert_shader, &s.vert_shader)
	s.initShader(gl.FRAGMENT_SHADER, frag_shader, &s.frag_shader)

	s.program = gl.CreateProgram()

	gl.AttachShader(s.program, s.vert_shader)
	gl.AttachShader(s.program, s.frag_shader)
	gl.LinkProgram(s.program)

	var (
		status      gl.Int
		info_length gl.Int
		message     *gl.Char
	)

	gl.GetProgramiv(s.program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		fmt.Println("Error linking program")
		gl.GetProgramiv(s.program, gl.INFO_LOG_LENGTH, &info_length)
		message = gl.GLStringAlloc(gl.Sizei(info_length))
		gl.GetProgramInfoLog(s.program, gl.Sizei(info_length), nil, message)
		fmt.Println(gl.GoString(message))
		gl.GLStringFree(message)
	}

	s.initAttributes()
	s.initUniforms()

}
Пример #3
0
func (s *Shader) initUniform(name string, uni *gl.Int) {
	uni_name := gl.GLString(name)
	defer gl.GLStringFree(uni_name)
	*uni = gl.GetUniformLocation(s.program, uni_name)
	if *uni == -1 {
		fmt.Println("Error in getting uniform", gl.GoString(uni_name))
	}
}
Пример #4
0
func (s *Shader) initAttribute(name string, att *gl.Uint) {
	att_name := gl.GLString(name)
	defer gl.GLStringFree(att_name)
	att_tmp := gl.GetAttribLocation(s.program, att_name)
	if att_tmp == -1 {
		fmt.Println("Error in getting attribute ", gl.GoString(att_name))
	} else {
		*att = gl.Uint(att_tmp)
	}
}
Пример #5
0
func checkShaderCompiled(id gl.Uint, shaderType ShaderType) error {

	var shader string
	if shaderType == Vertex {
		shader = "vertex"
	} else {
		shader = "fragment"
	}
	var status gl.Int
	gl.GetShaderiv(id, gl.COMPILE_STATUS, &status)
	if status == gl.FALSE {
		var infoLogLength gl.Int
		gl.GetShaderiv(id, gl.INFO_LOG_LENGTH, &infoLogLength)

		infoLog := make([]gl.Char, infoLogLength+1)
		gl.GetShaderInfoLog(id, gl.Sizei(infoLogLength), nil, &infoLog[0])

		return fmt.Errorf("shader %s: %s", shader, gl.GoString(&infoLog[0]))
	}

	return nil
}
Пример #6
0
func InitShaders() {
	render.Queue(func() {
		vertex_shaders = make(map[string]gl.Uint)
		fragment_shaders = make(map[string]gl.Uint)
		shader_progs = make(map[string]gl.Uint)
		warned_names = make(map[string]bool)
		RemoveRegistry("shaders")
		RegisterRegistry("shaders", make(map[string]*shaderDef))
		RegisterAllObjectsInDir("shaders", filepath.Join(GetDataDir(), "shaders"), ".json", "json")
		names := GetAllNamesInRegistry("shaders")
		for _, name := range names {
			// Load the shader files
			shader := Shader{Defname: name}
			GetObject("shaders", &shader)
			vdata, err := ioutil.ReadFile(filepath.Join(GetDataDir(), shader.Vertex_path))
			if err != nil {
				Error().Printf("Unable to load vertex shader '%s': %v", shader.Vertex_path, err)
				continue
			}
			fdata, err := ioutil.ReadFile(filepath.Join(GetDataDir(), shader.Fragment_path))
			if err != nil {
				Error().Printf("Unable to load fragment shader '%s': %v", shader.Fragment_path, err)
				continue
			}

			// Create the vertex shader
			vertex_id, ok := vertex_shaders[shader.Vertex_path]
			if !ok {
				vertex_id = gl.CreateShader(gl.VERTEX_SHADER)
				pointer := &vdata[0]
				length := gl.Int(len(vdata))
				gl.ShaderSource(vertex_id, 1, (**gl.Char)(unsafe.Pointer(&pointer)), &length)
				gl.CompileShader(vertex_id)
				var param gl.Int
				gl.GetShaderiv(vertex_id, gl.COMPILE_STATUS, &param)
				if param == 0 {
					Error().Printf("Failed to compile vertex shader '%s': %v", shader.Vertex_path, param)
					continue
				}
			}

			// Create the fragment shader
			fragment_id, ok := fragment_shaders[shader.Fragment_path]
			if !ok {
				fragment_id = gl.CreateShader(gl.FRAGMENT_SHADER)
				pointer := &fdata[0]
				length := gl.Int(len(fdata))
				gl.ShaderSource(fragment_id, 1, (**gl.Char)(unsafe.Pointer(&pointer)), &length)
				gl.CompileShader(fragment_id)
				var param gl.Int
				gl.GetShaderiv(fragment_id, gl.COMPILE_STATUS, &param)
				if param == 0 {
					errBuf := make([]gl.Char, 500)
					var errLength gl.Sizei
					gl.GetShaderInfoLog(fragment_id, gl.Sizei(len(errBuf)), &errLength, &errBuf[0])
					Error().Printf("Failed to compile fragment shader '%s': %s", shader.Fragment_path, gl.GoString(&errBuf[0]))
					continue
				}
			}

			// shader successfully compiled - now link
			program_id := gl.CreateProgram()
			gl.AttachShader(program_id, vertex_id)
			gl.AttachShader(program_id, fragment_id)
			gl.LinkProgram(program_id)
			var param gl.Int
			gl.GetProgramiv(program_id, gl.LINK_STATUS, &param)
			if param == 0 {
				Error().Printf("Failed to link shader '%s': %v", shader.Name, param)
				continue
			}

			vertex_shaders[shader.Vertex_path] = vertex_id
			fragment_shaders[shader.Fragment_path] = fragment_id
			shader_progs[shader.Name] = program_id
		}
	})
}