Example #1
0
 def Render( self, mode = None ):
     glMatrixMode(GL_MODELVIEW)
     glLoadIdentity()
     glMatrixMode(GL_PROJECTION);
     # load the identity matrix (reset the view)
     # calculate a 3D perspective view
     print '___________________________'
     from OpenGL.GL import glFrustum
     for near in (.2,1.,3.,4.,5.,19.):
         for far in arange(20.0,2**31,1000):
             try:
                 glLoadIdentity()
                 glFrustum( -20,20, -20,20, near, far )
                 f = frustum.Frustum.fromViewingMatrix()
                 farCurrent,nearCurrent = f.planes[-2:]
                 farCurrent = round(farCurrent[-1],4)
                 nearCurrent = round(nearCurrent[-1],4)
                 deltaFar = abs(farCurrent-far)
                 deltaNear = abs( nearCurrent+near)
                 
                 assert deltaFar < abs(far/100), "Far was %s, should have been ~ %s, delta was %s (%.3f%%)"%(farCurrent,far, deltaFar, 100*deltaFar/far)
                 assert deltaNear < abs(near/100), "Near was %s, should have been ~ %s, delta was %s"%(nearCurrent,near,deltaNear)
             except AssertionError, err:
                 log.warn(
                     "Accuracy < than 1%% of far with (near,far) = (%s,%s)",
                     near,far,
                 )
                 break
             except Exception, err:
                 traceback.print_exc()
                 self.OnQuit( )
Example #2
0
 def Render( self, mode = None ):
     glMatrixMode(GL_MODELVIEW)
     glLoadIdentity()
     glMatrixMode(GL_PROJECTION);
     # load the identity matrix (reset the view)
     # calculate a 3D perspective view
     print('___________________________')
     from OpenGL.GL import glFrustum
     for near in (.2,1.,3.,4.,5.,19.):
         for far in arange(20.0,2**31,1000):
             try:
                 glLoadIdentity()
                 glFrustum( -20,20, -20,20, near, far )
                 f = frustum.Frustum.fromViewingMatrix()
                 farCurrent,nearCurrent = f.planes[-2:]
                 farCurrent = round(farCurrent[-1],4)
                 nearCurrent = round(nearCurrent[-1],4)
                 deltaFar = abs(farCurrent-far)
                 deltaNear = abs( nearCurrent+near)
                 
                 assert deltaFar < abs(far/100), "Far was %s, should have been ~ %s, delta was %s (%.3f%%)"%(farCurrent,far, deltaFar, 100*deltaFar/far)
                 assert deltaNear < abs(near/100), "Near was %s, should have been ~ %s, delta was %s"%(nearCurrent,near,deltaNear)
             except AssertionError as err:
                 log.warn(
                     "Accuracy < than 1%% of far with (near,far) = (%s,%s)",
                     near,far,
                 )
                 break
             except Exception as err:
                 traceback.print_exc()
                 self.OnQuit( )
     glMatrixMode(GL_MODELVIEW);
     self.OnQuit()
Example #3
0
    def init_opengl(self, width, height, x, y):
        glutInit()
        glutInitWindowPosition(x, y)
        glutInitWindowSize(width, height)
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE)
        glutCreateWindow("Rainbow Alga")
        glutDisplayFunc(self.render)
        glutIdleFunc(self.render)
        glutReshapeFunc(self.resize)

        glutMouseFunc(self.mouse)
        glutMotionFunc(self.drag)
        glutKeyboardFunc(self.keyboard)
        glutSpecialFunc(self.special_keyboard)

        glClearDepth(1.0)
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 3000)
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)


        # Lighting
        light_ambient = (0.0, 0.0, 0.0, 1.0)
        light_diffuse = (1.0, 1.0, 1.0, 1.0)
        light_specular = (1.0, 1.0, 1.0, 1.0)
        light_position = (-100.0, 100.0, 100.0, 0.0)

        mat_ambient = (0.7, 0.7, 0.7, 1.0)
        mat_diffuse = (0.8, 0.8, 0.8, 1.0)
        mat_specular = (1.0, 1.0, 1.0, 1.0)
        high_shininess = (100)

        glEnable(GL_LIGHT0)
        glEnable(GL_NORMALIZE)
        glEnable(GL_COLOR_MATERIAL)
        glEnable(GL_LIGHTING)

        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
        glLightfv(GL_LIGHT0, GL_SPECULAR,  light_specular)
        glLightfv(GL_LIGHT0, GL_POSITION, light_position)

        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient)
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse)
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular)
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess)

        # Transparency
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Example #4
0
 def __resize(self, widget, event):
     gldrawable = widget.get_gl_drawable()
     glcontext = widget.get_gl_context()
     # OpenGL begin.
     if not gldrawable.gl_begin(glcontext):
         return
     width = widget.allocation.width
     height = widget.allocation.height
     glViewport (0, 0, width, height)
     glMatrixMode(GL_PROJECTION)
     glLoadIdentity()
     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 300.0)
     glMatrixMode (GL_MODELVIEW)
     gldrawable.gl_end()
     # OpenGL end
     return
