Skip to content

Commit

Permalink
mouse support; missing file; voronoi example
Browse files Browse the repository at this point in the history
  • Loading branch information
zokrezyl committed Jun 2, 2020
1 parent 01f909e commit 419978f
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 159 deletions.
15 changes: 8 additions & 7 deletions asciterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add_subparser(self):

def run(self, args):
from asciterm_vispy import ArtSciTermVispy
terminal = ArtSciTermVispy(args, 1500, 1500, scale=1.0)
terminal = ArtSciTermVispy(args)
terminal.run()


Expand All @@ -61,7 +61,7 @@ def add_subparser(self):

def run(self, args):
from asciterm_glumpy import ArtSciTermGlumpy
terminal = ArtSciTermGlumpy(args, 1000, 700, scale=1.0)
terminal = ArtSciTermGlumpy(args)
terminal.run()


Expand All @@ -74,7 +74,7 @@ def add_subparser(self):

def run(self, args):
from asciterm_kivy import ArtSciTermKivy
terminal = ArtSciTermKivy(args, 1000, 1000, scale=2.5)
terminal = ArtSciTermKivy(args)
terminal.run()


Expand All @@ -86,10 +86,12 @@ def run(raw_args=None):
epilog="")
parser.add_argument('--record',
default='a.mpg')
parser.add_argument('--log-level',
parser.add_argument('-s', '--scale', default='2')
parser.add_argument('-d', '--size', default='1000x1000')
parser.add_argument('-l', '--log-level',
choices=['debug', 'info', 'warn', 'error', 'fatal'],
default='info')
parser.add_argument('-l', '--libvterm-path',
parser.add_argument('-p', '--libvterm-path',
help="path to the libvterm library, otherwise we use ldconf to find it")

subparsers = parser.add_subparsers(dest='command')
Expand Down Expand Up @@ -121,8 +123,7 @@ def run(raw_args=None):
handler.setLevel(log_level)

if args[0].command is None:
parser.print_help()
return 1
args[0].command = "vispy-term"

cmd = cmd_map[args[0].command]
print(sys.argv)
Expand Down
19 changes: 13 additions & 6 deletions asciterm_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,18 @@ def adapt_to_dim(self, width, height):

self.handle_main_vbuffer()

def __init__(self, args, width, height, scale):
self.width = width
self.height = height
self.scale = scale
def __init__(self, args):

self.width = 1000
self.height = 1000
size = args[0].size.split("x")
if len(size) == 2:
self.width = int(size[0])
self.height = int(size[1])
else:
print("arg szie has to be in form <width>x<height>, instead got {args[0].size}, using defaults {self.width}, {self.height}")

self.scale = float(args[0].scale)
self.char_width = 6.0
self.char_height = 13.0
self.start_time = time.time()
Expand Down Expand Up @@ -332,9 +340,8 @@ def draw(self, event):

# self.window.clear()
with self.progman_lock:
mouse = (2*self.mouse[0]/self.width - 1, 1 - 2*self.mouse[1]/self.height )
for internal_id, prog_wrap in self.progman.prog_wraps.items():
prog_wrap.draw(time_now = time_now, mouse = mouse)
prog_wrap.draw(time_now = time_now, mouse = self.mouse)

self.program.draw(self.program.GL_POINTS)

Expand Down
6 changes: 2 additions & 4 deletions asciterm_glumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ def to_gl_constant(self, what):


class ArtSciTermGlumpy(ArtSciTerm):
def __init__(self, args, width, height, x=0, y=0, scale=2):
def __init__(self, args):
self.factory = Null()
setattr(self.factory, "create_program", ArtSciTermGlumpyProgram)
setattr(self.factory, "ortho", glm.ortho)

#self.gloo = gloo
#self._app = app
ArtSciTerm.__init__(self, args, width, height, scale)
ArtSciTerm.__init__(self, args)

def run(self):
app.use("qt5")
Expand Down
4 changes: 2 additions & 2 deletions asciterm_vispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def to_gl_constant(self, txt):


class ArtSciTermVispy(app.Canvas, ArtSciTerm):
def __init__(self, args, width, height, x=0, y=0, scale=2):
def __init__(self, args):
self.factory = Null()
setattr(self.factory, "create_program", ArtSciTermVispyProgram)
setattr(self.factory, "ortho", util.transforms.ortho)

#self.gloo = gloo
#self._app = app
app.Canvas.__init__(self)
ArtSciTerm.__init__(self, args, width, height, scale)
ArtSciTerm.__init__(self, args)

def run(self):
super().show()
Expand Down
25 changes: 25 additions & 0 deletions examples/client_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import msgpack
import base64

def transcode(what):
return base64.b64encode(what.encode('ascii')).decode('ascii')

def envelope(title="untitled", cmd="create", draw_mode="trianglestrip", start_col = 10, cols=80, rows=12, vertex_shader=None, fragment_shader=None,
attributes=None, uniforms=None):

ret = f"\033_Atitle='{title}',cmd='{cmd}',start_col={start_col},rows={rows},cols={cols},draw_mode='{draw_mode}'"
if vertex_shader:
ret += f",vertex_shader='{transcode(vertex_shader)}'"

if fragment_shader:
ret += f",fragment_shader='{transcode(fragment_shader)}'"

if attributes:
attributes = msgpack.packb(attributes)
ret += f",attributes='{base64.b64encode(attributes).decode('ascii')}'"

if uniforms:
ret += f",uniforms='{base64.b64encode(uniforms).decode('ascii')}'"

ret += "\033\\"
return ret
66 changes: 20 additions & 46 deletions examples/mandelbrot.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
#!/usr/bin/env python3
import base64
import msgpack
import time
import sys
import numpy as np
#import msgpack_numpy as m

#m.patch()
from client_lib import envelope

# Shader source code: from https://github.com/vispy/vispy/blob/master/examples/demo/gloo/mandelbrot.py
# -----------------------------------------------------------------------------
vertex_shader = """
attribute vec2 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
varying float v_zoom;
varying float v_depth;
uniform float time;
void main()
{
//gl_Position = <transform>;
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
v_texcoord = texcoord;
v_zoom = 1.0/pow(2, (1.01 + sin(time/8))*8);
v_depth = 400.0 * (2 + sin(time/8)) - 400;
}
$vertex_shader_variables;
attribute vec2 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
varying float v_zoom;
varying float v_depth;
void main()
{
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
v_texcoord = texcoord;
v_zoom = 1.0/pow(2, (1.01 + sin(time/8))*8);
v_depth = 400.0 * (2 + sin(time/8)) - 400;
$vertex_shader_epilog;
}
"""

fragment_shader = """
$fragment_shader_variables;
varying vec2 v_texcoord;
varying float v_zoom;
Expand Down Expand Up @@ -71,34 +68,10 @@
"""


def transcode(what):
return base64.b64encode(what.encode('ascii')).decode('ascii')

def envelope(cmd="create", draw_mode="triangle_strip", cols=80, rows=12, vertex_shader=None, fragment_shader=None,
attributes=None, uniforms=None):

ret = f"\033_Acmd='{cmd}',rows={rows},cols={cols},draw_mode='{draw_mode}'"
if vertex_shader:
ret += f",vertex_shader='{transcode(vertex_shader)}'"

if fragment_shader:
ret += f",fragment_shader='{transcode(fragment_shader)}'"

if attributes:
ret += f",attributes='{base64.b64encode(attributes).decode('ascii')}'"

if uniforms:
ret += f",uniforms='{base64.b64encode(uniforms).decode('ascii')}'"

ret += "\033\\"

return ret


def main():
attributes = msgpack.packb({
attributes = {
'position': [(-1,-1), (-1, 1), ( 1,-1), ( 1, 1)],
'texcoord': [( -1, 1), ( -1, -1), ( 1, 1), ( 1, -1)]})
'texcoord': [( -1, 1), ( -1, -1), ( 1, 1), ( 1, -1)]}

print("first we just print some lines...\r\n" * 5)
print(envelope(
Expand All @@ -111,7 +84,8 @@ def main():
attributes=attributes), end="")

sys.stdout.flush()
#time.sleep(100)
if len(sys.argv) > 1:
time.sleep(float(sys.argv[1]))

if __name__ == "__main__":
main()
35 changes: 5 additions & 30 deletions examples/random_points.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env python3
import base64
import msgpack
import time
import sys
import numpy as np

from client_lib import envelope

vertex_shader = """
attribute vec2 position;
Expand All @@ -17,43 +16,17 @@
"""

fragment_shader = """
uniform float time;
uniform vec3 color;
void main() {
gl_FragColor = vec4(sin(time), sin(time/2), sin(time/3), 1.0);
//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
"""


def transcode(what):
return base64.b64encode(what.encode('ascii')).decode('ascii')

def envelope(cmd="create", draw_mode="trianglestrip", cols=80, rows=12, vertex_shader=None, fragment_shader=None,
attributes=None, uniforms=None):

ret = f"\033_Acmd='{cmd}',rows={rows},cols={cols},draw_mode='{draw_mode}'"
if vertex_shader:
ret += f",vertex_shader='{transcode(vertex_shader)}'"

if fragment_shader:
ret += f",fragment_shader='{transcode(fragment_shader)}'"

if attributes:
ret += f",attributes='{base64.b64encode(attributes).decode('ascii')}'"

if uniforms:
ret += f",uniforms='{base64.b64encode(uniforms).decode('ascii')}'"

ret += "\033\\"

return ret


def main():

data = np.random.uniform(-1.0, 1.0, size=(20000, 2)).astype(np.float32).tolist()
attributes = msgpack.packb({"position": data})
attributes = {"position": data}

print("first we just print some lines...\r\n" * 5)
print(envelope(
Expand All @@ -66,7 +39,9 @@ def main():
attributes=attributes), end="")

sys.stdout.flush()
#time.sleep(100)
if len(sys.argv) > 1:
time.sleep(float(sys.argv[1]))


if __name__ == "__main__":
main()
75 changes: 75 additions & 0 deletions examples/voronoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
import sys
import time
from client_lib import envelope

# based on https://thebookofshaders.com/12/

vertex_shader = """
$vertex_shader_variables;
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
$vertex_shader_epilog;
}
"""

fragment_shader = """
$fragment_shader_variables;
void main() {
$fragment_shader_prolog;
vec2 st = relFragCoord.xy/viewport.zw;
st.x *= viewport.z/viewport.w;
vec3 color = vec3(.0);
// Cell positions
vec2 point[5];
point[0] = vec2(0.83,0.75);
point[1] = vec2(0.60,0.07);
point[2] = vec2(0.28,0.64);
point[3] = vec2(0.31,0.26);
point[4] = mousePos/viewport.zw;
float m_dist = 1 + (1 + sin(time))/100; // minimum distance
// Iterate through the points positions
for (int i = 0; i < 5; i++) {
float dist = distance(st, point[i]);
// Keep the closer distance
m_dist = min(m_dist, dist);
}
// Draw the min distance (distance field)
color += m_dist;
// Show isolines
//color -= step(.7,abs(sin(50.0*m_dist)))*.3;
gl_FragColor = vec4(color * (1 + (1 + sin(time)) / 2 ),1.0);
}
"""

def main():
attributes = {
'position': [(-1,-1), (-1, 1), ( 1,-1), ( 1, 1)]}

print("first we just print some lines...\r\n" * 5)
print(envelope(
title="voronoi",
cmd="create",
draw_mode="triangle_strip",
start_col = 10,
rows=35,
cols=80,
vertex_shader=vertex_shader,
fragment_shader=fragment_shader,
attributes=attributes), end="")

sys.stdout.flush()
if len(sys.argv) > 1:
time.sleep(float(sys.argv[1]))

if __name__ == "__main__":
main()
Loading

0 comments on commit 419978f

Please sign in to comment.