如何在python PyOpenGL中旋转魔方的切片? [英] How to rotate slices of a Rubik's Cube in python PyOpenGL?

查看:30
本文介绍了如何在python PyOpenGL中旋转魔方的切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Python 创建一个魔方,我已经尽可能直观地表示立方体.对如何实现轮换有点挣扎.

我想我是在寻求有关如何执行此操作的反馈.我一开始想,旋转每个立方体的顶点集,没有太多运气.

我基本上想从立方体对象(不同大小)的数组中选择一个切片,对每个对象执行旋转和平移.

导入pygame随机导入从 pygame.locals 导入 *从 OpenGL.GL 导入 *从 OpenGL.GLU 导入 *顶点 = ((1, -1, -1),(1, 1, -1),(-1, 1, -1),(-1, -1, -1),(1, -1, 1),(1, 1, 1),(-1, -1, 1),(-1, 1, 1))边 = ((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7))表面 = ((0,1,2,3),(3,2,7,6),(6,7,5,4),(4,5,1,0),(1,5,7,2),(4,0,3,6))颜色 = ((1,0,0), #红色(0,1,0), #绿色(1,0.5,0),#Orange(1,1,0), #黄色(1,1,1), #White(0,0,1), #蓝色)类立方体():'''设置立方体的顶点边缘和表面(彩色)'''def __init__(self):'''启动显示以显示立方体'''pygame.init()显示 = (800,600)pygame.display.set_mode(显示,DOUBLEBUF|OPENGL)glEnable(GL_DEPTH_TEST)gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)glTranslatef(1,1, -40)def setVertices(self, xmove, ymove, zmove):'''设置预定义的顶点'''xValueChange = xmoveyValueChange = ymovezValueChange = zmove新顶点 = []对于顶点中的 vert:newVert = []newX = vert[0] + xValueChangenewY = vert[1] + yValueChangenewZ = vert[2] + zValueChangenewVert.append(newX)newVert.append(newY)newVert.append(newZ)newVertices.append(newVert)返回新顶点def CreateCube(自我,顶点):'''用 OpenGL 创建'''glBegin(GL_QUADS)x = 0对于表面中的表面:glColor3fv(颜色[x])x+=1对于曲面中的顶点:glVertex3fv(顶点[顶点])glEnd()类 EntireCube():def __init__(self,typeOfCube):self.typeOfCube = typeOfCubeself.NewCube = Cube()def createEntireCube(self):'''对于每个维度 x,y,z 制作一个包含要显示的顶点的字典'''self.cubeDict = {}计数 = 0对于范围内的 x(self.typeOfCube):对于范围内的 y(self.typeOfCube):对于范围内的 z(self.typeOfCube):self.cubeDict[count] = self.NewCube.setVertices(x*2.1,y*2.1,z*2.1)计数 += 1定义主循环(自我):'''关键事件,创建立方体矩阵'''rotateUpKey、rotateDownKey、rotateLeftKey、rotateRightKey = 假、假、假、假旋转灵敏度 = 2而真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()退出()如果 event.type == KEYDOWN:如果 event.key == K_UP:旋转向上键 = 真如果 event.key == K_DOWN:rotateDownKey = 真如果 event.key == K_LEFT:旋转左键 = 真如果 event.key == K_RIGHT:旋转右键 = 真如果 event.type == KEYUP:如果 event.key == K_UP:旋转向上键 = False如果 event.key == K_DOWN:旋转向下键 = False如果 event.key == K_LEFT:旋转左键 = False如果 event.key == K_RIGHT:旋转右键 = False如果rotateUpKey:glRotatef(rotationalSensitivity,-rotationalSensitivity,0,0)如果rotateDownKey:glRotatef(rotationalSensitivity,rotationalSensitivity,0,0)如果旋转左键:glRotatef(rotationalSensitivity,0,-rotationalSensitivity,0)如果旋转右键:glRotatef(rotationalSensitivity,0,rotationalSensitivity,0)#最终实现键绑定以调用函数来旋转创建的矩阵切片# x = glGetDoublev(GL_MODELVIEW_MATRIX)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)对于 self.cubeDict 中的 eachCube:self.NewCube.CreateCube(self.cubeDict[eachCube])# glPushMatrix()# glRotatef(1,3,1,1)# glPopMatrix()pygame.display.flip()pygame.time.wait(10)定义主():NewEntireCube = EntireCube(3) #创建一个 3x3x3 的立方体NewEntireCube.createEntireCube()NewEntireCube.mainloop()如果 __name__ == '__main__':主要的()pygame.quit()退出()

我希望对此有更多了解的人可以给我一些关于如何进行的指导.

解决方案

魔方可以由 3x3x3 立方体的 3 维数组来组织.旋转立方体的切片似乎很容易,但请注意,如果切片旋转,立方体的位置会发生变化并且必须重新组织.不仅位置发生变化,(旋转的)单个立方体的方向也会发生变化.

首先从Cube类的构造函数中移除PyGame和OpenGL初始化.这是错误的地方.下面将生成 27 个 Cube 类型的对象.

每个立方体都必须知道它最初的位置 (self.init_i) 以及经过一些旋转后当前所在的位置 (self.current_i).该信息被编码在一个包含 3 个元素的列表中,每个轴一个.这些值是 NxNxN 魔方中立方体的索引,范围为 [0, N[.
单个立方体的方向编码为 3 维

当然,代码使用了 Legacy OpenGL关于您的原始代码.但是方法 Cube.transformMat 为单个部分立方体设置了一个通用的 4x4 模型矩阵.因此,可以轻松地将此代码移植到现代 OpenGL.

导入pygame随机导入从 pygame.locals 导入 *从 OpenGL.GL 导入 *从 OpenGL.GLU 导入 *顶点 = (( 1, -1, -1), ( 1, 1, -1), (-1, 1, -1), (-1, -1, -1),( 1, -1, 1), ( 1, 1, 1), (-1, -1, 1), (-1, 1, 1))边 = ((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7))表面 = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4), (4, 5, 1, 0), (1, 5, 7,2), (4, 0, 3, 6))颜色 = ((1, 0, 0), (0, 1, 0), (1, 0.5, 0), (1, 1, 0), (1, 1, 1), (0, 0, 1))类立方体():def __init__(self, id, N, scale):自我.N = Nself.scale = 比例self.init_i = [*id]self.current_i = [*id]self.rot = [[1 if i==j else 0 for i in range(3)] for j in range(3)]def isAffected(self, axis, slice, dir):返回 self.current_i[axis] == 切片定义更新(自我,轴,切片,目录):如果不是 self.isAffected(axis, slice, dir):返回i, j = (axis+1) % 3, (axis+2) % 3对于范围内的 k (3):self.rot[k][i], self.rot[k][j] = -self.rot[k][j]*dir, self.rot[k][i]*dirself.current_i[i], self.current_i[j] = (self.current_i[j] 如果 dir <0 否则 self.N - 1 - self.current_i[j],self.current_i[i] 如果 dir >0 否则 self.N - 1 - self.current_i[i] )def transformMat(self):scaleA = [[s*self.scale for s in a] for a in self.rot]scaleT = [(p-(self.N-1)/2)*2.1*self.scale for p in self.current_i]返回 [*scaleA[0], 0, *scaleA[1], 0, *scaleA[2], 0, *scaleT, 1]def draw(self, col, surf, vert, animate, angle,axis, slice, dir):glPushMatrix()如果 animate 和 self.isAffected(axis, slice, dir):glRotatef(angle*dir, *[1 if i==axis else 0 for i in range(3)] )glMultMatrixf( self.transformMat() )glBegin(GL_QUADS)对于我在范围内(len(冲浪)):glColor3fv(颜色[i])对于surf[i]中的j:glVertex3fv(顶点[j])glEnd()glPopMatrix()类 EntireCube():def __init__(self, N, scale):自我.N = Ncr = 范围(self.N)self.cubes = [Cube((x, y, z), self.N, scale) for x in cr for y in cr for z in cr]定义主循环(自我):rot_cube_map = { K_UP: (-1, 0), K_DOWN: (1, 0), K_LEFT: (0, -1), K_RIGHT: (0, 1)}rot_slice_map = {K_1: (0, 0, 1), K_2: (0, 1, 1), K_3: (0, 2, 1), K_4: (1, 0, 1), K_5: (1, 1, 1),K_6: (1, 2, 1), K_7: (2, 0, 1), K_8: (2, 1, 1), K_9: (2, 2, 1),K_F1: (0, 0, -1), K_F2: (0, 1, -1), K_F3: (0, 2, -1), K_F4: (1, 0, -1), K_F5: (1, 1), -1),K_F6: (1, 2, -1), K_F7: (2, 0, -1), K_F8: (2, 1, -1), K_F9: (2, 2, -1),}ang_x, ang_y, rot_cube = 0, 0, (0, 0)动画, animate_ang, animate_speed = False, 0, 5动作 = (0, 0, 0)而真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()退出()如果 event.type == KEYDOWN:如果 rot_cube_map 中的 event.key:rot_cube = rot_cube_map[event.key]如果在 rot_slice_map 中没有动画和 event.key:动画,动作 = True,rot_slice_map[event.key]如果 event.type == KEYUP:如果 rot_cube_map 中的 event.key:rot_cube = (0, 0)ang_x += rot_cube[0]*2ang_y += rot_cube[1]*2glMatrixMode(GL_MODELVIEW)glLoadIdentity()glTranslatef(0, 0, -40)glRotatef(ang_y, 0, 1, 0)glRotatef(ang_x, 1, 0, 0)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)如果动画:如果 animate_ang >= 90:对于 self.cubes 中的立方体:立方体.更新(*动作)动画,animate_ang = 假,0对于 self.cubes 中的立方体:cube.draw(颜色,表面,顶点,动画,animate_ang,*action)如果动画:animate_ang += animate_speedpygame.display.flip()pygame.time.wait(10)定义主():pygame.init()显示 = (800,600)pygame.display.set_mode(显示,DOUBLEBUF|OPENGL)glEnable(GL_DEPTH_TEST)glMatrixMode(GL_PROJECTION)gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)NewEntireCube = EntireCube(3, 1.5)NewEntireCube.mainloop()如果 __name__ == '__main__':主要的()pygame.quit()退出()

