简体   繁体   English

如何在python-opengl中连接“内部多维数据集”和“外部多维数据集”(4维对象)的顶点?

[英]How to join the vertices of the 'inner cube' and the 'outer cube' (4 Dimensional object) in python-opengl?

I was trying to make a model of tesseract (4D object) in python using OpenGL and Pygame tools. 我试图使用OpenGL和Pygame工具在python中创建tesseract(4D对象)模型。 Fortunately, I got the outlook (having a cube inside a cube) but couldn't join the vertices of inner cube with the outer one. 幸运的是,我获得了外观(在一个立方体内有一个立方体),但是无法将内部立方体的顶点与外部立方体的顶点连接在一起。 This is my code, below. 这是我的代码,如下。

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

I could easily get the inner cube (smaller one) surrounded by the outer cube (larger one). 我可以轻松地将内部立方体(较小的一个)包围在外部立方体(较大的一个)周围。 But not able to join the vertices of both. 但是无法将两者的顶点融合在一起。

Please help me sharing your valuable answers... 请帮助我分享您的宝贵答案...

This is the image which I wanted to get 这是我想要获得的图像

Thanks in advance. 提前致谢。

Group points from both lists zip(vertices1, vertices2) and you have lines to draw. 将两个列表zip(vertices1, vertices2)点分组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)

And you get 你得到

在此处输入图片说明

Full code 完整代码

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

You can prepare 3 VAOs . 您可以准备3个VAO。 1st containing vertices for inner cube. 第一个包含内部立方体的顶点。 2nd containing vertices for outer cube. 第二个包含外部立方体的顶点。 3rd create VAO of pair wise vertices. 第三对成对顶点创建VAO。 For example 例如

Lets say that ABCD are vertices of face of outer cube and abcd are vertices of face of inner cube. 可以说ABCD是外立方体的面的顶点,而abcd是内立方体的面的顶点。 Now you want to draw line between A to a, B to b, C to c and D to d. 现在您要在A到a,B到b,C到c和D到d之间画一条线。 So prepare VBO as (A ,a ,B ,b ,C ,c ,D ,d ). 因此,准备VBO为(A,a,B,b,C,c,D,d)。 After this to draw this VBO use GL_LINE primitive. 在此之后绘制此VBO使用GL_LINE原语。

I hope this is clear . 我希望这很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM