Moving Layered Cube using OpenGL

4 minute read

Python과 OpenGL을 사용하여 위키에 나오는 4차원 그림을 그려봄

image

image

import sys
import textwrap
import wx
import wx.glcanvas as glcanvas
import math
import numpy as np
from numpy import linalg as LA
try:
    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL.GLUT import *

    haveOpenGL = True
except ImportError:
    haveOpenGL = False


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

def make_program(vs, fs):
    id_program = glCreateProgram()
    glAttachShader(id_program, vs)
    glAttachShader(id_program, fs)
    glLinkProgram(id_program)

    result = glGetProgramiv(id_program, GL_LINK_STATUS)
    if not(result):
        raise RuntimeError(glGetProgramInfoLog(id_program))

    return id_program


def make_fs(source):
    id_fs = glCreateShader(GL_FRAGMENT_SHADER)
    glShaderSource(id_fs, source)
    glCompileShader(id_fs)

    result = glGetShaderiv(id_fs, GL_COMPILE_STATUS)
    if not(result):
        raise Exception("Error: {0}".format(
            glGetShaderInfoLog(id_fs)
        ))

    return id_fs


def make_vs(source):
    id_vs = glCreateShader(GL_VERTEX_SHADER)
    glShaderSource(id_vs, source)
    glCompileShader(id_vs)

    result = glGetShaderiv(id_vs, GL_COMPILE_STATUS)
    if not(result):
        raise Exception("Error: {0}".format(
            glGetShaderInfoLog(id_vs)
        ))

    return id_vs

class MyCanvasBase(glcanvas.GLCanvas):
    def __init__(self, parent):
        glcanvas.GLCanvas.__init__(self, parent, -1)
        self.init = False
        self.context = glcanvas.GLContext(self)

        self.lastx = self.x = 30
        self.lasty = self.y = 30
        self.size = None

        self.SetBackgroundStyle(wx.BG_STYLE_PAINT)

        self.timer = wx.Timer(self)
        self.timer.Start(10)

        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        self.Bind(wx.EVT_TIMER, self.onUpdate, self.timer)

        self.th = 0

    def onUpdate(self, event):
        self.lastx, self.lasty = self.x, self.y
        self.th = self.th + 0.01
        if self.th > math.pi * 2:
            self.th = 0

        self.Refresh(False)

    def OnSize(self, event):
        wx.CallAfter(self.DoSetViewport)
        event.Skip()

    def DoSetViewport(self):
        size = self.size = self.GetClientSize()
        self.SetCurrent(self.context)
        glViewport(0, 0, size.width, size.height)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.SetCurrent(self.context)
        if not self.init:
            self.InitGL()
            self.init = True
        self.OnDraw()

    def OnMouseDown(self, evt):
        self.CaptureMouse()
        self.x, self.y = self.lastx, self.lasty = evt.GetPosition()

    def OnMouseUp(self, evt):
        self.ReleaseMouse()

    def OnMouseMotion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            self.lastx, self.lasty = self.x, self.y
            self.x, self.y = evt.GetPosition()
            self.Refresh(False)