Example #5
0
    def on_draw(self, transform, want_camera=False):

        d = self.get_dimensions()

        glViewport(int(d['subwindow_origin_x']), int(d['subwindow_origin_y']),
                   int(d['subwindow_width']), int(d['subwindow_height']))

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        fov_degrees = 45.
        near = 1.0
        far = 100.
        ratio = float(d['subwindow_width']) / float(d['subwindow_height'])
        if d['subwindow_width'] < d['subwindow_height']:
            xt = np.tan(fov_degrees * np.pi / 180. / 2.0) * near
            yt = xt / ratio
            glFrustum(-xt, xt, -yt, yt, near, far)
        else:
            gluPerspective(fov_degrees, ratio, near, far)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)

        glTranslatef(0.0, 0.0, -6.0)
        #        glTranslatef(0.0,0.0,-3.5)

        glPushMatrix()
        glMultMatrixf(transform)
        glColor3f(1.0, 0.75, 0.75)

        if self.autorecenter:
            camera = self.draw_primitives_recentered(want_camera=want_camera)
        else:
            if hasattr(self, 'current_center') and hasattr(
                    self, 'current_scalefactor'):
                camera = self.draw_primitives(
                    scalefactor=self.current_scalefactor,
                    center=self.current_center)
            else:
                camera = self.draw_primitives(want_camera=want_camera)

        glPopMatrix()

        if want_camera:
            return camera
Example #6
0
    def on_resize(self, width, height):
        '''Event called when the window is resized'''
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-width / 2, width / 2, -height / 2, height / 2, .1, 1000)
        glScalef(5000, 5000, 1)
        glTranslatef(-width / 2, -height / 2, -500)
        glMatrixMode(GL_MODELVIEW)

        for w in self.children:
            shw, shh = w.size_hint
            if shw and shh:
                w.size = shw * width, shh * height
            elif shw:
                w.width = shw * width
            elif shh:
                w.height = shh * height
Example #7
0
    def _setup_projection(
        self,
        glselect=False
    ):  #bruce 050608 split this out; 050615 revised docstring
        """
        Set up standard projection matrix contents using aspect, vdist, and 
        some attributes of self.
        
        @warning: leaves matrixmode as GL_PROJECTION.
                  Optional arg glselect should be False (default) or a 4-tuple
                  (to prepare for GL_SELECT picking).
        """
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        scale = self.scale  #bruce 050608 used this to clarify following code
        near, far = self.near, self.far

        #bruce 080219 moved these from one of two callers into here,
        # to fix bug when insert from partlib is first operation in NE1
        self.aspect = (self.width + 0.0) / (self.height + 0.0)
        self.vdist = 6.0 * scale

        if glselect:
            x, y, w, h = glselect
            gluPickMatrix(
                x,
                y,
                w,
                h,
                glGetIntegerv(
                    GL_VIEWPORT
                )  #k is this arg needed? it might be the default...
            )

        if self.ortho:
            glOrtho(-scale * self.aspect, scale * self.aspect, -scale, scale,
                    self.vdist * near, self.vdist * far)
        else:
            glFrustum(-scale * near * self.aspect, scale * near * self.aspect,
                      -scale * near, scale * near, self.vdist * near,
                      self.vdist * far)
        return
Example #8
0
    def openglSetup(self):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glEnable(GL.GL_DEPTH_TEST) 
        glEnable(GL.GL_COLOR_MATERIAL)

        glViewport (0, 0, self.surf.get_width(), self.surf.get_height())
        glMatrixMode (GL.GL_PROJECTION)
        glLoadIdentity ()
        glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
        glMatrixMode (GL.GL_MODELVIEW)

        self.cardList = glGenLists(1)
        glNewList(self.cardList, GL.GL_COMPILE)
        glColor3f(1,1,1)
        with begin(GL.GL_QUADS):
            glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0,  0.0)
            glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, -1.0,  0.0)
            glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0,  0.0)
            glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0,  0.0)
        glEndList()
Example #9
0
    def ApplyProjection(self, ortho =False, asp =1.0):
        """
        プロジェクション行列のOpenGL適用
        - ortho: 平行投影モード
        - asp: 視界のアスペクト比率(横/縦)
        """
        try:
            from OpenGL.GL import glFrustum, glOrtho
        except:
            return False

        if self._far - self._near < EPSF: return False
        if not ortho and self._near < EPSF: return False
        if self._halfW < EPSF or self._halfH < EPSF: return False

        eoff = Vec3(self._eyeOff)
        if self._eyeType == LEFT_EYE: eoff.m_v[0] -= Frustum.__strOff
        elif self._eyeType == RIGHT_EYE: eoff.m_v[0] += Frustum.__strOff

        aspect = asp
        if aspect < EPSF: aspect = 1.0
        wBias = aspect * self._halfH / self._halfW

        (left, right, top, bottom) = (0,0,0,0)
        try:
            if not ortho:
                d = self._near / (self._dist + eoff.m_v[2])
                top    =  (self._halfH - eoff.m_v[1]) * d
                bottom = -(self._halfH + eoff.m_v[1]) * d
                right  =  (self._halfW * wBias - eoff.m_v[0]) * d
                left   = -(self._halfW * wBias + eoff.m_v[0]) * d
                glFrustum(left, right, bottom, top, self._near, self._far)
            else:
                top    =  self._halfH - eoff.m_v[1]
                bottom = -(self._halfH + eoff.m_v[1])
                right  =  self._halfW * wBias - eoff.m_v[0]
                left   = -(self._halfW * wBias + eoff.m_v[0])
                glOrtho(left, right, bottom, top, self._near, self._far)
        except:
            return False # no valid OpenGL context
        return True
Example #10
0
    def _setup_projection(self, glselect=False):  # bruce 050608 split this out; 050615 revised docstring
        """
        Set up standard projection matrix contents using aspect, vdist, and 
        some attributes of self.
        
        @warning: leaves matrixmode as GL_PROJECTION.
                  Optional arg glselect should be False (default) or a 4-tuple
                  (to prepare for GL_SELECT picking).
        """
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        scale = self.scale  # bruce 050608 used this to clarify following code
        near, far = self.near, self.far

        # bruce 080219 moved these from one of two callers into here,
        # to fix bug when insert from partlib is first operation in NE1
        self.aspect = (self.width + 0.0) / (self.height + 0.0)
        self.vdist = 6.0 * scale

        if glselect:
            x, y, w, h = glselect
            gluPickMatrix(x, y, w, h, glGetIntegerv(GL_VIEWPORT))  # k is this arg needed? it might be the default...

        if self.ortho:
            glOrtho(-scale * self.aspect, scale * self.aspect, -scale, scale, self.vdist * near, self.vdist * far)
        else:
            glFrustum(
                -scale * near * self.aspect,
                scale * near * self.aspect,
                -scale * near,
                scale * near,
                self.vdist * near,
                self.vdist * far,
            )
        return
Example #11
0
    def update_viewport(self):
        width, height = self.system_size
        w2 = width / 2.
        h2 = height / 2.

        # prepare the viewport
        glViewport(0, 0, width, height)

        # set the projection
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-w2, w2, -h2, h2, .1, 1000)
        glScalef(5000, 5000, 1)

        # use the rotated size.
        width, height = self.size
        w2 = width / 2.
        h2 = height / 2.
        glTranslatef(-w2, -h2, -500)

        # set the model view
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(w2, h2, 0)
        glRotatef(self._rotation, 0, 0, 1)
        glTranslatef(-w2, -h2, 0)

        # update window size
        for w in self.children:
            shw, shh = w.size_hint
            if shw and shh:
                w.size = shw * width, shh * height
            elif shw:
                w.width = shw * width
            elif shh:
                w.height = shh * height
