Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
examples [2007/05/04 11:52]
admin created
examples [2007/10/24 11:29] (current)
admin
Line 1: Line 1:
 ====== Examples ====== ====== Examples ======
 +
 +The project comes with a set of examples showing the different possibility of QGLViewer.
 +These examples are simply the translation of [[http://​artis.imag.fr/​~Gilles.Debunne/​QGLViewer/​examples/​index.html|QGLViewer examples]] into python.
 +
 +===== Simple example =====
 +
 +The simplest application example.
 +
 +A Viewer class is derived from QGLViewer and its draw() function is overloaded to specify the user defined OpenGL commands that describe the scene.
 +
 +This example can be cut and pasted to start the development of a new application. ​
 +
 +==== qgllogo.py ====
 +
 +<code python>
 +import OpenGL.GL as ogl
 +import math
 +
 +def draw_qgl_logo(nbSteps = 200.0):
 +    ogl.glBegin(ogl.GL_QUAD_STRIP);​
 +    for i in range(0,​int(nbSteps)):​
 +        ratio = i/​float(nbSteps)
 +        angle = 21.0*ratio
 +        c = math.cos(angle)
 +        s = math.sin(angle)
 +        r1 = 1.0 - 0.8*ratio
 +        r2 = 0.8 - 0.8*ratio
 +        alt = ratio - 0.5
 +        nor = 0.5
 +        up = math.sqrt(1.0-nor*nor)
 +        ogl.glColor3f(1.0-ratio,​ 0.2 , ratio)
 +        ogl.glNormal3f(nor*c,​ up, nor*s)
 +        ogl.glVertex3f(r1*c,​ alt, r1*s)
 +        ogl.glVertex3f(r2*c,​ alt+0.05, r2*s)
 +    ogl.glEnd()
 +</​code>​
 +
 +
 +==== simpleViewer.py ====
 +
 +<code python>
 +from PyQt4.QtGui import *
 +from PyQGLViewer import *
 +from qgllogo import draw_qgl_logo
 +
 +helpstr = """<​h2>​S i m p l e V i e w e r</​h2>​
 +Use the mouse to move the camera around the object. ​
 +You can respectively revolve around, zoom and translate with the three mouse buttons. ​
 +Left and middle buttons pressed together rotate around the camera view direction axis<​br><​br>​
 +Pressing <​b>​Alt</​b>​ and one of the function keys (<​b>​F1</​b>​..<​b>​F12</​b>​) defines a camera keyFrame. ​
 +Simply press the function key again to restore it. Several keyFrames define a 
 +camera path. Paths are saved when you quit the application and restored at next start.<​br><​br>​
 +Press <​b>​F</​b>​ to display the frame rate, <​b>​A</​b>​ for the world axis, 
 +<​b>​Alt+Return</​b>​ for full screen mode and <​b>​Control+S</​b>​ to save a snapshot. ​
 +See the <​b>​Keyboard</​b>​ tab in this window for a complete shortcut list.<​br><​br>​
 +Double clicks automates single click actions: A left button double click aligns ​
 +the closer axis with the camera (if close enough). A middle button double click 
 +fits the zoom of the camera and the right button re-centers the scene.<​br><​br>​
 +A left button double click while holding right button pressed defines the camera ​
 +<​i>​Revolve Around Point</​i>​.
 +See the <​b>​Mouse</​b>​ tab and the documentation web pages for details.<​br><​br>​
 +Press <​b>​Escape</​b>​ to exit the viewer."""​
 +
 +class Viewer(QGLViewer):​
 +    def __init__(self,​parent = None):
 +        QGLViewer.__init__(self,​parent)
 +    def draw(self):
 +        draw_qgl_logo()
 +    def init(self):
 +        self.restoreStateFromFile()
 +        self.help()
 +    def helpString(self):​
 +        return helpstr
 +
 +def main():
 +    qapp = QApplication([])
 +    viewer = Viewer()
 +    viewer.setWindowTitle("​simpleViewer"​)
 +    viewer.show()
 +    qapp.exec_()
 +
 +if __name__ == '​__main__':​
 +    main()
 +</​code>​
 +
 +==== Result ====
 +
 +{{simpleviewer.png|}}
 +
 +
 +===== Texture 3D example =====
 +
 +A simple example with texture 3D.
 +
 +In the ''​init''​ method, four small Qimages are generated and filled each with an unique color. The 3D texture is then binded.
 +
 +In the ''​draw''​ method, a dynamic pyramid is drawn using texture 3D.
 +
 +The ''​animate''​ method updates the coordinates of the pyramid.
 +
 +
 +
 +
 +==== texture3D.py ====
 +
 +<code python>
 +from PyQt4.QtGui import *
 +from PyQGLViewer import *
 +import OpenGL.GL as ogl
 +import math
 +
 +helpstr = """<​h2>​T e x t u r e 3 D</​h2>​
 +An example to show how to build a 3D texture and apply it on a dynamic pyramid."""​
 +
 +class Viewer(QGLViewer):​
 +    def __init__(self,​parent = None):
 +        QGLViewer.__init__(self,​parent)
 +        self.center = [ 0.0,0.0,1.0 ]
 +        self.base = [[ -1.0, -1.0, 0.0 ], [-1.0, 1.0, 0.0], [1.0, 1.0, 0.0], [1.0, -1.0, 0.0]]
 +        self.time = 0.
 +    def animate(self):​
 +        self.center[2] = math.sin(self.time / 15.0) * 2.0
 +        self.time += 1
 +    def draw(self):
 +        if self.texname > 0:
 +            ogl.glBindTexture(ogl.GL_TEXTURE_3D,​ self.texname)
 +        ogl.glBegin(ogl.GL_TRIANGLES)
 +        # texture coordinates are always specified before the vertex they apply to.
 +        for x in range(4):
 +            ogl.glTexCoord3dv(self.center)
 +            ogl.glVertex3dv(self.center)
 +            ​
 +            ogl.glTexCoord3dv(self.base[x])
 +            ogl.glVertex3dv(self.base[x])
 +            ​
 +            ogl.glTexCoord3dv(self.base[(x+1)%4])
 +            ogl.glVertex3dv(self.base[(x+1)%4])
 +        ogl.glEnd()
 +    def init(self):
 +        self.restoreStateFromFile()
 +        self.help()
 +        imgs = [QImage(4,​4,​QImage.Format_ARGB32) for i in range(4)]
 +        imgs[0].fill(qRgb(128,​0,​0))
 +        imgs[1].fill(qRgb(0,​128,​0))
 +        imgs[2].fill(qRgb(0,​0,​128))
 +        imgs[3].fill(qRgb(128,​128,​128))
 +        self.texname = self.bindTexture3D(imgs)
 +        self.setSceneRadius(1)
 +        self.startAnimation()
 +    def helpString(self):​
 +        return helpstr
 +    def closeEvent(self,​event):​
 +        if self.texname > 0:
 +            ogl.glDeleteTextures(self.texname)
 +        QGLViewer.closeEvent(self,​event)
 +
 +def main():
 +    qapp = QApplication([])
 +    viewer = Viewer()
 +    viewer.setWindowTitle("​texture3D"​)
 +    viewer.show()
 +    qapp.exec_()
 +
 +if __name__ == '​__main__':​
 +    main()
 +</​code>​
 +
 +==== Result ====
 +
 +{{texture3d.jpg?​300|}}
  
 
Back to top
examples.1178272353.txt.gz ยท Last modified: 2007/05/04 11:52 by admin
Valid CSS Driven by DokuWiki Valid XHTML 1.0