Exemplo n.º 1
0
// newRoadie is expected to be called once during engine initializes.
func newRoadie(ac audio.Audio, gc render.Renderer) *roadie {
	r := &roadie{}
	r.ld = data.NewLoader()
	r.gc = gc
	r.ac = ac
	return r
}
Exemplo n.º 2
0
// au demonstrates basic audio library (vu/audio/al) capabilities.
// It checks that OpenAL is installed and the bindings are working
// by loading and playing a sound.
func au() {
	al.Init() // map the bindings to the OpenAL dynamic library.

	// open the default device.
	if dev := al.OpenDevice(""); dev != nil {
		defer al.CloseDevice(dev)

		// create a context on the device.
		if ctx := al.CreateContext(dev, nil); ctx != nil {
			defer al.MakeContextCurrent(nil)
			defer al.DestroyContext(ctx)
			al.MakeContextCurrent(ctx)

			// create buffers and sources
			var buffer, source uint32
			al.GenBuffers(1, &buffer)
			al.GenSources(1, &source)

			// read in the audio data.
			sound := &data.Sound{}
			loader := data.NewLoader()
			loader.Load("bloop", &sound)
			if sound == nil {
				log.Printf("au: error loading audio file", "bloop")
				return
			}

			// copy the audio data into the buffer
			tag := &autag{}
			format := tag.audioFormat(sound)
			if format < 0 {
				log.Printf("au: error recognizing audio format")
				return
			}
			al.BufferData(buffer, int32(format), al.Pointer(&(sound.AudioData[0])), int32(sound.DataSize), int32(sound.Frequency))

			// attach the source to a buffer.
			al.Sourcei(source, al.BUFFER, int32(buffer))

			// check for any audio library errors that have happened up to this point.
			if err := al.GetError(); err != 0 {
				log.Printf("au: OpenAL error ", err)
				return
			}

			// Start playback and give enough time for the playback to finish.
			al.SourcePlay(source)
			time.Sleep(500 * time.Millisecond)
			return
		}
		log.Printf("au: error, failed to get a context")
	}
	log.Printf("au: error, failed to get a device")
}
Exemplo n.º 3
0
// initShader compiles shaders and links them into a shader program.
func (tag *trtag) initShader() {
	shader := &data.Shader{}
	loader := data.NewLoader()
	loader.Load("basic", &shader)
	tag.shaders = gl.CreateProgram()
	if err := gl.BindProgram(tag.shaders, shader.Vsh, shader.Fsh); err != nil {
		fmt.Printf("Failed to create program: %s\n", err)
	}
	tag.mvpRef = gl.GetUniformLocation(tag.shaders, "Mvpm")
	if tag.mvpRef < 0 {
		fmt.Printf("No model-view-projection matrix in vertex shader\n")
	}
}
Exemplo n.º 4
0
// initShader compiles shaders and links them into a shader program.
func (ld *ldtag) initShader() {
	shader := &data.Shader{}
	loader := data.NewLoader()
	loader.Load("monkey", &shader)
	ld.shaders = gl.CreateProgram()
	gl.BindAttribLocation(ld.shaders, 0, "inPosition")
	gl.BindAttribLocation(ld.shaders, 1, "inNormal")
	if err := gl.BindProgram(ld.shaders, shader.Vsh, shader.Fsh); err != nil {
		fmt.Printf("Failed to create program: %s\n", err)
	}
	ld.mvpref = gl.GetUniformLocation(ld.shaders, "modelViewProjectionMatrix")
	if ld.mvpref < 0 {
		fmt.Printf("No modelViewProjectionMatrix in vertex shader\n")
	}
}
Exemplo n.º 5
0
// initShader compiles shaders and links them into a shader program.
func (tb *tbtag) initShader() {
	shader := &data.Shader{}
	loader := data.NewLoader()
	loader.Load("tuv", &shader)
	tb.shaders = gl.CreateProgram()
	gl.BindAttribLocation(tb.shaders, 0, "vertexPosition")
	gl.BindAttribLocation(tb.shaders, 2, "uvPoint")
	if err := gl.BindProgram(tb.shaders, shader.Vsh, shader.Fsh); err != nil {
		fmt.Printf("Failed to create program: %s\n", err)
	}
	tb.mvpref = gl.GetUniformLocation(tb.shaders, "Mvpm")
	tb.sampler = gl.GetUniformLocation(tb.shaders, "uvSampler")
	if tb.mvpref < 0 {
		fmt.Printf("No model-view-projection matrix in vertex shader\n")
	}
}
Exemplo n.º 6
0
// initScene is called once on startup to load the 3D data.
func (ld *ldtag) initScene() {
	ld.persp = lin.NewM4()
	ld.mvp64 = lin.NewM4()
	ld.mvp32 = &render.M4{}
	gl.Init()
	mesh := &data.Mesh{}
	loader := data.NewLoader()
	loader.Load("monkey", &mesh)
	ld.faceCount = int32(len(mesh.F))

	// Gather the one scene into this one vertex array object.
	gl.GenVertexArrays(1, &ld.vao)
	gl.BindVertexArray(ld.vao)

	// vertex data.
	var vbuff uint32
	gl.GenBuffers(1, &vbuff)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbuff)
	gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.V)*4), gl.Pointer(&(mesh.V[0])), gl.STATIC_DRAW)
	var vattr uint32 = 0
	gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0)
	gl.EnableVertexAttribArray(vattr)

	// normal data.
	var nbuff uint32
	gl.GenBuffers(1, &nbuff)
	gl.BindBuffer(gl.ARRAY_BUFFER, nbuff)
	gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.N)*4), gl.Pointer(&(mesh.N[0])), gl.STATIC_DRAW)
	var nattr uint32 = 1
	gl.VertexAttribPointer(nattr, 3, gl.FLOAT, false, 0, 0)
	gl.EnableVertexAttribArray(nattr)

	// faces data, uint32 in this case, so 4 bytes per element.
	var ebuff uint32
	gl.GenBuffers(1, &ebuff)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff)
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(mesh.F)*2), gl.Pointer(&(mesh.F[0])), gl.STATIC_DRAW)

	ld.initShader()
	gl.ClearColor(0.2, 0.2, 0.2, 1.0)
	gl.Enable(gl.DEPTH_TEST)
	gl.Enable(gl.CULL_FACE)

	// set the initial perspetive matrix.
	ld.resize(0, 0, 800, 600)
}
Exemplo n.º 7
0
// initScene is one time initialization that creates a single VAO
func (sf *sftag) initScene() {
	sf.mvp32 = &render.M4{}
	sf.sTime = time.Now()
	sf.initData()

	// Bind the OpenGL calls and dump some version info.
	gl.Init()
	fmt.Printf("%s %s", gl.GetString(gl.RENDERER), gl.GetString(gl.VERSION))
	fmt.Printf(" GLSL %s\n", gl.GetString(gl.SHADING_LANGUAGE_VERSION))

	gl.GenVertexArrays(1, &sf.vao)
	gl.BindVertexArray(sf.vao)

	// vertex data.
	var vbuff uint32
	gl.GenBuffers(1, &vbuff)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbuff)
	gl.BufferData(gl.ARRAY_BUFFER, int64(len(sf.points)*4), gl.Pointer(&(sf.points[0])), gl.STATIC_DRAW)
	var vattr uint32 = 0
	gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0)
	gl.EnableVertexAttribArray(vattr)

	// faces data.
	var ebuff uint32
	gl.GenBuffers(1, &ebuff)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff)
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(sf.faces)), gl.Pointer(&(sf.faces[0])), gl.STATIC_DRAW)

	// create texture and shaders after all the data has been set up.
	shader := &data.Shader{}
	loader := data.NewLoader()
	loader.Load("fire", &shader)
	sf.shaders = gl.CreateProgram()
	if err := gl.BindProgram(sf.shaders, shader.Vsh, shader.Fsh); err != nil {
		fmt.Printf("Failed to create program: %s\n", err)
	}
	sf.mvpref = gl.GetUniformLocation(sf.shaders, "Mvpm")
	sf.gTime = gl.GetUniformLocation(sf.shaders, "time")
	sf.sizes = gl.GetUniformLocation(sf.shaders, "resolution")
	sf.ortho = lin.NewOrtho(0, 4, 0, 4, 0, 10)

	// set some state that doesn't need to change during drawing.
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
}
Exemplo n.º 8
0
// test that an audio resource can be loaded. Mimics the
// steps taken by the engine.
func TestAudio(t *testing.T) {
	a := &openal{}
	a.Init()
	sound := &data.Sound{}
	loader := data.NewLoader()
	loader.SetDir("../eg/audio", sound)
	loader.Load("bloop", &sound)
	err := a.BindSound(sound)
	if err != nil || sound.Buffer == 0 {
		t.Error("Failed to load audio resource")
	}

	// Don't play noises during normal testing, but if you're interested...
	// Need to "import time" and sleep a bit for the sound to happen.
	// 	n := NewSoundMaker(sound)
	// 	n.Play()
	// 	time.Sleep(500 * time.Millisecond)
	a.Shutdown()
}
Exemplo n.º 9
0
// initTexture loads the texture and binds it to the graphics device.
func (tb *tbtag) initTexture() {
	texture := &data.Texture{}
	loader := data.NewLoader()
	if loader.Load("image", &texture); texture != nil {
		tb.texture = texture
		gl.GenTextures(1, &texture.Tid)
		gl.BindTexture(gl.TEXTURE_2D, texture.Tid)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)

		// ensure image is in RGBA format
		b := texture.Img.Bounds()
		rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
		draw.Draw(rgba, rgba.Bounds(), texture.Img, b.Min, draw.Src)
		width, height := int32(b.Dx()), int32(b.Dy())
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&(rgba.Pix[0])))
		if glerr := gl.GetError(); glerr != gl.NO_ERROR {
			fmt.Printf("Failed binding texture image.png\n")
		}
	} else {
		fmt.Println("Could not load image.png file.")
	}

}