# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from itertools import chain
from array import array
from math import cos, sin

# TODO: with FOV
def sky_triangles(vsubdivs, distance, projection_ratio):
    assert vsubdivs > 0
    vertices = []
    hsubdivs = round(vsubdivs * projection_ratio)
    z = -distance
    width = distance * projection_ratio
    height = distance
    startx = width * -0.5
    starty = height * -0.5
    stepx = width / hsubdivs
    stepy = height / vsubdivs
    y1 = starty
    y2 = y1 + stepy
    for sy in range(vsubdivs):
        x1 = startx
        x2 = x1 + stepx
        for sx in range(hsubdivs):
            a = (x1, y2, z)
            b = (x2, y1, z)
            vertices.append((x1, y1, z))
            vertices.append(b)
            vertices.append(a)
            vertices.append((x2, y2, z))
            vertices.append(a)
            vertices.append(b)
            x1 = x2
            x2 += stepx
        y1 = y2
        y2 += stepy
    return array('f', chain.from_iterable(vertices))
