Skip to content

Commit

Permalink
gl2: Add the mesh transform property.
Browse files Browse the repository at this point in the history
This replaces the flatten property, and explicitly flattens into
a mesh, if possible. Optionally, this takes a tuple, that
defines the number of points in the horizontal and vertical
directions that make up the mesh. (The idea being that a
vertex shader can then distort the mesh.)
  • Loading branch information
renpytom committed Jul 6, 2020
1 parent ae2def4 commit 07f2fdf
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
9 changes: 8 additions & 1 deletion renpy/atl.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def matrixcolor(x):
return renpy.display.matrix.Matrix(x)


def mesh(x):
if isinstance(x, (renpy.gl2.gl2mesh2.Mesh2, renpy.gl2mesh3.Mesh3, tuple)):
return x

return bool(x)


# A dictionary giving property names and the corresponding default
# values.
PROPERTIES = {
Expand Down Expand Up @@ -147,7 +154,7 @@ def matrixcolor(x):
"ytile" : int,
"matrixcolor" : matrixcolor,
"shader" : any_object,
"flatten" : bool,
"mesh" : mesh,
}


Expand Down
23 changes: 17 additions & 6 deletions renpy/display/accelerator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ def transform_render(self, widtho, heighto, st, at):

state = self.state

if state.flatten:
child = self.flatten_cache

if child is None:
child = self.flatten_cache = renpy.display.layout.Flatten(self.child)

xsize = state.xsize
ysize = state.ysize
fit = state.fit
Expand Down Expand Up @@ -363,6 +357,23 @@ def transform_render(self, widtho, heighto, st, at):

rv = Render(width, height)

if state.mesh:

rv.operation = renpy.display.render.FLATTEN
rv.add_shader("renpy.texture")


if isinstance(state.mesh, tuple):
mesh_width, mesh_height = state.mesh

rv.mesh = renpy.gl2.gl2mesh2.Mesh2.texture_grid_mesh(
mesh_width, mesh_height,
0.0, 0.0, cr.width, cr.height,
0.0, 0.0, 1.0, 1.0)
else:
rv.mesh = True


if state.matrixcolor:
matrix = state.matrixcolor

Expand Down
20 changes: 8 additions & 12 deletions renpy/display/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TransformState(renpy.object.Object):
maxsize = None
matrixcolor = None
shader = None
flatten = False
mesh = False

def __init__(self):
self.alpha = 1
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(self):

self.matrixcolor = None
self.shader = None
self.flatten = False
self.mesh = False

self.delay = 0

Expand Down Expand Up @@ -221,7 +221,7 @@ def take_state(self, ts):

self.matrixcolor = ts.matrixcolor
self.shader = ts.shader
self.flatten = ts.flatten
self.mesh = ts.mesh

self.last_angle = ts.last_angle

Expand Down Expand Up @@ -311,8 +311,10 @@ def diff4(prop, new, inherited_new, old, inherited_old):
diff2("ytile", newts.ytile, self.ytile)

diff2("matrixcolor", newts.matrixcolor, self.matrixcolor)
diff2("shader", newts.shader, self.shader)
diff2("flatten", newts.flatten, self.flatten)

# It doesn't make sense to interpolate these.
# diff2("shader", newts.shader, self.shader)
# diff2("mesh", newts.mesh, self.mesh)

diff2("debug", newts.debug, self.debug)
diff2("events", newts.events, self.events)
Expand Down Expand Up @@ -485,7 +487,6 @@ class Transform(Container):

__version__ = 5
transform_event_responder = True
flatten_cache = None

# Proxying things over to our state.
nearest = Proxy("nearest")
Expand Down Expand Up @@ -548,7 +549,7 @@ class Transform(Container):
ytile = Proxy("ytile")

shader = Proxy("shader")
flatten = Proxy("flatten")
mesh = Proxy("mesh")

debug = Proxy("debug")
events = Proxy("events")
Expand Down Expand Up @@ -753,7 +754,6 @@ def take_state(self, t):
if (self.child is None) and (t.child is not None):
self.add(t.child)
self.child_st_base = t.child_st_base
self.flatten_cache = t.flatten_cache

# The arguments will be applied when the default function is
# called.
Expand Down Expand Up @@ -876,10 +876,6 @@ def set_child(self, child, duplicate=True):

if child._duplicatable:
self._duplicatable = True

if child is not self.child:
self.flatten_cache = None

self.child = child
self.children = [ child ]

Expand Down
54 changes: 53 additions & 1 deletion renpy/gl2/gl2mesh2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,59 @@ cdef class Mesh2(Mesh):

return rv


@staticmethod
def texture_grid_mesh(
int width, int height,
double pl, double pb, double pr, double pt,
double tl, double tb, double tr, double tt
):

cdef Mesh2 rv = Mesh2(TEXTURE_LAYOUT, width * height, 2 * (width - 1) * (height - 1))

cdef int x
cdef int y
cdef int i

cdef int p0
cdef int p1
cdef int p2
cdef int p3

rv.points = width * height

for 0 <= y < height:
for 0 <= x < width:
i = x + y * width

rv.point[i].x = pl + (pr - pl) * (1.0 * x / (width - 1))
rv.point[i].y = pb + (pt - pb) * (1.0 * y / (height - 1))

rv.attribute[i * 2] = tl + (tr - tl) * (1.0 * x / (width - 1))
rv.attribute[i * 2 + 1] = tb + (tt - tb) * (1.0 * y / (height - 1))

rv.triangles = 2 * (width - 1) * (height - 1)

for 0 <= y < height - 1:
for 0 <= x < width - 1:

i = 6 * (x + y * (width - 1))

p0 = x + y * width
p1 = p0 + 1
p2 = p0 + width + 1
p3 = p0 + width

rv.triangle[i + 0] = p0
rv.triangle[i + 1] = p1
rv.triangle[i + 2] = p2

rv.triangle[i + 3] = p0
rv.triangle[i + 4] = p2
rv.triangle[i + 5] = p3

return rv

cpdef Mesh2 crop(Mesh2 self, Polygon p):
"""
Crops this mesh against Polygon `p`, and returns a new Mesh2.
Expand All @@ -167,7 +220,6 @@ cdef class Mesh2(Mesh):
return crop_mesh(self, p)



###############################################################################
# Mesh cropping.

Expand Down

0 comments on commit 07f2fdf

Please sign in to comment.