I'm attempting to create a Rubik's Cube in Python, i have gotten as far as visually representing the cube. Struggling a bit with how to implement rotation.

I guess i'm asking for feedback as to how to go about doing this. I thought at first of, rotating each cubes set of vertices's, without much luck.

I basically want to select a slice from an array of cube objects (of varying size), perform a rotation and a translation on each object.

import pygame
import random
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )

surfaces = (
    (0,1,2,3),
    (3,2,7,6),
    (6,7,5,4),
    (4,5,1,0),
    (1,5,7,2),
    (4,0,3,6)
    )

colors = (
    (1,0,0), #Red
    (0,1,0), #Green
    (1,0.5,0), #Orange
    (1,1,0), #Yellow
    (1,1,1), #White
    (0,0,1), #Blue
    )

class Cube():
    '''set the vertices edges and surfaces(colored) for a Cube'''
    def __init__(self):
        '''initiate the display to show the cube'''
        pygame.init()
        display = (800,600)
        pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
        glEnable(GL_DEPTH_TEST) 
        gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

        glTranslatef(1,1, -40)

    def setVertices(self, xmove, ymove, zmove):
        '''set predefined vertices'''
        xValueChange = xmove
        yValueChange = ymove
        zValueChange = zmove

        newVertices = []

        for vert in vertices:
            newVert = []

            newX = vert[0] + xValueChange
            newY = vert[1] + yValueChange
            newZ = vert[2] + zValueChange

            newVert.append(newX)
            newVert.append(newY)
            newVert.append(newZ)

            newVertices.append(newVert)

        return newVertices

    def CreateCube(self, vertices):
        '''create with OpenGL'''
        glBegin(GL_QUADS)
        x = 0
        for surface in surfaces:
            glColor3fv(colors[x])
            x+=1
            for vertex in surface:
                glVertex3fv(vertices[vertex])
        glEnd()

class EntireCube():
    def __init__(self,typeOfCube):
        self.typeOfCube = typeOfCube
        self.NewCube = Cube()

    def createEntireCube(self):
        '''for each dimension x,y,z make a dictionary containing the vertices to be displayed'''
        self.cubeDict = {}
        count = 0
        for x in range(self.typeOfCube):
            for y in range(self.typeOfCube):
                for z in range(self.typeOfCube):
                    self.cubeDict[count] = self.NewCube.setVertices(x*2.1,y*2.1,z*2.1)
                    count += 1

    def mainloop(self):
        '''key events, creates the matrix of cubes'''
        rotateUpKey, rotateDownKey, rotateLeftKey, rotateRightKey = False, False, False, False
        rotationalSensitivity = 2

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == KEYDOWN:
                    if event.key == K_UP:
                        rotateUpKey = True
                    if event.key == K_DOWN:
                        rotateDownKey = True
                    if event.key == K_LEFT:
                        rotateLeftKey = True
                    if event.key == K_RIGHT:
                        rotateRightKey = True

                if event.type == KEYUP:
                    if event.key == K_UP:
                        rotateUpKey = False
                    if event.key == K_DOWN:
                        rotateDownKey = False
                    if event.key == K_LEFT:
                        rotateLeftKey = False
                    if event.key == K_RIGHT:
                        rotateRightKey = False

            if rotateUpKey:
                glRotatef(rotationalSensitivity,-rotationalSensitivity,0,0)
            if rotateDownKey:
                glRotatef(rotationalSensitivity,rotationalSensitivity,0,0)
            if rotateLeftKey:
                glRotatef(rotationalSensitivity,0,-rotationalSensitivity,0)
            if rotateRightKey:
                glRotatef(rotationalSensitivity,0,rotationalSensitivity,0)

            #eventually implement keysbindings to call function to rotate a slice of the matrix created

            # x = glGetDoublev(GL_MODELVIEW_MATRIX)

            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

            for eachCube in self.cubeDict:
                self.NewCube.CreateCube(self.cubeDict[eachCube])

            # glPushMatrix()
            # glRotatef(1,3,1,1)
            # glPopMatrix()

            pygame.display.flip()
            pygame.time.wait(10)

