Search code examples
pythonopenglpygletopengl-compat

How to make different orthogonal projections Pyglet?


I made two viewports of cube, but both of them are front orthogonal projections, and I need on of them to be from side. Here is a code of drawing draw_small is drawing of small static cube dwar_big is drawing of big cube, that can move,zoom and rotate

    def on_draw(self) :

        self.clear()

        pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)

        pgl.glViewport(0, 0, 650, 500)
        pgl.glMatrixMode(pgl.GL_PROJECTION)
        pgl.glLoadIdentity()
        pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 100, 500)
        pgl.glMatrixMode(pgl.GL_MODELVIEW)
        pgl.glLoadIdentity
        pgl.glTranslatef(0, 0, -100)
        pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
        draw_small()

        pgl.glPushMatrix()
        pgl.glTranslatef(self.x, self.y, self.z)
        pgl.glRotatef(self.xRotation, 1, 0, 0)
        pgl.glRotatef(self.yRotation, 0, 1, 0)
        pgl.glRotatef(self.zRotation, 0, 0, 1)
        pgl.glScalef(self.zoom, self.zoom, self.zoom)

        if not transparant:
            pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
        else:
            ogl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)

        draw_big()

        pgl.glPopMatrix()


        pgl.glViewport(650, 0, 650, 500)
        pgl.glMatrixMode(pgl.GL_PROJECTION)
        pgl.glLoadIdentity()
        pgl.glOrtho(-1300 / 8, 1300 / 8, -1000 / 8, 1000 / 8, 0, 500)
        pgl.glMatrixMode(pgl.GL_MODELVIEW)
        pgl.glLoadIdentity()
        pgl.glTranslatef(0, 0, -100)
        pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)

        draw_small()

        pgl.glPushMatrix()
        pgl.glTranslatef(self.x, self.y, self.z)
        pgl.glRotatef(self.xRotation, 1, 0, 0)
        pgl.glRotatef(self.yRotation, 0, 1, 0)
        pgl.glRotatef(self.zRotation, 0, 0, 1)
        pgl.glScalef(self.zoom, self.zoom, self.zoom)

        if not transparant:
            pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
        else:
            pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)

        draw_big()

        pgl.glPopMatrix()

Solution

  • Just rotate the view by 90 degrees around the y axis in view sapce:

    pgl.glMatrixMode(pgl.GL_MODELVIEW)
    pgl.glLoadIdentity
    pgl.glTranslatef(0, 0, -100) 
    pgl.glRotatef(-90, 0, 1, 0) # roatet 90 degrees around view space y axis
    

    Note, left and right view are generated by pgl.glRotatef(-90, 0, 1, 0) and pgl.glRotatef(90, 0, 1, 0).
    Top and bottom view are achieved by rotations aroud the view sapce x axis, by pgl.glRotatef(90, 1, 0, 0) respectively pgl.glRotatef(-90, 0, 1, 0).