Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
examples [2007/06/08 14:08]
admin
examples [2007/10/24 11:29] (current)
admin
Line 14: Line 14:
 ==== qgllogo.py ==== ==== qgllogo.py ====
  
-<​code>​+<​code ​python>
 import OpenGL.GL as ogl import OpenGL.GL as ogl
 import math import math
Line 40: Line 40:
 ==== simpleViewer.py ==== ==== simpleViewer.py ====
  
-<​code>​+<​code ​python>
 from PyQt4.QtGui import * from PyQt4.QtGui import *
 from PyQGLViewer import * from PyQGLViewer import *
Line 85: Line 85:
 </​code>​ </​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.1181304530.txt.gz ยท Last modified: 2007/06/08 14:08 by admin
Valid CSS Driven by DokuWiki Valid XHTML 1.0