def main():
    NewEntireCube = EntireCube(3) #create a 3x3x3 cube
    NewEntireCube.createEntireCube()
    NewEntireCube.mainloop()

if __name__ == '__main__':
    main()
    pygame.quit()
    quit()

I'm hoping someone who knows much more about this can give me some guidance as to how to proceed.

解决方案

A rubik's cube can be organized by an 3 dimensional array of 3x3x3 cubes. It seems to be easy to rotate a slice of the cube, but note if on slice is rotated the positions of the cube change and have to be reorganized. Not only the position changes, also the orientation of the (rotated) single cubes changes.

First of all remove the PyGame and OpenGL initialization form the constructor of the class Cube. That is the wrong place for this. In the following will generate 27 objects of type Cube.

Each cube has to know where it is initially located (self.init_i) and where it is current located after some rotations (self.current_i). This information is encoded in a list with 3 elements, one for each axis. The values are indices of cube in the NxNxN rubik's cube in range [0, N[.
The orientation of a single cube is encoded in 3 dimensional Rotation matrix (self.rot). The rotation matrix has to be initialized by the identity matrix.

class Cube():
    def __init__(self, id, N, scale):
        self.N = N
        self.scale = scale
        self.init_i = [*id]
        self.current_i = [*id]
        self.rot = [[1 if i==j else 0 for i in range(3)] for j in range(3)]

Create a list of the 27 cubes

cr = range(3)
self.cubes = [Cube((x, y, z), 3, scale) for x in cr for y in cr for z in cr]

If a slice of the rubik's cube is rotated, then it has to be checked which of the single cubes is affected. This can be done by checking if the slice matches the entry of the rotation axis of the current position.

def isAffected(self, axis, slice, dir):
    return self.current_i[axis] == slice

