Exemplo n.º 1
0
func (renderState *renderState) init(window mandala.Window) {
	window.MakeContextCurrent()

	renderState.window = window
	width, height := window.GetSize()

	// Set the viewport
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	renderState.boxProgram = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
	renderState.segmentProgram = shaders.NewProgram(shapes.DefaultSegmentFS, shapes.DefaultSegmentVS)
}
Exemplo n.º 2
0
func (renderState *renderState) init(window mandala.Window) {
	window.MakeContextCurrent()

	renderState.window = window
	width, height := window.GetSize()

	// Set the viewport
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	check()

	// Compile the shaders
	program := shaders.NewProgram(fsh, vsh)
	program.Use()
	check()

	// Get attributes
	attrPos = program.GetAttribute("pos")
	attrTexIn = program.GetAttribute("texIn")
	unifTexture = program.GetUniform("texture")
	gl.EnableVertexAttribArray(attrPos)
	gl.EnableVertexAttribArray(attrTexIn)
	check()

	// Upload texture data
	img, err := loadImage(GOPHER_PNG)
	if err != nil {
		panic(err)
	}

	// Prepare the image to be placed on a texture.
	bounds := img.Bounds()
	imgWidth, imgHeight := bounds.Size().X, bounds.Size().Y
	buffer := make([]byte, imgWidth*imgHeight*4)
	index := 0
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			buffer[index] = byte(r)
			buffer[index+1] = byte(g)
			buffer[index+2] = byte(b)
			buffer[index+3] = byte(a)
			index += 4
		}
	}

	gl.GenTextures(1, &textureBuffer)
	gl.BindTexture(gl.TEXTURE_2D, textureBuffer)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Void(&buffer[0]))
	check()

	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
}
Exemplo n.º 3
0
// loadFont loads the given font data. This does not deal with font scaling.
// Scaling should be handled by the independent Bitmap/Truetype loaders.
// We therefore expect the supplied image and charset to already be adjusted
// to the correct font scale.
//
// The image should hold a sprite sheet, defining the graphical layout for
// every glyph. The config describes font metadata.
func loadFont(texture Texture, config *FontConfig) (f *Font, err error) {
	f = new(Font)
	f.program = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
	f.config = config

	// // Resize image to next power-of-two.
	// img = glh.Pow2Image(img).(*image.RGBA)
	ib := texture.Bounds()

	texWidth := float32(ib.Dx())
	texHeight := float32(ib.Dy())

	for _, glyph := range config.Glyphs {
		// Update max glyph bounds.
		if glyph.Width > f.maxGlyphWidth {
			f.maxGlyphWidth = glyph.Width
		}

		if glyph.Height > f.maxGlyphHeight {
			f.maxGlyphHeight = glyph.Height
		}

		// Quad width/height
		vw := float32(glyph.Width)
		vh := float32(glyph.Height)

		// Texture coordinate offsets.
		glyph.tx1 = float32(glyph.X) / texWidth
		glyph.ty1 = 1.0 - float32(glyph.Y)/texHeight
		glyph.tx2 = (float32(glyph.X) + vw) / texWidth
		glyph.ty2 = 1.0 - (float32(glyph.Y)+vh)/texHeight

		// Advance width (or height if we render top-to-bottom)
		// adv := float32(glyph.Advance)

		shape := shapes.NewBox(f.program, vw, vh)
		// shape.SetColor(color.White)
		shape.SetTexture(
			texture.Id(),
			[]float32{
				glyph.tx1, glyph.ty2,
				glyph.tx2, glyph.ty2,
				glyph.tx1, glyph.ty1,
				glyph.tx2, glyph.ty1,
			},
		)

		f.listbase = append(f.listbase, shape)

	}

	return
}
Exemplo n.º 4
0
func NewWorld(width, height int) *World {
	world := &World{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), 0, float32(height)),
		viewMatrix: mathgl.Ident4f(),
		space:      chipmunk.NewSpace(),
	}

	world.space.Gravity = vect.Vect{0, Gravity}

	// Initialize the audio players
	var err error
	world.explosionPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	world.impactPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Read the PCM audio samples

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/explosion.pcm", responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.explosionBuffer = response.Buffer

	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/impact.pcm", responseCh)
	response = <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.impactBuffer = response.Buffer

	// Compile the shaders

	world.boxProgramShader = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
	world.segmentProgramShader = shaders.NewProgram(shapes.DefaultSegmentFS, shapes.DefaultSegmentVS)

	// Load the font
	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/freesans.ttf", responseCh)
	response = <-responseCh
	fontBuffer := response.Buffer
	err = response.Error
	if err != nil {
		panic(err)
	}

	world.font, err = gltext.LoadTruetype(bytes.NewBuffer(fontBuffer), world, 12, 32, 127, gltext.LeftToRight)
	if err != nil {
		panic(err)
	}

	return world
}