PyVEFViewer.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu, 03 Mar 2011 12:58:47 -0800
changeset 0 f591c821bbc4
child 1 da08d9c69f4b
permissions -rwxr-xr-x
Initial commit

#!/usr/bin/env python2

from    PyQt4.QtGui     import *
from    PyQGLViewer     import *
import  OpenGL.GL as ogl

from    points          import Points, centerMinMax, reduceCMM
from    opster          import command, dispatch


class VEFViewer(QGLViewer):
    def __init__(self):
        QGLViewer.__init__(self)
        self.setStateFileName('.PyVEFViewer.xml')
        self.models = []

    def draw(self):
        self.lights()
        for mod in self.models:
            mod.draw()

    def drawWithNames(self):
        for i,mod in enumerate(self.models):
            mod.drawWithNames(i)

    def lights(self):
        # GL_LIGHT0
        ogl.glLightfv(ogl.GL_LIGHT0, ogl.GL_POSITION, (self.center.x, self.center.y, self.center.z, 1.0));

        # GL_LIGHT1
        camera_pos = self.camera().position();
        camera_dir = self.camera().viewDirection();
        light_pos1 = (camera_pos.x, camera_pos.y, camera_pos.z, 1.0);
        light_spot_dir1 = (camera_dir.x, camera_dir.y, camera_dir.z);
        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_POSITION, light_pos1);


    def init(self):
        # ogl.glMaterialf(ogl.GL_FRONT_AND_BACK, ogl.GL_SHININESS, 50.0)
        # specular_color = [ 0.8, 0.8, 0.8, 1.0 ]
        # ogl.glMaterialfv(ogl.GL_FRONT_AND_BACK, ogl.GL_SPECULAR,  specular_color)
        self.restoreStateFromFile()
        # self.help()

        ogl.glShadeModel(ogl.GL_SMOOTH);
        ogl.glEnable(ogl.GL_COLOR_MATERIAL);
        ogl.glEnable(ogl.GL_NORMALIZE);
        ogl.glEnable(ogl.GL_LINE_SMOOTH);
        ogl.glEnable(ogl.GL_POINT_SMOOTH);
        ogl.glLightModeli(ogl.GL_LIGHT_MODEL_TWO_SIDE, ogl.GL_TRUE);
        ogl.glPointSize(2.0);
        ogl.glEnable(ogl.GL_CULL_FACE);


    #def helpString(self):
    #    return helpstr

    def read_points(self, points):
        for pts in points:
            self.models.append(Points(pts))

    def normalize_view(self):
        self.centerScene()
        self.setSceneBoundingBox(Vec(self.min.x, self.min.y, self.min.z), \
                                 Vec(self.max.x, self.max.y, self.max.z))

    def centerScene(self):
        self.center, self.min, self.max = reduceCMM((mod.center, mod.min, mod.max) for mod in self.models)

@command(usage = '%name [options]')
def main(points     = ('p', [], 'files with points'),       # TODO: add completer for filenames
         edges      = ('e', [], 'files with edges'),
         triangles  = ('t', [], 'files with triangles')):
    qapp = QApplication([])
    viewer = VEFViewer()
    viewer.setWindowTitle("PyVEFViewer")
    viewer.show()

    viewer.read_points(points)
    #viewer.read_edges(edges)
    #viewer.read_triangles(triangles)
    viewer.normalize_view()

    qapp.exec_()

if __name__ == '__main__':
    main()