Skip to content

Instantly share code, notes, and snippets.

@GuillaumeFavelier
Last active March 20, 2019 09:30
Show Gist options
  • Save GuillaumeFavelier/6df3a6d3379311f1226729bbdd777b9e to your computer and use it in GitHub Desktop.
Save GuillaumeFavelier/6df3a6d3379311f1226729bbdd777b9e to your computer and use it in GitHub Desktop.
VisPy dummy testing
This file is here to name the Gist.
#version 110
varying vec3 v_normal;
varying vec3 v_pos;
const vec3 lightPos = vec3(1.0,1.0,1.0);
const vec3 lightColor = vec3(1.0, 1.0, 1.0);
const float lightPower = 40.0;
const vec3 ambientColor = vec3(0.1, 0.1, 0.1);
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
const vec3 specColor = vec3(1.0, 1.0, 1.0);
const float shininess = 16.0;
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
void main()
{
vec3 normal = normalize(v_normal);
vec3 lightDir = lightPos - v_pos;
float distance = length(lightDir);
distance = distance * distance;
lightDir = normalize(lightDir);
float lambertian = max(dot(lightDir,normal), 0.0);
float specular = 0.0;
if(lambertian > 0.0) {
vec3 viewDir = normalize(-v_pos);
// this is blinn phong
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normal), 0.0);
specular = pow(specAngle, shininess);
}
vec3 colorLinear = ambientColor +
diffuseColor * lambertian * lightColor * lightPower / distance +
specColor * specular * lightColor * lightPower / distance;
// apply gamma correction (assume ambientColor, diffuseColor and specColor
// have been linearized, i.e. have no gamma correction in them)
vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0/screenGamma));
// use the gamma corrected color in the fragment
gl_FragColor = vec4(colorGammaCorrected, 0.5);
// test the depth buffer
//float x = gl_FragCoord.z;
//gl_FragColor = vec4(x, x, x, 0.5);
}
#version 110
attribute vec3 a_position;
mat4 view_frustum(
float angle_of_view,
float aspect_ratio,
float z_near,
float z_far) {
return mat4(
vec4(1.0/tan(angle_of_view), 0.0, 0.0, 0.0),
vec4(0.0, aspect_ratio/tan(angle_of_view), 0.0, 0.0),
vec4(0.0, 0.0, (z_far+z_near)/(z_far-z_near), 1.0),
vec4(0.0, 0.0, -2.0*z_far*z_near/(z_far-z_near), 0.0)
);
}
mat4 scale(float x, float y, float z)
{
return mat4(
vec4(x, 0.0, 0.0, 0.0),
vec4(0.0, y, 0.0, 0.0),
vec4(0.0, 0.0, z, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
mat4 translate(float x, float y, float z)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(x, y, z, 1.0)
);
}
mat4 rotate_x(float theta)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, cos(theta), sin(theta), 0.0),
vec4(0.0, -sin(theta), cos(theta), 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
void main()
{
gl_Position = view_frustum(radians(59.0), 4.0/3.0, 0.001, 100.0)
* translate(0.0, -0.05, 0.08)
* rotate_x(0.0)
* scale(0.001, 0.001, 0.001)
* vec4(a_position, 1.0);
}
#version 110
varying vec3 v_normal;
varying vec3 v_pos;
const vec3 lightPos = vec3(1.0,1.0,1.0);
const vec3 lightColor = vec3(1.0, 1.0, 1.0);
const float lightPower = 40.0;
const vec3 ambientColor = vec3(0.1, 0.1, 0.1);
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
const vec3 specColor = vec3(1.0, 1.0, 1.0);
const float shininess = 16.0;
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
void main()
{
vec3 normal = normalize(v_normal);
vec3 lightDir = lightPos - v_pos;
float distance = length(lightDir);
distance = distance * distance;
lightDir = normalize(lightDir);
float lambertian = max(dot(lightDir,normal), 0.0);
float specular = 0.0;
if(lambertian > 0.0) {
vec3 viewDir = normalize(-v_pos);
// this is blinn phong
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normal), 0.0);
specular = pow(specAngle, shininess);
}
vec3 colorLinear = ambientColor +
diffuseColor * lambertian * lightColor * lightPower / distance +
specColor * specular * lightColor * lightPower / distance;
// apply gamma correction (assume ambientColor, diffuseColor and specColor
// have been linearized, i.e. have no gamma correction in them)
vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0/screenGamma));
// use the gamma corrected color in the fragment
gl_FragColor = vec4(colorGammaCorrected, 0.5);
// test the depth buffer
//float x = gl_FragCoord.z;
//gl_FragColor = vec4(x, x, x, 0.5);
}
#version 110
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 v_normal;
varying vec3 v_pos;
mat4 view_frustum(
float angle_of_view,
float aspect_ratio,
float z_near,
float z_far) {
return mat4(
vec4(1.0/tan(angle_of_view), 0.0, 0.0, 0.0),
vec4(0.0, aspect_ratio/tan(angle_of_view), 0.0, 0.0),
vec4(0.0, 0.0, (z_far+z_near)/(z_far-z_near), 1.0),
vec4(0.0, 0.0, -2.0*z_far*z_near/(z_far-z_near), 0.0)
);
}
mat4 scale(float x, float y, float z)
{
return mat4(
vec4(x, 0.0, 0.0, 0.0),
vec4(0.0, y, 0.0, 0.0),
vec4(0.0, 0.0, z, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
mat4 translate(float x, float y, float z)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(x, y, z, 1.0)
);
}
mat4 rotate_x(float theta)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, cos(theta), sin(theta), 0.0),
vec4(0.0, -sin(theta), cos(theta), 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
void main()
{
v_pos = a_position;
v_normal = a_normal;
gl_Position = view_frustum(radians(59.0), 4.0/3.0, 0.1, 100.0)
* translate(0.0, -0.05, 0.04)
* rotate_x(0.0)
* scale(0.1, 0.1, 0.1)
* vec4(v_pos, 1.0);
}
#!/usr/bin/python
import numpy as np
from vispy import scene, app
from vispy.geometry.generation import create_arrow
from vispy.visuals.transforms import MatrixTransform
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
canvas.bgcolor = 'white'
view = canvas.central_widget.add_view()
arr_pos = np.array([[1, 0, 0], [-1.0, 0, 0]])
scene.visuals.Arrow(pos=arr_pos, color='black',
width=4, parent=view.scene)
v1 = arr_pos[1, :] - arr_pos[0, :]
vn = np.linalg.norm(v1)
v1 = v1 / vn
v2 = np.array([0, 0, 1])
cosangle = np.dot(v1, v2)
axis = np.cross(v2, v1)
md = create_arrow(rows=8, cols=8, length=vn)
arr = scene.visuals.Mesh(meshdata=md,
shading='flat', parent=view.scene)
mat = MatrixTransform()
if cosangle != 1:
mat.rotate(np.degrees(np.arccos(cosangle)), axis)
mat.translate(arr_pos[0, :])
arr.transform = mat
view.camera = scene.cameras.ArcballCamera(fov=60, parent=view.scene)
app.run()
#!/usr/bin/python
import numpy as np
from vispy import scene, app
from vispy.visuals.line.arrow import ARROW_TYPES
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
canvas.bgcolor = 'white'
view = canvas.central_widget.add_view()
for idx, arr_type in enumerate(ARROW_TYPES):
arr_pos = np.array([[0, idx, 0], [1, idx, 0]])
arr_dir = np.array([[0, idx, 0, 1, idx, 0]])
scene.visuals.Arrow(pos=arr_pos, arrow_type=arr_type, width=4,
arrows=arr_dir, arrow_size=10, color='black',
arrow_color='black', connect="segments",
parent=view.scene)
view.camera = scene.cameras.ArcballCamera(fov=60, parent=view.scene)
app.run()
#!/usr/bin/python
import numpy as np
from vispy import scene, app
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
canvas.bgcolor = 'white'
view = canvas.central_widget.add_view()
for idx in range(9):
arr_pos = np.array([[0, idx, 0], [1, idx, 0],
[1, idx, 0], [0.8, idx-0.1, 0],
[1, idx, 0], [0.8, idx+0.1, 0]])
scene.visuals.Line(pos=arr_pos, color='black', parent=view.scene)
view.camera = scene.cameras.ArcballCamera(fov=60, parent=view.scene)
app.run()
#!/usr/bin/python
import numpy as np
from vispy import io
from vispy import app
from vispy import gloo
import nibabel as nib
class Brain(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, vsync=True, keys='interactive')
with open('brain.vert', 'r') as myfile:
vertex_shader = myfile.read()
with open('brain.frag', 'r') as myfile:
fragment_shader = myfile.read()
self.program = gloo.Program(vertex_shader, fragment_shader)
surface_fname = 'inner_skull.surf'
vertices, faces = nib.freesurfer.read_geometry(surface_fname)
self.vertices = vertices.astype(np.float32)
self.faces = faces.astype(np.uint32)
self.program['a_position'] = self.vertices
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
def on_draw(self, event):
gloo.clear((0,0,0,1))
self.program.draw('triangles', gloo.IndexBuffer(self.faces))
brain = Brain()
brain.show()
app.run();
#!/usr/bin/python
import numpy as np
from vispy import io
from vispy import app
from vispy import gloo
import nibabel as nib
class Brain(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, vsync=True, keys='interactive')
with open('bunny.vert', 'r') as myfile:
vertex_shader = myfile.read()
with open('bunny.frag', 'r') as myfile:
fragment_shader = myfile.read()
self.program = gloo.Program(vertex_shader, fragment_shader)
# test IO with the Standford bunny
fileName = "bunny.obj"
[vertices, faces, self.normals, t] = io.read_mesh(fileName)
self.vertices = vertices.astype(np.float32)
self.faces = faces.astype(np.uint32)
self.program['a_position'] = self.vertices
self.program['a_normal'] = self.normals
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
def on_draw(self, event):
gloo.clear((0,0,0,1))
self.program.draw('triangles', gloo.IndexBuffer(self.faces))
brain = Brain()
brain.show()
app.run();
import sys
import numpy as np
# import vispy
from vispy import app, scene, io
# from vispy.visuals.filters import Alpha
# import nibabel as nib
# from vispy.visuals.transforms import STTransform
# from vispy.geometry import create_sphere
loaded_mesh = False
if len(sys.argv) > 1:
surface_fname = sys.argv[1]
loaded_mesh = True
else:
print('No input surface provided, SURF or OBJ format expected.')
# Prepare canvas
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
canvas.bgcolor = 'grey'
# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()
# Plot origin and axis direction
color = np.array([[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 0, 1, 1],
[0, 0, 1, 1]])
scene.visuals.XYZAxis(parent=view.scene, color=color)
scene.visuals.Text(color='white', text="TEXT", parent=view.scene,
font_size=100.0)
# BEGIN GOAL; create the spheres
# sphere_radius = 10.
# How to add shading on visuals.Sphere() ? Use visuals.Mesh() instead
# red_orb = scene.visuals.Sphere(color='red', radius=sphere_radius,
# parent=view.scene)
# TMP Workaround:
# sphere_mesh = create_sphere(radius=sphere_radius)
# orb1 = scene.visuals.Mesh(vertices=sphere_mesh.get_vertices()
# faces=sphere_mesh.get_faces(),
# color='blue', shading='smooth',
# parent=view.scene)
# orb1.transform=STTransform(translate=(65.,-5.,-50.))
# orb1.attach(Alpha(0.7))
# orb1.set_gl_state('translucent', depth_test=True, cull_face=True)
# orb2 = scene.visuals.Mesh(vertices=sphere_mesh.get_vertices(),
# faces=sphere_mesh.get_faces(),
# color='red', shading='smooth',
# parent=view.scene)
# orb2.transform=STTransform(translate=(-70.,-5.,-50.))
# orb2.attach(Alpha(0.7))
# orb2.set_gl_state('translucent', depth_test=True, cull_face=True)
# orb3 = scene.visuals.Mesh(vertices=sphere_mesh.get_vertices(),
# faces=sphere_mesh.get_faces(),
# color='green', shading='smooth',
# parent=view.scene)
# orb3.transform=STTransform(translate=(0.,80.,-30.))
# orb3.attach(Alpha(0.7))
# orb3.set_gl_state('translucent', depth_test=True, cull_face=True)
# END GOAL
# Create the mesh from Freesurfer
if loaded_mesh:
ext = surface_fname.split('.')[-1]
if ext == 'obj' or ext == 'OBJ':
[V, F, N, T] = io.read_mesh(surface_fname)
if ext == 'stl' or ext == 'STL':
[V, F, N, T] = io.read_mesh(surface_fname)
# elif ext == 'surf' or ext == 'SURF':
# V, F = nib.freesurfer.read_geometry(surface_fname)
vertices = V.astype(np.float32)
faces = F.astype(np.uint32)
mesh = scene.visuals.Mesh(vertices=vertices, faces=faces,
color='white', parent=view.scene,
shading='smooth')
# mesh.attach(Alpha(0.4))
# mesh.set_gl_state('translucent', depth_test=True, cull_face=True)
# BEGIN GOAL: display face normals
# in_vertices=mesh.mesh_data.get_vertices()
# in_faces=mesh.mesh_data.get_faces()
# in_normals=mesh.mesh_data.get_face_normals()
# out_vertices=np.zeros([2*len(in_faces), 3])
# i = 0
# sfactor = 3.0
# for f in faces:
# v = vertices[f]
# m1 = np.mean(v, axis=0)
# m2 = m1 + in_normals[i]/sfactor
# out_vertices[2*i] = m1
# out_vertices[2*i+1] = m2
# i = i + 1
# vnormals = scene.visuals.Line(out_vertices, color='blue',
# parent=view.scene, connect='segments')
# END GOAL
# BEGIN GOAL: display vertex normals
# in_vertices=mesh.mesh_data.get_vertices()
# in_normals=mesh.mesh_data.get_vertex_normals()
# out_vertices=np.zeros([2*len(in_vertices), 3])
# i = 0
# sfactor = 1.0
# for v in vertices:
# m1 = v
# m2 = m1 + in_normals[i]/sfactor
# out_vertices[2*i] = m1
# out_vertices[2*i+1] = m2
# i = i + 1
# vnormals = scene.visuals.Line(out_vertices, color='blue',
# parent=view.scene, connect='segments')
# END GOAL
# Add camera
cam = scene.cameras.ArcballCamera(fov=60, parent=view.scene, name='Cam2')
# Select camera
view.camera = cam
app.run()
#!/usr/bin/python
import numpy as np
from vispy import io
from vispy import app
from vispy import gloo
import nibabel as nib
class Brain(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, vsync=True, keys='interactive')
with open('plane.vert', 'r') as myfile:
vertex_shader = myfile.read()
with open('plane.frag', 'r') as myfile:
fragment_shader = myfile.read()
self.program = gloo.Program(vertex_shader, fragment_shader)
d = 1
cx = -0.5
cy = -0.5
vertices=np.asarray([[cx, cy, 0.0],
[cx+d, cy, 0.0],
[cx, cy+d, 0.0],
[cx+d, cy+d, 0.0]])
faces=np.asarray([[0, 1, 2],
[1, 2, 3]])
normals=np.asarray([[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0]])
self.vertices = vertices.astype(np.float32)
self.normals = normals.astype(np.float32)
self.faces = faces.astype(np.uint32)
self.program['a_position'] = self.vertices
self.program['a_normal'] = self.normals
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
def on_draw(self, event):
gloo.clear((0,0,0,1))
self.program.draw('triangles', gloo.IndexBuffer(self.faces))
brain = Brain()
brain.show()
app.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment