如何在python-opengl中连接'内部立方体'和“外部立方体”(4维对象)的顶点?

问题描述 投票:0回答:2

我试图使用OpenGL和Pygame工具在python中创建tesseract(4D对象)模型。幸运的是,我得到了前景(在立方体内有一个立方体),但无法将内部立方体的顶点与外部立方体连接起来。这是我的代码,如下。

import pygame
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))
vertices1 = ((2,-2,-2),(2,2,-2),(-2,2,-2),(-2,-2,-2),(2,-2,2),(2,2,2),(-2,-2,2),(-2,2,2))

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))

def cube(edges,vertices):
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex]) 
    glEnd()

def display_cube():
    pygame.init()
    display_window = (800,600)
    pygame.display.set_mode(display_window,DOUBLEBUF | OPENGL)
    gluPerspective(45,(display_window[0]/display_window[1]),0.1,50.0)
    glTranslatef(0.0,0.0,-10)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        glRotate(1,3,10,10) # (angle,x,y,z)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        cube(edges,vertices1) # large cube
        cube(edges,vertices) # small cube

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

display_cube()

我可以很容易地得到外部立方体(较大的立方体)包围的内部立方体(较小的立方体)。但是无法加入两者的顶点。

请帮我分享你的宝贵答案......

This is the image which I wanted to get

提前致谢。

python opengl 3d pygame
2个回答
2
投票

从两个列表zip(vertices1, vertices2)分组点,你有画线。

def lines(vertices1, vertices2):
    for v1, v2 in zip(vertices1, vertices2):
        glBegin(GL_LINES)
        glVertex3fv(v1) 
        glVertex3fv(v2) 
        glEnd()


lines(vertices, vertices1)

你得到了

enter image description here

完整代码

import pygame
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))
vertices1 = ((2,-2,-2),(2,2,-2),(-2,2,-2),(-2,-2,-2),(2,-2,2),(2,2,2),(-2,-2,2),(-2,2,2))

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))

def cube(edges,vertices):
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex]) 
    glEnd()

def lines(vertices1, vertices2):
    for v1, v2 in zip(vertices1, vertices2):
        glBegin(GL_LINES)
        glVertex3fv(v1) 
        glVertex3fv(v2) 
        glEnd()

def display_cube():
    pygame.init()
    display_window = (800,600)
    pygame.display.set_mode(display_window,DOUBLEBUF | OPENGL)
    gluPerspective(45,(display_window[0]/display_window[1]),0.1,50.0)
    glTranslatef(0.0,0.0,-10)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotate(1,3,10,10) # (angle,x,y,z)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        cube(edges,vertices1) # large cube
        cube(edges,vertices) # small cube
        lines(vertices1, vertices)

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

display_cube()

1
投票

您可以准备3个VAO。第一个包含内部立方体的顶点。第二个包含外部立方体的顶点。第3次创建成对顶点的VAO。例如

假设A B C D是外立方体的面的顶点,而b c d是内立方体的面的顶点。现在你想在A到a,B到b,C到c和D到d之间画线。因此,将VBO准备为(A,a,B,b,C,c,D,d)。在此之后绘制这个VBO使用GL_LINE原语。

我希望这很清楚。

© www.soinside.com 2019 - 2024. All rights reserved.