Example #1
0
func drawChunk(chunkX, chunkY int64) {
	chunk := terrain.GetChunkAt(chunkX, chunkY)

	if chunk.DisplayList == 0 {
		chunk.DisplayList = gl.GenLists(1)

		gl.NewList(chunk.DisplayList, gl.COMPILE)
		for x := 0; x < config.ChunkArraySize()-1; x++ {
			for y := 0; y < config.ChunkArraySize()-1; y++ {
				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				x++

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				y++

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				x--

				gl.Normal3d(chunk.Normals.Get(x, y))
				gl.Vertex3d(chunk.Vertices.Get(x, y))
				y--
			}
		}
		gl.EndList()
	}

	gl.CallList(chunk.DisplayList)
}
Example #2
0
func generateChunk(chunkCoord ChunkCoordinate) *Chunk {
	startX, startY := float64(chunkCoord.X<<config.ChunkShift), float64(chunkCoord.Y<<config.ChunkShift)

	coord := func(i, j int) (x, y float64) {
		return startX + float64(i)/float64(config.TerrainSubdivisions()), startY + float64(j)/float64(config.TerrainSubdivisions())
	}

	// Optimization: Pre-generate the noise values we'll be using instead of generating them repeatedly
	heightCount := config.ChunkArraySize() + config.TerrainSubdivisions()
	heights := make([][]float64, heightCount)
	for i := range heights {
		heights[i] = make([]float64, heightCount)
		for j := range heights[i] {
			x, y := coord(i, j)
			heights[i][j] = terrainHeight(x, y, config.TerrainDetail())
		}
	}

	chunk := new(Chunk)
	chunk.Vertices = make(Vec3Array, config.ChunkArraySize()*config.ChunkArraySize()*3)
	chunk.Normals = make(Vec3Array, config.ChunkArraySize()*config.ChunkArraySize()*3)

	for i := 0; i < config.ChunkArraySize(); i++ {
		for j := 0; j < config.ChunkArraySize(); j++ {
			x, y := coord(i, j)

			// Normal
			current, right, top := heights[i][j], heights[i+config.TerrainSubdivisions()][j], heights[i][j+config.TerrainSubdivisions()]
			v := vector.Vec3(current-right, current-top, scaleVertical/2)
			v.Normalize()
			chunk.Normals.Set(i, j, v[0], v[1], v[2])

			// Position
			chunk.Vertices.Set(i, j, x, y, current)
		}
	}

	chunk.markGet()

	return chunk
}