<div>Christophe,</div>
<div>Your problem is with your OpenGL syntax. Simply change GL.GL_QUADS (which draw quadrilaterals) with</div>
<div>GL.GL_TRIANGLES (self evident). Also, it would be better to move those declarations into your triangle routine as follows:</div>
<div> </div>
<div> def triangle(self, x1, y1, z1, x2, y2, z2, x3, y3, z3,r,g,b):<br> self.qglColor(QtGui.QColor(r,g,b))<br> GL.glBegin(GL.GL_TRIANGLES)<br> GL.glVertex3d(x1, y1, z1)<br> GL.glVertex3d(x2, y2, z2)<br>
GL.glVertex3d(x3, y3, z3)<br> GL.glEnd()</div>
<div> </div>
<div>This way, each glBegin is guaranteed to have a closing glEnd. <br>I believe it now works as you would expect...</div>
<div>Cheers - jamie<br></div>
<div class="gmail_quote">On Sat, Apr 4, 2009 at 9:04 AM, projetmbc <span dir="ltr"><<a href="mailto:projetmbc@club-internet.fr">projetmbc@club-internet.fr</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hello,<br>I would like to draw for example a 3D triangle with OpenGL. I've tried to play with the example Hello GL proposed by PyQt.<br>
<br>In the following code I would like to draw a red triangle and then a blue one. I also would like to set the xMin, xMax, yMin, yMax, zMin, zMax, for the point of view. My purpose would be to a 3D geometric viewers for educational purposes.<br>
<br>Best regards.<br>Christophe<br><br>========<br>The code<br>========<br><br>#!/usr/bin/env python<br><br>"""PyQt4 port of the opengl/hellogl example from Qt v4.x"""<br><br>import sys<br>import math<br>
from PyQt4 import QtCore, QtGui, QtOpenGL<br><br>try:<br> from OpenGL import GL<br>except ImportError:<br> app = QtGui.QApplication(sys.argv)<br> QtGui.QMessageBox.critical(None, "OpenGL hellogl",<br> "PyOpenGL must be installed to run this example.",<br>
QtGui.QMessageBox.Ok | QtGui.QMessageBox.Default,<br> QtGui.QMessageBox.NoButton)<br> sys.exit(1)<br><br><br>class Window(QtGui.QWidget):<br> def __init__(self, parent=None):<br>
QtGui.QWidget.__init__(self, parent)<br><br> self.glWidget = GLWidget()<br><br> self.xSlider = self.createSlider(QtCore.SIGNAL("xRotationChanged(int)"),<br> self.glWidget.setXRotation)<br>
self.ySlider = self.createSlider(QtCore.SIGNAL("yRotationChanged(int)"),<br> self.glWidget.setYRotation)<br> self.zSlider = self.createSlider(QtCore.SIGNAL("zRotationChanged(int)"),<br>
self.glWidget.setZRotation)<br><br> mainLayout = QtGui.QHBoxLayout()<br> mainLayout.addWidget(self.glWidget)<br> mainLayout.addWidget(self.xSlider)<br> mainLayout.addWidget(self.ySlider)<br>
mainLayout.addWidget(self.zSlider)<br> self.setLayout(mainLayout)<br><br> self.xSlider.setValue(15 * 16)<br> self.ySlider.setValue(345 * 16)<br> self.zSlider.setValue(0 * 16)<br><br> self.setWindowTitle(<a href="http://self.tr/" target="_blank">self.tr</a>("Hello GL"))<br>
<br> def createSlider(self, changedSignal, setterSlot):<br> slider = QtGui.QSlider(QtCore.Qt.Vertical)<br><br> slider.setRange(0, 360 * 16)<br> slider.setSingleStep(16)<br> slider.setPageStep(15 * 16)<br>
slider.setTickInterval(15 * 16)<br> slider.setTickPosition(QtGui.QSlider.TicksRight)<br><br> self.glWidget.connect(slider, QtCore.SIGNAL("valueChanged(int)"), setterSlot)<br> self.connect(self.glWidget, changedSignal, slider, QtCore.SLOT("setValue(int)"))<br>
<br> return slider<br><br><br>class GLWidget(QtOpenGL.QGLWidget):<br> def __init__(self, parent=None):<br> QtOpenGL.QGLWidget.__init__(self, parent)<br><br> self.object = 0<br> self.xRot = 0<br> self.yRot = 0<br>
self.zRot = 0<br><br> self.lastPos = QtCore.QPoint()<br><br> self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0)<br> self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)<br>
<br> def xRotation(self):<br> return self.xRot<br><br> def yRotation(self):<br> return self.yRot<br><br> def zRotation(self):<br> return self.zRot<br><br> def minimumSizeHint(self):<br> return QtCore.QSize(50, 50)<br>
<br> def sizeHint(self):<br> return QtCore.QSize(400, 400)<br><br> def setXRotation(self, angle):<br> angle = self.normalizeAngle(angle)<br> if angle != self.xRot:<br> self.xRot = angle<br> self.emit(QtCore.SIGNAL("xRotationChanged(int)"), angle)<br>
self.updateGL()<br><br> def setYRotation(self, angle):<br> angle = self.normalizeAngle(angle)<br> if angle != self.yRot:<br> self.yRot = angle<br> self.emit(QtCore.SIGNAL("yRotationChanged(int)"), angle)<br>
self.updateGL()<br><br> def setZRotation(self, angle):<br> angle = self.normalizeAngle(angle)<br> if angle != self.zRot:<br> self.zRot = angle<br> self.emit(QtCore.SIGNAL("zRotationChanged(int)"), angle)<br>
self.updateGL()<br><br> def initializeGL(self):<br> self.qglClearColor(self.trolltechPurple.dark())<br> self.object = self.makeObject()<br> GL.glShadeModel(GL.GL_FLAT)<br> GL.glEnable(GL.GL_DEPTH_TEST)<br>
GL.glEnable(GL.GL_CULL_FACE)<br><br> def paintGL(self):<br> GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)<br> GL.glLoadIdentity()<br> GL.glTranslated(0.0, 0.0, -10.0)<br> GL.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)<br>
GL.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)<br> GL.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)<br> GL.glCallList(self.object)<br><br> def resizeGL(self, width, height):<br> side = min(width, height)<br>
GL.glViewport((width - side) / 2, (height - side) / 2, side, side)<br><br> GL.glMatrixMode(GL.GL_PROJECTION)<br> GL.glLoadIdentity()<br> GL.glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0)<br> GL.glMatrixMode(GL.GL_MODELVIEW)<br>
<br> def mousePressEvent(self, event):<br> self.lastPos = QtCore.QPoint(event.pos())<br><br> def mouseMoveEvent(self, event):<br> dx = event.x() - self.lastPos.x()<br> dy = event.y() - self.lastPos.y()<br>
<br> if event.buttons() & QtCore.Qt.LeftButton:<br> self.setXRotation(self.xRot + 8 * dy)<br> self.setYRotation(self.yRot + 8 * dx)<br> elif event.buttons() & QtCore.Qt.RightButton:<br>
self.setXRotation(self.xRot + 8 * dy)<br> self.setZRotation(self.zRot + 8 * dx)<br><br> self.lastPos = QtCore.QPoint(event.pos())<br><br> def makeObject(self):<br> genList = GL.glGenLists(1)<br>
GL.glNewList(genList, GL.GL_COMPILE)<br><br> GL.glBegin(GL.GL_QUADS)<br><br> x1 = +0.06<br> y1 = +0.06<br> z1 = -0.54<br><br> x2 = +0.14<br> y2 = +0.06<br> z2 = -0.54<br><br> x3 = +0.08<br>
y3 = +0.00<br> z3 = -0.54<br><br> self.triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3,255,0,0)<br><br><br> GL.glBegin(GL.GL_QUADS)<br> x1 = +0.06<br> y1 = +0.06<br> z1 = 0.54<br>
x2 = +0.14<br> y2 = +0.06<br> z2 = 0.54<br> x3 = +0.08<br> y3 = +0.00<br> z3 = 0.54<br> self.triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3,0,0,255)<br> GL.glEnd()<br>
GL.glEndList()<br><br> return genList<br><br> def triangle(self, x1, y1, z1, x2, y2, z2, x3, y3, z3,r,g,b):<br> self.qglColor(QtGui.QColor(r,g,b))<br><br> GL.glVertex3d(x1, y1, z1)<br> GL.glVertex3d(x2, y2, z2)<br>
GL.glVertex3d(x3, y3, z3)<br><br><br> def extrude(self, x1, y1, x2, y2):<br> self.qglColor(self.trolltechGreen.dark(250 + int(100 * x1)))<br><br> GL.glVertex3d(x1, y1, +0.05)<br> GL.glVertex3d(x2, y2, +0.05)<br>
GL.glVertex3d(x2, y2, -0.05)<br> GL.glVertex3d(x1, y1, -0.05)<br><br> def normalizeAngle(self, angle):<br> while angle < 0:<br> angle += 360 * 16<br> while angle > 360 * 16:<br> angle -= 360 * 16<br>
return angle<br><br><br>if __name__ == '__main__':<br> app = QtGui.QApplication(sys.argv)<br> window = Window()<br> window.show()<br> sys.exit(app.exec_())<br><br><br><br>_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br><a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><br>