rk_island/game/triangles.py

65 lines
2.1 KiB
Python
Raw Normal View History

2022-08-28 05:09:52 +02:00
# 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
2022-12-31 19:16:44 +01:00
from engine import create_triangles, draw_triangles, destroy_triangles
2022-08-28 05:09:52 +02:00
2022-12-31 19:16:44 +01:00
class Triangles:
__slots__ = '_triangles'
def __init__(self, triangles):
self._triangles = create_triangles(triangles)
def __del__(self):
destroy_triangles(self._triangles)
def draw(self):
draw_triangles(self._triangles)
class SkyTriangles(Triangles):
# TODO: with FOV
def __init__(self, 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
super().__init__(array('f', chain.from_iterable(vertices)))