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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| #!/usr/bin/env python
"""PyQt4 port of the opengl/hellogl example from Qt v4.x"""
import sys
import math, random
from PyQt4 import QtCore, QtGui, QtOpenGL
try:
from OpenGL import GL,GLU
except ImportError:
app = QtGui.QApplication(sys.argv)
QtGui.QMessageBox.critical(None, "OpenGL hellogl",
"PyOpenGL must be installed to run this example.",
QtGui.QMessageBox.Ok | QtGui.QMessageBox.Default,
QtGui.QMessageBox.NoButton)
sys.exit(1)
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.glWidget = GLWidget()
self.xSlider = self.createSlider(QtCore.SIGNAL("xRotationChanged(int)"),
self.glWidget.setXRotation)
self.ySlider = self.createSlider(QtCore.SIGNAL("yRotationChanged(int)"),
self.glWidget.setYRotation)
self.zSlider = self.createSlider(QtCore.SIGNAL("zRotationChanged(int)"),
self.glWidget.setZRotation)
mainLayout = QtGui.QHBoxLayout()
mainLayout.addWidget(self.glWidget)
mainLayout.addWidget(self.xSlider)
mainLayout.addWidget(self.ySlider)
mainLayout.addWidget(self.zSlider)
self.setLayout(mainLayout)
self.xSlider.setValue(15 * 16)
self.ySlider.setValue(345 * 16)
self.zSlider.setValue(0 * 16)
self.setWindowTitle(self.tr("Hello GL"))
def createSlider(self, changedSignal, setterSlot):
slider = QtGui.QSlider(QtCore.Qt.Vertical)
slider.setRange(0, 360 * 16)
slider.setSingleStep(16)
slider.setPageStep(15 * 16)
slider.setTickInterval(15 * 16)
slider.setTickPosition(QtGui.QSlider.TicksRight)
self.glWidget.connect(slider, QtCore.SIGNAL("valueChanged(int)"), setterSlot)
self.connect(self.glWidget, changedSignal, slider, QtCore.SLOT("setValue(int)"))
return slider
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
QtOpenGL.QGLWidget.__init__(self, parent)
self.object = 0
self.xRot = 0
self.yRot = 0
self.zRot = 0
self.rep1=6
self.rep2=6
self.lastPos = QtCore.QPoint()
self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0)
self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)
def xRotation(self):
return self.xRot
def yRotation(self):
return self.yRot
def zRotation(self):
return self.zRot
def minimumSizeHint(self):
return QtCore.QSize(50, 50)
def sizeHint(self):
return QtCore.QSize(400, 400)
def setXRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.xRot:
self.xRot = angle
self.emit(QtCore.SIGNAL("xRotationChanged(int)"), angle)
self.updateGL()
def setYRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.yRot:
self.yRot = angle
self.emit(QtCore.SIGNAL("yRotationChanged(int)"), angle)
self.updateGL()
def setZRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.zRot:
self.zRot = angle
self.emit(QtCore.SIGNAL("zRotationChanged(int)"), angle)
self.updateGL()
def initializeGL(self):
self.qglClearColor(self.trolltechPurple.dark())
self.object = self.makeObject()
GL.glShadeModel(GL.GL_FLAT)
GL.glEnable(GL.GL_DEPTH_TEST)
# GL.glEnable(GL.GL_CULL_FACE)
def paintGL(self):
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
GL.glLoadIdentity()
GL.glTranslated(0.0, 0.0, -19.0)
GL.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
GL.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
GL.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
GL.glCallList(self.object)
def resizeGL(self, width, height):
side = min(width, height)
GL.glViewport((width - side) / 2, (height - side) / 2, side, side)
GL.glMatrixMode(GL.GL_PROJECTION)
GL.glLoadIdentity()
GL.glOrtho(-6, 6, -6, 6, 1, 37)
# http://www.talisman.org/opengl-1.1/Reference/glOrtho.html
GL.glMatrixMode(GL.GL_MODELVIEW)
def mousePressEvent(self, event):
self.lastPos = QtCore.QPoint(event.pos())
def mouseMoveEvent(self, event):
dx = event.x() - self.lastPos.x()
dy = event.y() - self.lastPos.y()
if event.buttons() & QtCore.Qt.LeftButton:
self.setXRotation(self.xRot + 8 * dy)
self.setYRotation(self.yRot + 8 * dx)
elif event.buttons() & QtCore.Qt.RightButton:
self.setXRotation(self.xRot + 8 * dy)
self.setZRotation(self.zRot + 8 * dx)
self.lastPos = QtCore.QPoint(event.pos())
def wheelEvent(self, event):
print 'La roue tourne...'
def makeObject(self):
genList = GL.glGenLists(1)
GL.glNewList(genList, GL.GL_COMPILE)
f=[]
NGrille=100
for i in range(NGrille+1):
z=[]
for j in range(NGrille+1):
z.append(5*math.sin(-5+10.0*i/NGrille) * math.cos(-5+10.0*j/NGrille))
f.append(z)
# print f
# print f[0][0]
for i in range(1,NGrille+1):
for j in range(1,NGrille+1):
# print 'i, j = ' +str((i,j))
# print (-5+10.0*(i-1)/NGrille, -5+10.0*(j-1)/NGrille)
# Attention, on doit taper "+10.0*" car sinon la division est euclidienne.
couleur = self.degrade(-6, 6, f[i][j])
self.quadrilatere(-5+10.0*(i-1)/NGrille, -5+10.0*(j-1)/NGrille, f[i-1][j-1],
-5+10.0*i/NGrille, -5+10.0*(j-1)/NGrille, f[i][j-1],
-5+10.0*i/NGrille, -5+10.0*j/NGrille, f[i][j],
-5+10.0*(i-1)/NGrille, -5+10.0*j/NGrille, f[i-1][j],
couleur[0], couleur[1], couleur[2])
GL.glEndList()
return genList
def degrade(self, zMin, zMax ,zActuel):
# Cas d'un degrade un peu chauvin avec trois couleurs
Couleur1 = [0, 0, 255]
Couleur2 = [255, 255, 255]
Couleur3 = [255, 0, 0]
k = 100.0*(zMax-zActuel)/(zMax - zMin)
if k <= 50:
c = k / 50.0
r = math.floor(Couleur1[0] + (Couleur2[0] - Couleur1[0])* c)
g = math.floor(Couleur1[1] + (Couleur2[1] - Couleur1[1])* c)
b = math.floor(Couleur1[2] + (Couleur2[2] - Couleur1[2])* c)
else:
c = (k - 50) / 50.0
r = math.floor(Couleur2[0] + (Couleur3[0] - Couleur2[0])* c)
g = math.floor(Couleur2[1] + (Couleur3[1] - Couleur2[1])* c)
b = math.floor(Couleur2[2] + (Couleur3[2] - Couleur2[2])* c)
return [r, g, b]
def quadrilatere(self, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, r, g, b):
self.qglColor(QtGui.QColor(r,g,b))
GL.glBegin(GL.GL_QUADS)
GL.glVertex3d(x1, y1, z1)
GL.glVertex3d(x2, y2, z2)
GL.glVertex3d(x3, y3, z3)
GL.glVertex3d(x4, y4, z4)
GL.glEnd()
# print '\nPoint 1 :'
# print (x1, y1, z1)
# print '\nPoint 2 :'
# print (x2, y2, z2)
# print '\nPoint 3 :'
# print (x3, y3, z3)
# print '\nPoint 4 :'
# print (x4, y4, z4)
def extrude(self, x1, y1, x2, y2):
self.qglColor(self.trolltechGreen.dark(250 + int(100 * x1)))
GL.glVertex3d(x1, y1, +0.05)
GL.glVertex3d(x2, y2, +0.05)
GL.glVertex3d(x2, y2, -0.05)
GL.glVertex3d(x1, y1, -0.05)
def normalizeAngle(self, angle):
while angle < 0:
angle += 360 * 16
while angle > 360 * 16:
angle -= 360 * 16
return angle
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_()) |