forked from thechargedneutron/GSoC-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geo-Shader-Polygon.py
129 lines (109 loc) · 4.01 KB
/
Geo-Shader-Polygon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 21 01:53:49 2018
@author: Ashu
"""
import vtk
def main():
colors = vtk.vtkNamedColors()
# Create the geometry of a point (the coordinate)
points = vtk.vtkPoints()
p = [65.0, -65.0, 0.0]
q = [65.0, 65.0, 0.0]
r = [-65.0, 65.0, 0.0]
s = [-65.0, -65.0, 0.0]
# Create the topology of the point (a vertex)
polys = vtk.vtkCellArray()
# We need an an array of point id's for InsertNextCell.
pid = [0, 1000, 0, 0]
pid[0] = points.InsertNextPoint(p)
pid[1] = points.InsertNextPoint(q)
pid[2] = points.InsertNextPoint(r)
pid[3] = points.InsertNextPoint(s)
polys.InsertNextCell(4, pid)
# Create a polydata object
polydata = vtk.vtkPolyData()
# Set the points and polys we created as the geometry and
# topology of the polydata
polydata.SetPoints(points)
polydata.SetPolys(polys)
# Create array of vertex colors
colorArray = vtk.vtkUnsignedCharArray()
colorArray.SetNumberOfTuples(4)
colorArray.SetNumberOfComponents(3)
colorArray.InsertTuple(0, (255, 0, 0))
colorArray.InsertTuple(1, (0, 255, 0))
colorArray.InsertTuple(2, (0, 0, 255))
colorArray.InsertTuple(3, (255, 255, 255))
polydata.GetPointData().SetScalars(colorArray)
# Visualize
mapper = vtk.vtkOpenGLPolyDataMapper()
mapper.SetInputData(polydata)
mapper.SetGeometryShaderCode("""
//VTK::System::Dec
//VTK::PositionVC::Dec
uniform mat4 MCDCMatrix;
uniform mat4 MCVCMatrix;
//VTK::PrimID::Dec
// declarations below aren't necessary because
// they are already injected by PrimID template
//in vec4 vertexColorVSOutput[];
//out vec4 vertexColorGSOutput;
//in vec4 vertexVCVSOutput[];
//out vec4 vertexVCVSOutput;
//VTK::Color::Dec
//VTK::Normal::Dec
//VTK::Light::Dec
//VTK::TCoord::Dec
//VTK::Picking::Dec
//VTK::DepthPeeling::Dec
//VTK::Clip::Dec
//VTK::Output::Dec
layout(lines) in;
layout(triangle_strip, max_vertices = 5) out;
void build_house(vec4 position)
{
vec4 point1 = vec4(-6.0, -6.0, 0.0, 0.0);
vec4 point2 = vec4(6.0, -6.0, 0.0, 0.0);
vec4 point3 = vec4(-6.0, 6.0, 0.0, 0.0);
vec4 point4 = vec4(6.0, 6.0, 0.0, 0.0);
vec4 point5 = vec4(0.0, 12.0, 0.0, 0.0);
gl_Position = position + (MCDCMatrix * point1);
vertexVCGSOutput = vertexVCVSOutput[0] + (MCVCMatrix * point1);
EmitVertex();
gl_Position = position + (MCDCMatrix * point2);
vertexVCGSOutput = vertexVCVSOutput[0] + (MCVCMatrix * point2);
EmitVertex();
gl_Position = position + (MCDCMatrix * point3);
vertexVCGSOutput = vertexVCVSOutput[0] + (MCVCMatrix * point3);
EmitVertex();
gl_Position = position + (MCDCMatrix * point4);
vertexVCGSOutput = vertexVCVSOutput[0] + (MCVCMatrix * point4);
EmitVertex();
gl_Position = position + (MCDCMatrix * point5);
vertexVCGSOutput = vertexVCVSOutput[0] + (MCVCMatrix * point5);
EmitVertex();
EndPrimitive();
}
void main() {
vertexColorGSOutput = vertexColorVSOutput[0];
build_house(gl_in[0].gl_Position);
}
""")
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToWireframe()
actor.GetProperty().SetPointSize(10)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetWindowName("Point")
renderWindow.SetSize(500, 500)
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d("Black"))
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
main()