Exemple #1
0
// list of boxes
// the point of origin
// the translated offset from the origin
// the angles co-ordinate
// the index of the array
// the degree of angular rotation
func wall(b []uint32, o, offset, a_cord cord, i int32, a float64, s size) []uint32 {
	b = append(b, gl.GenLists(1))
	gl.NewList(b[i], gl.COMPILE)
	gl.Translated(o.x+offset.x, o.y+offset.y, o.z+offset.z)
	gl.Rotated(a, a_cord.x, a_cord.y, a_cord.z)
	box(s.x, s.y, s.z)
	gl.EndList()
	return b
}
Exemple #2
0
func InitFont() {
	LoadTexture(filepath.FromSlash("/Users/Dmitri/Dropbox/Work/2015/Bitmap Fonts/Helvetica Neue.png"))

	oFontBase = gl.GenLists(32 + 96)
	for i := 0; i < 96; i++ {
		const shiftY = float64(1.0 / 6)

		indexX, indexY := i%16, i/16

		charWidth := shiftXs[indexY][indexX+1] - shiftXs[indexY][indexX]

		gl.NewList(oFontBase+uint32(i+32), gl.COMPILE)
		gl.Begin(gl.QUADS)
		gl.TexCoord2d(shiftXs[indexY][indexX], float64(indexY)*shiftY)
		gl.Vertex2d(0, 0)
		gl.TexCoord2d(shiftXs[indexY][indexX+1], float64(indexY)*shiftY)
		gl.Vertex2d(fontWidth*charWidth/float64(1.0/16), 0)
		gl.TexCoord2d(shiftXs[indexY][indexX+1], float64(indexY+1)*shiftY)
		gl.Vertex2d(fontWidth*charWidth/float64(1.0/16), fontHeight)
		gl.TexCoord2d(shiftXs[indexY][indexX], float64(indexY+1)*shiftY)
		gl.Vertex2d(0, fontHeight)
		gl.End()
		gl.Translated(fontWidth*charWidth/float64(1.0/16), 0.0, 0.0)
		gl.EndList()
	}

	oFontBackground = gl.GenLists(1)
	gl.NewList(oFontBackground, gl.COMPILE)
	gl.Begin(gl.QUADS)
	gl.Vertex2d(0, 0)
	gl.Vertex2d(0, fontHeight)
	gl.Vertex2d(fontWidth, fontHeight)
	gl.Vertex2d(fontWidth, 0)
	gl.End()
	gl.Translated(fontWidth, 0.0, 0.0)
	gl.EndList()

	CheckGLError()
}
Exemple #3
0
func InitFont() {
	LoadTexture(filepath.Join("data", "Font.png"))

	oFontBase = gl.GenLists(32 + 96*4)
	for i := 0; i < 96*4; i++ {
		const shiftX, shiftY = float64(1.0 / 16), float64(1.0 / 6 / 4)

		indexX, indexY := i%16, i/16

		gl.NewList(oFontBase+uint32(i+32), gl.COMPILE)
		gl.Begin(gl.QUADS)
		gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY)*shiftY)
		gl.Vertex2i(0, 0)
		gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY)*shiftY)
		gl.Vertex2i(fontWidth, 0)
		gl.TexCoord2d(float64(indexX+1)*shiftX, float64(indexY+1)*shiftY)
		gl.Vertex2i(fontWidth, fontHeight)
		gl.TexCoord2d(float64(indexX)*shiftX, float64(indexY+1)*shiftY)
		gl.Vertex2i(0, fontHeight)
		gl.End()
		gl.Translated(fontWidth, 0.0, 0.0)
		gl.EndList()
	}

	oFontBackground = gl.GenLists(1)
	gl.NewList(oFontBackground, gl.COMPILE)
	gl.Begin(gl.QUADS)
	gl.Vertex2i(0, 0)
	gl.Vertex2i(0, fontHeight)
	gl.Vertex2i(fontWidth, fontHeight)
	gl.Vertex2i(fontWidth, 0)
	gl.End()
	gl.Translated(fontWidth, 0.0, 0.0)
	gl.EndList()

	CheckGLError()
}
Exemple #4
0
func (ctx *DrawContext) drawSphere(p vector.V3, r float64, c colorful.Color) {
	/* TODO:
	   - decrease sphere detail if it's further away
	   - only draw spheres that would be visible inside the frustum:
	     - (no small spheres near the far plane)
	*/
	if ctx.cam.SphereInFrustum(p, r) == OUTSIDE {
		return
	}

	gl.Color3f(float32(c.R), float32(c.G), float32(c.B))

	gl.MatrixMode(gl.MODELVIEW)
	gl.PushMatrix()
	defer gl.PopMatrix()

	slices := int(math.Max(10, 5*math.Log(r+1)))

	gl.Translated(p.X, p.Y, p.Z)
	gl.Scaled(r, r, r)

	l, ok := uint32(0), false
	if ctx.wireframe {
		l, ok = ctx.spheresWireframe[slices]
	} else {
		l, ok = ctx.spheresSolid[slices]
	}

	if !ok {
		ctx.listId++ // XXX: atomic?
		l = ctx.listId
		gl.NewList(l, gl.COMPILE)
		for i := 0; i <= slices; i++ {
			lat0 := math.Pi * (-0.5 + float64(i-1)/float64(slices))
			z0 := math.Sin(lat0)
			zr0 := math.Cos(lat0)

			lat1 := math.Pi * (-0.5 + float64(i)/float64(slices))
			z1 := math.Sin(lat1)
			zr1 := math.Cos(lat1)

			if ctx.wireframe {
				gl.Begin(gl.LINES)
			} else {
				gl.Begin(gl.QUAD_STRIP)
			}
			for j := 0; j <= slices; j++ {
				lng := 2 * math.Pi * (float64(j-1) / float64(slices))
				x := math.Cos(lng)
				y := math.Sin(lng)

				gl.Normal3f(float32(x*zr0), float32(y*zr0), float32(z0))
				gl.Vertex3f(float32(x*zr0), float32(y*zr0), float32(z0))
				gl.Normal3f(float32(x*zr1), float32(y*zr1), float32(z1))
				gl.Vertex3f(float32(x*zr1), float32(y*zr1), float32(z1))
			}
			gl.End()
		}
		gl.EndList()
		if ctx.wireframe {
			ctx.spheresWireframe[slices] = l
		} else {
			ctx.spheresSolid[slices] = l
		}
	}

	gl.CallList(l)
}
Exemple #5
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(img *image.RGBA, config *FontConfig) (f *Font, err error) {
	f = new(Font)
	f.config = config

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

	// Create the texture itself. It will contain all glyphs.
	// Individual glyph-quads display a subset of this texture.
	gl.GenTextures(1, &f.texture)
	gl.BindTexture(gl.TEXTURE_2D, f.texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(ib.Dx()), int32(ib.Dy()), 0,
		gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(img.Pix))

	// Create display lists for each glyph.
	f.listbase = gl.GenLists(int32(len(config.Glyphs)))

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

	for index, 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.
		tx1 := float32(glyph.X) / texWidth
		ty1 := float32(glyph.Y) / texHeight
		tx2 := (float32(glyph.X) + vw) / texWidth
		ty2 := (float32(glyph.Y) + vh) / texHeight

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

		gl.NewList(f.listbase+uint32(index), gl.COMPILE)
		{
			gl.Begin(gl.QUADS)
			{
				gl.TexCoord2f(tx1, ty2)
				gl.Vertex2f(0, 0)
				gl.TexCoord2f(tx2, ty2)
				gl.Vertex2f(vw, 0)
				gl.TexCoord2f(tx2, ty1)
				gl.Vertex2f(vw, vh)
				gl.TexCoord2f(tx1, ty1)
				gl.Vertex2f(0, vh)
			}
			gl.End()

			switch config.Dir {
			case LeftToRight:
				gl.Translatef(adv, 0, 0)
			case RightToLeft:
				gl.Translatef(-adv, 0, 0)
			case TopToBottom:
				gl.Translatef(0, -adv, 0)
			}
		}
		gl.EndList()
	}

	err = checkGLError()
	return
}