试图创造3x3x3的魔方,但在OpenGL创造型4x4x4代替(Trying to Create 3

2019-10-16 13:20发布

我想创建一个3D立方体rubiks不过尺寸都没有设定尺寸对齐。 当我设置的尺寸为3,创建一个3x3x3的立方体,我结束了创建型4x4x4立方体。 然而,当我设置尺寸为2,它创建一个3x3x3的立方体,并且当尺寸被设置为1,则创建一个1x1x1立方体。 有人能帮助呢? 谢谢!

代码立方体:

#imports
dim = 3
cube = [[['#' for c in range(dim)] for c in range(dim)] for r in range(dim)]

for i in range(dim):
    for j in range(dim):
        for k in range(dim):
            print(cube[i][j][k], end='')
        print()
    print()

for i in range(dim):
    for j in range(dim):
        for k in range(dim):
            scale = 0.5
            cube[i][j][k] = Piece(i * scale, j * scale, k * scale, scale)

def Cube():
    for i in range(dim):
        for j in range(dim):
            for k in range(dim):
                p = cube[i][j][k]
                p.draw()


def main():
    pygame.init()
    display = (1200, 1000)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

    gluPerspective(45, (display[0] / display[1]), 0.1, 50)

    glClearColor(0.6, 0.6, 0.6, 0)
    glTranslatef(0.0, 0.0, -5)
    glRotatef(45, 1, 1, 0)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)


main()

代码海贼王:

from OpenGL.GL import *

colors = (
    # white
    (1, 1, 1),
    # blue
    (1, 1, 0),
    # orange
    (1, 0, 0),
    # red
    (1, 0.5, 0.1),
    # green
    (0, 1, 0),
    # yellow
    (0, 0, 1),
)

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


class Piece:

    def __init__(self, x, y, z, length):
        self.x = x
        self.y = y
        self.z = z
        self.len = length

        self.v = [
            (self.x - self.len, self.y - self.len, self.z - self.len),
            (self.x + self.len, self.y - self.len, self.z - self.len),
            (self.x + self.len, self.y + self.len, self.z - self.len),
            (self.x - self.len, self.y + self.len, self.z - self.len),
            (self.x - self.len, self.y - self.len, self.z + self.len),
            (self.x + self.len, self.y - self.len, self.z + self.len),
            (self.x + self.len, self.y + self.len, self.z + self.len),
            (self.x - self.len, self.y + self.len, self.z + self.len),
        ]


    def getVertices(self):
        return self.x, self.y, self.z

    def draw(self):
        glEnable(GL_DEPTH_TEST)

        glLineWidth(5)
        glColor3fv((0, 0, 0))
        glBegin(GL_LINES)
        for edge in edges:
            glVertex3fv(self.v[edge[0]])
            glVertex3fv(self.v[edge[1]])
        glEnd()

        glEnable(GL_POLYGON_OFFSET_FILL)
        glPolygonOffset(1.0, 1.0)

        glBegin(GL_QUADS)
        for i, quad in enumerate(surfaces):
            glColor3fv(colors[i])
            for iv in quad:
                glVertex3fv(self.v[iv])
        glEnd()

        glDisable(GL_POLYGON_OFFSET_FILL)

Answer 1:

它看起来,那是因为你的作品是重叠的。

您在距离创建片scale 。 如果我们想象,在一维,我们得到( s代表scale ):

          o         o         o  
           `-- s --´ `-- s --´ 

件之间延伸pos - lenpos + len 。 您可以设置len = scale 。 因此,你得到的是:

           ---------o--------- 
 ---------o--------- ---------o----------    
           `-- s --´ `-- s --´ 

为了解决这个问题,您应该指定的长度一半的距离。 所以,无论是:

cube[i][j][k] = Piece(2 * i * scale, 2 * j * scale, 2 * k * scale, scale)

要么

cube[i][j][k] = Piece(i * scale, j * scale, k * scale, 0.5 * scale)



文章来源: Trying to Create 3x3x3 Cube but created 4x4x4 instead in OpenGL