Example #12
0
    def openglSetup(self):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glEnable(GL.GL_DEPTH_TEST)
        glEnable(GL.GL_COLOR_MATERIAL)

        glViewport(0, 0, self.surf.get_width(), self.surf.get_height())
        glMatrixMode(GL.GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
        glMatrixMode(GL.GL_MODELVIEW)

        self.cardList = glGenLists(1)
        glNewList(self.cardList, GL.GL_COMPILE)
        glColor3f(1, 1, 1)
        with begin(GL.GL_QUADS):
            glTexCoord2f(0.0, 1.0)
            glVertex3f(-1.0, -1.0, 0.0)
            glTexCoord2f(1.0, 1.0)
            glVertex3f(1.0, -1.0, 0.0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f(1.0, 1.0, 0.0)
            glTexCoord2f(0.0, 0.0)
            glVertex3f(-1.0, 1.0, 0.0)
        glEndList()
Example #13
0
    def update_viewport(self):
        width, height = self.system_size
        w2 = width / 2.0
        h2 = height / 2.0

        # prepare the viewport
        glViewport(0, 0, width, height)

        # set the projection
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-w2, w2, -h2, h2, 0.1, 1000)
        glScalef(5000, 5000, 1)

        # use the rotated size.
        width, height = self.size
        w2 = width / 2.0
        h2 = height / 2.0
        glTranslatef(-w2, -h2, -500)

        # set the model view
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(w2, h2, 0)
        glRotatef(self._rotation, 0, 0, 1)
        glTranslatef(-w2, -h2, 0)

        # update window size
        for w in self.children:
            shw, shh = w.size_hint
            if shw and shh:
                w.size = shw * width, shh * height
            elif shw:
                w.width = shw * width
            elif shh:
                w.height = shh * height
Example #14
0
    def draw(self, width, height, selection_box=None):
        scene = context.application.scene
        camera = context.application.camera

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        if selection_box is not None:
            # set up a select buffer
            glSelectBuffer(self.select_buffer_size)
            glRenderMode(GL_SELECT)
            glInitNames()

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        # Apply the pick matrix if selecting
        if selection_box is not None:
            gluPickMatrix(0.5 * (selection_box[0] + selection_box[2]),
                          height - 0.5 * (selection_box[1] + selection_box[3]),
                          selection_box[2] - selection_box[0] + 1,
                          selection_box[3] - selection_box[1] + 1,
                          (0, 0, width, height))

        # Apply the frustum matrix
        znear = camera.znear
        zfar = camera.znear + camera.window_depth
        if width > height:
            w = 0.5*float(width) / float(height)
            h = 0.5
        else:
            w = 0.5
            h = 0.5*float(height) / float(width)
        if znear > 0.0:
            glFrustum(-w*camera.window_size, w*camera.window_size, -h*camera.window_size, h*camera.window_size, znear, zfar)
        else:
            glOrtho(-w*camera.window_size, w*camera.window_size, -h*camera.window_size, h*camera.window_size, znear, zfar)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        # Move to eye position (reverse)
        gl_apply_inverse(camera.eye)
        glTranslatef(0.0, 0.0, -znear)
        # Draw the rotation center, only when realy drawing objects:
        if selection_box is None:
            glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
            glShadeModel(GL_SMOOTH)
            self.call_list(scene.rotation_center_list)
        # Now rotate to the model frame and move back to the model center (reverse)
        gl_apply_inverse(camera.rotation)
        # Then bring the rotation center at the right place (reverse)
        gl_apply_inverse(camera.rotation_center)
        gl_apply_inverse(scene.model_center)

        scene.draw()

        if selection_box is not None:
            # now let the caller analyze the hits by returning the selection
            # buffer. Note: The selection buffer can be used as an iterator
            # over 3-tupples (near, far, names) where names is tuple that
            # contains the gl_names associated with the encountered vertices.
            return glRenderMode(GL_RENDER)
        else:
            # draw the interactive tool (e.g. selection rectangle):
            glCallList(self.tool.total_list)
Example #15
0
    def draw(self, width, height, selection_box=None):
        scene = context.application.scene
        camera = context.application.camera

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        if selection_box is not None:
            # set up a select buffer
            glSelectBuffer(self.select_buffer_size)
            glRenderMode(GL_SELECT)
            glInitNames()

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        # Apply the pick matrix if selecting
        if selection_box is not None:
            gluPickMatrix(0.5 * (selection_box[0] + selection_box[2]),
                          height - 0.5 * (selection_box[1] + selection_box[3]),
                          selection_box[2] - selection_box[0] + 1,
                          selection_box[3] - selection_box[1] + 1,
                          (0, 0, width, height))

        # Apply the frustum matrix
        znear = camera.znear
        zfar = camera.znear + camera.window_depth
        if width > height:
            w = 0.5 * float(width) / float(height)
            h = 0.5
        else:
            w = 0.5
            h = 0.5 * float(height) / float(width)
        if znear > 0.0:
            glFrustum(-w * camera.window_size, w * camera.window_size,
                      -h * camera.window_size, h * camera.window_size, znear,
                      zfar)
        else:
            glOrtho(-w * camera.window_size, w * camera.window_size,
                    -h * camera.window_size, h * camera.window_size, znear,
                    zfar)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        # Move to eye position (reverse)
        gl_apply_inverse(camera.eye)
        glTranslatef(0.0, 0.0, -znear)
        # Draw the rotation center, only when realy drawing objects:
        if selection_box is None:
            glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
            glShadeModel(GL_SMOOTH)
            self.call_list(scene.rotation_center_list)
        # Now rotate to the model frame and move back to the model center (reverse)
        gl_apply_inverse(camera.rotation)
        # Then bring the rotation center at the right place (reverse)
        gl_apply_inverse(camera.rotation_center)
        gl_apply_inverse(scene.model_center)

        scene.draw()

        if selection_box is not None:
            # now let the caller analyze the hits by returning the selection
            # buffer. Note: The selection buffer can be used as an iterator
            # over 3-tupples (near, far, names) where names is tuple that
            # contains the gl_names associated with the encountered vertices.
            return glRenderMode(GL_RENDER)
        else:
            # draw the interactive tool (e.g. selection rectangle):
            glCallList(self.tool.total_list)
Example #16
0
 def camera_free(_):
     glFrustum(*_.frustum)
Example #17
0
    def _setup_projection(self, glselect = False):
        ### WARNING: This is not actually private! TODO: rename it.
        """
        Set up standard projection matrix contents using various attributes of
        self (aspect, vdist, scale, zoomFactor).  Also reads the current OpenGL
        viewport bounds in window coordinates.
        
        (Warning: leaves matrixmode as GL_PROJECTION.)
        
        @param glselect: False (default) normally, or a 4-tuple
               (format not documented here) to prepare for GL_SELECT picking by
               calling gluPickMatrix().

        If you are really going to draw in the pick window (for GL_RENDER and
        glReadPixels picking, rather than GL_SELECT picking), don't forget to
        set the glViewport *after* calling _setup_projection.  Here's why:

           gluPickMatrix needs to know the current *whole-window* viewport, in
           order to set up a projection matrix to map a small portion of it to
           the clipping boundaries for GL_SELECT.

           From the gluPickMatrix doc page:
             viewport:
               Specifies the current viewport (as from a glGetIntegerv call).
             Description:
               gluPickMatrix creates a projection matrix that can be used to
               restrict drawing to a small region of the viewport.

           In the graphics pipeline, the clipper actually works in homogeneous
           coordinates, clipping polygons to the {X,Y}==+-W boundaries.  This
           saves the work of doing the homogeneous division: {X,Y}/W==+-1.0,
           (and avoiding problems when W is zero for points on the eye plane in
           Z,) but it comes down to the same thing as clipping to X,Y==+-1 in
           orthographic.

           So the projection matrix decides what 3D model-space planes map to
           +-1 in X,Y.  (I think it maps [near,far] to [0,1] in Z, because
           they're not clipped symmetrically.)

           Then glViewport sets the hardware transform that determines where the
           +-1 square of clipped output goes in screen pixels within the window.

           Normally you don't actually draw pixels while picking in GL_SELECT
           mode because the pipeline outputs hits after the clipping stage, so
           gluPickMatrix only reads the viewport and sets the projection matrix.
        """
        #bruce 080912 moved this from GLPane into GLPane_minimal
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        scale = self.scale * self.zoomFactor
        near, far = self.near, self.far

        aspect = self.aspect
        vdist = self.vdist

        if glselect:
            x, y, w, h = glselect
            gluPickMatrix(
                x, y,
                w, h,
                glGetIntegerv( GL_VIEWPORT ) #k is this arg needed? it might be the default...
            )

        if self.ortho:
            glOrtho( - scale * aspect, scale * aspect,
                     - scale,          scale,
                     vdist * near, vdist * far )
        else:
            glFrustum( - scale * near * aspect, scale * near * aspect,
                       - scale * near,          scale * near,
                       vdist * near, vdist * far)
        return
Example #18
0
    def _setup_projection(self, glselect=False):
        ### WARNING: This is not actually private! TODO: rename it.
        """
        Set up standard projection matrix contents using various attributes of
        self (aspect, vdist, scale, zoomFactor).  Also reads the current OpenGL
        viewport bounds in window coordinates.
        
        (Warning: leaves matrixmode as GL_PROJECTION.)
        
        @param glselect: False (default) normally, or a 4-tuple
               (format not documented here) to prepare for GL_SELECT picking by
               calling gluPickMatrix().

        If you are really going to draw in the pick window (for GL_RENDER and
        glReadPixels picking, rather than GL_SELECT picking), don't forget to
        set the glViewport *after* calling _setup_projection.  Here's why:

           gluPickMatrix needs to know the current *whole-window* viewport, in
           order to set up a projection matrix to map a small portion of it to
           the clipping boundaries for GL_SELECT.

           From the gluPickMatrix doc page:
             viewport:
               Specifies the current viewport (as from a glGetIntegerv call).
             Description:
               gluPickMatrix creates a projection matrix that can be used to
               restrict drawing to a small region of the viewport.

           In the graphics pipeline, the clipper actually works in homogeneous
           coordinates, clipping polygons to the {X,Y}==+-W boundaries.  This
           saves the work of doing the homogeneous division: {X,Y}/W==+-1.0,
           (and avoiding problems when W is zero for points on the eye plane in
           Z,) but it comes down to the same thing as clipping to X,Y==+-1 in
           orthographic.

           So the projection matrix decides what 3D model-space planes map to
           +-1 in X,Y.  (I think it maps [near,far] to [0,1] in Z, because
           they're not clipped symmetrically.)

           Then glViewport sets the hardware transform that determines where the
           +-1 square of clipped output goes in screen pixels within the window.

           Normally you don't actually draw pixels while picking in GL_SELECT
           mode because the pipeline outputs hits after the clipping stage, so
           gluPickMatrix only reads the viewport and sets the projection matrix.
        """
        #bruce 080912 moved this from GLPane into GLPane_minimal
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        scale = self.scale * self.zoomFactor
        near, far = self.near, self.far

        aspect = self.aspect
        vdist = self.vdist

        if glselect:
            x, y, w, h = glselect
            gluPickMatrix(
                x,
                y,
                w,
                h,
                glGetIntegerv(
                    GL_VIEWPORT
                )  #k is this arg needed? it might be the default...
            )

        if self.ortho:
            glOrtho(-scale * aspect, scale * aspect, -scale, scale,
                    vdist * near, vdist * far)
        else:
            glFrustum(-scale * near * aspect, scale * near * aspect,
                      -scale * near, scale * near, vdist * near, vdist * far)
        return
Example #19
0
    def OnDraw(self):
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-50.0 * self.zoom, 50.0 * self.zoom, -50.0 * self.zoom,
                  50.0 * self.zoom, 200, 800.0)
        #glTranslatef(0.0, 0.0, -400.0)
        gluLookAt(200.0, -200.0, 400.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0)

        glMatrixMode(GL_MODELVIEW)
        if self.resetView:
            glLoadIdentity()
            self.lastx = self.x = 0
            self.lasty = self.y = 0
            self.anglex = self.angley = self.anglez = 0
            self.transx = self.transy = 0
            self.resetView = False

        if self.size is None:
            self.size = self.GetClientSize()
        w, h = self.size
        w = max(w, 1.0)
        h = max(h, 1.0)
        xScale = 180.0 / w
        yScale = 180.0 / h
        glRotatef(self.angley * yScale, 1.0, 0.0, 0.0)
        glRotatef(self.anglex * xScale, 0.0, 1.0, 0.0)
        glRotatef(self.anglez, 0.0, 0.0, 1.0)
        glTranslatef(self.transx, self.transy, 0.0)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_LIGHT1)

        if self.drawGrid:
            glBegin(GL_LINES)
            for i in range(len(self.gridVertices)):
                glColor(self.gridColors[i])
                p = self.gridVertices[i]
                glVertex3f(p[0], p[1], p[2])
                glVertex3f(p[3], p[4], p[5])
            glEnd()

        colors = {MT_RAPID: [0.0, 1.0, 1.0, 1], MT_NORMAL: [1.0, 1.0, 1.0, 1]}

        currentLineType = MT_RAPID
        glColor(colors[MT_RAPID])

        glBegin(GL_LINE_STRIP)
        for p in self.dataPoints:
            if p[3] != currentLineType:
                currentLineType = p[3]
                glColor(colors[currentLineType])
            glVertex3f(p[0], p[1], p[2])

        glEnd()

        self.SwapBuffers()

        glDisable(GL_LIGHT0)
        glDisable(GL_LIGHT1)
        glDisable(GL_LIGHTING)

        self.anglex = self.angley = self.anglez = 0
        self.transx = self.transy = 0