class MainView(ZApplicationView):
    # Inner classes
    #--------------------------------------------
    class ColorBox(QWidget):
        def __init__(self, parent):
            QWidget.__init__(self, parent)
            self.color = QColor(255, 128, 0)

        def paintEvent(self, event):
            painter = QPainter(self)
            painter.setBrush(self.color)
            painter.drawRect(20, 20, 200, 200)

        def update_color(self, r, g, b):
            self.color = QColor(r, g, b)
            self.update()

    # MainView Constructor
    def __init__(self, title, x, y, width, height):
        super(MainView, self).__init__(title, x, y, width, height)

        # 1 Create first imageview.
        self.colorbox = self.ColorBox(self)

        # 2 Add imageviews to the main_layout which is a horizontal layouter.
        self.add(self.colorbox)

        self.show()

    def add_control_pane(self, fixed_width=280):
        # Control pane widget
        print("add_control_pane")
        self.vpane = ZVerticalPane(self, fixed_width)
        self.positioner = ZColorPositioner(self)
        self.positioner.set_rgb_colors(255, 128, 0)

        self.vpane.add(self.positioner)

        self.positioner.add_value_changed_callback(self.slider_value_changed)

        self.set_right_dock(self.vpane)

    def slider_value_changed(self, value):
        values = self.positioner.get_values()
        self.colorbox.update_color(values[0], values[1], values[2])
class MainView(ZOpenGLMainView):
    ##--------------------------------------------
    class OpenGLView(ZOpenGLView):
        def __init__(self, parent, color, eye, light):
            #self.parent = parent
            super(ZOpenGLView, self).__init__(parent)
            self.angle = 0
            self.vcolor = color
            self.veye = eye
            self.vlight = light

        def initializeGL(self):
            glEnable(GL_DEPTH_TEST)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            glMatrixMode(GL_MODELVIEW)

            self.light = ZOpenGLLight()
            self.material = ZOpenGLMaterial(GL_FRONT)
            self.geometry = ZOpenGLGeometry(None)

        def paintGL(self):
            glFrustum(1, -1, -1, 1, 1, 10)

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            glLoadIdentity()
            glMatrixMode(GL_MODELVIEW)
            glClearColor(1.0, 1.0, 1.0, 1.0)
            glTranslate(-0.5, -0.5, 0.0)

            gluLookAt(self.veye[0], self.veye[1], self.veye[2], 0.0, 0.0, 0.0,
                      0.0, 1.0, 0.0)

            glRotate(0.0, 0.0, 1.0, 0.0)

            glEnable(GL_LIGHTING)
            self.light.position(self.vlight[0], self.vlight[1], self.vlight[2],
                                0.0)

            self.material.diffuse(self.vcolor[0] / 255.0,
                                  self.vcolor[1] / 255.0,
                                  self.vcolor[2] / 255.0, 0.0)

            self.material.specular(1.0, 1.0, 1.0, 0.0)
            self.material.shininess(100.0)

            glFrontFace(GL_CCW)

            glEnable(GL_CULL_FACE)
            glCullFace(GL_BACK)
            glEnable(GL_NORMALIZE)

            glPushMatrix()
            self.geometry.solidTeapot(2.0)
            glPopMatrix()

            glFlush()

        def resizeGL(self, width, height):
            side = min(width, height)
            if side < 0: return
            glViewport(0, 0, width, height)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            gluPerspective(16.0, width / height, 0.5, 40.0)

            glMatrixMode(GL_MODELVIEW)

        def setColorPosition(self, color):
            self.vcolor = color
            self.update()

        def setEyePosition(self, eye):
            self.veye = eye
            self.update()

        def setLigtPosition(self, light):
            self.vlight = light
            self.update()

    ##--------------------------------------------

    # class variables
    COLOR_RANGE = [0, 255]
    COLOR_POS = [0.0, 0.0, 255.0]
    EYE_RANGE = [-40, 40]
    EYE_POS = [2.0, 0.0, 30.0]
    LIGHT_RANGE = [-40, 40]
    LIGHT_POS = [30.0, -10.0, -30.0]

    # MainView Constructor
    def __init__(self, title, x, y, width, height):
        super(ZOpenGLMainView, self).__init__(title, x, y, width, height)
        # 1 Create first imageview.

        self.opengl_view = self.OpenGLView(self, self.COLOR_POS, self.EYE_POS,
                                           self.LIGHT_POS)

        # 2 Add the image view to a main_layout of this main view.
        self.add(self.opengl_view)

        # 4 Add callbacks to positioners.
        self.colorPositioner.add_value_changed_callback(self.color_changed)
        self.eyePositioner.add_value_changed_callback(self.eye_changed)
        self.lightPositioner.add_value_changed_callback(self.light_changed)

        self.show()

    # Add control pane to MainView
    def add_control_pane(self, fixed_width=270):
        self.vpane = ZVerticalPane(self, fixed_width)

        self.colorPositioner = ZColorPositioner(self, "ColorPositioner",
                                                ["R", "G", "B"],
                                                self.COLOR_RANGE,
                                                self.COLOR_POS)

        self.eyePositioner = ZEyePositioner(self, "EyePositioner",
                                            ["X", "Y", "Z"], self.EYE_RANGE,
                                            self.EYE_POS)

        self.lightPositioner = ZLightPositioner(self, "LightPositioner",
                                                ["X", "Y", "Z"],
                                                self.LIGHT_RANGE,
                                                self.LIGHT_POS)

        self.vpane.add(self.colorPositioner)
        self.vpane.add(self.eyePositioner)
        self.vpane.add(self.lightPositioner)

        self.set_right_dock(self.vpane)

    def color_changed(self, v):
        values = self.colorPositioner.get_values()
        self.opengl_view.setColorPosition(values)

    def eye_changed(self, v):
        values = self.eyePositioner.get_values()
        self.opengl_view.setEyePosition(values)

    def light_changed(self, v):
        values = self.lightPositioner.get_values()
        self.opengl_view.setLigtPosition(values)
