-
Notifications
You must be signed in to change notification settings - Fork 0
/
CylGLView.py
143 lines (118 loc) · 5.13 KB
/
CylGLView.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
""" Qt Widget for displaying a cylinder array using OpenGL
Author: Dr Stephen Hanham
"""
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
#from OpenGL.GLUT import *
from PyQt4 import QtGui, QtOpenGL
class CylGLView(QtOpenGL.QGLWidget):
aspect_ratio = 1.3
def initializeGL(self):
glViewport(0, 0, self.width(), self.height())
quadratic = gluNewQuadric()
gluQuadricDrawStyle(quadratic, GLU_FILL)
gluQuadricNormals(quadratic, GLU_SMOOTH) # Create Smooth Normals (NEW)
glClearColor(0, 0, 0, 1.)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
lightZeroPosition = [0, 15., 15, 1.]
lightZeroColor = [1, 1.0, 1, 1.0]
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.01)
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.02)
glEnable(GL_LIGHT0)
#glutDisplayFunc(self.paintGL)
glMatrixMode(GL_PROJECTION)
gluPerspective(40., 1., 0.1, 4000.)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, 40, 50, # Camera pos (x,y,z)
0, 20, 25, # Camera pointed towards (x,y,z)
0, -1, 0) # Camera orientation (x,y,z)
glPushMatrix()
self.diameter = 0
self.period = 10
#def heightForWidth(self):
#return int(self.width)
def sizeHint(self):
s = self.size()
s.setHeight(s.width()*self.aspect_ratio)
return s
def set_camera(self, cam_pos, cam_aim):
''' Method to set camera position and pointing'''
gluLookAt(cam_pos[0], cam_pos[1], cam_pos[2], # Camera pos (x,y,z)
cam_aim[0], cam_aim[1], cam_aim[2], # Camera pointed towards (x,y,z)
0, -1, 0) # Camera orientation (x,y,z)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
cyl_colour = [1, 1, 1, 1.]
box_colour = [1, 1, 1]
m = 5
if self.diameter != 0:
x_list = np.linspace(-m*self.period, m*self.period, 2*m + 1)
y_list = np.linspace(-m*self.period, m*self.period, 2*m + 1)
# Draw cylinder array
for i in range(len(x_list)-1):
for k in range(len(y_list)-1):
x = x_list[i]
y = y_list[k]
self.draw_cylinder(x, y, 0, self.diameter / 2.0, 10, cyl_colour)
#self.draw_box(0.0, 0, 6, self.period, self.period, 12, box_colour)
def draw_box(self, x, y, z, dx, dy, dz, colour):
''' Draws a rectangular box centred at (x,y,z) with dimensions (dx,dy,dz) and supplied colour '''
glPushMatrix()
glDisable(GL_LIGHTING) # Turn off lighting to render box
glColor3f(colour[0], colour[1], colour[2])
glTranslatef(x, y, z) # Re-centre at (x,y,z)
glLineWidth(0.1)
glColor3f(1.0, 0.0, 0.0)
glBegin(GL_LINES)
glVertex3f(-dx / 2, dy / 2, dz / 2)
glVertex3f(dx / 2, dy / 2, dz / 2)
glVertex3f(dx / 2, dy / 2, dz / 2)
glVertex3f(dx / 2, dy / 2, -dz / 2)
glVertex3f(dx / 2, dy / 2, -dz / 2)
glVertex3f(-dx / 2, dy / 2, -dz / 2)
glVertex3f(-dx / 2, dy / 2, -dz / 2)
glVertex3f(-dx / 2, dy / 2, dz / 2)
glVertex3f(-dx / 2, -dy / 2, dz / 2)
glVertex3f(dx / 2, -dy / 2, dz / 2)
glVertex3f(dx / 2, -dy / 2, dz / 2)
glVertex3f(dx / 2, -dy / 2, -dz / 2)
glVertex3f(dx / 2, -dy / 2, -dz / 2)
glVertex3f(-dx / 2, -dy / 2, -dz / 2)
glVertex3f(-dx / 2, -dy / 2, -dz / 2)
glVertex3f(-dx / 2, -dy / 2, dz / 2)
glVertex3f(-dx / 2, dy / 2, dz / 2)
glVertex3f(-dx / 2, -dy / 2, dz / 2)
glVertex3f(dx / 2, dy / 2, dz / 2)
glVertex3f(dx / 2, -dy / 2, dz / 2)
glVertex3f(-dx / 2, dy / 2, -dz / 2)
glVertex3f(-dx / 2, -dy / 2, -dz / 2)
glVertex3f(dx / 2, dy / 2, -dz / 2)
glVertex3f(dx / 2, -dy / 2, -dz / 2)
glEnd()
glPopMatrix()
glEnable(GL_LIGHTING)
def draw_cylinder(self, x, y, z, radius, height, colour):
''' Draws a cylinder centred at (x,y,z) with given radius, height and colour '''
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour)
glPushMatrix()
glTranslatef(x, y, z)
# Draw side of cylinder using quadric fn
quadObj = gluNewQuadric()
gluQuadricDrawStyle(quadObj, GLU_FILL)
gluQuadricNormals(quadObj, GLU_SMOOTH)
gluCylinder(quadObj, radius, radius, height, 24, 24)
gluDisk(quadObj, 0, radius, 100, 24) # Bottom disk
glTranslatef(0, 0, height)
gluDisk(quadObj, 0, radius, 100, 24) # Top disk
glPopMatrix()
def update_geom(self, diam, period):
''' Updates the geometry of the cylinder and repaints '''
self.diameter = diam/100
self.period = period/100
self.repaint()