Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

having trouble adding three more cube

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()
like image 938
rttyui Avatar asked May 05 '26 07:05

rttyui


1 Answers

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()
like image 148
Rabbid76 Avatar answered May 06 '26 21:05

Rabbid76