3

I'm attempting to make a cube, but I can only manage two. As you can see, the third cube is broken. I am having trouble making three more cubes because I am having a hard time understanding vertices and edges.

I tried adding and removing numbers from vertices, and while the faces are sometimes correct and the edges are broken.

Can somebody help me?

Here's the code:

import pygame
from pygame.locals import *

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

verticies0 = (
    (0, 0.5, -0.5),
    (0.5, 0.5, -0.5),
    (0.5, 0, -0.5),
    (0, 0, -0.5),
    
    (0, 0.5, 0),
    (0.5, 0.5, 0),
    (0.5, 0, 0),
    (0, 0, 0)
    )

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

verticies1 = (
    (0.5, 0.5, -0.5),
    (1, 0.5, -0.5),
    (1, 0, -0.5),
    (0.5, 0, -0.5),
    
    (0.5, 0.5, 0),
    (1, 0.5, 0),
    (1, 0, 0),
    (0.5, 0, 0)
    )

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

verticies2 = (
    (-0.5, 0.5, 0),
    (0, 0.5, 0),
    (-0.5, 0, 0.5),
    (-0.5, 0, 0),
    
    (0, 0.5, 0),
    (0, 0, 0),
    (0, 0, 0),
    (0, 0, 0)
    )

edges2 = (
    (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():

    glBegin(GL_LINES)
    for edge in edges0:
        for vertex in edge:
            glVertex3fv(verticies0[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges1:
        for vertex in edge:
            glVertex3fv(verticies1[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges2:
        for vertex in edge:
            glVertex3fv(verticies2[vertex])
    glEnd()
    
def main():
    pygame.init()
    display = (1200,800)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(45, (display[0]/display[1]), 0.1, 500)
    button_down = False

    glMatrixMode(GL_MODELVIEW)  
    modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
    
    glTranslatef(0.0,0.0, -5)

    while True:
        glPushMatrix()
        glLoadIdentity()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(-1, 0, 0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(1, 0, 0)
                if event.key == pygame.K_UP:
                    glTranslatef(0, 1, 0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0, -1, 0)
            if event.type == pygame.MOUSEMOTION:
                if button_down == True:
                    glRotatef(event.rel[1], 1, 0, 0)
                    glRotatef(event.rel[0], 0, 1, 0)
                print(event.rel)

        for event in pygame.mouse.get_pressed():
            print(pygame.mouse.get_pressed())
            if pygame.mouse.get_pressed()[0] == 1:
                button_down = True
            elif pygame.mouse.get_pressed()[0] == 0:
                button_down = False

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        glMultMatrixf(modelMatrix)
        modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

        glLoadIdentity()
        glTranslatef(0, 0, -5)
        glMultMatrixf(modelMatrix)
        
        Cube()
        
        glPopMatrix()        
        pygame.display.flip()
        pygame.time.wait(10)


main()
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
rttyui
  • 55
  • 4
  • 5
    This seems like more of a geometry or math problem than a programming problem; if you don't know how you'd design this on paper then you can't put it into code until you figure out the design. But the design requires math/geometry, not programming, to figure out, and this is a site about programming. Maybe another StackExchange site would be more help though. – Random Davis May 27 '22 at 15:52
  • @RandomDavis It's more a question of how to set a transformation matrix for each cube than a math question. It is not necessary to define vertices for 5 cubes. 1 cube is enough. – Rabbid76 May 27 '22 at 16:50
  • what is `five-dimensional cube`? – jsotola May 27 '22 at 18:58

1 Answers1

2

One cube mesh is completely sufficient. Draw this one cube fife times and use glTranslate to move the cubes to different positions in the world. The current transformation matrix can be saved and restored with glPushMatrix and glPopMatrix:

for i in range(5):
    glPushMatrix()
    glTranslate(-2+i, 0, 0)
    Cube()
    glPopMatrix()

import pygame
from pygame.locals import *

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

verticies = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
             (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))
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():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()
    
def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(60, (display[0]/display[1]), 0.1, 500)
    button_down = False

    glMatrixMode(GL_MODELVIEW)  
    modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
    
    glTranslatef(0.0,0.0, -5)

    while True:
        glPushMatrix()
        glLoadIdentity()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(-1, 0, 0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(1, 0, 0)
                if event.key == pygame.K_UP:
                    glTranslatef(0, 1, 0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0, -1, 0)
            if event.type == pygame.MOUSEMOTION:
                if button_down == True:
                    glRotatef(event.rel[1], 1, 0, 0)
                    glRotatef(event.rel[0], 0, 1, 0)

        for event in pygame.mouse.get_pressed():
            if pygame.mouse.get_pressed()[0] == 1:
                button_down = True
            elif pygame.mouse.get_pressed()[0] == 0:
                button_down = False

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        glMultMatrixf(modelMatrix)
        modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

        glLoadIdentity()
        glTranslatef(0, 0, -5)
        glMultMatrixf(modelMatrix)
        
        for i in range(5):
            glPushMatrix()
            glTranslate(-2+i, 0, 0)
            Cube()
            glPopMatrix()
        
        glPopMatrix()        
        pygame.display.flip()
        pygame.time.wait(10)

main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174