To rotate a cube, the position and the orientation has to be rotated by 90° around an axis. A 3 dimension rotation matrix consists of 3 direction vectors. A d dimensional vector can be rotated by swapping the coordinates of the vector and inverting the x coordinate of the result for a right rotation and inverting the y coordinate of the result for a left rotation:

rotate right: (x, y) -> (-y, x)
rotate left:  (x, y) -> (y, -x)

Since all the vectors of the rotation matrix are in an axis aligned plane this algorithm can be used to change the orientation and the position of the cube. axis the rotation axis (x=0, y=1, z=2) and dir is the rotation direction (1 is right and -1 left) To rotate the axis vector, 2 components of the vector have to be swapped and one of them inverted.

e.g rotate left around the Y-axis:

(x, y, z) -> (z, y, -x)

When the position is rotated, then the indices have to be swapped. Inverting an index means to map the index i to the index N-1-i:

e.g rotate left around the Y-axis:

(ix, iy, iz) -> (iz, iy, N-1-ix)

Rotation of a single cube:

i, j = (axis+1) % 3, (axis+2) % 3
for k in range(3):
    self.rot[k][i], self.rot[k][j] = -self.rot[k][j]*dir, self.rot[k][i]*dir

self.current_i[i], self.current_i[j] = (
    self.current_i[j] if dir < 0 else self.N - 1 - self.current_i[j],
    self.current_i[i] if dir > 0 else self.N - 1 - self.current_i[i] )

When the cube has to be drawn, then the current position of the cube (self.current_i) and the orientation self.rot can be used to set up 4x4 transformation matrix:

def transformMat(self):
    scaleA = [[s*self.scale for s in a] for a in self.rot]  
    scaleT = [(p-(self.N-1)/2)*2.1*self.scale for p in self.current_i] 
    return [
        *scaleA[0], 0,
        *scaleA[1], 0,
        *scaleA[2], 0,
        *scaleT,    1]

With glPushMatrix respectively glPushMatrix. By glMultMatrix a matrix can be multiplied to the current matrix.
The following function draws a single cube. The parameters angle, axis, slice, dir and it can even apply an animation to the cube, by setting animate=True and setting parameters angle, axis, slice, dir:

def draw(self, col, surf, vert, animate, angle, axis, slice, dir):

    glPushMatrix()
    if animate and self.isAffected(axis, slice, dir):
        glRotatef( angle*dir, *[1 if i==axis else 0 for i in range(3)] )
    glMultMatrixf( self.transformMat() )

    glBegin(GL_QUADS)
    for i in range(len(surf)):
        glColor3fv(colors[i])
        for j in surf[i]:
            glVertex3fv(vertices[j])
    glEnd()

    glPopMatrix()

To draw the cubes, it is sufficient to call the method draw in a loop:

for cube in self.cubes:
    cube.draw(colors, surfaces, vertices, animate, animate_ang, *action)

The implementation of the class Cube works for any NxNxN Rubik's cube.

See the example program for a 3x3x3 cube. The slices of the cube are rotated to the right by the keys 1 to 9 and to the left by the keys F1 to F9:

Of course the code uses the Legacy OpenGL in regard to your original code. But the method Cube.transformMat sets a general 4x4 model matrix for a single partial cube. Thus it is possible to port this code to modern OpenGL with ease.

import pygame
import random
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

vertices = (
    ( 1, -1, -1), ( 1,  1, -1), (-1,  1, -1), (-1, -1, -1),
    ( 1, -1,  1), ( 1,  1,  1), (-1, -1,  1), (-1,  1,  1)
)
edges = ((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7))
surfaces = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4), (4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6))
colors = ((1, 0, 0), (0, 1, 0), (1, 0.5, 0), (1, 1, 0), (1, 1, 1), (0, 0, 1))

