PyVEFViewer.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Sat, 05 Mar 2011 23:06:27 -0800
changeset 6 6ce97bd3e70f
parent 5 b7757ccad2f4
child 9 a500b28a675d
permissions -rw-r--r--
Added color icons

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

from    points          import Points, centerMinMax, reduceCMM
from    edges           import Edges
from    triangles       import Triangles


class VEFViewer(QGLViewer):
    def __init__(self, parent = None):
        super(VEFViewer, self).__init__(parent)
        self.setStateFileName('.PyVEFViewer.xml')

    def models(self):
        for i in xrange(self.model_list.count()):
            yield self.model_list.item(i)

    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 modelsContextMenu(self, position):
        item = self.model_list.itemAt(position)
        if item:
            item.contextMenu(self.model_list.mapToGlobal(position))
        self.updateGL()

    def modelDoubleClicked(self, item):
        item.toggleVisible()
        self.updateGL()

    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.model_list.addItem(Points(pts, self.model_list))

    def read_edges(self, edges):
        for edg in edges:
            self.model_list.addItem(Edges(edg, self.model_list))

    def read_triangles(self, triangles):
        for tri in triangles:
            self.model_list.addItem(Triangles(tri, self.model_list))

    def set_list(self, model_list):
        self.model_list = model_list
        self.model_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.model_list.customContextMenuRequested.connect(self.modelsContextMenu)
        self.model_list.itemDoubleClicked.connect(self.modelDoubleClicked)

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

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