Beispiel #3
0
class MainView(ZOpenGLMainView):
    ##--------------------------------------------
    class OpenGLView(ZOpenGLView):
        def __init__(self, parent, color, eye, light):
            #self.parent = parent
            super(ZOpenGLView, self).__init__(parent)
            self.angle = 0
            self.vcolor = color
            self.veye = eye
            self.vlight = light

        def initializeGL(self):
            glEnable(GL_DEPTH_TEST)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            glMatrixMode(GL_MODELVIEW)

            self.light = ZOpenGLLight()
            self.material = ZOpenGLMaterial(GL_FRONT)

            self.vertices = [
                [-1.0, -1.0, 1.0],
                [1.0, -1.0, 1.0],
                [1.0, -1.0, -1.0],
                [-1.0, -1.0, -1.0],
                [0.0, 1.0, 0.0],
            ]

            self.indices = [
                [0, 1, 4],  #Triangle: face1
                [1, 2, 4],  #Triangle: face2
                [2, 3, 4],  #Triangle: face3
                [3, 0, 4],  #Triangle] face4
            ]

            self.surfaces = ZOpenGLTriangleSurfaces(self.vertices,
                                                    self.indices)
            self.normals = self.surfaces.calculateSurfaceNormals()

        def paintGL(self):
            glFrustum(1, -1, -1, 1, 1, 10)

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            glLoadIdentity()
            glMatrixMode(GL_MODELVIEW)
            glClearColor(1.0, 1.0, 1.0, 1.0)
            glTranslate(-0.5, -0.5, 0.0)

            gluLookAt(self.veye[0], self.veye[1], self.veye[2], 0.0, 0.0, 0.0,
                      0.0, 1.0, 0.0)

            glRotate(0.0, 0.0, 1.0, 0.0)

            #glTranslate(0.0, 0.0, -1.0)

            #glEnableClientState(GL_VERTEX_ARRAY)
            #glEnableClientState(GL_COLOR_ARRAY)

            #avertices =  np.array(self.vertices, dtype="float32")

            #glVertexPointer(3, GL_FLOAT, 0, self.avertices)
            #nindices     =  np.array(self.indices, dtype="uint32")

            glEnable(GL_LIGHTING)
            self.light.position(self.vlight[0], self.vlight[1], self.vlight[2],
                                0.0)
            self.material.diffuse(self.vcolor[0] / 255.0,
                                  self.vcolor[1] / 255.0,
                                  self.vcolor[2] / 255.0, 0.0)

            self.material.specular(1.0, 1.0, 1.0, 0.0)
            self.material.shininess(100.0)

            glFrontFace(GL_CCW)

            glEnable(GL_CULL_FACE)
            glCullFace(GL_BACK)
            glEnable(GL_NORMALIZE)
            for i in range(len(self.indices)):
                glBegin(GL_TRIANGLES)
                tri = self.indices[i]
                self.draw(self.normals[i], self.vertices[tri[0]],
                          self.vertices[tri[1]], self.vertices[tri[2]])
                glEnd()
                glFlush()

            #glDisableClientState(GL_VERTEX_ARRAY)
            #glDisableClientState(GL_COLOR_ARRAY)

            glFlush()

        def resizeGL(self, width, height):
            side = min(width, height)
            if side < 0: return
            glViewport(0, 0, width, height)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            gluPerspective(16.0, width / height, 0.5, 40.0)

            glMatrixMode(GL_MODELVIEW)

        def draw(self, n, v1, v2, v3):
            glNormal3fv(n)
            glVertex3fv(v1)
            glVertex3fv(v2)
            glVertex3fv(v3)

        def setColorPosition(self, color):
            self.vcolor = color
            self.update()

        def setEyePosition(self, eye):
            self.veye = eye
            self.update()

        def setLigtPosition(self, light):
            self.vlight = light
            self.update()

    ##--------------------------------------------

    # class variables
    COLOR_RANGE = [0, 255]
    COLOR_POS = [0.0, 0.0, 255.0]
    EYE_RANGE = [-40, 40]
    EYE_POS = [2.0, 0.0, 30.0]
    LIGHT_RANGE = [-40, 40]
    LIGHT_POS = [30.0, -10.0, -30.0]

    # MainView Constructor
    def __init__(self, title, x, y, width, height):
        super(ZOpenGLMainView, self).__init__(title, x, y, width, height)
        # 1 Create first imageview.

        self.opengl_view = self.OpenGLView(self, self.COLOR_POS, self.EYE_POS,
                                           self.LIGHT_POS)

        # 2 Add the image view to a main_layout of this main view.
        self.add(self.opengl_view)

        # 4 Add callbacks to positioners.
        self.colorPositioner.add_value_changed_callback(self.color_changed)
        self.eyePositioner.add_value_changed_callback(self.eye_changed)
        self.lightPositioner.add_value_changed_callback(self.light_changed)

        self.show()

    # Add control pane to MainView
    def add_control_pane(self, fixed_width=270):
        self.vpane = ZVerticalPane(self, fixed_width)

        self.colorPositioner = ZColorPositioner(self, "ColorPositioner",
                                                ["R", "G", "B"],
                                                self.COLOR_RANGE,
                                                self.COLOR_POS)

        self.eyePositioner = ZEyePositioner(self, "EyePositioner",
                                            ["X", "Y", "Z"], self.EYE_RANGE,
                                            self.EYE_POS)

        self.lightPositioner = ZLightPositioner(self, "LightPositioner",
                                                ["X", "Y", "Z"],
                                                self.LIGHT_RANGE,
                                                self.LIGHT_POS)

        self.vpane.add(self.colorPositioner)
        self.vpane.add(self.eyePositioner)
        self.vpane.add(self.lightPositioner)

        self.set_right_dock(self.vpane)

    def color_changed(self, v):
        values = self.colorPositioner.get_values()
        self.opengl_view.setColorPosition(values)

    def eye_changed(self, v):
        values = self.eyePositioner.get_values()
        self.opengl_view.setEyePosition(values)

    def light_changed(self, v):
        values = self.lightPositioner.get_values()
        self.opengl_view.setLigtPosition(values)