class Cube():
    def __init__(self, id, N, scale):
        self.N = N
        self.scale = scale
        self.init_i = [*id]
        self.current_i = [*id]
        self.rot = [[1 if i==j else 0 for i in range(3)] for j in range(3)]

    def isAffected(self, axis, slice, dir):
        return self.current_i[axis] == slice

    def update(self, axis, slice, dir):

        if not self.isAffected(axis, slice, dir):
            return

        i, j = (axis+1) % 3, (axis+2) % 3
        for k in range(3):
            self.rot[k][i], self.rot[k][j] = -self.rot[k][j]*dir, self.rot[k][i]*dir

        self.current_i[i], self.current_i[j] = (
            self.current_i[j] if dir < 0 else self.N - 1 - self.current_i[j],
            self.current_i[i] if dir > 0 else self.N - 1 - self.current_i[i] )

    def transformMat(self):
        scaleA = [[s*self.scale for s in a] for a in self.rot]  
        scaleT = [(p-(self.N-1)/2)*2.1*self.scale for p in self.current_i] 
        return [*scaleA[0], 0, *scaleA[1], 0, *scaleA[2], 0, *scaleT, 1]

    def draw(self, col, surf, vert, animate, angle, axis, slice, dir):

        glPushMatrix()
        if animate and self.isAffected(axis, slice, dir):
            glRotatef( angle*dir, *[1 if i==axis else 0 for i in range(3)] )
        glMultMatrixf( self.transformMat() )

        glBegin(GL_QUADS)
        for i in range(len(surf)):
            glColor3fv(colors[i])
            for j in surf[i]:
                glVertex3fv(vertices[j])
        glEnd()

        glPopMatrix()

class EntireCube():
    def __init__(self, N, scale):
        self.N = N
        cr = range(self.N)
        self.cubes = [Cube((x, y, z), self.N, scale) for x in cr for y in cr for z in cr]

    def mainloop(self):

        rot_cube_map  = { K_UP: (-1, 0), K_DOWN: (1, 0), K_LEFT: (0, -1), K_RIGHT: (0, 1)}
        rot_slice_map = {
            K_1: (0, 0, 1), K_2: (0, 1, 1), K_3: (0, 2, 1), K_4: (1, 0, 1), K_5: (1, 1, 1),
            K_6: (1, 2, 1), K_7: (2, 0, 1), K_8: (2, 1, 1), K_9: (2, 2, 1),
            K_F1: (0, 0, -1), K_F2: (0, 1, -1), K_F3: (0, 2, -1), K_F4: (1, 0, -1), K_F5: (1, 1, -1),
            K_F6: (1, 2, -1), K_F7: (2, 0, -1), K_F8: (2, 1, -1), K_F9: (2, 2, -1),
        }  

        ang_x, ang_y, rot_cube = 0, 0, (0, 0)
        animate, animate_ang, animate_speed = False, 0, 5
        action = (0, 0, 0)
        while True:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == KEYDOWN:
                    if event.key in rot_cube_map:
                        rot_cube = rot_cube_map[event.key]
                    if not animate and event.key in rot_slice_map:
                        animate, action = True, rot_slice_map[event.key]
                if event.type == KEYUP:
                    if event.key in rot_cube_map:
                        rot_cube = (0, 0)

            ang_x += rot_cube[0]*2
            ang_y += rot_cube[1]*2

            glMatrixMode(GL_MODELVIEW)
            glLoadIdentity()
            glTranslatef(0, 0, -40)
            glRotatef(ang_y, 0, 1, 0)
            glRotatef(ang_x, 1, 0, 0)

            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

            if animate:
                if animate_ang >= 90:
                    for cube in self.cubes:
                        cube.update(*action)
                    animate, animate_ang = False, 0

            for cube in self.cubes:
                cube.draw(colors, surfaces, vertices, animate, animate_ang, *action)
            if animate:
                animate_ang += animate_speed

            pygame.display.flip()
            pygame.time.wait(10)

def main():

    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    glEnable(GL_DEPTH_TEST) 

    glMatrixMode(GL_PROJECTION)
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    NewEntireCube = EntireCube(3, 1.5) 
    NewEntireCube.mainloop()

if __name__ == '__main__':
    main()
    pygame.quit()
    quit()

这篇关于如何在python PyOpenGL中旋转魔方的切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