class CubeCanvas(MyCanvasBase):
    def InitGL(self):
        # set viewing projection
        glMatrixMode(GL_PROJECTION)
        glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 10.0)

        # position viewer
        glMatrixMode(GL_MODELVIEW)
        glTranslatef(0.0, 0.0, -4.0)

        # position object
        glRotatef(20, 1.0, 0.0, 0.0)
        glRotatef(-20, 0.0, 1.0, 0.0)

        self.gluQuadric = gluNewQuadric()

        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)

        ambient = [1.0, 1.0, 1.0, 0.0]
        diffuse = [1.0, 1.0, 1.0, 0.0]
        specular = [1.0, 1.0, 1.0, 0.0]
        position = [6.0, 6.0, 5.0, 0.0]

        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient)
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse)
        glLightfv(GL_LIGHT0, GL_SPECULAR, specular)
        glLightfv(GL_LIGHT0, GL_POSITION, position)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_NORMALIZE)
        glEnable(GL_DEPTH_TEST)

        # glEnable(GL_MAP1_VERTEX_3)
        # cubic_point = [[-1, -1, 0], [-2, 1, 0], [2, -1, 0], [1, 1, 0]]
        # glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, cubic_point)
        # v = np.array([1], dtype='float32')
        # glGetMapfv(GL_MAP1_VERTEX_3, GL_DOMAIN, v)
        # print("v="+str(v))

        print("a")
        start = np.array([0, 0, 0])
        end = np.array([1, 1, 1])
        print(end-start)
        normal_v = end - start
        print("original n_z="+str(normal_v/LA.norm(normal_v)))
        rot_v = np.array([-normal_v[1], normal_v[0], 0])
        print(rot_v)
        print(LA.norm(rot_v))
        rot_norm = LA.norm(rot_v)
        rot_norm_v = rot_v / rot_norm
        print(rot_norm_v)
        rot_angle = math.acos(normal_v[2]/LA.norm(normal_v))
        print("rot_angle="+str(rot_angle))
        x = math.sin(rot_angle/2)*rot_norm_v[0]
        y = math.sin(rot_angle/2)*rot_norm_v[1]
        z = math.sin(rot_angle/2)*rot_norm_v[2]
        w = math.cos(rot_angle/2)
        q = np.matrix([[1 - 2*y*y - 2*z*z, 2*x*y - 2*w*z, 2*x*z + 2*w*y],
                       [2*x*y + 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z - 2*w*x],
                       [2*x*z - 2*w*y, 2*y*z + 2*w*x, 1 - 2*x*x - 2*y*y]])
        print(q)

        # self.prog_axis = make_program(
        #     make_vs(textwrap.dedent("""
        #         #version 410
        #
        #         layout (location = 0) in vec3 a_position;
        #         layout (location = 1) in vec3 a_color;
        #
        #         out vec3 color;
        #
        #         uniform mat4 projection;
        #         uniform mat4 view;
        #         uniform mat4 model;
        #
        #         void main () {
        #             color=a_color;
        #             gl_Position = projection*view*model*vec4(a_position, 0.0);
        #         }
        #     """)),
        #     make_fs(textwrap.dedent("""
        #         #version 410
        #         in vec3 color;
        #         out vec4 frag_colour;
        #
        #         void main () {
        #           frag_colour = vec4(color,1.0);
        #         }
        #     """))
        # )

    # https://jebae.github.io/quaternion-rotation
    def drawCylinder(self, start, end):
        start = np.array(start)
        end = np.array(end)
        normal_v = end - start
        length = LA.norm(normal_v)
        rot_v = np.array([-normal_v[1], normal_v[0], 0])
        rot_norm = LA.norm(rot_v)
        # rot_norm_v = rot_v / rot_norm
        # rot_angle = math.acos(normal_v[2] / length)
        if rot_norm < 0.001:
            glPushMatrix()
            glTranslate(end[0], end[1], end[2])
            gluCylinder(self.gluQuadric, 0.04, 0.04, length, 100, 100)
            glPopMatrix()
        else:
            rot_norm_v = rot_v / rot_norm
            rot_angle = math.acos(normal_v[2] / length)
            x = math.sin(rot_angle/2)*rot_norm_v[0]
            y = math.sin(rot_angle/2)*rot_norm_v[1]
            z = math.sin(rot_angle/2)*rot_norm_v[2]
        # w = math.cos(rot_angle/2)
        # q = np.matrix([[1 - 2*y*y - 2*z*z, 2*x*y - 2*w*z, 2*x*z + 2*w*y],
        #                [2*x*y + 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z - 2*w*x],
        #                [2*x*z - 2*w*y, 2*y*z + 2*w*x, 1 - 2*x*x - 2*y*y]])
            glPushMatrix()
            glTranslate(start[0], start[1], start[2])
            glRotate(math.degrees(rot_angle), x, y, z)
            gluCylinder(self.gluQuadric, 0.04, 0.04, length, 100, 100)
            glPopMatrix()

    def SetColor(self, r, g, b):
        mat_ambient = [0.2*r, 0.2*g, 0.2*b, 0.0]
        mat_diffuse = [0.6*r, 0.6*g, 0.6*b, 0.0]
        mat_specular = [r, g, b, 0.0]
        mat_emissive = [0.0, 0.0, 0.0, 0.0]
        mat_shininess = [100.0]

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient)
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse)
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular)
        glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess)
        glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emissive)

    def drawSolidCone(self, radius, height, slice):
        th = 2*math.pi / slice
        for i in range(slice):
            glBegin(GL_TRIANGLES)
            at = math.atan(height/radius)
            sat = math.sin(at)
            glNormal3f(sat*math.cos(th*i), sat*math.sin(th*i), math.cos(at))
            glVertex3f(0.0, 0.0, height)
            glVertex3f(radius*math.cos(th*i), radius*math.sin(th*i), 0.0)
            glVertex3f(radius*math.cos(th*(i+1)), radius*math.sin(th*(i+1)), 0.0)
            glEnd()

    def mapTh(self, th):
        th = math.fmod(th, math.pi*2)
        if th >= 0 and th <= math.pi*0.5:
            return th*2
        elif th > math.pi*0.5:
            return (th-math.pi*0.5)*0.67 + math.pi

    def drawCoordArrow(self):
        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_NORMALIZE)

        self.SetColor(1.0, 0, 0)
        glPushMatrix()
        glTranslate(1.0, 0.0, 0.0)
        glRotate(90, 0.0, 1.0, 0.0)
        self.drawSolidCone(0.1, 0.2, 10)
        glPopMatrix()

        self.SetColor(0, 1.0, 0)
        glPushMatrix()
        glTranslate(0.0, 1.0, 0.0)
        glRotate(-90, 1.0, 0.0, 0.0)
        self.drawSolidCone(0.1, 0.2, 10)
        glPopMatrix()

        self.SetColor(0, 0, 1.0)
        glPushMatrix()
        glTranslate(0.0, 0.0, 1.0)
        self.drawSolidCone(0.1, 0.2, 10)
        glPopMatrix()

        glDisable(GL_LIGHTING)
        glDisable(GL_LIGHT0)
        glDisable(GL_NORMALIZE)

        glBegin(GL_LINES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glVertex3f(1.0, 0.0, 0.0)
        glEnd()

        glBegin(GL_LINES)
        glColor3f(0.0, 1.0, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glVertex3f(0.0, 1.0, 0.0)
        glEnd()

        glBegin(GL_LINES)
        glColor3f(0.0, 0.0, 1.0)
        glVertex3f(0.0, 0.0, 0.0)
        glVertex3f(0.0, 0.0, 1.0)
        glEnd()

    def OnDraw(self):
        # clear color and depth buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # prog = self.prog_axis
        # glUseProgram(prog)

        glMatrixMode(GL_MODELVIEW)


        # glBegin(GL_LINE_STRIP)
        # for i in range(31):
        #     glEvalCoord1f(float(i) / 31)
        # glEnd()
        # draw six faces of a cube
        # glBegin(GL_QUADS)
        # glNormal3f(0.0, 0.0, 1.0)
        # glVertex3f(0.5, 0.5, 0.5)
        # glVertex3f(-0.5, 0.5, 0.5)
        # glVertex3f(-0.5, -0.5, 0.5)
        # glVertex3f(0.5, -0.5, 0.5)
        #
        # glNormal3f(0.0, 0.0, -1.0)
        # glVertex3f(-0.5, -0.5, -0.5)
        # glVertex3f(-0.5, 0.5, -0.5)
        # glVertex3f(0.5, 0.5, -0.5)
        # glVertex3f(0.5, -0.5, -0.5)
        #
        # glNormal3f(0.0, 1.0, 0.0)
        # glVertex3f(0.5, 0.5, 0.5)
        # glVertex3f(0.5, 0.5, -0.5)
        # glVertex3f(-0.5, 0.5, -0.5)
        # glVertex3f(-0.5, 0.5, 0.5)
        #
        # glNormal3f(0.0, -1.0, 0.0)
        # glVertex3f(-0.5, -0.5, -0.5)
        # glVertex3f(0.5, -0.5, -0.5)
        # glVertex3f(0.5, -0.5, 0.5)
        # glVertex3f(-0.5, -0.5, 0.5)
        #
        # glNormal3f(1.0, 0.0, 0.0)
        # glVertex3f(0.5, 0.5, 0.5)
        # glVertex3f(0.5, -0.5, 0.5)
        # glVertex3f(0.5, -0.5, -0.5)
        # glVertex3f(0.5, 0.5, -0.5)
        #
        # glNormal3f(-1.0, 0.0, 0.0)
        # glVertex3f(-0.5, -0.5, -0.5)
        # glVertex3f(-0.5, -0.5, 0.5)
        # glVertex3f(-0.5, 0.5, 0.5)
        # glVertex3f(-0.5, 0.5, -0.5)
        # glEnd()

        """
        
        """
        a = 0.4
        b = 0.4
        p = 0.6

        th = self.th
        th1 = self.mapTh(th)
        x1 = a * math.cos(th1)
        y1 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z1 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x5 = a * math.cos(th1)
        y5 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z5 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x9 = a * math.cos(th1)
        y9 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z9 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        x13 = a * math.cos(th1)
        y13 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z13 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        # x1 = a * math.cos(th)
        # y1 = 0
        # z1 = (b * math.sin(th) + p)

        th = th + math.pi * 0.5
        th1 = self.mapTh(th)
        x2 = a * math.cos(th1)
        y2 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z2 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x6 = a * math.cos(th1)
        y6 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z6 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x10 = a * math.cos(th1)
        y10 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z10 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        x14 = a * math.cos(th1)
        y14 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z14 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        # x2 = a * math.cos(th)
        # y2 = 0
        # z2 = (b * math.sin(th) + p)

        th = th + math.pi * 0.5
        th1 = self.mapTh(th)
        x3 = a * math.cos(th1)
        y3 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z3 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x7 = a * math.cos(th1)
        y7 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z7 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x11 = a * math.cos(th1)
        y11 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z11 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        x15 = a * math.cos(th1)
        y15 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z15 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        # x3 = a * math.cos(th)
        # y3 = 0
        # z3 = (b * math.sin(th) + p)

        th = th + math.pi * 0.5
        th1 = self.mapTh(th)
        x4 = a * math.cos(th1)
        y4 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z4 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x8 = a * math.cos(th1)
        y8 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z8 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        x12 = a * math.cos(th1)
        y12 = 1/math.sqrt(2)*(b * math.sin(th1) + p)
        z12 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        x16 = a * math.cos(th1)
        y16 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        z16 = -1/math.sqrt(2)*(b * math.sin(th1) + p)
        # x4 = a * math.cos(th)
        # y4 = 0
        # z4 = (b * math.sin(th) + p)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_NORMALIZE)
        self.SetColor(0, 0, 1.0)
        # self.drawCylinder([0, 0, 0], [0, 0, 1])
        # self.drawCylinder([0, 0, 0], [x1, y1, z1])
        glPushMatrix()
        glTranslate(x1, y1, z1)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x2, y2, z2)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x3, y3, z3)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x4, y4, z4)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x5, y5, z5)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x6, y6, z6)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x7, y7, z7)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x8, y8, z8)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x9, y9, z9)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x10, y10, z10)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x11, y11, z11)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x12, y12, z12)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x13, y13, z13)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x14, y14, z14)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x15, y15, z15)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()
        glPushMatrix()
        glTranslate(x16, y16, z16)
        gluSphere(self.gluQuadric, 0.04, 100, 100)
        glPopMatrix()

        self.drawCylinder([x1, y1, z1], [x2, y2, z2])
        self.drawCylinder([x2, y2, z2], [x3, y3, z3])
        self.drawCylinder([x3, y3, z3], [x4, y4, z4])
        self.drawCylinder([x4, y4, z4], [x1, y1, z1])

        self.drawCylinder([x5, y5, z5], [x6, y6, z6])
        self.drawCylinder([x6, y6, z6], [x7, y7, z7])
        self.drawCylinder([x7, y7, z7], [x8, y8, z8])
        self.drawCylinder([x8, y8, z8], [x5, y5, z5])

        self.drawCylinder([x9, y9, z9], [x10, y10, z10])
        self.drawCylinder([x10, y10, z10], [x11, y11, z11])
        self.drawCylinder([x11, y11, z11], [x12, y12, z12])
        self.drawCylinder([x12, y12, z12], [x9, y9, z9])

        self.drawCylinder([x13, y13, z13], [x14, y14, z14])
        self.drawCylinder([x14, y14, z14], [x15, y15, z15])
        self.drawCylinder([x15, y15, z15], [x16, y16, z16])
        self.drawCylinder([x16, y16, z16], [x13, y13, z13])

        self.drawCylinder([x1, y1, z1], [x5, y5, z5])
        self.drawCylinder([x2, y2, z2], [x6, y6, z6])
        self.drawCylinder([x3, y3, z3], [x7, y7, z7])
        self.drawCylinder([x4, y4, z4], [x8, y8, z8])

        self.drawCylinder([x9, y9, z9], [x13, y13, z13])
        self.drawCylinder([x10, y10, z10], [x14, y14, z14])
        self.drawCylinder([x11, y11, z11], [x15, y15, z15])
        self.drawCylinder([x12, y12, z12], [x16, y16, z16])

        self.drawCylinder([x1, y1, z1], [x9, y9, z9])
        self.drawCylinder([x2, y2, z2], [x10, y10, z10])
        self.drawCylinder([x3, y3, z3], [x11, y11, z11])
        self.drawCylinder([x4, y4, z4], [x12, y12, z12])

        self.drawCylinder([x5, y5, z5], [x13, y13, z13])
        self.drawCylinder([x6, y6, z6], [x14, y14, z14])
        self.drawCylinder([x7, y7, z7], [x15, y15, z15])
        self.drawCylinder([x8, y8, z8], [x16, y16, z16])
        # glPushMatrix()
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(0.5, 0, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(0, 0.5, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(-0.5, 0, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(0, -0.5, 0.5)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(0.5, 0, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(0, 0.5, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glTranslate(-0.5, 0, 0)
        # gluSphere(self.gluQuadric, 0.1, 100, 100)
        # glPopMatrix()
        #
        # glPushMatrix()
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0, 0.5, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(-0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glPopMatrix()
        #
        # glPushMatrix()
        # glTranslate(0, 0.5, 0)
        # glRotate(90, 1, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0, 0.5, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(-0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glPopMatrix()
        #
        # glPushMatrix()
        # glTranslate(0, 0, 0.5)
        # glRotate(90, 0, 1, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(0, 0.5, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glTranslate(-0.5, 0, 0)
        # gluCylinder(self.gluQuadric, 0.1, 0.1, 0.5, 100, 100)
        # glPopMatrix()

        # self.drawCylinder([0, 0, 0], [1, 1, 1])

        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.y - self.lasty) * yScale, 1.0, 0.0, 0.0);
        glRotatef((self.x - self.lastx) * xScale, 0.0, 1.0, 0.0);

        self.SwapBuffers()


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


if __name__ == '__main__':
    app = wx.App(False)
    glutInit()
    if not haveOpenGL:
        wx.MessageBox('This sample requires the PyOpenGL package.', 'Sorry')
    else:
        frm = wx.Frame(None, title='GLCanvas Sample', size=(500, 500))
        canvas = CubeCanvas(frm)
        frm.Show()
    app.MainLoop()

Categories:

Updated: